package com.bonus.material.push.service.impl;

import com.alibaba.nacos.common.utils.CollectionUtils;
import com.bonus.material.push.domain.*;

import com.bonus.material.push.mapper.ProDataUseInfoMapper;
import com.bonus.material.push.service.ProDataUseInfoService;
import com.bonus.system.api.domain.SysUser;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author：liang.chao
 * @Date：2025/8/23 - 14:30
 */
@Service
public class ProDataUseInfoImpl implements ProDataUseInfoService {

    @Resource
    private ProDataUseInfoMapper proDataUseInfoMapper;

    @Override
    public List<ProIdsBean> getProDataUseInfo(ProIdsBean bean) {
        List<Integer> typeIds = proDataUseInfoMapper.getTypeId();
        List<ProIdsBean> results = new ArrayList<>();
        for (String proId : bean.getProIds()) {
            List<ProIdsBean> proDataUseInfo = proDataUseInfoMapper.getProDataUseInfo(typeIds, proId);
            results.addAll(proDataUseInfo);
        }
        return results;
    }

    @Override
    public List<MachineInfoBean> getMachineDetails(ProIdsBean bean) {
        // 入参 proId /isStatics
        List<MachineInfoBean> machineDetails = proDataUseInfoMapper.getMachineDetails(bean);
        for (MachineInfoBean machineDetail : machineDetails) {
            String typeKeeperName = proDataUseInfoMapper.getTypeKeeperName(machineDetail);
            machineDetail.setTypeKeeperName(typeKeeperName);
        }
        return machineDetails;
    }

    /**
     * 统计数据
     *
     * @param bean
     * @return
     */
    @Override
    public List<TotalNumsBean> getNumsByStatics(ProIdsBean bean) {
        List<TotalNumsBean> beans = new ArrayList<>();
        List<StaticsNumsBean> list = proDataUseInfoMapper.getNumsByStatics(bean);

        if (CollectionUtils.isNotEmpty(list)) {
            // 初始化7个分类的统计对象
            TotalNumsBean[] categoryBeans = new TotalNumsBean[7];
            for (int i = 0; i < 7; i++) {
                categoryBeans[i] = new TotalNumsBean();
                categoryBeans[i].setIsStatics(i + 1); // 设置分类类型(1-7)
                // 初始化所有数值字段为0
                categoryBeans[i].setInventoryNum(BigDecimal.ZERO.setScale(3, RoundingMode.HALF_UP));
                categoryBeans[i].setUseNum(BigDecimal.ZERO.setScale(3, RoundingMode.HALF_UP));
                categoryBeans[i].setRepairNum(BigDecimal.ZERO.setScale(3, RoundingMode.HALF_UP));
                categoryBeans[i].setNum(BigDecimal.ZERO.setScale(3, RoundingMode.HALF_UP));
                categoryBeans[i].setApproachingNum(BigDecimal.ZERO.setScale(3, RoundingMode.HALF_UP));
                categoryBeans[i].setTimeoutNum(BigDecimal.ZERO.setScale(3, RoundingMode.HALF_UP));
            }

            // 总统计对象
            TotalNumsBean totalBean = new TotalNumsBean();
            totalBean.setIsStatics(0); // 0表示总计
            totalBean.setInventoryNum(BigDecimal.ZERO.setScale(3, RoundingMode.HALF_UP));
            totalBean.setUseNum(BigDecimal.ZERO.setScale(3, RoundingMode.HALF_UP));
            totalBean.setRepairNum(BigDecimal.ZERO.setScale(3, RoundingMode.HALF_UP));
            totalBean.setNum(BigDecimal.ZERO.setScale(3, RoundingMode.HALF_UP));
            totalBean.setApproachingNum(BigDecimal.ZERO.setScale(3, RoundingMode.HALF_UP));
            totalBean.setTimeoutNum(BigDecimal.ZERO.setScale(3, RoundingMode.HALF_UP));

            // 遍历数据并分类统计
            for (StaticsNumsBean staticsNumsBean : list) {
                Integer category = staticsNumsBean.getIsStatics();

                if (category != null && category >= 1 && category <= 7) {
                    TotalNumsBean categoryBean = categoryBeans[category - 1];

                    // 累加各个数值字段
                    addToTotal(categoryBean, staticsNumsBean);
                    // 同时累加到总计
                    addToTotal(totalBean, staticsNumsBean);
                }
            }

            // 添加到结果列表
            beans.add(totalBean); // 先添加总计
            Collections.addAll(beans, categoryBeans);
        }

        return beans;
    }

    /**
     * 将源对象的数值累加到目标对象
     */
    private void addToTotal(TotalNumsBean target, StaticsNumsBean source) {
        if (source.getInventoryNum() != null) {
            target.setInventoryNum(target.getInventoryNum().add(source.getInventoryNum()));
        }
        if (source.getUseNum() != null) {
            target.setUseNum(target.getUseNum().add(source.getUseNum()));
        }
        if (source.getRepairNum() != null) {
            target.setRepairNum(target.getRepairNum().add(source.getRepairNum()));
        }
        if (source.getNum() != null) {
            target.setNum(target.getNum().add(source.getNum()));
        }
        if (source.getApproachingNum() != null) {
            target.setApproachingNum(target.getApproachingNum().add(source.getApproachingNum()));
        }
        if (source.getTimeoutNum() != null) {
            target.setTimeoutNum(target.getTimeoutNum().add(source.getTimeoutNum()));
        }
    }

    @Override
    public List<StaticsNumsBean> getApproachingAndTimeout(ProIdsBean bean) {
        return proDataUseInfoMapper.getApproachingAndTimeout(bean);
    }

    @Override
    public List<MachineInfoBean> getApproachingDetails(ProIdsBean bean) {
        return proDataUseInfoMapper.getApproachingDetails(bean);
    }

    @Override
    public List<MachineInfoBean> getCompletionNotRefunded(ProIdsBean bean) {
        return proDataUseInfoMapper.getCompletionNotRefunded(bean);
    }

    @Override
    public List<MachineInfoBean> getCompletionNotRefundedDetails(ProIdsBean bean) {
        return proDataUseInfoMapper.getCompletionNotRefundedDetails(bean);
    }

    @Override
    public List<InventoryAlertBean> getInventoryAlert(ProIdsBean bean) {
        return proDataUseInfoMapper.getInventoryAlert(bean);
    }

    /**
     * @param bean
     * @return
     */
    @Override
    public List<InventoryAlertBean> getInventoryAlertName(ProIdsBean bean) {
        return proDataUseInfoMapper.getInventoryAlertName(bean);
    }

    /**
     * @return
     */
    @Override
    public List<Map<String, Object>> getApproachingDetailsByUnit() {
        return proDataUseInfoMapper.getApproachingDetailsByUnit();
    }

    /**
     * @param bean
     * @return
     */
    @Override
    public List<MachineInfoBean> getMaTypeList(ProIdsBean bean) {
        List<MachineInfoBean> maTypeList = proDataUseInfoMapper.getMaTypeList(bean);
        for (MachineInfoBean machineDetail : maTypeList) {
            SysUser user = proDataUseInfoMapper.getPersonNameAndPhone(machineDetail);
            machineDetail.setPersonName(user.getNickName());
            machineDetail.setPersoPhone(user.getPhonenumber());
        }
        return maTypeList;
    }

    @Override
    public List<MachineInfoBean> getUseNumsDetails(ProIdsBean bean) {
        // 在用量
        return proDataUseInfoMapper.getUseNumsDetails(bean);
    }

    @Override
    public List<MachineInfoBean> getRepairNumsDetails(ProIdsBean bean) {
        // 在修量
        return proDataUseInfoMapper.getRepairNumsDetails(bean);
    }

    @Override
    public List<MachineInfoBean> getNumsDetails(ProIdsBean bean) {
        // 库存量
        return proDataUseInfoMapper.getNumsDetails(bean);
    }

    @Override
    public List<MachineInfoBean> getAllMacheDetails(ProIdsBean bean) {
        List<MachineInfoBean> objects = proDataUseInfoMapper.getAllMacheDetails(bean);
        return objects;
    }

    @Override
    public List<MachineInfoBean> getCompletionNotRefundedSub(ProIdsBean bean) {
        return proDataUseInfoMapper.getCompletionNotRefundedSub(bean);
    }

    @Override
    public List<MachineInfoBean> getCompletionNotRefundedDetailsSub(ProIdsBean bean) {
        return proDataUseInfoMapper.getCompletionNotRefundedDetailsSub(bean);
    }
}
