package com.bonus.sgzb.largeScreen.util;

import com.bonus.sgzb.common.core.utils.StringUtils;
import com.bonus.sgzb.largeScreen.domain.CommonConstants;
import com.bonus.sgzb.largeScreen.domain.MaTypeDo;
import com.bonus.sgzb.largeScreen.domain.MaterialDataVo;
import com.bonus.sgzb.largeScreen.domain.ScrapAnalysisVo;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author 10488
 * 大屏公共util
 */
@Component
public class CommonUtil {

    /**
     * @param num
     * @param totalNum
     * @return String
     * @description 百分比
     * @author cwchen
     * @date 2023/12/12 9:39
     */
    public String getRate(int num, int totalNum) {
        DecimalFormat decimalFormat = new DecimalFormat("0.00%");
        if (totalNum == 0 || num == 0) {
            return "0%";
        } else {
            return decimalFormat.format(num * 1.0 / totalNum * 1.0);
        }
    }


    /**
     * @param list
     * @param maType
     * @return Integer
     * @description 新购验收入库分析-统计数量
     * @author cwchen
     * @date 2023/12/14 18:54
     */
    public Integer totalValue(List<Map<String, String>> list, String maType) {
        if (CollectionUtils.isEmpty(list) || StringUtils.isEmpty(maType)) {
            return 0;
        }
        BigDecimal value = new BigDecimal("0");
        for (int i = 0; i < list.size(); i++) {
            Map<String, String> map = list.get(i);
            String num = Objects.isNull(map.get("num")) ? "0" : String.valueOf(map.get("num"));
            BigDecimal value2 = new BigDecimal(num);
            value = value.add(value2);
        }
        double doubleValue = value.doubleValue();
        return (int) Math.round(doubleValue);
    }

    /**
     * @param list
     * @param maType
     * @return Integer
     * @description 当月领料分析-统计数量
     * @author cwchen
     * @date 2023/12/14 18:57
     */
    public List<Integer> totalValue2(List<MaTypeDo> list, String maType) {
        List<Integer> valueList = new ArrayList<>();
        if (CollectionUtils.isEmpty(list) || StringUtils.isEmpty(maType)) {
            valueList.add(0);
            valueList.add(0);
            return valueList;
        }
        BigDecimal numberValue = new BigDecimal("0");
        BigDecimal countValue = new BigDecimal("0");
        for (int i = 0; i < list.size(); i++) {
            if (Objects.nonNull(list.get(i).getNum()) && Objects.equals("0", list.get(i).getManageType())) {
                // 编号
                BigDecimal value = BigDecimal.valueOf(list.get(i).getNum());
                numberValue = numberValue.add(value);
            } else if (Objects.nonNull(list.get(i).getNum()) && Objects.equals("1", list.get(i).getManageType())) {
                // 计数
                BigDecimal value = BigDecimal.valueOf(list.get(i).getNum());
                countValue = countValue.add(value);
            }
        }
        double doubleNumberValue = numberValue.doubleValue();
        double doubleCountValue = countValue.doubleValue();
        valueList.add((int) Math.round(doubleNumberValue));
        valueList.add((int) Math.round(doubleCountValue));
        return valueList;
    }

    /**
     * @param list
     * @return List<Integer>
     * @description 领料数据-统计
     * @author cwchen
     * @date 2023/12/14 19:42
     */
    public List<Integer> totalValue3(List<MaTypeDo> list) {
        List<Integer> valueList = new ArrayList<>();
        if (CollectionUtils.isEmpty(list)) {
            valueList.add(0);
            valueList.add(0);
            return valueList;
        }
        BigDecimal jjValue = new BigDecimal("0");
        BigDecimal tsValue = new BigDecimal("0");
        for (int i = 0; i < list.size(); i++) {
            if (Objects.isNull(list.get(i))) {
                continue;
            }
            if (Objects.equals(list.get(i).getCompanyName(), CommonConstants.JJ)) {
                BigDecimal value = BigDecimal.valueOf(list.get(i).getNum());
                jjValue = jjValue.add(value);
            } else if (Objects.equals(list.get(i).getCompanyName(), CommonConstants.TS)) {
                BigDecimal value = BigDecimal.valueOf(list.get(i).getNum());
                tsValue = tsValue.add(value);
            }
        }
        double doubleJJValue = jjValue.doubleValue();
        double doubleTSValue = tsValue.doubleValue();
        valueList.add((int) Math.round(doubleJJValue));
        valueList.add((int) Math.round(doubleTSValue));
        return valueList;
    }

    /**
     * @param lyList
     * @param bfList
     * @return List<Object>
     * @description 当月报废分析-数据统计
     * @author cwchen
     * @date 2023/12/15 10:02
     */
    public List<Object> setScrapAnalysisData(List<MaterialDataVo> lyList, List<MaterialDataVo> bfList) {
        List<Object> list = new ArrayList<>();
        // 单位、领用数量、报废数量
        List<String> nameList = new ArrayList<>();
        List<Integer> valueList = new ArrayList<>();
        List<Integer> valueList2 = new ArrayList<>();
        for (int i = 0; i < lyList.size(); i++) {
            MaterialDataVo vo = lyList.get(i);
            if (Objects.nonNull(vo.getUnitName())) {
                nameList.add(vo.getUnitName());
                valueList.add(vo.getNum());
                for (int j = 0; j < bfList.size(); j++) {
                    MaterialDataVo vo2 = bfList.get(j);
                    if (Objects.nonNull(vo2.getUnitName()) && Objects.equals(vo.getUnitName(), vo2.getUnitName())) {
                        valueList2.add(vo2.getNum());
                    }
                }
            }
        }
        list.add(nameList);
        list.add(valueList);
        list.add(valueList2);
        return list;
    }

    /**
     * @param list
     * @return List<Integer>
     * @description 当月退料分析-统计
     * @author cwchen
     * @date 2023/12/15 14:59
     */
    public List<Integer> setMaterialReturnData(List<ScrapAnalysisVo> list) {
        List<Integer> valueList = new ArrayList<>();
        if (CollectionUtils.isEmpty(list)) {
            valueList.add(0);
            valueList.add(0);
            return valueList;
        }
        BigDecimal planValue = new BigDecimal("0");
        BigDecimal realityValue = new BigDecimal("0");
        for (int i = 0; i < list.size(); i++) {
            ScrapAnalysisVo vo = list.get(i);
            BigDecimal value = BigDecimal.valueOf(vo.getPlanNum());
            BigDecimal value2 = BigDecimal.valueOf(vo.getRealityNum());
            planValue = planValue.add(value);
            realityValue = realityValue.add(value2);
        }
        double doublePlanValue = planValue.doubleValue();
        double doubleRealityValue = realityValue.doubleValue();
        valueList.add((int) Math.round(doublePlanValue));
        valueList.add((int) Math.round(doubleRealityValue));
        return valueList;
    }

    /**
     * @param list
     * @return List<Integer>
     * @description 当月维修分析-统计
     * @author cwchen
     * @date 2023/12/15 15:20
     */
    public List<Integer> setMaintenanceData(List<ScrapAnalysisVo> list) {
        List<Integer> valueList = new ArrayList<>();
        if (CollectionUtils.isEmpty(list)) {
            for (int i = 0; i < 4; i++) {
                valueList.add(0);
            }
            return valueList;
        }
        /*维修总量/维修合格数量/维修报废数量/待维修数量*/
        BigDecimal repairValue = new BigDecimal("0");
        BigDecimal repairedValue = new BigDecimal("0");
        BigDecimal scrapValue = new BigDecimal("0");
        BigDecimal waitRepairedValue = new BigDecimal("0");
        for (int i = 0; i < list.size(); i++) {
            ScrapAnalysisVo vo = list.get(i);
            BigDecimal value = BigDecimal.valueOf(vo.getRepairNum());
            BigDecimal value2 = BigDecimal.valueOf(vo.getRepairedNum());
            BigDecimal value3 = BigDecimal.valueOf(vo.getScrapNum());
            repairValue = repairValue.add(value);
            repairedValue = repairedValue.add(value2);
            scrapValue = scrapValue.add(value3);
        }
        waitRepairedValue = repairValue.subtract(repairedValue).subtract(scrapValue);
        double doubleRepairValue = repairValue.doubleValue();
        double doubleRepairedValue = repairedValue.doubleValue();
        double doubleScrapValue = scrapValue.doubleValue();
        double doubleWaitRepairedValue = waitRepairedValue.doubleValue();

        valueList.add((int) Math.round(doubleRepairValue));
        valueList.add((int) Math.round(doubleRepairedValue));
        valueList.add((int) Math.round(doubleScrapValue));
        valueList.add((int) Math.round(doubleWaitRepairedValue));
        return valueList;
    }

    /**
     * @param list
     * @return Integer
     * @description 保有量-在库数量
     * @author cwchen
     * @date 2023/12/15 19:21
     */
    public Integer setZKData(List<ScrapAnalysisVo> list) {
        if (CollectionUtils.isEmpty(list)) {
            return 0;
        }
        BigDecimal totalValue = new BigDecimal("0");
        for (int i = 0; i < list.size(); i++) {
            ScrapAnalysisVo vo = list.get(i);
            BigDecimal value = BigDecimal.valueOf(vo.getNum());
            totalValue = totalValue.add(value);
        }
        double doubleTotalValue = totalValue.doubleValue();
        return (int) Math.round(doubleTotalValue);
    }

    /**
     * @param list
     * @return Integer
     * @description 保有量-待入库数量
     * @author cwchen
     * @date 2023/12/16 13:20
     */
    public Integer setDRKData(List<ScrapAnalysisVo> list) {
        if (CollectionUtils.isEmpty(list)) {
            return 0;
        }
        BigDecimal finalValue = new BigDecimal("0");
        BigDecimal totalValue = new BigDecimal("0");
        BigDecimal totalValue2 = new BigDecimal("0");
        for (int i = 0; i < list.size(); i++) {
            ScrapAnalysisVo vo = list.get(i);
            BigDecimal value = BigDecimal.valueOf(vo.getNum());
            totalValue = totalValue.add(value);
            BigDecimal value2 = BigDecimal.valueOf(vo.getNum2());
            totalValue2 = totalValue2.add(value2);
        }
        finalValue = totalValue.subtract(totalValue2);
        double finalDoubleValue = finalValue.doubleValue() >= 0 ? finalValue.doubleValue() : 0;
        return (int) Math.round(finalDoubleValue);
    }

    /**
     * @param list
     * @return Integer
     * @description 保有量-在用数量
     * @author cwchen
     * @date 2023/12/16 14:01
     */
    public Integer setUSEData(List<ScrapAnalysisVo> list) {
        if (CollectionUtils.isEmpty(list)) {
            return 0;
        }
        BigDecimal finalValue = new BigDecimal("0");
        BigDecimal totalValue = new BigDecimal("0");
        BigDecimal totalValue2 = new BigDecimal("0");
        for (int i = 0; i < list.size(); i++) {
            ScrapAnalysisVo vo = list.get(i);
            if (Objects.equals("1", vo.getType())) {
                BigDecimal value = BigDecimal.valueOf(vo.getNum());
                totalValue = totalValue.add(value);
            } else if (Objects.equals("2", vo.getType())) {
                BigDecimal value2 = BigDecimal.valueOf(vo.getNum());
                totalValue2 = totalValue2.add(value2);
            }
        }
        finalValue = totalValue.subtract(totalValue2);
        double finalDoubleValue = finalValue.doubleValue() >= 0 ? finalValue.doubleValue() : 0;
        return (int) Math.round(finalDoubleValue);
    }

    /**
     * @param list
     * @return Integer
     * @description 保有量-在修数量
     * @author cwchen
     * @date 2023/12/16 14:07
     */
    public Integer setZXData(List<ScrapAnalysisVo> list) {
        if (CollectionUtils.isEmpty(list)){
            return 0;
        }
        /*维修总量/维修合格数量/维修报废数量/待维修数量*/
        BigDecimal repairValue = new BigDecimal("0");
        BigDecimal repairedValue = new BigDecimal("0");
        BigDecimal scrapValue = new BigDecimal("0");
        BigDecimal waitRepairedValue = new BigDecimal("0");
        for (int i = 0; i < list.size(); i++) {
            ScrapAnalysisVo vo = list.get(i);
            BigDecimal value = BigDecimal.valueOf(vo.getRepairNum());
            BigDecimal value2 = BigDecimal.valueOf(vo.getRepairedNum());
            BigDecimal value3 = BigDecimal.valueOf(vo.getScrapNum());
            repairValue = repairValue.add(value);
            repairedValue = repairedValue.add(value2);
            scrapValue = scrapValue.add(value3);
        }
        waitRepairedValue = repairValue.subtract(repairedValue).subtract(scrapValue);
        double finalDoubleValue = waitRepairedValue.doubleValue() >= 0 ? waitRepairedValue.doubleValue() : 0;
        return (int) Math.round(finalDoubleValue);
    }

    /**
     * @param list
     * @return Integer
     * @description 保有量-报废
     * @author cwchen
     * @date 2023/12/16 14:22
     */
    public Integer setBFData(List<ScrapAnalysisVo> list) {
        if (CollectionUtils.isEmpty(list)){
            return 0;
        }
        BigDecimal totalValue = new BigDecimal("0");
        for (int i = 0; i < list.size(); i++) {
            ScrapAnalysisVo vo = list.get(i);
            BigDecimal value = BigDecimal.valueOf(vo.getNum());
            totalValue = totalValue.add(value);
        }
        double doubleTotalValue = totalValue.doubleValue();
        return (int) Math.round(doubleTotalValue);
    }

    /**
     * 修饰待入库
     * @param list
     * @return
     */
    public Integer setDeData(List<ScrapAnalysisVo> list) {
        if (CollectionUtils.isEmpty(list)){
            return 0;
        }
        BigDecimal finalValue = new BigDecimal("0");
        BigDecimal totalValue = new BigDecimal("0");
        BigDecimal totalValue2 = new BigDecimal("0");
        for (int i = 0; i < list.size(); i++) {
            ScrapAnalysisVo vo = list.get(i);
            BigDecimal value = BigDecimal.valueOf(vo.getNum());
            totalValue = totalValue.add(value);
            BigDecimal value2 = BigDecimal.valueOf(vo.getNum2());
            totalValue2 = totalValue2.add(value2);
        }
        finalValue = totalValue.subtract(totalValue2);
        double doubleValue = finalValue.doubleValue();
        return (int) Math.round(doubleValue);
    }
}
