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.poi.ExcelUtil;
import com.bonus.common.core.web.controller.BaseController;
import com.bonus.common.core.web.domain.AjaxResult;
import com.bonus.material.basic.domain.report.*;
import com.bonus.material.basic.service.BmReportService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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

/**
 * @Author ma_sh
 * @create 2025/1/22 9:52
 */
@Api(tags = "报表查询")
@RestController
@RequestMapping("/bm_report")
public class BmReportController extends BaseController {

    @Resource
    private BmReportService bmReportService;

    /**
     * 新购入库报表查询
     * @param bean
     * @return
     */
    @ApiOperation(value = "设备追溯查询--新购入库报表查询")
    @GetMapping("/getPurChaseReportList")
    public AjaxResult getPurChaseReportList(PurChaseReportInfo bean) {
        bean.setIsExport(1);
        startPage();
        List<PurChaseReportInfo> pageList = bmReportService.getPurChaseReportList(bean);
        return AjaxResult.success(getDataTable(pageList));
    }

    @ApiOperation(value = "设备追溯查询--新购入库报表查询不分页")
    @GetMapping("/getPurChaseReportListNoPage")
    public AjaxResult getPurChaseReportListNoPage(PurChaseReportInfo bean) {
        bean.setIsExport(0);
        List<PurChaseReportInfo> list = bmReportService.getPurChaseReportList(bean);
        PurChaseReportInfo reportInfo = new PurChaseReportInfo();
        if (CollectionUtils.isNotEmpty(list)) {
            PurChaseReportInfo info = list.get(0);
            reportInfo.setPurchaseNum(info.getPurchaseNum());
            reportInfo.setPassNum(info.getPassNum());
            reportInfo.setInputNum(info.getInputNum());
            reportInfo.setPurchasePrice(info.getPurchasePrice());
            reportInfo.setPurchasePriceNoTax(info.getPurchasePriceNoTax());
            reportInfo.setPendingInputNum(info.getPendingInputNum());
        }
        return AjaxResult.success(reportInfo);
    }

    /**
     * 新购入库报表详情查询
     * @param bean
     * @return
     */
    @ApiOperation(value = "设备追溯查询--新购入库报表详情查询")
    @GetMapping("/getPurChaseReportDetailsList")
    public AjaxResult getPurChaseReportDetailsList(PurChaseReportInfo bean) {
        startPage();
        List<PurChaseReportDetails> pageList = bmReportService.getPurChaseReportDetailsList(bean);
        return AjaxResult.success(getDataTable(pageList));
    }


    /**
     * 新购入库验收合格报表详情查询
     * @param bean
     * @return
     */
    @ApiOperation(value = "设备追溯查询--新购入库验收合格报表详情查询")
    @GetMapping("/getPurChaseReportSuccessList")
    public AjaxResult getPurChaseReportSuccessList(PurChaseReportInfo bean) {
        startPage();
        List<PurChaseReportDetails> pageList = bmReportService.getPurChaseReportSuccessList(bean);
        return AjaxResult.success(getDataTable(pageList));
    }

    /**
     * 新购入库数量报表详情查询
     * @param bean
     * @return
     */
    @ApiOperation(value = "设备追溯查询--新购入库数量报表详情查询")
    @GetMapping("/getPurChaseReportInBoundList")
    public AjaxResult getPurChaseReportInBoundList(PurChaseReportInfo bean) {
        startPage();
        List<PurChaseReportDetails> pageList = bmReportService.getPurChaseReportInBoundList(bean);
        return AjaxResult.success(getDataTable(pageList));
    }

    /**
     * 新购未入库数量报表详情查询
     * @param bean
     * @return
     */
    @ApiOperation(value = "设备追溯查询--新购未入库数量报表详情查询")
    @GetMapping("/getPurChaseReportNotInBoundList")
    public AjaxResult getPurChaseReportNotInBoundList(PurChaseReportInfo bean) {
        startPage();
        List<PurChaseReportDetails> pageList = bmReportService.getPurChaseReportNotInBoundList(bean);
        return AjaxResult.success(getDataTable(pageList));
    }

    /**
     * 导出设备追溯查询-新购入库报表
     * @param response
     * @param bean
     */
    @ApiOperation("导出设备追溯查询-新购入库报表")
    @PostMapping("/exportPurChaseReportList")
    public void exportPurChaseReportList(HttpServletResponse response, PurChaseReportInfo bean)
    {
        bean.setIsExport(0);
        List<PurChaseReportInfo> list = bmReportService.getPurChaseReportList(bean);
        ExcelUtil<PurChaseReportInfo> util = new ExcelUtil<>(PurChaseReportInfo.class);
        util.exportExcel(response, list, "设备追溯查询-新购入库报表采购详情");
    }

    /**
     * 导出设备追溯查询-新购入库报表详情
     * @param response
     * @param bean
     */
    @ApiOperation("导出设备追溯查询-新购入库报表详情")
    @PostMapping("/exportPurChaseReportDetailsList")
    public void exportPurChaseReportDetailsList(HttpServletResponse response, PurChaseReportInfo bean)
    {
        List<PurChaseReportDetails> list = bmReportService.getPurChaseReportDetailsList(bean);
        ExcelUtil<PurChaseReportDetails> util = new ExcelUtil<>(PurChaseReportDetails.class);
        util.exportExcel(response, list, "设备追溯查询-新购入库报表详情");
    }

    /**
     * 导出设备追溯查询-新购入库验收合格报表详情
     * @param response
     * @param bean
     */
    @ApiOperation("导出设备追溯查询-新购入库验收合格报表详情")
    @PostMapping("/exportPurChaseReportSuccessList")
    public void exportPurChaseReportSuccessList(HttpServletResponse response, PurChaseReportInfo bean)
    {
        List<PurChaseReportDetails> list = bmReportService.getPurChaseReportSuccessList(bean);
        ExcelUtil<PurChaseReportDetails> util = new ExcelUtil<>(PurChaseReportDetails.class);
        util.exportExcel(response, list, "设备追溯查询-新购入库报表验收合格详情");
    }

    /**
     * 导出设备追溯查询-新购入库数量报表详情
     * @param response
     * @param bean
     */
    @ApiOperation("导出设备追溯查询-新购入库数量报表详情")
    @PostMapping("/exportPurChaseReportInBoundList")
    public void exportPurChaseReportInBoundList(HttpServletResponse response, PurChaseReportInfo bean)
    {
        List<PurChaseReportDetails> list = bmReportService.getPurChaseReportInBoundList(bean);
        ExcelUtil<PurChaseReportDetails> util = new ExcelUtil<>(PurChaseReportDetails.class);
        util.exportExcel(response, list, "设备追溯查询-新购入库报表入库详情");
    }

    /**
     * 导出设备追溯查询-新购未入库报表详情
     * @param response
     * @param bean
     */
    @ApiOperation("导出设备追溯查询-新购未入库报表详情")
    @PostMapping("/exportPurChaseReportNotInBoundList")
    public void exportPurChaseReportNotInBoundList(HttpServletResponse response, PurChaseReportInfo bean)
    {
        List<PurChaseReportDetails> list = bmReportService.getPurChaseReportNotInBoundList(bean);
        ExcelUtil<PurChaseReportDetails> util = new ExcelUtil<>(PurChaseReportDetails.class);
        util.exportExcel(response, list, "设备追溯查询-新购入库报表未入库详情");
    }

    /**
     * 领料出库报表查询
     * @param bean
     * @return
     */
    @ApiOperation(value = "设备追溯查询--领料出库报表查询")
    @GetMapping("/getLeaseOutList")
    public AjaxResult getLeaseOutList(LeaseOutInfo bean) {
        bean.setIsExport(1);
        Integer pageIndex = Convert.toInt(ServletUtils.getParameter("pageNum"), 1);
        Integer pageSize = Convert.toInt(ServletUtils.getParameter("pageSize"), 10);
        List<LeaseOutInfo> pageList = bmReportService.getLeaseOutList(bean);
        return AjaxResult.success(ListPagingUtil.paging(pageIndex, pageSize, pageList));
    }

    /**
     * 领料出库报表查询
     * @param bean
     * @return
     */
    @ApiOperation(value = "设备追溯查询--领料出库报表查询不分页")
    @GetMapping("/getLeaseOutListNoPage")
    public AjaxResult getLeaseOutListNoPage(LeaseOutInfo bean) {
        bean.setIsExport(0);
        List<LeaseOutInfo> list = bmReportService.getLeaseOutList(bean);
        LeaseOutInfo info = new LeaseOutInfo();
        if (CollectionUtils.isNotEmpty(list)) {
            LeaseOutInfo leaseOutInfo = list.get(0);
            info.setLeaseNum(leaseOutInfo.getLeaseNum());
            info.setOutNum(leaseOutInfo.getOutNum());
            info.setPendingOutNum(leaseOutInfo.getPendingOutNum());
        }
        return AjaxResult.success(info);
    }

    /**
     * 领料出库报表详情查询
     * @param bean
     * @return
     */
    @ApiOperation(value = "设备追溯查询--领料出库报表详情查询")
    @GetMapping("/getLeaseOutDetailsList")
    public AjaxResult getLeaseOutDetailsList(LeaseOutInfo bean) {
        startPage();
        List<LeaseOutDetails> pageList = bmReportService.getLeaseOutDetailsList(bean);
        return AjaxResult.success(getDataTable(pageList));
    }

    /**
     * 导出设备追溯查询-领料出库报表查询
     * @param response
     * @param bean
     */
    @ApiOperation("导出设备追溯查询-领料出库报表查询")
    @PostMapping("/exportLeaseOutList")
    public void exportLeaseOutList(HttpServletResponse response, LeaseOutInfo bean)
    {
        bean.setIsExport(0);
        List<LeaseOutInfo> list = bmReportService.getLeaseOutList(bean);
        ExcelUtil<LeaseOutInfo> util = new ExcelUtil<>(LeaseOutInfo.class);
        util.exportExcel(response, list, "设备追溯查询-领料出库报表查询");
    }

    /**
     * 导出设备追溯查询-领料出库报表详情查询
     * @param response
     * @param bean
     */
    @ApiOperation("导出设备追溯查询-领料出库报表详情查询")
    @PostMapping("/exportLeaseOutDetailsList")
    public void exportLeaseOutDetailsList(HttpServletResponse response, LeaseOutInfo bean)
    {
        List<LeaseOutDetails> list = bmReportService.getLeaseOutDetailsList(bean);
        ExcelUtil<LeaseOutDetails> util = new ExcelUtil<>(LeaseOutDetails.class);
        util.exportExcel(response, list, "设备追溯查询-领料出库报表详情查询");
    }

    /**
     * 退料报表查询
     * @param bean
     * @return
     */
    @ApiOperation(value = "设备追溯查询--退料报表查询")
    @GetMapping("/getBackInputList")
    public AjaxResult getBackInputList(BackInputInfo bean) {
        bean.setIsExport(1);
        Integer pageIndex = Convert.toInt(ServletUtils.getParameter("pageNum"), 1);
        Integer pageSize = Convert.toInt(ServletUtils.getParameter("pageSize"), 10);
        List<BackInputInfo> pageList = bmReportService.getBackInputList(bean);
        return AjaxResult.success(ListPagingUtil.paging(pageIndex, pageSize, pageList));
    }

    /**
     * 退料报表查询
     * @param bean
     * @return
     */
    @ApiOperation(value = "设备追溯查询--退料报表查询不分页")
    @GetMapping("/getBackInputListNoPage")
    public AjaxResult getBackInputListNoPage(BackInputInfo bean) {
        bean.setIsExport(0);
        List<BackInputInfo> list = bmReportService.getBackInputList(bean);
        BackInputInfo info = new BackInputInfo();
        if (CollectionUtils.isNotEmpty(list)) {
            BackInputInfo backInputInfo = list.get(0);
            info.setBackNum(backInputInfo.getBackNum());
        }
        return AjaxResult.success(info);
    }

    /**
     * 退料报表查询详情查询
     * @param bean
     * @return
     */
    @ApiOperation(value = "设备追溯查询--退料报表查询详情")
    @GetMapping("/getBackDetailsInputList")
    public AjaxResult getBackDetailsInputList(BackInputInfo bean) {
        startPage();
        List<BackInputDetails> pageList = bmReportService.getBackDetailsInputList(bean);
        return AjaxResult.success(getDataTable(pageList));
    }

    /**
     * 导出设备追溯查询-退料报表查询
     * @param response
     * @param bean
     */
    @ApiOperation("导出设备追溯查询-退料报表查询")
    @PostMapping("/exportBackInputList")
    public void exportBackInputList(HttpServletResponse response, BackInputInfo bean)
    {
        bean.setIsExport(0);
        List<BackInputInfo> list = bmReportService.getBackInputList(bean);
        ExcelUtil<BackInputInfo> util = new ExcelUtil<>(BackInputInfo.class);
        util.exportExcel(response, list, "设备追溯查询-退料报表查询");
    }

    /**
     * 导出设备追溯查询-退料报表查询详情
     * @param response
     * @param bean
     */
    @ApiOperation("导出设备追溯查询-退料报表查询详情")
    @PostMapping("/exportBackDetailsInputList")
    public void exportBackDetailsInputList(HttpServletResponse response, BackInputInfo bean)
    {
        List<BackInputDetails> list = bmReportService.getBackDetailsInputList(bean);
        ExcelUtil<BackInputDetails> util = new ExcelUtil<>(BackInputDetails.class);
        util.exportExcel(response, list, "设备追溯查询-退料报表查询详情");
    }

    /**
     * 维修报表
     * @param bean
     * @return
     */
    @ApiOperation(value = "设备追溯查询--维修报表")
    @GetMapping("/getRepairList")
    public AjaxResult getRepairList(RepairInfo bean) {
        bean.setIsExport(1);
        startPage();
        List<RepairInfo> pageList = bmReportService.getRepairList(bean);
        return AjaxResult.success(getDataTable(pageList));
    }

    /**
     * 维修报表查询不分页
     * @param bean
     * @return
     */
    @ApiOperation(value = "设备追溯查询--维修报表查询不分页")
    @GetMapping("/getRepairListNoPage")
    public AjaxResult getRepairListNoPage(RepairInfo bean) {
        bean.setIsExport(0);
        List<RepairInfo> list = bmReportService.getRepairList(bean);
        RepairInfo info = new RepairInfo();
        if (CollectionUtils.isNotEmpty(list)) {
            RepairInfo repairInfo = list.get(0);
            info.setBackNum(repairInfo.getBackNum());
            info.setRepairedNum(repairInfo.getRepairedNum());
            info.setPendingScrapNum(repairInfo.getPendingScrapNum());
            info.setPendingRepairNum(repairInfo.getPendingRepairNum());
        }
        return AjaxResult.success(info);
    }

    /**
     * 维修报表查询详情查询
     * @param bean
     * @return
     */
    @ApiOperation(value = "设备追溯查询--维修报表查询详情")
    @GetMapping("/getRepairDetailsList")
    public AjaxResult getRepairDetailsList(RepairInfo bean) {
        startPage();
        List<RepairDetails> list = bmReportService.getRepairDetailsList(bean);
        return AjaxResult.success(getDataTable(list));
    }

    /**
     * 导出设备追溯查询-维修报表查询
     * @param response
     * @param bean
     */
    @ApiOperation("导出设备追溯查询-维修报表查询")
    @PostMapping("/exportRepairList")
    public void exportRepairList(HttpServletResponse response, RepairInfo bean)
    {
        bean.setIsExport(0);
        List<RepairInfo> list = bmReportService.getRepairList(bean);
        ExcelUtil<RepairInfo> util = new ExcelUtil<>(RepairInfo.class);
        util.exportExcel(response, list, "设备追溯查询-维修报表查询");
    }

    /**
     * 导出设备追溯查询-维修报表查询详情
     * @param response
     * @param bean
     */
    @ApiOperation("导出设备追溯查询-维修报表查询详情")
    @PostMapping("/exportRepairDetailsList")
    public void exportRepairDetailsList(HttpServletResponse response, RepairInfo bean)
    {
        List<RepairDetails> list = bmReportService.getRepairDetailsList(bean);
        ExcelUtil<RepairDetails> util = new ExcelUtil<>(RepairDetails.class);
        util.exportExcel(response, list, "设备追溯查询-维修报表查询详情");
    }

    /**
     * 修饰入库报表
     * @param bean
     * @return
     */
    @ApiOperation(value = "设备追溯查询--修饰入库报表")
    @GetMapping("/getRepairInputList")
    public AjaxResult getRepairInputList(RepairInputDto bean) {
        bean.setIsExport(1);
        startPage();
        List<RepairInputDto> pageList = bmReportService.getRepairInputList(bean);
        return AjaxResult.success(getDataTable(pageList));
    }

    /**
     * 修饰入库报表查询不分页
     * @param bean
     * @return
     */
    @ApiOperation(value = "设备追溯查询--修饰入库报表查询不分页")
    @GetMapping("/getRepairInputListNoPage")
    public AjaxResult getRepairInputListNoPage(RepairInputDto bean) {
        bean.setIsExport(0);
        List<RepairInputDto> pageList = bmReportService.getRepairInputList(bean);
        RepairInputDto info = new RepairInputDto();
        if (CollectionUtils.isNotEmpty(pageList)) {
            RepairInputDto repairInputInfo = pageList.get(0);
            info.setRepairedNum(repairInputInfo.getRepairedNum());
            info.setInputNum(repairInputInfo.getInputNum());
            info.setPendingInputNum(repairInputInfo.getPendingInputNum());
        }
        return AjaxResult.success(info);
    }

    /**
     * 修饰入库报表查询详情查询
     * @param bean
     * @return
     */
    @ApiOperation(value = "设备追溯查询--修饰入库报表查询详情")
    @GetMapping("/getRepairInputDetailsList")
    public AjaxResult getRepairInputDetailsList(RepairInputDto bean) {
        startPage();
        List<RepairInputDetails> list = bmReportService.getRepairInputDetailsList(bean);
        return AjaxResult.success(getDataTable(list));
    }

    /**
     * 导出设备追溯查询-修饰入库报表查询
     * @param response
     * @param bean
     */
    @ApiOperation("导出设备追溯查询-修饰入库报表查询")
    @PostMapping("/exportRepairInputList")
    public void exportRepairList(HttpServletResponse response, RepairInputDto bean)
    {
        bean.setIsExport(0);
        List<RepairInputDto> list = bmReportService.getRepairInputList(bean);
        ExcelUtil<RepairInputDto> util = new ExcelUtil<>(RepairInputDto.class);
        util.exportExcel(response, list, "设备追溯查询-修饰入库报表");
    }

    /**
     * 导出设备追溯查询-修饰入库报表查询详情查询
     * @param response
     * @param bean
     */
    @ApiOperation("导出设备追溯查询-修饰入库报表详情查询")
    @PostMapping("/exportRepairInputDetailsList")
    public void exportRepairInputDetailsList(HttpServletResponse response, RepairInputDto bean)
    {
        List<RepairInputDetails> list = bmReportService.getRepairInputDetailsList(bean);
        ExcelUtil<RepairInputDetails> util = new ExcelUtil<>(RepairInputDetails.class);
        util.exportExcel(response, list, "设备追溯查询-修饰入库报表详情查询");
    }

    /**
     * 报废报表
     * @param bean
     * @return
     */
    @ApiOperation(value = "设备追溯查询--报废报表")
    @GetMapping("/getScrapList")
    public AjaxResult getScrapList(ScrapInfo bean) {
        bean.setIsExport(1);
        Integer pageIndex = Convert.toInt(ServletUtils.getParameter("pageNum"), 1);
        Integer pageSize = Convert.toInt(ServletUtils.getParameter("pageSize"), 10);
        List<ScrapInfo> list = bmReportService.getScrapList(bean);
        return AjaxResult.success(ListPagingUtil.paging(pageIndex, pageSize, list));
    }

    /**
     * 报废报表查询不分页
     * @param bean
     * @return
     */
    @ApiOperation(value = "设备追溯查询--报废报表查询不分页")
    @GetMapping("/getScrapListNoPage")
    public AjaxResult getScrapListNoPage(ScrapInfo bean) {
        bean.setIsExport(0);
        List<ScrapInfo> pageList = bmReportService.getScrapList(bean);
        ScrapInfo info = new ScrapInfo();
        if (CollectionUtils.isNotEmpty(pageList)) {
            ScrapInfo scrapInfo = pageList.get(0);
            info.setScrapNum(scrapInfo.getScrapNum());
        }
        return AjaxResult.success(info);
    }

    /**
     * 导出设备追溯查询-报废报表查询
     * @param response
     * @param bean
     */
    @ApiOperation("导出设备追溯查询-报废报表查询")
    @PostMapping("/exportScrapList")
    public void exportScrapList(HttpServletResponse response, ScrapInfo bean)
    {
        bean.setIsExport(0);
        List<ScrapInfo> list = bmReportService.getScrapList(bean);
        ExcelUtil<ScrapInfo> util = new ExcelUtil<>(ScrapInfo.class);
        util.exportExcel(response, list, "设备追溯查询-报废报表报表");
    }

    /**
     * 报废报表查询详情
     * @param bean
     * @return
     */
    @ApiOperation(value = "设备追溯查询--报废报表查询详情")
    @GetMapping("/getScrapDetailsList")
    public AjaxResult getScrapDetailsList(ScrapInfo bean) {
        startPage();
        List<ScrapDetailsInfo> list = bmReportService.getScrapDetailsList(bean);
        return AjaxResult.success(getDataTable(list));
    }

    /**
     * 导出设备追溯查询-报废报表详情查询
     * @param response
     * @param bean
     */
    @ApiOperation("导出设备追溯查询-报废报表详情查询")
    @PostMapping("/exportScrapDetailsList")
    public void exportScrapDetailsList(HttpServletResponse response, ScrapInfo bean)
    {
        List<ScrapDetailsInfo> list = bmReportService.getScrapDetailsList(bean);
        ExcelUtil<ScrapDetailsInfo> util = new ExcelUtil<>(ScrapDetailsInfo.class);
        util.exportExcel(response, list, "设备追溯查询-报废报表详情查询");
    }
}
