package com.bonus.material.lease.controller;

import cn.hutool.core.convert.Convert;
import com.bonus.common.biz.annotation.StoreLog;
import com.bonus.common.biz.config.ListPagingUtil;
import com.bonus.common.biz.domain.lease.LeaseApplyInfoExport;
import com.bonus.common.biz.domain.lease.LeaseOutDetails;
import com.bonus.common.core.utils.ServletUtils;
import com.bonus.common.core.utils.bean.BeanUtils;
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.archives.service.ArchivesService;
import com.bonus.material.basic.domain.BmQrcodeInfo;
import com.bonus.material.basic.domain.ProjUsingRecord;
import com.bonus.material.basic.domain.ProjUsingRecordExport;
import com.bonus.material.common.annotation.PreventRepeatSubmit;
import com.bonus.common.biz.domain.lease.LeaseApplyInfo;
import com.bonus.material.lease.domain.LeaseApplyDetails;
import com.bonus.material.lease.domain.vo.LeaseApplyRequestVo;
import com.bonus.common.biz.domain.lease.LeaseOutRequestVo;
import com.bonus.material.lease.service.ILeaseApplyInfoService;
import com.bonus.system.api.RemoteFileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

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

/**
 * 领料任务Controller
 *
 * @author xsheng
 * @date 2024-10-16
 */
@Api(tags = "领料任务接口")
@RestController
@RequestMapping("/lease_apply_info")
public class LeaseApplyInfoController extends BaseController {

    @Resource
    private ILeaseApplyInfoService leaseApplyInfoService;

    @Resource
    private RemoteFileService sysFileService;

    @Resource
    private ArchivesService archivesService;

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

    /**
     * 统计时间段出库总数量
     * @param leaseApplyInfo
     * @return
     */
    @ApiOperation(value = "统计时间段出库总数量")
    @GetMapping("/getOutNum")
    public AjaxResult getOutNum(LeaseApplyInfo leaseApplyInfo) {
        LeaseApplyInfo info = leaseApplyInfoService.getOutNum(leaseApplyInfo);
        return AjaxResult.success(info);
    }

    /**
     * 查询已完成出库的任务
     */
    @ApiOperation(value = "查询已完成出库的任务")
    //@RequiresPermissions("lease:info:list")
    @GetMapping("/getCompleteOutTaskList")
    public AjaxResult getCompleteOutTaskList(LeaseApplyInfo leaseApplyInfo) {
        Integer pageIndex = Convert.toInt(ServletUtils.getParameter("pageNum"), 1);
        Integer pageSize = Convert.toInt(ServletUtils.getParameter("pageSize"), 10);
        List<LeaseApplyInfo> list = leaseApplyInfoService.getCompleteOutTaskList(leaseApplyInfo);
        return AjaxResult.success(ListPagingUtil.paging(pageIndex, pageSize, list));
    }
    /**
     * 材料员查询页面
     */
    @ApiOperation(value = "材料员查询页面")
    //@RequiresPermissions("lease:info:list")
    @GetMapping("/getConfirmTaskList")
    public AjaxResult getConfirmTaskList(LeaseApplyInfo leaseApplyInfo) {
        Integer pageIndex = Convert.toInt(ServletUtils.getParameter("pageNum"), 1);
        Integer pageSize = Convert.toInt(ServletUtils.getParameter("pageSize"), 10);
        List<LeaseApplyInfo> list = leaseApplyInfoService.getConfirmTaskList(leaseApplyInfo);
        return AjaxResult.success(ListPagingUtil.paging(pageIndex, pageSize, list));
    }

    @ApiOperation(value = "材料员确认接口")
    @PostMapping("/confirmMaterial")
    public AjaxResult confirmMaterial(@RequestBody LeaseApplyDetails detail) {
        int confirmResult = leaseApplyInfoService.confirmLeaseTask(detail);
        return confirmResult > 0 ? success("确认成功") : error("确认失败");
    }

    /**
     * 上传领料单PDF文件
     */
    @PostMapping("/uploadPdf")
    @SysLog(title = "上传领料单PDF", businessType = OperaType.INSERT, module = "仓储管理->上传领料单PDF")
    public AjaxResult uploadPdf(@RequestParam("file") MultipartFile file, @RequestParam("id") Long id) {
        try {
            AjaxResult result = sysFileService.upload(file);
            if (!result.isSuccess()) {
                return AjaxResult.error("SysFile文件服务上传失败：" + result.get("msg"));
            }

            @SuppressWarnings("unchecked")
            Map<String, Object> jsonObject = (Map<String, Object>) result.get("data");
            String fileName = file.getOriginalFilename();
            String filePath = jsonObject.get("url").toString();
            return archivesService.uploadArchivesProjectPdf("领料单", fileName, filePath, Math.toIntExact(id));
        } catch (Exception e) {
            logger.error("上传领料单PDF文件失败", e);
            return AjaxResult.error("上传领料单PDF文件失败：" + e.getMessage());
        }
    }

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

    /**
     * 导出领料出库列表
     */
    @ApiOperation(value = "导出领料出库列表")
    @PreventRepeatSubmit
    //@RequiresPermissions("lease:info:export")
    @SysLog(title = "领料出库", businessType = OperaType.EXPORT, logType = 1,module = "仓储管理->导出领料出库")
    @PostMapping("/exportLeaseOutRecord")
    public void exportLeaseOutRecord(HttpServletResponse response, LeaseApplyInfo leaseApplyInfo) {
        List<LeaseApplyInfo> list = leaseApplyInfoService.selectLeaseApplyInfoList(leaseApplyInfo);
        List<LeaseApplyInfoExport> exportList = new ArrayList<>();
        for (LeaseApplyInfo leaseApplyInfo1 : list) {
            LeaseApplyInfoExport leaseApplyInfoExport = new LeaseApplyInfoExport();
            BeanUtils.copyProperties(leaseApplyInfo1, leaseApplyInfoExport);
            exportList.add(leaseApplyInfoExport);
        }
        ExcelUtil<LeaseApplyInfoExport> util = new ExcelUtil<>(LeaseApplyInfoExport.class);
        util.exportExcel(response, exportList, "领料出库数据");
    }

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

    /**
     * 获取领料出库详细信息
     * @param bean
     * @return
     */
    @ApiOperation(value = "获取领料出库详细信息")
    //@RequiresPermissions("lease:info:query")
    @GetMapping(value = "/getOutInfo")
    public AjaxResult getOutInfo(LeaseApplyInfo bean) {
        return success(leaseApplyInfoService.getOutInfo(bean));
    }

    /**
     * 获取领料任务详细信息
     */
    @ApiOperation(value = "获取领料任务详细信息")
    //@RequiresPermissions("lease:info:query")
    @GetMapping(value = "/getInfoById")
    public AjaxResult getLeaseInfo(LeaseApplyInfo bean) {
        return success(leaseApplyInfoService.selectLeaseApplyInfoByIdTwo(bean));
    }

    /**
     * 获取领料单
     */
    @ApiOperation(value = "获取领料单")
    //@RequiresPermissions("lease:info:query")
    @GetMapping(value = "/getLeaseRequestVo")
    public AjaxResult getLeaseRequestVo(LeaseApplyDetails bean) {
        return success(leaseApplyInfoService.getLeaseRequestVo(bean));
    }

    /**
     * 获取领料出库内部详细信息
     * @param leaseApplyDetails
     * @return
     */
    @ApiOperation(value = "获取领料出库内部详细信息")
    //@RequiresPermissions("lease:info:query")
    @GetMapping(value = "/getInnerById")
    public AjaxResult getInnerById(LeaseApplyDetails leaseApplyDetails) {
        return success(leaseApplyInfoService.getInnerById(leaseApplyDetails));
    }

    /**
     * 获取领料出库内部详细信息
     * @param leaseApplyDetails
     * @return
     */
    @ApiOperation(value = "获取领料出库内部详细信息")
    //@RequiresPermissions("lease:info:query")
    @GetMapping(value = "/getDetailsById")
    public AjaxResult getDetailsById(LeaseApplyDetails leaseApplyDetails) {
        startPage();
        List<LeaseApplyDetails> list = leaseApplyInfoService.getDetailsById(leaseApplyDetails);
        return AjaxResult.success(getDataTable(list));
    }

    /**
     * 获取领料出库单详细信息
     * @param leaseApplyInfo
     * @return
     */
    @ApiOperation(value = "获取领料出库单详细信息")
    //@RequiresPermissions("lease:info:query")
    @GetMapping(value = "/getInfo")
    public AjaxResult getInfo(LeaseApplyInfo leaseApplyInfo) {
        return success(leaseApplyInfoService.getInfo(leaseApplyInfo));
    }

    @ApiOperation(value = "导出领料出库单详情")
    //@RequiresPermissions("lease:info:export")
    @SysLog(title = "领料任务", businessType = OperaType.EXPORT, logType = 1,module = "仓储管理->导出领料任务详情")
    @PostMapping("/exportInfo")
    public void exportInfo(HttpServletResponse response, LeaseApplyInfo leaseApplyInfo) {
        LeaseApplyRequestVo leaseApplyRequestVo =  leaseApplyInfoService.getInfo(leaseApplyInfo);
        leaseApplyInfoService.exportInfo(leaseApplyInfo, leaseApplyRequestVo, response);
    }

    @ApiOperation(value = "导出领料任务详情")
    @PreventRepeatSubmit
    //@RequiresPermissions("lease:info:export")
    @SysLog(title = "领料任务", businessType = OperaType.EXPORT, logType = 1,module = "仓储管理->导出领料任务详情")
    @PostMapping("/exportDetails")
    public void exportDetails(HttpServletResponse response, LeaseApplyInfo leaseApplyInfo) {
        List<LeaseApplyDetails> list = leaseApplyInfoService.selectLeaseApplyDetailsList(leaseApplyInfo);
        ExcelUtil<LeaseApplyDetails> util = new ExcelUtil<>(LeaseApplyDetails.class);
        util.exportExcel(response, list, "领料任务详情数据");
    }

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

    /**
     * 修改领料任务
     */
    @ApiOperation(value = "修改领料任务")
    @PreventRepeatSubmit
    //@RequiresPermissions("lease:info:edit")
    @SysLog(title = "领料任务", businessType = OperaType.UPDATE, logType = 1,module = "仓储管理->修改领料任务")
    @PutMapping
    public AjaxResult edit(@RequestBody @NotNull LeaseApplyRequestVo leaseApplyRequestVo) {
        try {
            return toAjax(leaseApplyInfoService.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 LeaseApplyInfo leaseApplyInfo) {
        try {
            return toAjax(leaseApplyInfoService.updateLeaseApplyInfoSign(leaseApplyInfo));
        } catch (Exception e) {
            return error("系统错误, " + e.getMessage());
        }
    }

    /**
     * 领料任务发布
     */
    @ApiOperation(value = "领料任务发布")
    @PreventRepeatSubmit
    @SysLog(title = "领料任务发布", businessType = OperaType.UPDATE, logType = 1,module = "仓储管理->发布领料任务")
    //@RequiresPermissions("lease:info:add")
    @PostMapping("/publish")
    public AjaxResult publish(@RequestBody @NotNull(message = "任务信息不能为空") LeaseApplyInfo leaseApplyInfo) {
        return leaseApplyInfoService.publish(leaseApplyInfo);
    }

    /**
     * 领料任务批量发布
     */
    @ApiOperation(value = "领料任务批量发布")
    @PreventRepeatSubmit
    @SysLog(title = "领料任务批量发布", businessType = OperaType.UPDATE, logType = 1,module = "仓储管理->批量发布领料任务")
    //@RequiresPermissions("lease:info:add")
    @PostMapping("/publishBatch")
    public AjaxResult publishBatch(@RequestBody @NotNull(message = "任务信息不能为空") List<LeaseApplyInfo> leaseApplyInfos) {
        for (LeaseApplyInfo leaseApplyInfo : leaseApplyInfos) {
            leaseApplyInfoService.publish(leaseApplyInfo);
        }
        return success("批量发布完成");
    }

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


    /**
     * 领料：出库
     */
    @ApiOperation(value = "领料出库")
    @PreventRepeatSubmit
    //@RequiresPermissions("lease:info:leaseOut")
    @SysLog(title = "领料出库", businessType = OperaType.INSERT, logType = 1,module = "仓储管理->领料出库")
    @StoreLog(title = "领料出库", module = "仓储管理->领料出库")
    @PostMapping("/leaseOut")
    public AjaxResult leaseOut(@NotNull(message = "领料出库信息不能为空") @RequestBody LeaseOutRequestVo leaseOutRequestVo) {
        try {
            return leaseApplyInfoService.leaseOut(leaseOutRequestVo);
        } catch (Exception e) {
            return error("系统错误, " + e.getMessage());
        }
    }

    /**
     * 信息采集领料出库
     * @param leaseOutRequestVo
     * @return
     */
    @ApiOperation(value = "信息采集领料出库")
    @PostMapping("/leaseOutByInfo")
    public AjaxResult leaseOutByInfo(@NotNull(message = "领料出库信息不能为空") @RequestBody LeaseOutRequestVo leaseOutRequestVo) {
        try {
            return leaseApplyInfoService.leaseOutByInfo(leaseOutRequestVo);
        } catch (Exception e) {
            return error("系统错误, " + e.getMessage());
        }
    }

    /**
     * app领料出库退回
     * @param leaseOutDetails
     * @return
     */
    @ApiOperation(value = "app领料出库退回")
    @PreventRepeatSubmit
    @PostMapping("/leaseOutBack")
    public AjaxResult leaseOutBack(@NotNull(message = "领料出库信息不能为空") @RequestBody LeaseOutDetails leaseOutDetails) {
        try {
            return leaseApplyInfoService.leaseOutBack(leaseOutDetails);
        } catch (Exception e) {
            return error("系统错误, " + e.getMessage());
        }
    }

    /**
     * 删除领料任务
     */
    @ApiOperation(value = "删除领料任务")
    @PreventRepeatSubmit
    //@RequiresPermissions("lease:info:remove")
    @SysLog(title = "领料任务", businessType = OperaType.DELETE, logType = 1,module = "仓储管理->删除领料任务")
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(leaseApplyInfoService.deleteLeaseApplyInfoByIds(ids));
    }


    /**
     * 领用任务批量审核
     */
    @ApiOperation(value = "领用任务批量审核")
    @PreventRepeatSubmit
    @SysLog(title = "领用任务批量审核", businessType = OperaType.UPDATE, logType = 1,module = "领用审核->领用任务批量审核")
    @PostMapping("/useExamine")
    public AjaxResult useExamine(@RequestBody @NotNull(message = "任务信息不能为空") List<LeaseApplyInfo> leaseApplyInfos) {
        for (LeaseApplyInfo leaseApplyInfo : leaseApplyInfos) {
            leaseApplyInfoService.useExamine(leaseApplyInfo);
        }
        return success("审核完成");
    }

    /**
     * 修改预出库数量
     */
    @ApiOperation(value = "修改预出库数量")
    @SysLog(title = "修改预出库数量", businessType = OperaType.UPDATE, logType = 1,module = "领料出库->修改预出库数量")
    @PostMapping("/updateLeaseNum")
    public AjaxResult updateLeaseNum(@RequestBody LeaseApplyDetails leaseApplyDetails) {
        return leaseApplyInfoService.updateLeaseNum(leaseApplyDetails);
    }
}
