package com.ruoyi.web.controller.shipping.controller;

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

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.utils.StringUtils;
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.shipping.domain.TVoyage;
import com.ruoyi.shipping.service.ITVoyageService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 航次Controller
 * 
 * @author ruoyi
 * @date 2021-04-02
 */
@RestController
@RequestMapping("/shipping/voyage")
public class TVoyageController extends BaseController
{
    @Autowired
    private ITVoyageService tVoyageService;

    /**
     * 查询航次列表
     */
    //@PreAuthorize("@ss.hasPermi('shipping:voyage:list')")
    @GetMapping("/list")
    public TableDataInfo list(TVoyage tVoyage)
    {
        startPage();
        List<TVoyage> list = tVoyageService.selectTVoyageList(tVoyage);
        return getDataTable(list);
    }

    /**
     * 导出航次列表
     */
    @PreAuthorize("@ss.hasPermi('shipping:voyage:export')")
    @Log(title = "航次", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TVoyage tVoyage)
    {
        List<TVoyage> list = tVoyageService.selectTVoyageList(tVoyage);
        ExcelUtil<TVoyage> util = new ExcelUtil<TVoyage>(TVoyage.class);
        return util.exportExcel(list, "voyage");
    }

    /**
     * 获取航次详细信息
     */
    @PreAuthorize("@ss.hasPermi('shipping:voyage:query')")
    @GetMapping(value = "/{fId}")
    public AjaxResult getInfo(@PathVariable("fId") Long fId)
    {
        return AjaxResult.success(tVoyageService.selectTVoyageById(fId));
    }

    /**
     * 新增航次
     */
    @PreAuthorize("@ss.hasPermi('shipping:voyage:add')")
    @Log(title = "航次", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TVoyage tVoyage)
    {
        if (StringUtils.isEmpty(tVoyage.getfNo())){
            return AjaxResult.error("航次不能为空");
        }
        if (tVoyage.getfPortofloadid() == null){
            return AjaxResult.error("装货港不能为空");
        }
        /*if (tVoyage.getfPortofdischargeid() == null){
            return AjaxResult.error("卸货港不能为空");
        }*/
        if (tVoyage.getfDistinationid() == null){
            return AjaxResult.error("目的港不能为空");
        }
        /*if (tVoyage.getfPortoftransshipment() == null){
            return AjaxResult.error("中转港不能为空");
        }*/
        if (tVoyage.getfEtd() == null){
            return AjaxResult.error("预计开船日期不能为空");
        }
        if (tVoyage.getfEta() == null){
            return AjaxResult.error("预计到达日期不能为空");
        }
        if (tVoyage.getfDucomentrayoffdate() == null){
            return AjaxResult.error("截单日期不能为空");
        }
        if (StringUtils.isEmpty(tVoyage.getfTel())){
            return AjaxResult.error("电话不能为空");
        }
        if (UserConstants.NOT_UNIQUE.equals(tVoyageService.selectTVoyagefNo(tVoyage))) {
            return AjaxResult.error(tVoyage.getfNo() + "编号已存在");
        }
        if (tVoyage.getfId()==null){
            return toAjax(tVoyageService.insertTVoyage(tVoyage));
        }else {
            if (tVoyage.getfStatus().equals("T") ||tVoyage.getfStatus().equals("正常")){
                tVoyage.setfStatus("T");
            }else {
                tVoyage.setfStatus("F");
            }
            return toAjax(tVoyageService.updateTVoyage(tVoyage));
        }

    }

    /**
     * 修改航次
     */
    @PreAuthorize("@ss.hasPermi('shipping:voyage:edit')")
    @Log(title = "航次", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TVoyage tVoyage)
    {
        if (tVoyage.getfStatus().equals("T") ||tVoyage.getfStatus().equals("正常")){
            tVoyage.setfStatus("T");
        }else {
            tVoyage.setfStatus("F");
        }
        return toAjax(tVoyageService.updateTVoyage(tVoyage));
    }

    /**
     * 删除航次
     */
    @PreAuthorize("@ss.hasPermi('shipping:voyage:remove')")
    @Log(title = "航次", businessType = BusinessType.DELETE)
	@DeleteMapping("/{fIds}")
    public AjaxResult remove(@PathVariable Long[] fIds)
    {
        return toAjax(tVoyageService.deleteTVoyageByIds(fIds));
    }
    /**
     * 查询航次列表
     */
    //@PreAuthorize("@ss.hasPermi('shipping:voyage:list')")
    @GetMapping("/freightList")
    public TableDataInfo freightList(TVoyage tVoyage)
    {
        startPage();
        List<TVoyage> list = tVoyageService.createOrder(tVoyage);
        return getDataTable(list);
    }

    /**
     * 查询航次详情接口暂时不用
     * @param tVoyage
     * @return
     *//*
    @GetMapping("/freightselect")
    public TableDataInfo freightselect(TVoyage tVoyage)
    {
        startPage();
        List<TVoyage> list = tVoyageService.freightselect(tVoyage);
        return getDataTable(list);
    }*/

    /**
     * 查询航次
     * @return
     */
    @GetMapping("/selectTVoyagefNumber")
    public TableDataInfo selectTVoyagefNumber(TVoyage tVoyage)
    {
        startPage();
        List<Map<String, Object>> list = tVoyageService.selectTVoyagefNumber(tVoyage);
        return getDataTable(list);
    }
    /**
     * 查询航次列表
     */
    //@PreAuthorize("@ss.hasPermi('shipping:voyage:list')")
    @PostMapping("/selectMessage")
    public TableDataInfo selectMessage(@RequestBody TVoyage tVoyage)
    {
        startPage();
        List<TVoyage> list = tVoyageService.selectMessage(tVoyage);
        return getDataTable(list);
    }
    @Log(title = "新增航次运价", businessType = BusinessType.INSERT)
    @PostMapping("/insertMessage")
    public AjaxResult insertMessage(@RequestParam String tVoyage,
                          @RequestParam String tCtnpriceItems)
    {
        if (StringUtils.isEmpty(tVoyage)|| "[]".equals(tVoyage)){
            return AjaxResult.error("未找到航次信息,请确认");
        }
        if (StringUtils.isEmpty(tCtnpriceItems) || "[]".equals(tCtnpriceItems)){
            return AjaxResult.error("未找到运价信息,请确认");
        }
        return tVoyageService.insertMessage(tVoyage,tCtnpriceItems);
    }
}
