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

/**
 * @Author ZhaoQian La
 * @Date 2021/7/2 15:44
 * @Version 1.0
 */

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.shipping.domain.TCntrno;
import com.ruoyi.shipping.excel.SingleVoyageByShipExcel;
import com.ruoyi.warehouseBusiness.domain.TWarehouseBills;
import com.ruoyi.warehouseBusiness.service.ITWarehouseBillsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 凯和修改订单信息
 */
@Slf4j
@RestController
@RequestMapping("/khwarehouse/updateOrderMessage")
public class KHUpdateOrderMessage extends BaseController {
    @Autowired
    private ITWarehouseBillsService tWarehousebillsService;

    //修改订单申请审批 XGDD 471
    //修改完后提交审批 XGTJ 472
    //删除订单申请审批 DDSC 473
    @PostMapping("/orderExamine")
    public AjaxResult orderExamine(@RequestBody TWarehouseBills tWarehouseBills){
        if (StringUtils.isNull(tWarehouseBills.getfBilltype())){
            return AjaxResult.error("提交审核类型不能为空");
        }
        if (tWarehouseBills.getfId() == null){
            return AjaxResult.error("请选择数据后再请核");
        }
        LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());
        return tWarehousebillsService.orderExamine(tWarehouseBills,loginUser);
    }
    //修改订单信息
    @PostMapping("/waybillChanges")
    public AjaxResult waybillChanges(@RequestParam(value = "tWarehousebills") String tWarehousebills,
                                              @RequestParam(value = "tWarehousebillsCntr",required = false) String tWarehousebillsCntr,
                                              @RequestParam(value = "tWarehousebillsCntritems",required = false) String tWarehousebillsCntritems){
        if (StringUtils.isEmpty(tWarehousebills)){
            return AjaxResult.error("未找到订单主表信息,请确认");
        }
        String billsType = "KHDD";
        LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());
        return tWarehousebillsService.waybillChanges(tWarehousebills,tWarehousebillsCntr,tWarehousebillsCntritems, loginUser,billsType);

    }

    /**
     * 凯和公司端导出订单
     * 船名，航次必填
     * @param tWarehouseBills
     * @return
     */
    @Log(title = "凯和公司端导出订单", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult exportInventory(TWarehouseBills tWarehouseBills)
    {
        if (tWarehouseBills.getfVoyid() == null){
            return AjaxResult.error("航次不能为空");
        }
        if (tWarehouseBills.getfVslid() == null){
            return AjaxResult.error("船名不能为空");
        }
        return tWarehousebillsService.exportInventory(tWarehouseBills);
    }

    /**
     * 凯和若依端删除订单
     * @param tWarehouseBills
     * @return
     */
    @GetMapping("/deleteOrder")
    public AjaxResult deleteOrder(TWarehouseBills tWarehouseBills){
        if (tWarehouseBills.getfId() == null){
            return AjaxResult.error("请选择要删除的订单");
        }
        LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());
        return tWarehousebillsService.deleteOrder(tWarehouseBills,loginUser);
    }

    /**
     * 凯和单航次配船
     * @param tWarehouseBills
     * @return
     */
    @GetMapping("/singleVoyageByShip")
    public TableDataInfo singleVoyageByShip (TWarehouseBills tWarehouseBills){
        startPage();
        List<SingleVoyageByShipExcel> SingleVoyageByShipExcels = tWarehousebillsService.singleVoyageByShip(tWarehouseBills);
        return getDataTable(SingleVoyageByShipExcels);
    }
    /**
     * 单航次配船 离港出运 1 到港卸船 2
     * @param tCntrno
     * @return
     */
    @PostMapping("/updateAirForceStation")
    public AjaxResult updateBoxMessage(@RequestBody TCntrno tCntrno){
        if (tCntrno.getUpdateStatus() == null){
            return AjaxResult.error("请选择离港出运或到港卸船");
        }
        if (StringUtils.isEmpty(tCntrno.getfVsl()) ||StringUtils.isEmpty(tCntrno.getfVoy())){
            return AjaxResult.error("请选择船名航次");
        }
        if (tCntrno.getfUpdatetime() == null){
            return AjaxResult.error("请选择离港出运或到港卸船时间");
        }
        LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());
        return tWarehousebillsService.updateBoxMessage(tCntrno,loginUser);
    }
    /**
     * 凯和商务端导出订单
     * 船名，航次必填
     * @param tWarehouseBills
     * @return
     */
    @Log(title = "凯和商务端导出订单", businessType = BusinessType.EXPORT)
    @GetMapping("/exportOrderMessage")
    public AjaxResult exportOrderMessage(TWarehouseBills tWarehouseBills)
    {
        if (tWarehouseBills.getfVoyid() == null){
            return AjaxResult.error("航次不能为空");
        }
        if (tWarehouseBills.getfVslid() == null){
            return AjaxResult.error("船名不能为空");
        }
        LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());
        tWarehouseBills.setCreateBy(loginUser.getUser().getUserName());
        return tWarehousebillsService.exportInventory(tWarehouseBills);
    }

}


