package com.bonus.material.basic.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.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.security.utils.SecurityUtils;
import com.bonus.material.basic.domain.ProjUsingRecordExports;
import com.bonus.material.basic.domain.*;
import com.bonus.material.basic.domain.vo.MaTypeSelectInfo;
import com.bonus.material.basic.service.ComplexQueryService;
import com.bonus.material.lease.mapper.LeaseApplyInfoMapper;
import com.bonus.material.ma.domain.Type;
import com.bonus.system.api.model.LoginUser;
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 java.util.ArrayList;
import java.util.List;

/**
 * @Author ma_sh
 * @create 2024/12/16 10:52
 */
@Api(tags = "综合查询")
@RestController
@RequestMapping("/complex_query")
public class ComplexQueryController extends BaseController {

    @Resource
    private ComplexQueryService complexQueryService;

    /**
     * 保有设备总量查询
     * @param bean
     * @return
     */
    @ApiOperation(value = "综合查询--保有设备总量查询")
    @GetMapping("/getRetainedEquipmentList")
    public AjaxResult getRetainedEquipmentList(RetainedEquipmentInfo bean) {
        bean.setIsExport(1);
        bean.setUserId(SecurityUtils.getLoginUser().getUserid());
        if (bean.getIsApp() != null && bean.getIsApp() == 1) {
            List<RetainedEquipmentInfo> pageList = complexQueryService.getRetainedEquipmentList(bean);
            return AjaxResult.success(pageList);
        }
        startPage();
        List<RetainedEquipmentInfo> pageList = complexQueryService.getRetainedEquipmentList(bean);
        return AjaxResult.success(getDataTable(pageList));
    }

    /**
     * 保有设备总量查询不带分页
     * @param bean
     * @return
     */
    @ApiOperation(value = "综合查询--保有设备总量查询不带分页")
    @GetMapping("/getRetainedEquipmentListNoPage")
    public AjaxResult getRetainedEquipmentListNoPage(RetainedEquipmentInfo bean) {
        bean.setIsExport(0);
        bean.setUserId(SecurityUtils.getLoginUser().getUserid());
        List<RetainedEquipmentInfo> list = complexQueryService.getRetainedEquipmentList(bean);
        RetainedEquipmentInfo dto = new RetainedEquipmentInfo();
        if (CollectionUtils.isNotEmpty(list)) {
            RetainedEquipmentInfo retainedEquipmentInfo = list.get(0);
            dto.setStoreNum(retainedEquipmentInfo.getStoreNum());
            dto.setUsNum(retainedEquipmentInfo.getUsNum());
            dto.setRepairNum(retainedEquipmentInfo.getRepairNum());
            dto.setInputNum(retainedEquipmentInfo.getInputNum());
            dto.setRepairInputNum(retainedEquipmentInfo.getRepairInputNum());
            dto.setDsNum(retainedEquipmentInfo.getDsNum());
            dto.setRepairAuditNum(retainedEquipmentInfo.getRepairAuditNum());
            dto.setPendingScrapNum(retainedEquipmentInfo.getPendingScrapNum());
            dto.setScrapNum(retainedEquipmentInfo.getScrapNum());
            dto.setAllNum(retainedEquipmentInfo.getAllNum());
            dto.setTotalPrice(retainedEquipmentInfo.getTotalPrice());
            dto.setFiveReplacementRate(retainedEquipmentInfo.getFiveReplacementRate());
            dto.setTenReplacementRate(retainedEquipmentInfo.getTenReplacementRate());
            dto.setTenPlusReplacementRate(retainedEquipmentInfo.getTenPlusReplacementRate());
        }
        return AjaxResult.success(dto);
    }

    /**
     * 导出综合查询保有设备总量查询
     * @param response
     * @param bean
     */
    @ApiOperation("导出综合查询保有设备总量查询")
    @PostMapping("/exportRetainedEquipmentList")
    public void exportRetainedEquipmentList(HttpServletResponse response, RetainedEquipmentInfo bean)
    {
        bean.setUserId(SecurityUtils.getLoginUser().getUserid());
        bean.setIsExport(0);
        List<RetainedEquipmentInfo> list = complexQueryService.getRetainedEquipmentList(bean);
        ExcelUtil<RetainedEquipmentInfo> util = new ExcelUtil<>(RetainedEquipmentInfo.class);
        util.exportExcel(response, list, "综合查询--保有设备总量查询");
    }

    /**
     * 查询在库机具设备详情
     * @param bean
     * @return
     */
    @ApiOperation(value = "综合查询--查询在库机具设备详情")
    @GetMapping("/getStorageInfoList")
    public AjaxResult getStorageInfoList(StorageInfo bean) {
        Integer pageIndex = Convert.toInt(ServletUtils.getParameter("pageNum"), 1);
        Integer pageSize = Convert.toInt(ServletUtils.getParameter("pageSize"), 10);
        List<StorageInfo> list = complexQueryService.getMaCodeList(bean);
        return AjaxResult.success(ListPagingUtil.paging(pageIndex, pageSize, list));
    }

    /**
     * 导出查询在库机具设备详情
     * @param bean
     * @return
     */
    @ApiOperation(value = "综合查询--查询在库机具设备详情")
    @PostMapping("/exportStorageInfoList")
    public void exportStorageInfoList(HttpServletResponse response, StorageInfo bean) {
        List<StorageInfo> list = complexQueryService.getMaCodeList(bean);
        ExcelUtil<StorageInfo> util = new ExcelUtil<>(StorageInfo.class);
        util.exportExcel(response, list, "综合查询--在库机具设备详情");
    }

    /**
     * 查询在用设备详情
     * @param bean
     * @return
     */
    @ApiOperation(value = "综合查询--查询在用设备详情")
    @GetMapping("/getUserRecordList")
    public AjaxResult getUserRecords(UseStorageInfo bean) {
        Integer pageIndex = Convert.toInt(ServletUtils.getParameter("pageNum"), 1);
        Integer pageSize = Convert.toInt(ServletUtils.getParameter("pageSize"), 10);
        List<UseStorageInfo> list = complexQueryService.getUserRecords(bean);
        return AjaxResult.success(ListPagingUtil.paging(pageIndex, pageSize, list));
    }

    /**
     * 导出在用设备详情
     * @param response
     * @param bean
     */
    @ApiOperation(value = "综合查询--导出在用设备详情")
    @PostMapping("/exportUserRecordList")
    public void exportUserRecordList(HttpServletResponse response, UseStorageInfo bean) {
        List<UseStorageInfo> list = complexQueryService.getUserRecords(bean);
        ExcelUtil<UseStorageInfo> util = new ExcelUtil<>(UseStorageInfo.class);
        util.exportExcel(response, list, "综合查询--导出在用设备详情");
    }

    /**
     * 查询在修设备详情
     * @param bean
     * @return
     */
    @ApiOperation(value = "综合查询--查询在修设备详情")
    @GetMapping("/getRepairRecordList")
    public AjaxResult getRepairRecordList(RepairStorageInfo bean) {
        Integer pageIndex = Convert.toInt(ServletUtils.getParameter("pageNum"), 1);
        Integer pageSize = Convert.toInt(ServletUtils.getParameter("pageSize"), 10);
        List<RepairStorageInfo> list = complexQueryService.getRepairRecordList(bean);
        return AjaxResult.success(ListPagingUtil.paging(pageIndex, pageSize, list));
    }

    /**
     * 导出在修设备详情
     * @param response
     * @param bean
     */
    @ApiOperation(value = "综合查询--导出在修设备详情")
    @PostMapping("/exportRepairRecordList")
    public void exportRepairRecordList(HttpServletResponse response, RepairStorageInfo bean) {
        List<RepairStorageInfo> list = complexQueryService.getRepairRecordList(bean);
        ExcelUtil<RepairStorageInfo> util = new ExcelUtil<>(RepairStorageInfo.class);
        util.exportExcel(response, list, "综合查询--导出在修设备详情");
    }

    /**
     * 查询新购待入库设备详情
     * @param bean
     * @return
     */
    @ApiOperation(value = "综合查询--查询新购待入库设备详情")
    @GetMapping("/getPurchaseRecordList")
    public AjaxResult getPurchaseRecordList(PurchaseInputInfo bean) {
        startPage();
        List<PurchaseInputInfo> list = complexQueryService.getPurchaseRecordList(bean);
        return AjaxResult.success(getDataTable(list));
    }

    /**
     * 导出新购待入库详情
     * @param response
     * @param bean
     */
    @ApiOperation(value = "综合查询--导出新购待入库详情")
    @PostMapping("/exportPurchaseRecordList")
    public void exportPurchaseRecordList(HttpServletResponse response, PurchaseInputInfo bean) {
        List<PurchaseInputInfo> list = complexQueryService.getPurchaseRecordList(bean);
        ExcelUtil<PurchaseInputInfo> util = new ExcelUtil<>(PurchaseInputInfo.class);
        util.exportExcel(response, list, "综合查询--导出新购待入库详情");
    }

    /**
     * 查询修饰待入库设备详情
     * @param bean
     * @return
     */
    @ApiOperation(value = "综合查询--查询修饰待入库设备详情")
    @GetMapping("/getRepairInputList")
    public AjaxResult getRepairInputList(RepairInputRecord bean) {
        startPage();
        List<RepairInputRecord> list = complexQueryService.getRepairInputList(bean);
        return AjaxResult.success(getDataTable(list));
    }

    /**
     * 导出修饰待入库详情
     * @param response
     * @param bean
     */
    @ApiOperation(value = "综合查询--导出修饰待入库详情")
    @PostMapping("/exportRepairInputList")
    public void exportRepairInputList(HttpServletResponse response, RepairInputRecord bean) {
        List<RepairInputRecord> list = complexQueryService.getRepairInputList(bean);
        ExcelUtil<RepairInputRecord> util = new ExcelUtil<>(RepairInputRecord.class);
        util.exportExcel(response, list, "综合查询--导出修饰待入库详情");
    }

    /**
     * 查询待报废设备详情
     * @param bean
     * @return
     */
    @ApiOperation(value = "综合查询--查询待报废设备详情")
    @GetMapping("/getScrapList")
    public AjaxResult getScrapList(ScrapRecordInfo bean) {
        startPage();
        List<ScrapRecordInfo> list = complexQueryService.getScrapList(bean);
        return AjaxResult.success(getDataTable(list));
    }

    /**
     * 导出待报废设备详情
     * @param response
     * @param bean
     */
    @ApiOperation(value = "综合查询--导出待报废设备详情")
    @PostMapping("/exportScrapList")
    public void exportScrapList(HttpServletResponse response, ScrapRecordInfo bean) {
        List<ScrapRecordInfo> list = complexQueryService.getScrapList(bean);
        ExcelUtil<ScrapRecordInfo> util = new ExcelUtil<>(ScrapRecordInfo.class);
        util.exportExcel(response, list, "综合查询--导出待报废设备详情");
    }

    /**
     * 查询已审核报废设备详情
     * @param bean
     * @return
     */
    @ApiOperation(value = "综合查询--查询已审核报废设备详情")
    @GetMapping("/getScrapAuditList")
    public AjaxResult getScrapAuditList(ScrapAuditInfo bean) {
        startPage();
        List<ScrapAuditInfo> list = complexQueryService.getScrapAuditList(bean);
        return AjaxResult.success(getDataTable(list));
    }

    /**
     * 导出已审核报废设备详情
     * @param response
     * @param bean
     */
    @ApiOperation(value = "综合查询--导出已审核报废设备详情")
    @PostMapping("/exportScrapAuditList")
    public void exportScrapAuditList(HttpServletResponse response, ScrapAuditInfo bean) {
        List<ScrapAuditInfo> list = complexQueryService.getScrapAuditList(bean);
        ExcelUtil<ScrapAuditInfo> util = new ExcelUtil<>(ScrapAuditInfo.class);
        util.exportExcel(response, list, "综合查询--导出已审核报废设备详情");
    }

    /**
     * 工程机具使用列表
     * @param bean
     * @return
     */
    @ApiOperation(value = "综合查询--工程机具使用列表")
    @GetMapping("/getProjUsingRecordList")
    public AjaxResult getProjUsingRecordList(ProjUsingRecord bean) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        bean.setUserId(loginUser.getUserid());
        startPage();
        List<ProjUsingRecord> pageList = complexQueryService.getProjUsingRecordList(bean);
        return AjaxResult.success(getDataTable(pageList));
    }

    /**
     * 工程机具使用列表不带分页
     * @param bean
     * @return
     */
    @ApiOperation(value = "综合查询--工程机具使用列表不带分页")
    @GetMapping("/getProjUsingRecordListNoPage")
    public AjaxResult getProjUsingRecordListNoPage(ProjUsingRecord bean) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        bean.setUserId(loginUser.getUserid());
        List<ProjUsingRecord> list = complexQueryService.exportProjUsingRecord(bean);
        ProjUsingDto projUsingDto = new ProjUsingDto();
        if (CollectionUtils.isNotEmpty(list)) {
            ProjUsingRecord projUsingRecord = list.get(0);
            projUsingDto.setTotalLeaseNum(projUsingRecord.getLeaseNum());
            projUsingDto.setTotalBackNum(projUsingRecord.getBackNum());
            projUsingDto.setTotalUsNum(projUsingRecord.getUsNum());
            projUsingDto.setTotalUsPrice(projUsingRecord.getUsPrice());
            projUsingDto.setTotalTotalPrice(projUsingRecord.getTotalPrice());
        }
        return AjaxResult.success(projUsingDto);
    }

    /**
     * 导出工程机具使用列表
     * @param response
     * @param bean
     */
    @ApiOperation("导出综合查询工程机具使用列表")
    @PostMapping("/exportProjUsingRecord")
    public void exportProjUsingRecord(HttpServletResponse response, ProjUsingRecord bean)
    {
        bean.setUserId(SecurityUtils.getLoginUser().getUserid());
        List<ProjUsingRecord> list = complexQueryService.exportProjUsingRecord(bean);
        ExcelUtil<ProjUsingRecord> util = new ExcelUtil<>(ProjUsingRecord.class);
        util.exportExcel(response, list, "综合查询--工程机具使用查询");
    }

    /**
     * 设备使用追溯查询
     * @param bean
     * @return
     */
    @ApiOperation(value = "综合查询--设备使用追溯查询")
    @GetMapping("/getMachineHistoryRecordList")
    public AjaxResult getMachineHistoryRecordList(MachineHistoryRecordBean bean) {
        bean.setUserId(SecurityUtils.getLoginUser().getUserid());
        startPage();
        List<MachineHistoryRecordBean> list = complexQueryService.getMachineHistoryRecordList(bean);
        return AjaxResult.success(getDataTable(list));
    }

    /**
     * 导出设备使用追溯查询
     * @param response
     * @param bean
     */
    @ApiOperation("导出综合查询设备使用追溯查询")
    @PostMapping("/exportMachineHistoryRecord")
    public void exportMachineHistoryRecord(HttpServletResponse response, MachineHistoryRecordBean bean)
    {
        bean.setUserId(SecurityUtils.getLoginUser().getUserid());
        List<MachineHistoryRecordBean> list = complexQueryService.getMachineHistoryRecordList(bean);
        ExcelUtil<MachineHistoryRecordBean> util = new ExcelUtil<>(MachineHistoryRecordBean.class);
        util.exportExcel(response, list, "综合查询--设备使用追溯查询");
    }

    /**
     * 机具入库查询列表
     */
    @ApiOperation(value = "综合查询--机具入库查询列表")
    @GetMapping("/getInputRecordList")
    public AjaxResult getInputRecordList(InputRecordInfo bean) {
        Integer pageIndex = Convert.toInt(ServletUtils.getParameter("pageNum"), 1);
        Integer pageSize = Convert.toInt(ServletUtils.getParameter("pageSize"), 10);
        List<InputRecordInfo> list = complexQueryService.getInputRecordList(bean);
        return AjaxResult.success(ListPagingUtil.paging(pageIndex, pageSize, list));
    }

    /**
     * 导出机具入库查询
     * @param response
     * @param bean
     */
    @ApiOperation("导出综合查询机具入库查询")
    @PostMapping("/exportInputRecord")
    public void exportInputRecord(HttpServletResponse response, InputRecordInfo bean)
    {
        List<InputRecordInfo> list = complexQueryService.getInputRecordList(bean);
        ExcelUtil<InputRecordInfo> util = new ExcelUtil<>(InputRecordInfo.class);
        util.exportExcel(response, list, "综合查询--机具入库查询");
    }

    /**
     * 机具出库查询列表
     * @param bean
     * @return
     */
    @ApiOperation(value = "综合查询--机具出库查询列表")
    @GetMapping("/getOutRecordList")
    public AjaxResult getOutRecordList(OutRecordInfo bean) {
        Integer pageIndex = Convert.toInt(ServletUtils.getParameter("pageNum"), 1);
        Integer pageSize = Convert.toInt(ServletUtils.getParameter("pageSize"), 10);
        List<OutRecordInfo> list = complexQueryService.getOutRecordList(bean);
        return AjaxResult.success(ListPagingUtil.paging(pageIndex, pageSize, list));
    }

    @ApiOperation("导出综合查询机具出库查询")
    @PostMapping("/exportOutRecord")
    public void exportOutRecord(HttpServletResponse response, OutRecordInfo bean)
    {
        List<OutRecordInfo> list = complexQueryService.getOutRecordList(bean);
        ExcelUtil<OutRecordInfo> util = new ExcelUtil<>(OutRecordInfo.class);
        util.exportExcel(response, list, "综合查询--机具出库查询");
    }

    /**
     * 修试查询--一机一档案查询
     * @param bean
     * @return
     */
    @ApiOperation(value = "修试查询--一机一档案查询")
    @GetMapping("/getMaTypeSelectList")
    public AjaxResult getMaTypeSelectList(MaTypeSelectInfo bean) {
        Integer pageIndex = Convert.toInt(ServletUtils.getParameter("pageNum"), 1);
        Integer pageSize = Convert.toInt(ServletUtils.getParameter("pageSize"), 10);
        List<MaTypeSelectInfo> list = complexQueryService.getMaTypeSelectList(bean);
        return AjaxResult.success(ListPagingUtil.paging(pageIndex, pageSize, list));
    }

    /**
     * 导出修试查询--一机一档案查询
     * @param response
     * @param bean
     */
    @ApiOperation("导出一机一档案查询")
    @PostMapping("/exportMaTypeSelectList")
    public void exportMaTypeSelectList(HttpServletResponse response, MaTypeSelectInfo bean)
    {
        List<MaTypeSelectInfo> list = complexQueryService.getMaTypeSelectList(bean);
        ExcelUtil<MaTypeSelectInfo> util = new ExcelUtil<>(MaTypeSelectInfo.class);
        util.exportExcel(response, list, "修试查询--导出一机一档案查询");
    }

    /**
     * 机具库存个人查询
     * @param bean
     * @return
     */
    @ApiOperation(value = "综合查询--机具库存个人查询")
    @GetMapping("/getPersonNum")
    public AjaxResult getPersonNum(RetainedEquipmentInfo bean) {
        startPage();
        List<RetainedEquipmentInfo> pageList = complexQueryService.getPersonNum(bean);
        return AjaxResult.success(getDataTable(pageList));
    }

    /**
     * 配件库存个人查询
     * @param bean
     * @return
     */
    @ApiOperation(value = "综合查询--配件库存个人查询")
    @GetMapping("/getPartPersonNum")
    public AjaxResult getPartPersonNum(RetainedEquipmentInfo bean) {
        startPage();
        List<RetainedEquipmentInfo> pageList = complexQueryService.getPartPersonNum(bean);
        return AjaxResult.success(getDataTable(pageList));
    }


    /**
     * 施工机具需求、供应分析统计表
     * @param bean
     * @return
     */
    @ApiOperation(value = "施工机具需求、供应分析统计表")
    @GetMapping("/getStatisticsList")
    public AjaxResult getStatisticsList(ProjUsingRecord bean) {
        startPage();
        List<ProjUsingRecord> pageList = complexQueryService.getStatisticsList(bean);
        return AjaxResult.success(getDataTable(pageList));
    }

    /**
     * 施工机具需求、供应分析统计表二级页面---总在用工程明细
     * @param bean
     * @return
     */
    @ApiOperation(value = "总在用工程明细")
    @GetMapping("/getAllUsDetails")
    public AjaxResult getAllUsDetails(ProjUsingRecord bean) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        bean.setUserId(loginUser.getUserid());
        startPage();
        List<ProjUsingRecord> pageList = complexQueryService.getAllUsDetails(bean);
        return AjaxResult.success(getDataTable(pageList));
    }



    @ApiOperation("导出施工机具需求、供应分析统计表")
    @PostMapping("/exportStatisticsList")
    public void exportStatisticsList(HttpServletResponse response, ProjUsingRecord bean)
    {
        List<ProjUsingRecord> list = complexQueryService.getStatisticsList(bean);
        List<ProjUsingRecordExport> exportList = new ArrayList<>();
        //将list复制到exportList
        for (ProjUsingRecord projUsingRecord : list) {
            ProjUsingRecordExport projUsingRecordExport = new ProjUsingRecordExport();
            BeanUtils.copyProperties(projUsingRecord, projUsingRecordExport);
            exportList.add(projUsingRecordExport);
        }
        ExcelUtil<ProjUsingRecordExport> util = new ExcelUtil<>(ProjUsingRecordExport.class);
        util.exportExcel(response, exportList, "施工机具需求、供应分析统计表");
    }

    @ApiOperation("导出总在用工程明细")
    @PostMapping("/exportAllUsDetails")
    public void exportAllUsDetails(HttpServletResponse response, ProjUsingRecord bean)
    {
        List<ProjUsingRecord> list = complexQueryService.getAllUsDetails(bean);
        List<ProjUsingRecordExports> exportList = new ArrayList<>();
        //将list复制到exportList
        for (ProjUsingRecord projUsingRecord : list) {
            ProjUsingRecordExports projUsingRecordExports = new ProjUsingRecordExports();
            BeanUtils.copyProperties(projUsingRecord, projUsingRecordExports);
            exportList.add(projUsingRecordExports);
        }
        ExcelUtil<ProjUsingRecordExports> util = new ExcelUtil<>(ProjUsingRecordExports.class);
        util.exportExcel(response, exportList, "总在用工程明细");
    }

    /**
     * 运维管理---库存操作日志列表
     * @param bean
     * @return
     */
    @ApiOperation(value = "运维管理---库存操作日志列表")
    @GetMapping("/getStorageNumLogList")
    public AjaxResult getStorageNumLogList(Type bean) {
        startPage();
        List<Type> pageList = complexQueryService.getStorageNumLogList(bean);
        return AjaxResult.success(getDataTable(pageList));
    }

    /**
     * 运维管理---库存操作日志列表--详情
     * @param bean
     * @return
     */
    @ApiOperation(value = "运维管理---库存操作日志列表--详情")
    @GetMapping("/getStorageNumLogDetails")
    public AjaxResult getStorageNumLogDetails(Type bean) {
        startPage();
        List<Type> pageList = complexQueryService.getStorageNumLogDetails(bean);
        return AjaxResult.success(getDataTable(pageList));
    }
}
