package com.bonus.material.clz.controller;

import cn.hutool.core.convert.Convert;
import com.alibaba.nacos.common.utils.CollectionUtils;
import com.bonus.common.biz.config.ListPagingUtil;
import com.bonus.common.core.utils.ServletUtils;
import com.bonus.common.core.utils.poi.ExcelUtil;
import com.bonus.common.core.web.controller.BaseController;
import com.bonus.common.core.web.domain.AjaxResult;
import com.bonus.common.log.annotation.SysLog;
import com.bonus.common.log.enums.OperaType;
import com.bonus.material.back.domain.vo.MaCodeVo;
import com.bonus.material.basic.domain.BmQrcodeInfo;
import com.bonus.material.common.annotation.PreventRepeatSubmit;
import com.bonus.material.clz.domain.lease.LeaseApplyDetailsInfo;
import com.bonus.material.clz.domain.lease.LeaseOutDetailsInfo;
import com.bonus.material.clz.domain.lease.MaterialLeaseApplyDetails;
import com.bonus.material.clz.domain.lease.MaterialLeaseApplyInfo;
import com.bonus.material.clz.domain.vo.lease.LeaseTotalInfo;
import com.bonus.material.clz.domain.vo.lease.MaterialLeaseApplyRequestVo;
import com.bonus.material.clz.service.MaterialLeaseInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @Author ma_sh
 * @create 2025/6/24 14:34
 */
@Api(tags = "材料站领料任务接口")
@RestController
@RequestMapping("/material_lease_apply_info")
public class MaterialLeaseInfoController extends BaseController {

    @Resource
    private MaterialLeaseInfoService materialLeaseInfoService;

    /**
     * 查询领料任务列表
     */
    @ApiOperation(value = "查询领料任务列表")
    @GetMapping("/list")
    public AjaxResult list(MaterialLeaseApplyInfo leaseApplyInfo) {
        Integer pageIndex = Convert.toInt(ServletUtils.getParameter("pageNum"), 1);
        Integer pageSize = Convert.toInt(ServletUtils.getParameter("pageSize"), 10);
        List<MaterialLeaseApplyInfo> list = materialLeaseInfoService.selectLeaseApplyInfoList(leaseApplyInfo);
        return AjaxResult.success(ListPagingUtil.paging(pageIndex, pageSize, list));
    }

    /**
     * 查询总站点领料记录数据
     * @param leaseApplyInfo
     * @return
     */
    @ApiOperation(value = "查询总站点领料记录数据")
    @GetMapping("/getTotalList")
    public AjaxResult getTotalList(MaterialLeaseApplyInfo leaseApplyInfo) {
        Integer pageIndex = Convert.toInt(ServletUtils.getParameter("pageNum"), 1);
        Integer pageSize = Convert.toInt(ServletUtils.getParameter("pageSize"), 10);
        List<MaterialLeaseApplyInfo> list = materialLeaseInfoService.getTotalList(leaseApplyInfo);
        return AjaxResult.success(ListPagingUtil.paging(pageIndex, pageSize, list));
    }

    /**
     * 查询总站点领料详情数据
     * @param leaseApplyInfo
     * @return
     */
    @ApiOperation(value = "查询总站点领料详情数据")
    @GetMapping("/getDetailsList")
    public AjaxResult getDetailsList(MaterialLeaseApplyInfo leaseApplyInfo) {
        startPage();
        List<MaterialLeaseApplyInfo> list = materialLeaseInfoService.getDetailsList(leaseApplyInfo);
        return AjaxResult.success(getDataTable(list));
    }

    /**
     * 导出领料任务列表
     */
    @ApiOperation(value = "导出领料任务列表")
    @PreventRepeatSubmit
    @SysLog(title = "领料任务", businessType = OperaType.EXPORT, logType = 1,module = "仓储管理->导出领料任务")
    @PostMapping("/export")
    public void export(HttpServletResponse response, MaterialLeaseApplyInfo leaseApplyInfo) {
        List<MaterialLeaseApplyInfo> list = materialLeaseInfoService.selectLeaseApplyInfoList(leaseApplyInfo);
        ExcelUtil<MaterialLeaseApplyInfo> util = new ExcelUtil<>(MaterialLeaseApplyInfo.class);
        util.exportExcel(response, list, "领料任务数据");
    }

    /**
     * 获取领料任务详细信息
     * @param id
     * @param keyWord
     * @return
     */
    @ApiOperation(value = "获取领料任务详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@NotNull(message = "领料任务ID不能为空") @PathVariable("id")  Long id,
                              @RequestParam(value = "keyWord", required = false) String keyWord) {
        return materialLeaseInfoService.selectLeaseApplyInfoById(id, keyWord);
    }

    /**
     * 获取领料单信息
     * @param leaseApplyInfo
     * @return
     */
    @ApiOperation(value = "获取领料单信息")
    @GetMapping(value = "/getPickList")
    public AjaxResult getPickList(MaterialLeaseApplyInfo leaseApplyInfo) {
        return materialLeaseInfoService.getPickList(leaseApplyInfo);
    }

    @ApiOperation(value = "获取领料任务详细信息")
    @GetMapping(value = "/getOutNum")
    public AjaxResult getOutNum(MaterialLeaseApplyDetails leaseApplyDetails) {
        return success(materialLeaseInfoService.getOutNum(leaseApplyDetails));
    }

    /**
     * 获取工器具领料出库详情
     * @param leaseApplyInfo
     * @return
     */
    @ApiOperation(value = "获取工器具领料出库详情")
    @GetMapping(value = "/outDetailsById")
    public AjaxResult outDetailsById(MaterialLeaseApplyInfo leaseApplyInfo) {
        return success(materialLeaseInfoService.outDetailsById(leaseApplyInfo));
    }

    @ApiOperation(value = "导出工器具领料出库详情")
    @PostMapping("/exportOutDetails")
    public void exportOutDetails(HttpServletResponse response, MaterialLeaseApplyInfo leaseApplyInfo) {
        List<LeaseOutDetailsInfo> list = materialLeaseInfoService.exportOutDetails(leaseApplyInfo);
        ExcelUtil<LeaseOutDetailsInfo> util = new ExcelUtil<>(LeaseOutDetailsInfo.class);
        util.exportExcel(response, list, "工器具领料出库详情数据");
    }

    /**
     * 工器具领料数据
     * @param dto
     * @return
     */
    @ApiOperation(value = "工器具领料数据")
    @GetMapping(value = "/getLeaseInfo")
    public AjaxResult getLeaseInfo(LeaseApplyDetailsInfo dto) {
        dto.setIsExport(1);
        Integer pageIndex = Convert.toInt(ServletUtils.getParameter("pageNum"), 1);
        Integer pageSize = Convert.toInt(ServletUtils.getParameter("pageSize"), 10);
        List<LeaseApplyDetailsInfo> list = materialLeaseInfoService.getLeaseInfo(dto);
        return AjaxResult.success(ListPagingUtil.paging(pageIndex, pageSize, list));
    }

    /**
     * 工器具领料数据查询不带分页
     * @param dto
     * @return
     */
    @ApiOperation(value = "工器具领料数据查询不带分页")
    @GetMapping("/getLeaseInfoNoPage")
    public AjaxResult getLeaseInfoNoPage(LeaseApplyDetailsInfo dto) {
        dto.setIsExport(0);
        List<LeaseApplyDetailsInfo> list = materialLeaseInfoService.getLeaseInfo(dto);
        LeaseApplyDetailsInfo info = new LeaseApplyDetailsInfo();
        if (CollectionUtils.isNotEmpty(list)) {
            info = list.get(0);
        }
        return AjaxResult.success(info);
    }

    /**
     * 导出工器具领料数据查询
     * @param response
     * @param dto
     */
    @ApiOperation("导出工器具领料数据查询")
    @PostMapping("/exportRetainedEquipmentList")
    public void exportRetainedEquipmentList(HttpServletResponse response, LeaseApplyDetailsInfo dto) {
        dto.setIsExport(0);
        List<LeaseApplyDetailsInfo> list = materialLeaseInfoService.getLeaseInfo(dto);
        ExcelUtil<LeaseApplyDetailsInfo> util = new ExcelUtil<>(LeaseApplyDetailsInfo.class);
        util.exportExcel(response, list, "工器具领料数据查询");
    }

    /**
     * 新增领料任务
     * @param leaseApplyRequestVo
     * @return
     */
    @ApiOperation(value = "新增领料任务")
    @PreventRepeatSubmit
    @SysLog(title = "领料任务", businessType = OperaType.INSERT, logType = 1,module = "仓储管理->新增领料任务")
    @PostMapping
    public AjaxResult add(@NotNull(message = "领料任务不能为空") @RequestBody MaterialLeaseApplyRequestVo leaseApplyRequestVo) {
        try {
            return materialLeaseInfoService.insertLeaseApplyInfo(leaseApplyRequestVo);
        } catch (Exception e) {
            return error("系统错误, " + e.getMessage());
        }
    }

    /**
     * 修改领料任务
     */
    @ApiOperation(value = "修改领料任务")
    @PreventRepeatSubmit
    @SysLog(title = "领料任务", businessType = OperaType.UPDATE, logType = 1,module = "仓储管理->修改领料任务")
    @PutMapping
    public AjaxResult edit(@RequestBody @NotNull MaterialLeaseApplyRequestVo leaseApplyRequestVo) {
        try {
            return toAjax(materialLeaseInfoService.updateLeaseApplyInfo(leaseApplyRequestVo));
        } catch (Exception e) {
            return error("系统错误, " + e.getMessage());
        }
    }

    /**
     * 修改领料任务
     */
    @ApiOperation(value = "领料任务电子签名")
    @PreventRepeatSubmit
    @SysLog(title = "领料任务电子签名", businessType = OperaType.UPDATE, logType = 1,module = "仓储管理->修改领料任务签字")
    @PostMapping("/updateLeaseApplyInfoSign")
    public AjaxResult updateLeaseApplyInfoSign(@RequestBody @NotNull MaterialLeaseApplyInfo leaseApplyInfo) {
        try {
            return toAjax(materialLeaseInfoService.updateLeaseApplyInfoSign(leaseApplyInfo));
        } catch (Exception e) {
            return error("系统错误, " + e.getMessage());
        }
    }

    /**
     * 二维码出库：根据qrcode查询在库机具信息
     * @param bmQrcodeInfo
     * @return
     */
    @ApiOperation(value = "根据qrcode查询在库机具信息")
    @GetMapping("/getInfoByQrcode")
    public AjaxResult getInfoByQrcode(BmQrcodeInfo bmQrcodeInfo) {
        return materialLeaseInfoService.getInfoByQrcode(bmQrcodeInfo);
    }

    /**
     * OCR出库：根据code查询在库机具信息
     * @param bmQrcodeInfo
     * @return
     */
    @ApiOperation(value = "根据code查询在库机具信息")
    @GetMapping("/getInfoByCode")
    public AjaxResult getInfoByCode(BmQrcodeInfo bmQrcodeInfo) {
        return materialLeaseInfoService.getInfoByCode(bmQrcodeInfo);
    }

    /**
     * app领料出库提交
     * @param leaseApplyDetails
     * @return
     */
    @ApiOperation(value = "app领料出库提交")
    @PostMapping("/submitLeaseOut")
    public AjaxResult submitLeaseOut(@RequestBody MaterialLeaseApplyDetails leaseApplyDetails) {
        try {
            return materialLeaseInfoService.submitLeaseOut(leaseApplyDetails);
        } catch (Exception e) {
            return error("系统错误, " + e.getMessage());
        }
    }

    /**
     * 删除领料任务
     */
    @ApiOperation(value = "删除领料任务")
    @PreventRepeatSubmit
    @SysLog(title = "领料任务", businessType = OperaType.DELETE, logType = 1,module = "仓储管理->删除领料任务")
    @PostMapping("/delete")
    public AjaxResult delete(@RequestBody MaterialLeaseApplyInfo info) {
        return toAjax(materialLeaseInfoService.deleteLeaseApplyInfoByIds(info));
    }

    /**
     * 工器具领料记录
     * @param info
     * @return
     */
    @ApiOperation(value = "工器具领料记录")
    @GetMapping("/getLeaseInfoDetails")
    public AjaxResult getLeaseInfoDetails(LeaseTotalInfo info) {
        info.setIsExport(1);
        Integer pageIndex = Convert.toInt(ServletUtils.getParameter("pageNum"), 1);
        Integer pageSize = Convert.toInt(ServletUtils.getParameter("pageSize"), 10);
        List<LeaseTotalInfo> list = materialLeaseInfoService.getLeaseInfoDetails(info);
        return AjaxResult.success(ListPagingUtil.paging(pageIndex, pageSize, list));
    }

    /**
     * 工器具领料记录不带分页
     * @param info
     * @return
     */
    @ApiOperation(value = "工器具领料记录不带分页")
    @GetMapping("/getLeaseInfoDetailsNoPageList")
    public AjaxResult getLeaseInfoDetailsNoPageList(LeaseTotalInfo info) {
        info.setIsExport(0);
        LeaseTotalInfo leaseTotalInfo = new LeaseTotalInfo();
        List<LeaseTotalInfo> list = materialLeaseInfoService.getLeaseInfoDetails(info);
        if (CollectionUtils.isNotEmpty(list)) {
            leaseTotalInfo = list.get(0);
        }
        return AjaxResult.success(leaseTotalInfo);
    }

    /**
     * 导出工器具领料记录查询
     * @param response
     * @param bean
     */
    @ApiOperation("导出工器具领料记录查询")
    @PostMapping("/exportLeaseInfoDetails")
    public void exportRetainedEquipmentList(HttpServletResponse response, LeaseTotalInfo bean) {
        bean.setIsExport(0);
        List<LeaseTotalInfo> list = materialLeaseInfoService.getLeaseInfoDetails(bean);
        ExcelUtil<LeaseTotalInfo> util = new ExcelUtil<>(LeaseTotalInfo.class);
        util.exportExcel(response, list, "综合查询--工器具领料记录查询");
    }

    /**
     * 工器具领料二级页面记录
     * @param info
     * @return
     */
    @ApiOperation(value = "工器具领料二级页面记录")
    @GetMapping("/getSecondLeaseInfo")
    public AjaxResult getSecondLeaseInfo(LeaseTotalInfo info) {
        startPage();
        List<MaCodeVo> list = materialLeaseInfoService.getSecondLeaseInfo(info);
        return AjaxResult.success(getDataTable(list));
    }

    /**
     * 导出工器具领料二级页面查询
     * @param response
     * @param bean
     */
    @ApiOperation("导出工器具领料二级页面查询")
    @PostMapping("/exportSecondLeaseInfo")
    public void exportSecondLeaseInfo(HttpServletResponse response, LeaseTotalInfo bean) {
        List<MaCodeVo> list = materialLeaseInfoService.getSecondLeaseInfo(bean);
        ExcelUtil<MaCodeVo> util = new ExcelUtil<>(MaCodeVo.class);
        util.exportExcel(response, list, "综合查询--工器具领料二级页面查询");
    }

    /**
     * 在库设备类型树
     * @param bean
     * @return
     */
    @ApiOperation(value = "在库设备类型树")
    @PostMapping("/equipmentType")
    public AjaxResult getUseTypeTree(@RequestBody MaterialLeaseApplyInfo bean) {
        try {
            return materialLeaseInfoService.getUseTypeTree(bean);
        } catch (Exception e) {
            return error("系统错误,请联系管理员");
        }
    }

    /**
     * 根据班组和工程id查询领料机具
     * @param dto
     * @return
     */
    @ApiOperation(value = "根据班组和工程id查询领料机具")
    @GetMapping("/getMachineById")
    public AjaxResult getMachineById(MaterialLeaseApplyInfo dto) {
        try {
            return materialLeaseInfoService.getMachineById(dto);
        } catch (Exception e) {
            return error("系统错误,请联系管理员");
        }
    }
}
