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.material.clz.domain.TeamVo;
import com.bonus.material.clz.domain.machine.MaterialUseStorageInfo;
import com.bonus.material.clz.domain.vo.MaterialRetainedTeamTotalVo;
import com.bonus.material.clz.domain.vo.MaterialRetainedTeamVo;
import com.bonus.material.clz.domain.vo.MaterialTotalMentInfo;
import com.bonus.material.ma.domain.Machine;
import com.bonus.material.clz.domain.machine.MaterialStorageInfo;
import com.bonus.material.clz.domain.vo.MaterialRetainedEquipmentInfo;
import com.bonus.material.clz.service.MaterialMachineService;
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/6/25 13:26
 */
@Api(tags = "材料站机具设备管理接口")
@RestController
@RequestMapping("/material_maMachine")
public class MaterialMachineController extends BaseController {

    @Resource
    private MaterialMachineService materialMachineService;

    /**
     * 获取机具追溯管理数据
     * @param machine
     * @return
     */
    @ApiOperation(value = "获取机具追溯管理数据")
    @GetMapping(value = "/getMachineInfo")
    public AjaxResult getMachineInfo(Machine machine) {
        Integer pageIndex = Convert.toInt(ServletUtils.getParameter("pageNum"), 1);
        Integer pageSize = Convert.toInt(ServletUtils.getParameter("pageSize"), 10);
        List<Machine> list = materialMachineService.getMachineInfo(machine);
        return AjaxResult.success(ListPagingUtil.paging(pageIndex, pageSize, list));
    }

    /**
     * 保有设备总量查询
     * @param bean
     * @return
     */
    @ApiOperation(value = "综合查询--保有设备总量查询")
    @GetMapping("/getRetainedEquipmentList")
    public AjaxResult getRetainedEquipmentList(MaterialRetainedEquipmentInfo bean) {
        Integer pageIndex = Convert.toInt(ServletUtils.getParameter("pageNum"), 1);
        Integer pageSize = Convert.toInt(ServletUtils.getParameter("pageSize"), 10);
        List<MaterialRetainedEquipmentInfo> list = materialMachineService.getRetainedEquipmentList(bean);
        return AjaxResult.success(ListPagingUtil.paging(pageIndex, pageSize, list));
    }

    /**
     * 工器具台账
     * @param bean
     * @return
     */
    @ApiOperation(value = "工器具台账查询")
    @GetMapping("/getTotalList")
    public AjaxResult getTotalList(MaterialRetainedEquipmentInfo bean) {
        MaterialTotalMentInfo info = materialMachineService.getTotalList(bean);
        return AjaxResult.success(info);
    }


    /**
     * 导出保有设备总量查询
     * @param bean
     * @return
     */
    @ApiOperation(value = "综合查询--导出保有设备总量查询")
    @PostMapping("/exportRetainedEquipmentList")
    public void exportRetainedEquipmentList(HttpServletResponse response, MaterialRetainedEquipmentInfo bean) {
        List<MaterialRetainedEquipmentInfo> list = materialMachineService.getRetainedEquipmentList(bean);
        ExcelUtil<MaterialRetainedEquipmentInfo> util = new ExcelUtil<>(MaterialRetainedEquipmentInfo.class);
        util.exportExcel(response, list, "综合查询--保有设备总量查询");
    }

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

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

    /**
     * 查询在用设备详情
     * @param bean
     * @return
     */
    @ApiOperation(value = "综合查询--查询在用设备详情")
    @GetMapping("/getUserRecordList")
    public AjaxResult getUserRecords(MaterialUseStorageInfo bean) {
        startPage();
        List<MaterialUseStorageInfo> list = materialMachineService.getUserRecords(bean);
        return AjaxResult.success(getDataTable(list));
    }

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

    /**
     * 班组库存查询
     * @param bean
     * @return
     */
    @ApiOperation(value = "班组库存查询")
    @GetMapping("/getTeamUseNumList")
    public AjaxResult getTeamUseNumList(MaterialRetainedEquipmentInfo bean) {
        List<MaterialRetainedEquipmentInfo> list = materialMachineService.getTeamUseNumList(bean);
        return AjaxResult.success(list);
    }

    /**
     * 班组预警详情查询
     * @param bean
     * @return
     */
    @ApiOperation(value = "班组预警详情查询")
    @GetMapping("/getTeamWarnDetailsList")
    public AjaxResult getTeamWarnDetailsList(MaterialRetainedEquipmentInfo bean) {
        List<MaterialRetainedEquipmentInfo> list = materialMachineService.getTeamWarnDetailsList(bean);
        return AjaxResult.success(list);
    }

    /**
     * 工器具台账--APP
     * @param bean
     * @return
     */
    @ApiOperation(value = "工器具台账查询")
    @GetMapping("/getToolsLedgerList")
    public AjaxResult getToolsLedgerList(MaterialRetainedEquipmentInfo bean) {
        return AjaxResult.success(materialMachineService.getToolsLedgerList(bean));
    }

    /**
     * 工器具预警设备数量查询--APP
     * @param bean
     * @return
     */
    @ApiOperation(value = "工器具预警设备数量查询")
    @GetMapping("/getToolsLedgerDetailsList")
    public AjaxResult getToolsLedgerDetailsList(MaterialRetainedEquipmentInfo bean) {
        return AjaxResult.success(materialMachineService.getToolsLedgerDetailsList(bean));
    }

    /**
     * 工器具台账--APP详情查询
     * @param bean
     * @return
     */
    @ApiOperation(value = "工器具台账详情查询")
    @GetMapping("/getToolsDetailsList")
    public AjaxResult getToolsDetailsList(MaterialRetainedEquipmentInfo bean) {
        return AjaxResult.success(materialMachineService.getToolsDetailsList(bean));
    }

    /**
     * 安全工器具预警页面查询
     * @param bean
     * @return
     */
    @ApiOperation(value = "安全工器具预警页面查询")
    @GetMapping("/getSafeDetailsList")
    public AjaxResult getSafeDetailsList(MaterialRetainedEquipmentInfo bean) {
        startPage();
        List<MaterialRetainedEquipmentInfo> list = materialMachineService.getSafeDetailsList(bean);
        return AjaxResult.success(getDataTable(list));
    }

    /**
     * 安全工器具预警页面查询获取协议id
     * @param bean
     * @return
     */
    @ApiOperation(value = "安全工器具预警页面查询获取协议id")
    @GetMapping("/getAgreementId")
    public AjaxResult getAgreementId(MaterialRetainedEquipmentInfo bean) {
        return materialMachineService.getAgreementId(bean);
    }

    /**
     * 安全工器具预警数量查询
     * @param bean
     * @return
     */
    @ApiOperation(value = "安全工器具预警数量查询")
    @GetMapping("/getSafeNumList")
    public AjaxResult getSafeNumList(MaterialRetainedEquipmentInfo bean) {
        return AjaxResult.success(materialMachineService.getSafeNumList(bean));
    }

    /**
     * 后台班组在用库存查询
     * @param bean
     * @return
     */
    @ApiOperation(value = "后台班组在用库存查询")
    @GetMapping("/getTeamNumList")
    public AjaxResult getTeamNumList(MaterialRetainedEquipmentInfo bean) {
        Integer pageIndex = Convert.toInt(ServletUtils.getParameter("pageNum"), 1);
        Integer pageSize = Convert.toInt(ServletUtils.getParameter("pageSize"), 10);
        List<MaterialRetainedTeamTotalVo> list = materialMachineService.getTeamNumList(bean);
        return AjaxResult.success(ListPagingUtil.paging(pageIndex, pageSize, list));
    }

    /**
     * 导出后台班组在用库存查询
     * @param response
     * @param bean
     */
    @ApiOperation(value = "综合查询--导出后台班组在用库存查询")
    @PostMapping("/exportTeamNumList")
    public void exportTeamNumList(HttpServletResponse response, MaterialRetainedEquipmentInfo bean) {
        List<MaterialRetainedTeamTotalVo> list = materialMachineService.getTeamNumList(bean);
        ExcelUtil<MaterialRetainedTeamTotalVo> util = new ExcelUtil<>(MaterialRetainedTeamTotalVo.class);
        util.exportExcel(response, list, "综合查询--导出后台班组在用库存查询");
    }

    /**
     * 后台班组在用库存查询二级页面
     * @param bean
     * @return
     */
    @ApiOperation(value = "后台班组在用库存查询二级页面")
    @GetMapping("/getTeamNumSecondList")
    public AjaxResult getTeamNumSecondList(MaterialRetainedEquipmentInfo bean) {
        startPage();
        List<MaterialRetainedTeamVo> list = materialMachineService.getTeamNumSecondList(bean);
        return AjaxResult.success(getDataTable(list));
    }

    /**
     * 后台班组在用库存查询二级页面
     * @param response
     * @param bean
     */
    @ApiOperation(value = "综合查询--导出后台班组在用库存查询二级页面")
    @PostMapping("/exportTeamNumSecondList")
    public void exportTeamNumSecondList(HttpServletResponse response, MaterialRetainedEquipmentInfo bean) {
        List<MaterialRetainedTeamVo> list = materialMachineService.getTeamNumSecondList(bean);
        ExcelUtil<MaterialRetainedTeamVo> util = new ExcelUtil<>(MaterialRetainedTeamVo.class);
        util.exportExcel(response, list, "综合查询--导出后台班组在用库存查询二级页面");
    }

    /**
     * 后台班组在用库存查询三级页面
     * @param bean
     * @return
     */
    @ApiOperation(value = "后台班组在用库存查询三级页面")
    @GetMapping("/getTeamNumThirdList")
    public AjaxResult getTeamNumThirdList(MaterialRetainedEquipmentInfo bean) {
        startPage();
        List<MaterialRetainedEquipmentInfo> list = materialMachineService.getTeamNumThirdList(bean);
        return AjaxResult.success(getDataTable(list));
    }

    /**
     * 班组进出场记录
     * @param bean
     * @return
     */
    @ApiOperation(value = "班组进出场记录")
    @GetMapping("/getTeamInOrOutInfo")
    public AjaxResult getTeamInOrOutInfo(TeamVo bean) {
        startPage();
        List<TeamVo> list = materialMachineService.getTeamInOrOutInfo(bean);
        return AjaxResult.success(getDataTable(list));
    }

    /**
     * 导出班组进出场记录
     * @param response
     * @param bean
     */
    @ApiOperation(value = "导出班组进出场记录")
    @PostMapping("/exportTeamInOrOutInfo")
    public void exportTeamInOrOutInfo(HttpServletResponse response, TeamVo bean) {
        List<TeamVo> list = materialMachineService.getTeamInOrOutInfo(bean);
        ExcelUtil<TeamVo> util = new ExcelUtil<>(TeamVo.class);
        util.exportExcel(response, list, "导出班组进出场记录");
    }
}
