package org.stm.audit.service.impl;

import org.stm.audit.entity.AuditComplianeItem;
import org.stm.audit.mapper.AuditComplianeItemMapper;
import org.stm.audit.service.IAuditComplianeItemService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @Description: audit_compliane_item
 * @Author: jeecg-boot
 * @Date:   2024-08-23
 * @Version: V1.0
 */
@Service
public class AuditComplianeItemServiceImpl extends ServiceImpl<AuditComplianeItemMapper, AuditComplianeItem> implements IAuditComplianeItemService {

    @Override
    public Map<Integer, Map<String, String>> queryAllActForItems(List<Integer> idList) {
        if (idList == null || idList.isEmpty()) {
            return Collections.emptyMap();
        }

        List<Map<String, Object>> dataList = this.baseMapper.queryAllActForItems(idList);
        if (dataList == null || dataList.isEmpty()) {
            return Collections.emptyMap();
        }

        // 使用分组优化性能，将时间复杂度从 O(n²) 降为 O(n)
        Map<Integer, List<Map<String, Object>>> groupedData = dataList.stream()
                .filter(Objects::nonNull)
                .collect(Collectors.groupingBy(
                        map -> getIntegerValue(map, "citemid")
                ));

        Map<Integer, Map<String, String>> resultMap = new HashMap<>();

        for (Integer targetId : idList) {
            List<Map<String, Object>> itemData = groupedData.get(targetId);
            if (itemData == null || itemData.isEmpty()) {
                continue;
            }

            // 使用Set去重
            Set<String> checkIdSet = new LinkedHashSet<>();
            Set<String> actIdSet = new LinkedHashSet<>();
            Set<String> caNameSet = new LinkedHashSet<>();

            for (Map<String, Object> map : itemData) {
                String checkId = getStringValue(map, "checkid");
                String actId = getStringValue(map, "actId");
                String caName = getStringValue(map, "caName");

                if (checkId != null) {
                    checkIdSet.add(checkId);
                }
                if (actId != null && caName != null) {
                    // actId和caName一一对应，一起处理
                    actIdSet.add(actId);
                    caNameSet.add(caName);
                }
            }

            if (!checkIdSet.isEmpty()) {
                Map<String, String> itemMap = new HashMap<>();
                itemMap.put("checkIds", String.join(",", checkIdSet));

                if (!actIdSet.isEmpty()) {
                    itemMap.put("actIds", String.join(",", actIdSet));
                    itemMap.put("caNames", String.join(",", caNameSet));
                }

                resultMap.put(targetId, itemMap);
            }
        }

        return resultMap;
    }

    /**
     * 安全获取Integer值
     */
    private Integer getIntegerValue(Map<String, Object> map, String key) {
        if (map == null || key == null) {
            return null;
        }
        Object value = map.get(key);
        if (value instanceof Integer) {
            return (Integer) value;
        } else if (value != null) {
            try {
                return Integer.valueOf(value.toString());
            } catch (NumberFormatException e) {
                return null;
            }
        }
        return null;
    }

    /**
     * 安全获取String值
     */
    private String getStringValue(Map<String, Object> map, String key) {
        if (map == null || key == null) {
            return null;
        }
        Object value = map.get(key);
        return value != null ? value.toString() : null;
    }
}