package com.rj.common;

import com.rj.entity.Dealership;
import com.rj.entity.ProjectManagement;
import com.rj.entity.SalesManagement;
import com.rj.service.IDealershipService;
import com.rj.service.IProjectManagementService;
import com.rj.service.ISalesManagementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * DataEnrichmentUtil 使用示例
 * 展示如何使用数据丰富工具类来减少代码冗余
 *
 * @author 系统生成
 * @since 2025-08-07
 */
@Component
public class DataEnrichmentUtilExample {

    @Autowired
    private DataEnrichmentUtil dataEnrichmentUtil;

    @Autowired
    private IDealershipService dealershipService;

    @Autowired
    private IProjectManagementService projectManagementService;

    @Autowired
    private ISalesManagementService salesManagementService;

    /**
     * 示例1：基本用法 - 批量获取门店名称映射
     */
    public Map<String, String> getDealershipNameMap(List<String> dealershipIds) {
        return DataEnrichmentUtil.batchGetNameMap(
                dealershipIds,
                dealershipService,
                Dealership::getId,
                Dealership::getDealershipName
        );
    }

    /**
     * 示例1-1：使用便捷方法 - 批量获取门店名称映射
     */
    public Map<String, String> getDealershipNameMapEasy(List<String> dealershipIds) {
        return dataEnrichmentUtil.getDealershipIdNameMap(dealershipIds);
    }

    /**
     * 示例2：带条件的批量获取 - 只获取启用的门店
     */
    public Map<String, String> getActiveDealershipNameMap(List<String> dealershipIds) {
        return DataEnrichmentUtil.batchGetNameMap(
                dealershipIds,
                dealershipService,
                Dealership::getId,
                Dealership::getDealershipName,
                queryWrapper -> queryWrapper.eq(Dealership::getStatus, true)
        );
    }

    /**
     * 示例3：获取多个字段 - 获取门店的名称和地区
     */
    public Map<String, Map<String, Object>> getDealershipInfoMap(List<String> dealershipIds) {
        Map<String, java.util.function.Function<Dealership, Object>> fieldGetters = Map.of(
                "name", Dealership::getDealershipName,
                "region", Dealership::getRegion,
                "type", Dealership::getDealershipType
        );

        return DataEnrichmentUtil.batchGetFieldMap(
                dealershipIds,
                dealershipService,
                Dealership::getId,
                fieldGetters
        );
    }

    /**
     * 示例4：从实体列表中提取ID
     */
    public List<String> extractDealershipIds(List<SalesManagement> salesList) {
        return DataEnrichmentUtil.extractIds(salesList, SalesManagement::getDealershipId);
    }

    /**
     * 示例5：带过滤条件的ID提取 - 只提取有门店ID的记录
     */
    public List<String> extractValidDealershipIds(List<SalesManagement> salesList) {
        return DataEnrichmentUtil.extractIds(
                salesList,
                SalesManagement::getDealershipId,
                sales -> sales.getDealershipId() != null && !sales.getDealershipId().trim().isEmpty()
        );
    }

    /**
     * 示例6：完整的销售数据丰富流程
     */
    public void enrichSalesData(List<SalesManagement> salesList) {
        // 1. 提取关联ID
        List<String> dealershipIds = DataEnrichmentUtil.extractIds(salesList, SalesManagement::getDealershipId);
        List<String> projectIds = DataEnrichmentUtil.extractIds(salesList, SalesManagement::getProjectId);

        // 2. 批量获取关联数据
        Map<String, String> dealershipNameMap = DataEnrichmentUtil.batchGetNameMap(
                dealershipIds, dealershipService, Dealership::getId, Dealership::getDealershipName);
        
        Map<String, String> projectNameMap = DataEnrichmentUtil.batchGetNameMap(
                projectIds, projectManagementService, ProjectManagement::getId, ProjectManagement::getProjectName);

        // 3. 填充关联数据
        salesList.forEach(sales -> {
            sales.setDealershipName(dealershipNameMap.get(sales.getDealershipId()));
            sales.setProjectName(projectNameMap.get(sales.getProjectId()));
        });
    }

    /**
     * 示例6-1：使用便捷方法的销售数据丰富流程
     */
    public void enrichSalesDataEasy(List<SalesManagement> salesList) {
        // 1. 提取关联ID
        List<String> dealershipIds = DataEnrichmentUtil.extractIds(salesList, SalesManagement::getDealershipId);
        List<String> projectIds = DataEnrichmentUtil.extractIds(salesList, SalesManagement::getProjectId);

        // 2. 使用便捷方法批量获取关联数据
        Map<String, String> dealershipNameMap = dataEnrichmentUtil.getDealershipIdNameMap(dealershipIds);
        Map<String, String> projectNameMap = dataEnrichmentUtil.getProjectIdNameMap(projectIds);

        // 3. 填充关联数据
        salesList.forEach(sales -> {
            sales.setDealershipName(dealershipNameMap.get(sales.getDealershipId()));
            sales.setProjectName(projectNameMap.get(sales.getProjectId()));
        });
    }

    /**
     * 示例7：获取项目详细信息（包含多个字段）
     */
    public Map<String, Map<String, Object>> getProjectDetailMap(List<String> projectIds) {
        Map<String, java.util.function.Function<ProjectManagement, Object>> fieldGetters = Map.of(
                "name", ProjectManagement::getProjectName,
                "version", ProjectManagement::getScriptVersion,
                "status", ProjectManagement::getStatus,
                "group", ProjectManagement::getGroupName
        );

        return DataEnrichmentUtil.batchGetFieldMap(
                projectIds,
                projectManagementService,
                ProjectManagement::getId,
                fieldGetters
        );
    }
}
