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

import com.bonus.material.device.domain.vo.DevInfoVo;
import com.bonus.material.equipment.domain.ConfigEntity;
import com.bonus.material.equipment.domain.DeptEquipmentConfig;
import com.bonus.material.equipment.mapper.DeptEquipmentConfigMapper;
import com.bonus.material.equipment.service.IDeptEquipmentConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class DeptEquipmentConfigServiceImpl implements IDeptEquipmentConfigService {

    @Autowired
    private DeptEquipmentConfigMapper mapper;

    @Override
    public List<DeptEquipmentConfig> selectByDeptId(Long deptId) {
        return mapper.selectConfigByDept(deptId);
    }

    @Override
    public int saveBatch(DeptEquipmentConfig configList) {
        configList.setTypeId(configList.getEquipmentId());
        configList.setConfigValue(configList.getBasicConfig());
        DeptEquipmentConfig team = new DeptEquipmentConfig();
        team.setDeptId(configList.getDeptId());
        team.setTypeId(configList.getTypeId());
        mapper.deleteByDeptIdAndTypeId(team);

        List<ConfigEntity> originalConfigs = configList.getConfigs();
        if (originalConfigs == null || originalConfigs.isEmpty()) {
            return 1; // 只删除，无新增
        }
        // 合并逻辑：以 deptId + typeId + configType 为 key，configValue 累加
        Map<String, ConfigEntity> mergedMap = new LinkedHashMap<>();
        for (ConfigEntity item : originalConfigs) {
            item.setDeptId(configList.getDeptId());
            item.setTypeId(configList.getTypeId());
            item.setRemark(configList.getRemark());
            String key = item.getDeptId() + "_" + item.getTypeId() + "_" + item.getConfigurationType() + "_" + configList.getJijuType();

            if (mergedMap.containsKey(key)) {
                ConfigEntity existing = mergedMap.get(key);
                // configValue 是 String，需转数字再相加
                try {
                    double newVal = parseDoubleOrZero(item.getBasicConfig());
                    double oldVal = parseDoubleOrZero(existing.getBasicConfig());
                    double newrateVal = parseDoubleOrZero(item.getConfigurationRate());
                    double oldrateVal = parseDoubleOrZero(existing.getConfigurationRate());
                    existing.setBasicConfig(String.valueOf(newVal + oldVal));
                    existing.setConfigurationRate(String.valueOf(newrateVal + oldrateVal));
                } catch (NumberFormatException e) {
                    e.printStackTrace();
                }
            } else {
                mergedMap.put(key, item);
            }
        }

        configList.setConfigs(new ArrayList<>(mergedMap.values()));
        return mapper.insertOrUpdateBatch(configList);
    }

    @Override
    public List<DevInfoVo> selectDevInfoLists(DevInfoVo devInfoVo) {
        List<DevInfoVo> list = mapper.selectDevInfoLists(devInfoVo);
        return list;
    }

    private double parseDoubleOrZero(String value) {
        if (value == null || value.trim().isEmpty()) {
            return 0.0;
        }
        try {
            return Double.parseDouble(value.trim());
        } catch (NumberFormatException e) {
            return 0.0;
        }
    }

}
