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

import com.ruoyi.common.annotation.DataScope;
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.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.poi.ExcelUtil;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.domain.SysCheckCode;
import com.ruoyi.warehouseBusiness.domain.TWarehouseBills;
import com.ruoyi.warehouseBusiness.domain.dto.WarehousebillsDTO;
import com.ruoyi.warehouseBusiness.domain.enums.WarehouseTypeEnum;
import com.ruoyi.warehouseBusiness.excel.InStock;
import com.ruoyi.warehouseBusiness.service.ITWarehouseBillsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

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

/**
 * 仓库详情主表Controller
 *
 * @author ruoyi
 * @date 2020-12-11
 */
@RestController
@RequestMapping("/warehouseBusiness/inStock")
public class TWarehouseInStockController extends BaseController {
    @Autowired
    private ITWarehouseBillsService itWarehouseBillsService;

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

    /**
     * 查询仓储仓管员角色用户
     */
    @GetMapping("/queryClerkRoleList")
    public AjaxResult queryClerkRoleList() {
        return itWarehouseBillsService.queryClerkRoleList();
    }

    /**
     * 查询入库详情主表列表
     */
    @PreAuthorize("@ss.hasPermi('warehouseBusiness:inStock:list')")
    @DataScope(deptAlias = "d", userAlias = "u")
    @GetMapping("/list")
    public TableDataInfo list(TWarehouseBills tWarehouseBills) {
        startPage();
        tWarehouseBills.setfBilltype("SJRK");
        List<Map<String, Object>> list = itWarehouseBillsService.selectWarehouseBusinessList(tWarehouseBills);
        return getDataTable(list);
    }

    /**
     * 导出入库详情主表列表
     */
    @PreAuthorize("@ss.hasPermi('warehouseBusiness:inStock:export')")
    @Log(title = "详情主表", businessType = BusinessType.EXPORT)
    @DataScope(deptAlias = "d", userAlias = "u")
    @GetMapping("/export")
    public AjaxResult export(TWarehouseBills tWarehouseBills) throws Exception {
        tWarehouseBills.setfBilltype("SJRK");
        List<InStock> list = itWarehouseBillsService.inStockExport(tWarehouseBills);
        ExcelUtil<InStock> util = new ExcelUtil<InStock>(InStock.class);
        return util.exportExcel(list, "入库");
    }

    /**
     * 获取入库仓库详细信息
     */
    @PreAuthorize("@ss.hasPermi('warehouseBusiness:inStock:query')")
    @GetMapping(value = "/{fId}")
    @RepeatSubmit
    public AjaxResult getInfo(@PathVariable("fId") Long fId) {
        return AjaxResult.success(itWarehouseBillsService.selectTWarehousebillsById(fId));
    }

    /**
     * 导入【入库明细箱信息】Excel
     * 待完成
     */
    //@Log(title = "导入集装箱动态", businessType = BusinessType.IMPORT)
    @PostMapping(value = "/importInstock")
    public AjaxResult importInstock(@RequestParam("file") MultipartFile file) {
        return itWarehouseBillsService.importLibraryInformation(file);
    }

    /**
     * 入库暂存
     */
    @PreAuthorize("@ss.hasPermi('warehouseBusiness:inStock:add')")
    @Log(title = "详情主表", businessType = BusinessType.INSERT)
    @PostMapping(value = "/add")
    @RepeatSubmit
    public AjaxResult add(@RequestParam("tWarehouseBills") String tWarehouseBills,
                          @RequestParam("tEnclosure") String tEnclosure,
                          @RequestParam("tCntr") String tCntr,
                          @RequestParam("tWarehousebillsfeesCr") String tWarehousebillsfeesCr,
                          @RequestParam("tWarehousebillsfeesDr") String tWarehousebillsfeesDr,
                          @RequestParam("tWarehousebillsitems") String tWarehousebillsitems,
                          @RequestParam("mblnoStatus") String mblnoStatus) {
        String billsType = "SJRK";
        if (StringUtils.isNull(tWarehouseBills) || "{}".equals(tWarehouseBills)) {
            return AjaxResult.error("未找到仓库主表数据，请确认");
        }
        if (StringUtils.isEmpty(mblnoStatus)) {
            // 判断是提单号是否在一天当中出现第二次或以上
            AjaxResult result = itWarehouseBillsService.selectContainMblno(tWarehouseBills, billsType);
            String data = result.get("data").toString();
            if (StringUtils.isNotEmpty(data) && "false".equals(data)) {
                return result;
            }
        }
        LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());
        return itWarehouseBillsService.insertTWarehousebills(tWarehouseBills, tWarehousebillsfeesCr,
                tWarehousebillsfeesDr, tWarehousebillsitems, tEnclosure, loginUser, tCntr, billsType);
    }

    /**
     * 入库提交
     */
    @PreAuthorize("@ss.hasPermi('warehouseBusiness:inStock:add')")
    @Log(title = "详情主表", businessType = BusinessType.INSERT)
    @PostMapping(value = "/warehouseSubmission")
    @RepeatSubmit
    public AjaxResult warehouseSubmission(@RequestParam("tWarehouseBills") String tWarehouseBills,
                                          @RequestParam("tEnclosure") String tEnclosure,
                                          @RequestParam("tCntr") String tCntr,
                                          @RequestParam("tWarehousebillsfeesCr") String tWarehousebillsfeesCr,
                                          @RequestParam("tWarehousebillsfeesDr") String tWarehousebillsfeesDr,
                                          @RequestParam("tWarehousebillsitems") String tWarehousebillsitems) {
        String billsType = "SJRK";
        if (StringUtils.isNull(tWarehouseBills) || "{}".equals(tWarehouseBills)) {
            return AjaxResult.error("未找到仓库主表数据，请确认");
        }
        /*if (StringUtils.isNull(tWarehousebillsitems) || "[]".equals(tWarehousebillsitems)) {
            return AjaxResult.error("未找到仓库明细数据，请确认");
        }*/
        LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());
        return itWarehouseBillsService.warehouseSubmission(tWarehouseBills, tWarehousebillsfeesCr,
                tWarehousebillsfeesDr, tWarehousebillsitems, tEnclosure, loginUser, tCntr, billsType);
    }

    /**
     * 提交仓库主表审批
     */
    @PreAuthorize("@ss.hasPermi('warehouseBusiness:inStock:add')")
    @Log(title = "详情主表", businessType = BusinessType.INSERT)
    @PostMapping(value = "/submitWarehousingApproval")
    @RepeatSubmit
    public AjaxResult submitWarehousingApproval(@RequestParam("tWarehouseBills") String tWarehouseBills,
                                                @RequestParam("tEnclosure") String tEnclosure,
                                                @RequestParam("tCntr") String tCntr,
                                                @RequestParam("tWarehousebillsfeesCr") String tWarehousebillsfeesCr,
                                                @RequestParam("tWarehousebillsfeesDr") String tWarehousebillsfeesDr,
                                                @RequestParam("tWarehousebillsitems") String tWarehousebillsitems) {
        String billsType = "SJRK";
        if (StringUtils.isNull(tWarehouseBills) || "{}".equals(tWarehouseBills)) {
            return AjaxResult.error("未找到仓库主表数据，请确认");
        }
        return itWarehouseBillsService.submitWarehousingApproval(tWarehouseBills, tWarehousebillsfeesCr,
                tWarehousebillsfeesDr, tWarehousebillsitems, tEnclosure, tCntr, billsType);
    }

    /**
     * 修改入库详情主表
     */
    @PreAuthorize("@ss.hasPermi('warehouseBusiness:inStock:edit')")
    @Log(title = "详情主表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TWarehouseBills tWarehouseBills) {
        return toAjax(itWarehouseBillsService.updateTWarehousebills(tWarehouseBills));
    }

    /**
     * 删除入库详情主表
     */
    @PreAuthorize("@ss.hasPermi('warehouseBusiness:inStock:remove')")
    @Log(title = "详情主表", businessType = BusinessType.DELETE)
    @DeleteMapping("/{fIds}")
    @RepeatSubmit
    public AjaxResult remove(@PathVariable Long[] fIds) {
        return itWarehouseBillsService.deleteTWarehousebillsByIds(fIds);
    }

    @PreAuthorize("@ss.hasPermi('warehouseBusiness:inStock:remove')")
    @Log(title = "详情主表", businessType = BusinessType.DELETE)
    @DeleteMapping("/queryRemove/{fId}")
    public AjaxResult queryRemove(@PathVariable("fId") Long fId) {
        return itWarehouseBillsService.queryRemove(fId);
    }

    /**
     * 撤销已审核通过的入库信息
     */
    @PreAuthorize("@ss.hasPermi('warehouseBusiness:inStock:revoke')")
    @Log(title = "详情主表", businessType = BusinessType.UPDATE)
    @GetMapping("/withdrawById/{fId}")
    @RepeatSubmit
    public AjaxResult withdrawById(@PathVariable("fId") Long fId) {
        // 查询是否对账、收费、付费
        int result = itWarehouseBillsService.derecognition(fId);
        if (result == 1) {
            return AjaxResult.error("撤销请核失败，财务已对账");
        } else if (result == 2) {
            return AjaxResult.error("撤销请核失败，财务已收费");
        } else if (result == 3) {
            return AjaxResult.error("撤销请核失败，财务已付费");
        }
        String billsType = "SJRKRevoke";
        // 获取当前的用户
        LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());
        return itWarehouseBillsService.revokeTWarehousebillsById(fId, loginUser, billsType);
    }

    /**
     * 撤销已审核通过的接单入库信息
     */
    @Log(title = "详情主表", businessType = BusinessType.UPDATE)
    @GetMapping("/withdrawOrdersById/{fId}")
    @RepeatSubmit
    public AjaxResult withdrawOrdersById(@PathVariable("fId") Long fId) {
        return itWarehouseBillsService.withdrawOrdersById(fId);
    }

    /**
     * 撤销入库
     */
    @PreAuthorize("@ss.hasPermi('warehouseBusiness:inStock:add')")
    @Log(title = "详情主表", businessType = BusinessType.INSERT)
    @PostMapping(value = "/updateCredit")
    public AjaxResult deleteCredit(@RequestParam("warehouseBills") String warehouseBills,
                                   @RequestParam("tEnclosure") String tEnclosure,
                                   @RequestParam("tCntr") String tCntr,
                                   @RequestParam("warehousebillsfeesCr") String warehousebillsfeesCr,
                                   @RequestParam("warehousebillsfeesDr") String warehousebillsfeesDr,
                                   @RequestParam("warehousebillsitems") String warehousebillsitems) {
        String billsType = "SJRKRevoke";
        // 获取当前的用户
        LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());
        return itWarehouseBillsService.revokeTWarehousebillsById1(warehouseBills, warehousebillsitems,
                tEnclosure, warehousebillsfeesCr, warehousebillsfeesDr, loginUser, tCntr, billsType);
    }

    /**
     * 打印作业单  状态 改为 -->  待入库
     * 生成 流水号 --> log 记录 20
     * waitWarehousing
     */
    @PreAuthorize("@ss.hasPermi('warehouseBusiness:inStock:add')")
    @Log(title = "详情主表", businessType = BusinessType.INSERT)
    @PostMapping(value = "/waitWarehousing")
    @RepeatSubmit
    public AjaxResult waitWarehousing(@RequestParam("warehouseBills") String warehouseBills,
                                      @RequestParam("tEnclosure") String tEnclosure,
                                      @RequestParam("tCntr") String tCntr,
                                      @RequestParam("warehousebillsfeesCr") String warehousebillsfeesCr,
                                      @RequestParam("warehousebillsfeesDr") String warehousebillsfeesDr,
                                      @RequestParam("warehousebillsitems") String warehousebillsitems
    ) {
        Long billsType = 20L;
        // 获取当前的用户
        LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());
        return itWarehouseBillsService.waitWarehousing(warehouseBills, warehousebillsitems, tEnclosure,
                warehousebillsfeesCr, warehousebillsfeesDr, loginUser, tCntr, billsType, "RK");
    }


    /**
     * 卸货      状态 改为 -->  卸货中
     * log 记录 30
     * ischargeCargo
     */
    @PreAuthorize("@ss.hasPermi('warehouseBusiness:inStock:add')")
    @Log(title = "详情主表", businessType = BusinessType.INSERT)
    @PostMapping(value = "/ischargeCargo")
    @RepeatSubmit
    public AjaxResult ischargeCargo(@RequestParam("warehouseBills") String tWarehouseBills,
                                    @RequestParam("tEnclosure") String tEnclosure,
                                    @RequestParam("tCntr") String tCntr,
                                    @RequestParam("warehousebillsfeesCr") String tWarehousebillsfeesCr,
                                    @RequestParam("warehousebillsfeesDr") String tWarehousebillsfeesDr,
                                    @RequestParam("warehousebillsitems") String tWarehousebillsitems) {
        Long billsType = 30L;
        // 获取当前的用户
        LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());
        return itWarehouseBillsService.waitWarehousing(tWarehouseBills, tWarehousebillsitems, tEnclosure,
                tWarehousebillsfeesCr, tWarehousebillsfeesDr, loginUser, tCntr, billsType, "1");
    }

    /**
     * 入库确认   状态 改为 -->  已入库
     * log 记录 40
     */
    @PreAuthorize("@ss.hasPermi('warehouseBusiness:inStock:add')")
    @Log(title = "详情主表", businessType = BusinessType.INSERT)
    @PostMapping(value = "/addCredit")
    @RepeatSubmit
    public AjaxResult addCredit(@RequestParam("warehouseBills") String warehouseBills,
                                @RequestParam("tEnclosure") String tEnclosure,
                                @RequestParam("tCntr") String tCntr,
                                @RequestParam("warehousebillsfeesCr") String warehousebillsfeesCr,
                                @RequestParam("warehousebillsfeesDr") String warehousebillsfeesDr,
                                @RequestParam("warehousebillsitems") String warehousebillsitems) {
        String billsType = "SJRK";
        if (StringUtils.isNull(warehouseBills) || "{}".equals(warehouseBills)) {
            return AjaxResult.error("未找到主表信息，请确认");
        }
        if (StringUtils.isNull(warehousebillsitems) || "[]".equals(warehousebillsitems)) {
            return AjaxResult.error("未找到明细信息，请确认");
        }
        // 获取当前的用户
        LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());
        return itWarehouseBillsService.addCredit(warehouseBills, warehousebillsitems, tEnclosure, warehousebillsfeesCr,
                warehousebillsfeesDr, loginUser, tCntr, billsType);
    }

    /**
     * 入库明细提交审批确认
     */
    @PreAuthorize("@ss.hasPermi('warehouseBusiness:inStock:add')")
    @Log(title = "详情主表", businessType = BusinessType.INSERT)
    @GetMapping(value = "/addDetails/{fId}")
    @RepeatSubmit
    public AjaxResult addDetails(@PathVariable("fId") Long fId) {
        String billsType = "SJRKITEM";
        LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());
        return itWarehouseBillsService.addDetails(fId, billsType, loginUser);
    }

    /**
     * 明細流水号
     */
    @PostMapping(value = "/serialNumber")
    public AjaxResult serialNumber() {
        String billsType = "SJRK";
        return itWarehouseBillsService.serialNumber(billsType);
    }

    /**
     * 根据id更新费用审核状态
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/feeReview")
    @RepeatSubmit
    public AjaxResult feeReviewSubmit(@RequestParam("id") Long id) {
        return itWarehouseBillsService.feeReviewSubmit(id);
    }

    /**
     * 根据id撤销已费用审核状态
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/revokefeeReview")
    @RepeatSubmit
    public AjaxResult revokefeeReview(@RequestParam("id") Long id) {
        return itWarehouseBillsService.revokefeeReview(id);
    }

    /**
     * 快速入库-暂存
     */
    @PreAuthorize("@ss.hasPermi('warehouseBusiness:inStock:add')")
    @Log(title = "详情主表", businessType = BusinessType.INSERT)
    @PostMapping(value = "/quick_staging")
    @RepeatSubmit
    public AjaxResult quickStaging(@RequestBody WarehousebillsDTO warehousebillsDTO) {
        // 判断有箱号 就加箱号判断、无箱号则不加箱号判断
        warehousebillsDTO.setfBilltype(WarehouseTypeEnum.SJRK.getType());
        // 判断是提单号是否在一天当中出现第二次或以上
        itWarehouseBillsService.checkMblNoRepeat(warehousebillsDTO);
        LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());
        return itWarehouseBillsService.quickStaging(warehousebillsDTO, loginUser);
    }

    /**
     * 快速入库-提交
     */
    @PreAuthorize("@ss.hasPermi('warehouseBusiness:inStock:add')")
    @Log(title = "详情主表", businessType = BusinessType.INSERT)
    @PostMapping(value = "/quick_storage")
    @RepeatSubmit
    public AjaxResult quickStorage(@RequestBody WarehousebillsDTO warehousebillsDTO) {
        // 判断有箱号 就加箱号判断、无箱号则不加箱号判断
        warehousebillsDTO.setfBilltype(WarehouseTypeEnum.SJRK.getType());
        // 判断是提单号是否在一天当中出现第二次或以上
        itWarehouseBillsService.checkMblNoRepeat(warehousebillsDTO);
        LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());
        return itWarehouseBillsService.quickOperation(warehousebillsDTO, loginUser);
    }

}
