package com.ruoyi.web.controller.warehouse.quotation;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.quotation.domain.TSeaprice;
import com.ruoyi.quotation.domain.TSeapriceitem;
import com.ruoyi.quotation.service.ITSeapriceService;
import com.ruoyi.system.domain.SysCheckCode;
import com.ruoyi.warehouseBusiness.service.ITWarehouseBillsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 海运价主Controller
 * 报价
 * @author ruoyi
 * @date 2021-06-01
 */
@RestController
@RequestMapping("/warehouse/seaprice")
public class TSeapriceController extends BaseController
{
    @Autowired
    private ITSeapriceService tSeapriceService;

    @Autowired
    private ITWarehouseBillsService itWarehouseBillsService;

    /**
     * 查询单据是否允许修改
     */
    @GetMapping("/checkCode")
    public boolean checkCode(SysCheckCode sysCheckCode) {
        if (StringUtils.isNull(sysCheckCode.getfId())) {
            return true;
        }
        return itWarehouseBillsService.checkCode(sysCheckCode);
    }

    /**
     * 查询海运价主列表
     */
    @GetMapping("/list")
    public TableDataInfo list(TSeaprice tSeaprice)
    {
        startPage();
        List<TSeaprice> list = tSeapriceService.selectTSeapriceList(tSeaprice);
        return getDataTable(list);
    }

    /**
     * 查询海运报价列表信息
     */
//    @PreAuthorize("@ss.hasPermi('warehouse:seaprice:list')")
    @GetMapping("/query")
    public TableDataInfo queryItem(TSeaprice tSeaprice)
    {
        startPage();
        List<Map<String, Object>> list = tSeapriceService.selectTSeapriceMapList(tSeaprice);
        return getDataTable(list);
    }

    /**
     * 手机端专用
     * @param tSeaprice
     * @return
     */
    @GetMapping("/queryApp")
    public TableDataInfo queryApp(TSeaprice tSeaprice)
    {
        startPage();
        List<Map<String, Object>> list = tSeapriceService.selectTSeapriceMapListApp(tSeaprice);
        return getDataTable(list);
    }

    /**
     *  查询app 必要的基础资料
     * @return
     */
    @GetMapping("/appBasicInformation")
    public AjaxResult getAppBasicInformation() {
        return tSeapriceService.getAppBasicInformation();
    }

    /**
     * 导出海运价主列表
     */
//    @PreAuthorize("@ss.hasPermi('warehouse:seaprice:export')")
    @Log(title = "海运价主", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TSeaprice tSeaprice)
    {
        List<TSeaprice> list = tSeapriceService.selectTSeapriceList(tSeaprice);
        ExcelUtil<TSeaprice> util = new ExcelUtil<TSeaprice>(TSeaprice.class);
        return util.exportExcel(list, "seaprice");
    }

    /**
     * 获取海运价主详细信息
     */
//    @PreAuthorize("@ss.hasPermi('warehouse:seaprice:query')")
    @GetMapping(value = "/{fId}")
    public AjaxResult getInfo(@PathVariable("fId") Long fId)
    {
        return tSeapriceService.selectTSeapriceById(fId);
    }

    /**
     * 计算海运费
     */
    @RepeatSubmit
    @GetMapping(value = "/calculateCost")
    public AjaxResult calculateOceanFreight(TSeaprice tSeaprice)
    {
        if (StringUtils.isNull(tSeaprice.getfBilltype())) {
            return AjaxResult.error("请维护业务类型");
        }
        if (StringUtils.isNull(tSeaprice.getfCorpid())) {
            return AjaxResult.error("请维护船公司");
        }
        if (StringUtils.isNull(tSeaprice.getfSpecification())) {
            return AjaxResult.error("请维护箱型");
        }
        if (StringUtils.isNull(tSeaprice.getfQty())) {
            return AjaxResult.error("请维护箱量");
        }

        //TODO
        /**
         * 20210708 dongyongwei
         * 新增查询主表ID的逻辑,如果存在,赋值条件给合计金额
         * 如果查询不到主表ID,提醒:没有找到符合条件的运价
         */

        return tSeapriceService.calculateOceanFreight(tSeaprice);
    }

    /**
     * 动态查询海运费、汇率
     */
    @GetMapping(value = "/changeSelectShippingFee")
    public AjaxResult changeSelectShippingFee(TSeaprice tSeaprice)
    {
        if (StringUtils.isNull(tSeaprice.getfBilltype())) {
            return AjaxResult.error("请维护业务类型");
        }
        if (StringUtils.isNull(tSeaprice.getfCorpid())) {
            return AjaxResult.error("请维护船公司");
        }
        if (StringUtils.isNull(tSeaprice.getfSpecification())) {
            return AjaxResult.error("请维护箱型");
        }
        return tSeapriceService.changeSelectShippingFee(tSeaprice);
    }

    /**
     * 保存海运价主
     */
//    @PreAuthorize("@ss.hasPermi('warehouse:seaprice:add')")
    @Log(title = "海运价主", businessType = BusinessType.INSERT)
    @PostMapping("/saveSeaprice")
    @RepeatSubmit
    public AjaxResult saveSeaprice(@RequestParam("seaprice") String tSeaprice,
                           @RequestParam("seapriceItem") String tSeapriceItem,
                           @RequestParam("seapriceFlees") String tSeapriceFlees)
    {
        if (StringUtils.isEmpty(tSeaprice)) {
            return AjaxResult.error("未找到主表信息");
        }
        return tSeapriceService.saveTSeaprice(tSeaprice, tSeapriceItem, tSeapriceFlees);
    }

    /**
     * 新增海运价主
     */
//    @PreAuthorize("@ss.hasPermi('warehouse:seaprice:add')")
    @Log(title = "海运价主", businessType = BusinessType.INSERT)
    @PostMapping("/addSeaprice")
    @RepeatSubmit
    public AjaxResult add(@RequestParam("seaprice") String tSeaprice,
                           @RequestParam("seapriceItem") String tSeapriceItem,
                           @RequestParam("seapriceFlees") String tSeapriceFlees)
    {
        if (StringUtils.isEmpty(tSeaprice)) {
            return AjaxResult.error("未找到主表信息");
        }
        return tSeapriceService.insertTSeaprice(tSeaprice, tSeapriceItem, tSeapriceFlees);
    }

    /**
     * 修改海运价主
     */
//    @PreAuthorize("@ss.hasPermi('warehouse:seaprice:edit')")
    @Log(title = "海运价主", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TSeaprice tSeaprice)
    {
        return toAjax(tSeapriceService.updateTSeaprice(tSeaprice));
    }

    /**
     * 删除海运价主
     */
//    @PreAuthorize("@ss.hasPermi('warehouse:seaprice:remove')")
    @Log(title = "海运价主", businessType = BusinessType.DELETE)
	@DeleteMapping("/{fIds}")
    public AjaxResult remove(@PathVariable Long[] fIds)
    {
        return tSeapriceService.deleteTSeapriceByIds(fIds);
    }
}
