package com.bonus.material.push.controller;

import com.bonus.common.core.exception.ServiceException;
import com.bonus.common.core.web.controller.BaseController;
import com.bonus.common.core.web.domain.AjaxResult;
import com.bonus.common.core.web.page.TableDataInfo;
import com.bonus.material.push.domain.*;
import com.bonus.material.push.mapper.ProDataUseInfoMapper;
import com.bonus.material.push.service.ProDataUseInfoService;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author：liang.chao
 * @Date：2025/8/23 - 14:28
 */

@RequestMapping("/dataUseInfo")
@RestController
public class PushProDataUseInfoController extends BaseController {

    @Resource
    private ProDataUseInfoService service;

    @Resource
    private ProDataUseInfoMapper proDataUseInfoMapper;

    /**
     * 查询推送数据使用信息
     *
     * @param bean
     * @return
     */
    @PostMapping(value = "getProDataUseInfo")
    public AjaxResult getProDataUseInfo(@RequestBody ProIdsBean bean) {
        try {
            List<ProIdsBean> results = service.getProDataUseInfo(bean);
            return AjaxResult.success(results);
        } catch (Exception e) {
            logger.error(e.toString(), e);
            throw new ServiceException("数据查询异常，请联系运维人员查询日志处理");
        }
    }

    /**
     * 查询每个分类机具详情
     *
     * @param bean
     * @return
     */

    @GetMapping(value = "getMachineDetails")
    public TableDataInfo getMachineDetails(ProIdsBean bean) {
        try {
            startPage();
            List<MachineInfoBean> results = service.getMachineDetails(bean);
            return getDataTable(results);
        } catch (Exception e) {
            logger.error(e.toString(), e);
            throw new ServiceException("数据查询异常，请联系运维人员查询日志处理");
        }
    }


    @GetMapping(value = "getMaTypeList")
    public TableDataInfo getMaTypeList(ProIdsBean bean) {
        try {
            startPage();
            List<MachineInfoBean> results = service.getMaTypeList(bean);
            return getDataTable(results);
        } catch (Exception e) {
            logger.error(e.toString(), e);
            throw new ServiceException("数据查询异常，请联系运维人员查询日志处理");
        }
    }

    /**
     * 查询七大类设备保有量、在用量、再修量、库存量
     *
     * @param bean
     * @return
     */
    @GetMapping(value = "getNumsByStatics")
    public AjaxResult getNumsByStatics(ProIdsBean bean) {
        try {
            List<TotalNumsBean> results = service.getNumsByStatics(bean);
            return AjaxResult.success(results);
        } catch (Exception e) {
            logger.error(e.toString(), e);
            throw new ServiceException("数据查询异常，请联系运维人员查询日志处理");
        }
    }

    /**
     * 保有量、在用量、再修量、库存量 装备详情
     *
     * @param bean
     * @return
     */
    @GetMapping(value = "getAllMacheDetails")
    public TableDataInfo getAllMacheDetails(ProIdsBean bean) {
        List<MachineInfoBean> objects = new ArrayList<>();
        try {
            startPage();
            if (StringUtils.isNotBlank(bean.getStatus()) && bean.getStatus().equals("在用")) {
                objects = service.getUseNumsDetails(bean);
            } else if (StringUtils.isNotBlank(bean.getStatus()) && bean.getStatus().equals("在修")) {
                objects = service.getRepairNumsDetails(bean);
            } else if (StringUtils.isNotBlank(bean.getStatus()) && bean.getStatus().equals("在库")) {
                objects = service.getNumsDetails(bean);
            } else {
                objects = service.getAllMacheDetails(bean);
            }
            for (MachineInfoBean machineDetail : objects) {
                String typeKeeperName = proDataUseInfoMapper.getTypeKeeperName(machineDetail);
                machineDetail.setTypeKeeperName(typeKeeperName);
            }
            return getDataTable(objects);
        } catch (Exception e) {
            logger.error(e.toString(), e);
            throw new ServiceException("数据查询异常，请联系运维人员查询日志处理");
        }
    }

    // 检验临期和检验超期数量
    @GetMapping(value = "getApproachingAndTimeout")
    public AjaxResult getApproachingAndTimeout(ProIdsBean bean) {
        try {
            List<StaticsNumsBean> results = service.getApproachingAndTimeout(bean);
            return AjaxResult.success(results);
        } catch (Exception e) {
            logger.error(e.toString(), e);
            throw new ServiceException("数据查询异常，请联系运维人员查询日志处理");
        }
    }

    // 临期、超期设备详情
    @GetMapping(value = "getApproachingDetails")
    public TableDataInfo getApproachingDetails(ProIdsBean bean) {
        try {
            startPage();
            List<MachineInfoBean> results = service.getApproachingDetails(bean);
            return getDataTable(results);
        } catch (Exception e) {
            logger.error(e.toString(), e);
            throw new ServiceException("数据查询异常，请联系运维人员查询日志处理");
        }
    }


    @GetMapping(value = "getApproachingDetailsByUnit")
    public AjaxResult getApproachingDetailsByUnit() {
        try {
            List<Map<String, Object>> map = service.getApproachingDetailsByUnit();
            return AjaxResult.success(map);
        } catch (Exception e) {
            logger.error(e.toString(), e);
            throw new ServiceException("数据查询异常，请联系运维人员查询日志处理");
        }
    }

    // 竣工未退预警数量（总公司）
    @GetMapping(value = "getCompletionNotRefunded")
    public AjaxResult getCompletionNotRefunded(ProIdsBean bean) {
        try {
            List<MachineInfoBean> results = service.getCompletionNotRefunded(bean);
            return AjaxResult.success(results);
        } catch (Exception e) {
            logger.error(e.toString(), e);
            throw new ServiceException("数据查询异常，请联系运维人员查询日志处理");
        }
    }

    // 竣工未退预警数量（分公司）
    @GetMapping(value = "getCompletionNotRefundedSub")
    public AjaxResult getCompletionNotRefundedSub(ProIdsBean bean) {
        try {
            List<MachineInfoBean> results = service.getCompletionNotRefundedSub(bean);
            return AjaxResult.success(results);
        } catch (Exception e) {
            logger.error(e.toString(), e);
            throw new ServiceException("数据查询异常，请联系运维人员查询日志处理");
        }
    }


    //竣工未退预警总量
    @GetMapping(value = "getCompletionNotRefundedNum")
    public AjaxResult getCompletionNotRefundedNum(ProIdsBean bean) {
        try {
            List<MachineInfoBean> results = service.getCompletionNotRefunded(bean);
            BigDecimal totalNum = new BigDecimal(0);
            for (MachineInfoBean completionNotRefundedDetail : results) {
                totalNum = totalNum.add(completionNotRefundedDetail.getNum());
            }
            return AjaxResult.success(totalNum);
        } catch (Exception e) {
            logger.error(e.toString(), e);
            throw new ServiceException("数据查询异常，请联系运维人员查询日志处理");
        }
    }

    //竣工未退预警详情
    @GetMapping(value = "getCompletionNotRefundedDetails")
    public TableDataInfo getCompletionNotRefundedDetails(ProIdsBean bean) {
        try {
            List<MachineInfoBean> results = new ArrayList<>();
            startPage();
            if (bean.getDeptId() != null) {
                results = service.getCompletionNotRefundedDetails(bean);
            } else {
                results = service.getCompletionNotRefundedDetailsSub(bean);
            }
            return getDataTable(results);
        } catch (Exception e) {
            logger.error(e.toString(), e);
            throw new ServiceException("数据查询异常，请联系运维人员查询日志处理");
        }
    }

    // 施工机具库存预警 (7大类)
    @GetMapping(value = "getInventoryAlert")
    public AjaxResult getInventoryAlert(ProIdsBean bean) {
        try {
            List<InventoryAlertBean> results = service.getInventoryAlert(bean);
            return AjaxResult.success(results);
        } catch (Exception e) {
            logger.error(e.toString(), e);
            throw new ServiceException("数据查询异常，请联系运维人员查询日志处理");
        }
    }


    @GetMapping(value = "getInventoryAlertName")
    public AjaxResult getInventoryAlertName(ProIdsBean bean) {
        try {
            List<InventoryAlertBean> results = service.getInventoryAlertName(bean);
            return AjaxResult.success(results);
        } catch (Exception e) {
            logger.error(e.toString(), e);
            throw new ServiceException("数据查询异常，请联系运维人员查询日志处理");
        }
    }

}
