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.warehouseBusiness.domain.TWarehouseBills;
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 java.util.List;
import java.util.Map;

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

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



    /**
     * 查询出库详情主表列表
     */
    @GetMapping("/getWarehouseOutStock")
    @DataScope(deptAlias = "d", userAlias = "u")
    public String getWarehouseOutStockBillsNo(TWarehouseBills tWarehouseBills) {
        return itWarehouseBillsService.getWarehouseOutStockBillsNo(tWarehouseBills);
    }

    /**
     * 出库提交
     */
    @PreAuthorize("@ss.hasPermi('warehouseBusiness:outStock:add')")
    @Log(title = "详情主表", businessType = BusinessType.INSERT)
    @PostMapping(value = "/warehouseSubmission")
    @RepeatSubmit
    public AjaxResult warehouseSubmission(@RequestParam("warehouseBills") String tWarehouseBills,
                                          @RequestParam("tEnclosure") String tEnclosure,
                                          @RequestParam("warehousebillsfeesCr") String tWarehousebillsfeesCr,
                                          @RequestParam("warehousebillsfeesDr") String tWarehousebillsfeesDr,
                                          @RequestParam("warehousebillsitems") String tWarehousebillsitems) {
        String billsType = "SJCK";
        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, billsType);
    }

    /**
     * 导出出库详情主表列表
     */
    @PreAuthorize("@ss.hasPermi('warehouseBusiness:outStock:export')")
    @Log(title = "详情主表", businessType = BusinessType.EXPORT)
    @RepeatSubmit
    @GetMapping("/export")
    public AjaxResult export(TWarehouseBills tWarehouseBills) {
        tWarehouseBills.setfBilltype("SJCK");
        List<TWarehouseBills> list = itWarehouseBillsService.selectTWarehousebillsList(tWarehouseBills);
        ExcelUtil<TWarehouseBills> util = new ExcelUtil<TWarehouseBills>(TWarehouseBills.class);
        return util.exportExcel(list, "warehousebills");
    }

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

    /**
     * 出库暂存
     */
    @PreAuthorize("@ss.hasPermi('warehouseBusiness:outStock:add')")
    @Log(title = "详情主表", businessType = BusinessType.INSERT)
    @PostMapping(value = "/add")
    @RepeatSubmit
    public AjaxResult add(@RequestParam("warehouseBills") String tWarehouseBills,
                          @RequestParam("tEnclosure") String tEnclosure,
                          @RequestParam("warehousebillsfeesCr") String tWarehousebillsfeesCr,
                          @RequestParam("warehousebillsfeesDr") String tWarehousebillsfeesDr,
                          @RequestParam("warehousebillsitems") String tWarehousebillsitems) {
        String billsType = "SJCK";
        if (StringUtils.isEmpty(tWarehouseBills) || "{}".equals(tWarehouseBills)) {
            return AjaxResult.error("提交失败：请检查主表数据");
        }
        // 获取当前的用户
        LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());
        return itWarehouseBillsService.insertTWarehousebills(tWarehouseBills, tWarehousebillsfeesCr,
                tWarehousebillsfeesDr, tWarehousebillsitems, tEnclosure, loginUser, billsType);
    }


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

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

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


    /**
     *  撤销出库信息
     */
    @PreAuthorize("@ss.hasPermi('warehouseBusiness:outStock:revoke')")
    @Log(title = "详情主表", businessType = BusinessType.UPDATE)
    @GetMapping("/get/{fId}")
    @RepeatSubmit
    public AjaxResult revoke(@PathVariable("fId") Long fId) {
        String  billsType="SJCKRevoke";
        // 获取当前的用户
        LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());
        return itWarehouseBillsService.revokeTWarehousebillsById(fId,loginUser,billsType);
    }


    /**
     * 撤销入库信息
     */
    @PreAuthorize("@ss.hasPermi('warehouseBusiness:outStock:edit')")
    @Log(title = "详情主表", businessType = BusinessType.UPDATE)
    @GetMapping("/withdrawById/{fId}")
    @RepeatSubmit
    public AjaxResult withdrawById(@PathVariable("fId") Long fId) {
        String billsType = "SJCKRevoke";
        // 获取当前的用户
        LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());
        return itWarehouseBillsService.revokeTWarehousebillsById(fId, loginUser, billsType);
    }

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

    /**
     *  打印作业单  状态 改为 -->  待出库
     *      生成 流水号 --> log 记录 20
     *      waitWarehousing
     */
    @PreAuthorize("@ss.hasPermi('warehouseBusiness:outStock:add')")
    @Log(title = "详情主表", businessType = BusinessType.INSERT)
    @PostMapping(value = "/waitWarehousing")
    @RepeatSubmit
    public AjaxResult waitWarehousing(@RequestParam("warehouseBills") String warehouseBills,
                                      @RequestParam("tEnclosure") String tEnclosure,
                                      @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, billsType , "CK");
    }

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


    /**
     *   出库确认   状态 改为 -->  已出库
     *                   log 记录 40
     */
    @PreAuthorize("@ss.hasPermi('warehouseBusiness:outStock:add')")
    @Log(title = "详情主表", businessType = BusinessType.INSERT)
    @PostMapping(value = "/addCredit")
    @RepeatSubmit
    public AjaxResult addCredit(@RequestParam("warehouseBills") String warehouseBills,
                                @RequestParam("tEnclosure") String tEnclosure,
                                @RequestParam("warehousebillsfeesCr") String warehousebillsfeesCr,
                                @RequestParam("warehousebillsfeesDr") String warehousebillsfeesDr,
                                @RequestParam("warehousebillsitems") String warehousebillsitems) {
        String billsType = "SJCK";
        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.updateCredit(warehouseBills, warehousebillsitems, tEnclosure,
                warehousebillsfeesCr, warehousebillsfeesDr, loginUser, billsType);
    }

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

}
