package com.ruoyi.app.order;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.List;
import jakarta.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.app.utils.GetJvli;
import com.ruoyi.app.utils.RsaMima;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.system.domain.InfoUser;
import com.ruoyi.system.domain.vo.UserDTO;
import com.ruoyi.system.utils.JwtUtil;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.TaxiPrices;
import com.ruoyi.system.service.ITaxiPricesService;
import com.ruoyi.common.utils.MessageUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * TaxiPricesController
 * 
 * @author ruoyi
 * @date 2024-01-26
 */
@RestController
@RequestMapping("/system/txprices")
public class TaxiPricesController extends BaseController
{
    @Autowired
    private ITaxiPricesService taxiPricesService;


    @Anonymous
    @GetMapping("/getTaxiPrices")
    public AjaxResult getTaxiPrices() {
        TaxiPrices prices = taxiPricesService.getById(1);
        return success(prices);
    }
    //订算运费
    @Anonymous
    @GetMapping("/ganyunfei")
    public AjaxResult ganyunfei(@RequestParam double lat1,
                                @RequestParam double lng1,
                                @RequestParam double lat2,
                                @RequestParam double lng2) {
        GetJvli getjvli = new GetJvli();
        TaxiPrices prices = taxiPricesService.getById(1);
        JSONObject bje = new JSONObject();
        DecimalFormat df = new DecimalFormat("#.0");
        Double jvli = getjvli.getDistance(lat1,lng1,lat2,lng2);
        Double yunjia = Double.valueOf(0);
        Double jjjvli = jvli-prices.getStartingDistance();
        Double jlyunj = jjjvli*prices.getSkipPrice();
        if(jvli>=prices.getStartingDistance()){
            yunjia = prices.getStartingPrice()+jlyunj;
        }else {
            yunjia = prices.getStartingPrice();
        }
        bje.put("jvli",df.format(jvli));
        bje.put("freight",df.format(yunjia));
        return success(bje);
    }
    /**
     * 查询TaxiPrices列表
     */
    @PreAuthorize("@ss.hasPermi('system:txprices:list')")
    @GetMapping("/list")
    public TableDataInfo list(TaxiPrices taxiPrices)
    {
        startPage();
        List<TaxiPrices> list = taxiPricesService.selectTaxiPricesList(taxiPrices);
        return getDataTable(list);
    }

    /**
     * 导出TaxiPrices列表
     */
    @PreAuthorize("@ss.hasPermi('system:txprices:export')")
    @Log(title = "TaxiPrices", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TaxiPrices taxiPrices)
    {
        List<TaxiPrices> list = taxiPricesService.selectTaxiPricesList(taxiPrices);
        ExcelUtil<TaxiPrices> util = new ExcelUtil<TaxiPrices>(TaxiPrices.class);
        util.exportExcel(response, list, MessageUtils.message("no.export.excel.taxiprices"));
    }

    /**
     * 获取TaxiPrices详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:txprices:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(taxiPricesService.selectTaxiPricesById(id));
    }

    /**
     * 新增TaxiPrices
     */
    @PreAuthorize("@ss.hasPermi('system:txprices:add')")
    @Log(title = "TaxiPrices", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TaxiPrices taxiPrices)
    {
        return toAjax(taxiPricesService.insertTaxiPrices(taxiPrices));
    }

    /**
     * 修改TaxiPrices
     */
    @PreAuthorize("@ss.hasPermi('system:txprices:edit')")
    @Log(title = "TaxiPrices", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TaxiPrices taxiPrices)
    {
        return toAjax(taxiPricesService.updateTaxiPrices(taxiPrices));
    }

    /**
     * 删除TaxiPrices
     */
    @PreAuthorize("@ss.hasPermi('system:txprices:remove')")
    @Log(title = "TaxiPrices", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(taxiPricesService.deleteTaxiPricesByIds(ids));
    }
}
