package com.ruoyi.info.collection.service.impl;

import com.ruoyi.info.collection.domain.excel.CcdiAssetInfoExcel;
import com.ruoyi.info.collection.domain.excel.CcdiBaseStaffAssetInfoExcel;
import com.ruoyi.info.collection.domain.excel.CcdiBaseStaffExcel;
import com.ruoyi.info.collection.domain.excel.CcdiStaffFmyRelationExcel;
import com.ruoyi.info.collection.service.ICcdiAssetInfoImportService;
import com.ruoyi.info.collection.service.ICcdiBaseStaffAssetImportService;
import com.ruoyi.info.collection.service.ICcdiBaseStaffImportService;
import com.ruoyi.info.collection.service.ICcdiStaffFmyRelationImportService;
import jakarta.annotation.Resource;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 双Sheet导入后台顺序编排。
 */
@Service
public class CcdiDualSheetImportOrchestrationService {

    @Resource
    private ICcdiBaseStaffImportService baseStaffImportService;

    @Resource
    private ICcdiBaseStaffAssetImportService baseStaffAssetImportService;

    @Resource
    private ICcdiStaffFmyRelationImportService relationImportService;

    @Resource
    private ICcdiAssetInfoImportService assetInfoImportService;

    @Async
    public void importBaseStaffWithAssetsAsync(List<CcdiBaseStaffExcel> staffList,
                                               String staffTaskId,
                                               List<CcdiBaseStaffAssetInfoExcel> assetList,
                                               String assetTaskId,
                                               String userName) {
        Set<String> successIdCards = baseStaffImportService.importBaseStaffSync(staffList, staffTaskId);
        baseStaffAssetImportService.importAssetInfoSync(
                assetList,
                assetTaskId,
                userName,
                buildSelfOwnerMappings(successIdCards)
        );
    }

    @Async
    public void importRelationWithAssetsAsync(List<CcdiStaffFmyRelationExcel> relationList,
                                              String relationTaskId,
                                              List<CcdiAssetInfoExcel> assetList,
                                              String assetTaskId,
                                              String userName) {
        Map<String, String> successRelationMappings = relationImportService.importRelationSync(relationList, relationTaskId, userName);
        assetInfoImportService.importAssetInfoSync(
                assetList,
                assetTaskId,
                userName,
                buildRelationOwnerMappings(successRelationMappings)
        );
    }

    private Map<String, Set<String>> buildSelfOwnerMappings(Set<String> idCards) {
        Map<String, Set<String>> result = new LinkedHashMap<>();
        if (idCards == null || idCards.isEmpty()) {
            return result;
        }
        for (String idCard : idCards) {
            result.computeIfAbsent(idCard, key -> new LinkedHashSet<>()).add(idCard);
        }
        return result;
    }

    private Map<String, Set<String>> buildRelationOwnerMappings(Map<String, String> relationMappings) {
        Map<String, Set<String>> result = new LinkedHashMap<>();
        if (relationMappings == null || relationMappings.isEmpty()) {
            return result;
        }
        for (Map.Entry<String, String> entry : relationMappings.entrySet()) {
            result.computeIfAbsent(entry.getKey(), key -> new LinkedHashSet<>()).add(entry.getValue());
        }
        return result;
    }
}
