package com.xisc.pm.app.service.impl;

import com.xisc.pm.api.dto.PmPolicyReqDTO;
import com.xisc.pm.api.dto.PmPolicyResDTO;
import com.xisc.pm.api.dto.PmPolicySaveDTO;
import com.xisc.pm.app.service.PmPolicyService;
import com.xisc.pm.domain.entity.PmPolicy;
import com.xisc.pm.domain.entity.PmPolicySection;
import com.xisc.pm.domain.entity.PmPolicySlabMethod;
import com.xisc.pm.domain.entity.PmPolicySteelGroup;
import com.xisc.pm.domain.entity.PmPolicySteelType;
import com.xisc.pm.domain.entity.PmPolicyVip;
import com.xisc.pm.domain.entity.PmProductionLine;
import com.xisc.pm.domain.repository.PmPolicyRepository;
import com.xisc.pm.domain.repository.PmPolicySectionRepository;
import com.xisc.pm.domain.repository.PmPolicySlabMethodRepository;
import com.xisc.pm.domain.repository.PmPolicySteelGroupRepository;
import com.xisc.pm.domain.repository.PmPolicySteelTypeRepository;
import com.xisc.pm.domain.repository.PmPolicyVipRepository;
import com.xisc.pm.domain.repository.PmProductionLineRepository;
import com.xisc.pm.infra.constant.Constants;
import io.choerodon.core.domain.Page;
import io.choerodon.core.exception.CommonException;
import io.choerodon.mybatis.pagehelper.PageHelper;
import io.choerodon.mybatis.pagehelper.domain.PageRequest;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.util.Strings;
import org.hzero.boot.platform.code.builder.CodeRuleBuilder;
import org.hzero.boot.platform.code.constant.CodeConstants;
import org.hzero.mybatis.domian.Condition;
import org.hzero.mybatis.util.Sqls;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 策略信息表(PmPolicy)应用服务
 *
 * @author 纪文桃 wentao.ji01@hand-china.com
 * @since 2024-10-16 10:43:06
 */
@Service
public class PmPolicyServiceImpl implements PmPolicyService {
    @Autowired
    private PmPolicyRepository pmPolicyRepository;

    @Autowired
    private PmPolicyVipRepository pmPolicyVipRepository;

    @Autowired
    private PmPolicySlabMethodRepository pmPolicySlabMethodRepository;

    @Autowired
    private PmPolicySectionRepository pmPolicySectionRepository;

    @Autowired
    private CodeRuleBuilder codeRuleBuilder;

    @Autowired
    private PmPolicySteelTypeRepository pmPolicySteelTypeRepository;

    @Autowired
    private PmPolicySteelGroupRepository pmPolicySteelGroupRepository;
    @Autowired
    private PmProductionLineRepository pmProductionLineRepository;


    @Override
    public Page<PmPolicy> selectList(PageRequest pageRequest, PmPolicyReqDTO dto, Long tenantId) {
        // 筛选客户信息
        Set<Long> policyIdSet = new HashSet<>(16);
        if (StringUtils.isNotEmpty(dto.getCustomerCode())) {
            List<PmPolicyVip> pmPolicyVipList = pmPolicyVipRepository.selectByCondition(Condition.builder(PmPolicyVip.class)
                    .andWhere(Sqls.custom().andLike(PmPolicyVip.FIELD_CUSTOMER_CODE, dto.getCustomerCode()))
                    .andWhere(Sqls.custom().andEqualTo(PmPolicyVip.FIELD_TENANT_ID, tenantId))
                    .build());
            if (CollectionUtils.isEmpty(pmPolicyVipList)) {
                return new Page<>();
            }
            policyIdSet.addAll(pmPolicyVipList.stream().map(PmPolicyVip::getPolicyId).collect(Collectors.toSet()));
        }

        // 钢种
        List<String> stNoList = new ArrayList<>();
        if (StringUtils.isNotEmpty(dto.getStNo())) {
            for (String stNo : dto.getStNo().split(",")) {
                stNoList.add(stNo);
            }
        }
        // 断面
        if (StringUtils.isNotEmpty(dto.getMoldThick()) && StringUtils.isNotEmpty(dto.getMoldWid())) {
            String[] splitMoldThick = dto.getMoldThick().split(",");
            String[] splitMoldWid = dto.getMoldWid().split(",");
            Condition.Builder build = Condition.builder(PmPolicySection.class);
            for (int i = 0; i < splitMoldThick.length; i++) {
                if (i == 0) {
                    build.where(Sqls.custom().andEqualTo(PmPolicySection.FIELD_MOLD_THICK, splitMoldThick[i])
                            .andEqualTo(PmPolicySection.FIELD_MOLD_WID, splitMoldWid[i])
                            .andEqualTo(PmPolicySection.FIELD_TENANT_ID, tenantId));
                } else {
                    build.orWhere(Sqls.custom().andEqualTo(PmPolicySection.FIELD_MOLD_THICK, splitMoldThick[i])
                            .andEqualTo(PmPolicySection.FIELD_MOLD_WID, splitMoldWid[i])
                            .andEqualTo(PmPolicySection.FIELD_TENANT_ID, tenantId));
                }
            }
            List<PmPolicySection> pmPolicySectionList = pmPolicySectionRepository.selectByCondition(build.build());
            if (CollectionUtils.isEmpty(pmPolicySectionList)) {
                return new Page<>();
            }
            if (CollectionUtils.isNotEmpty(policyIdSet)) {
                // 取交集
                policyIdSet.retainAll(pmPolicySectionList.stream().map(PmPolicySection::getPolicyId).collect(Collectors.toSet()));
            } else {
                policyIdSet.addAll(pmPolicySectionList.stream().map(PmPolicySection::getPolicyId).collect(Collectors.toSet()));
            }
        }

        // 分页查询
        Page<PmPolicy> pageResult = PageHelper.doPageAndSort(pageRequest, () -> pmPolicyRepository.selectList(dto, tenantId, new ArrayList<>(policyIdSet), stNoList));
        if (CollectionUtils.isEmpty(pageResult.getContent())) {
            return new Page<>();
        }

        // 获取id集合
        List<Long> idList = pageResult.getContent().stream().map(PmPolicy::getPolicyId).collect(Collectors.toList());

        // 内部钢种
        List<PmPolicySteelType> pmPolicySteelTypeList = pmPolicySteelTypeRepository.selectByCondition(Condition.builder(PmPolicySteelType.class)
                .andWhere(Sqls.custom().andIn(PmPolicySteelType.FIELD_POLICY_ID, idList))
                .andWhere(Sqls.custom().andEqualTo(PmPolicySteelType.FIELD_TENANT_ID, tenantId))
                .build());
        Map<Long, List<PmPolicySteelType>> pmPolicySteelTypeMap = new HashMap<>();
        if (CollectionUtils.isNotEmpty(pmPolicySteelTypeList)) {
            pmPolicySteelTypeMap = pmPolicySteelTypeList.stream().collect(Collectors.groupingBy(PmPolicySteelType::getPolicyId));
        }

        // 钢种组
        List<PmPolicySteelGroup> pmPolicySteelGroupList = pmPolicySteelGroupRepository.selectByCondition(Condition.builder(PmPolicySteelGroup.class)
                .andWhere(Sqls.custom().andIn(PmPolicySteelGroup.FIELD_POLICY_ID, idList))
                .andWhere(Sqls.custom().andEqualTo(PmPolicySteelGroup.FIELD_TENANT_ID, tenantId))
                .build());
        Map<Long, List<PmPolicySteelGroup>> pmPolicySteelGroupMap = new HashMap<>();
        if (CollectionUtils.isNotEmpty(pmPolicySteelGroupList)) {
            pmPolicySteelGroupMap = pmPolicySteelGroupList.stream().collect(Collectors.groupingBy(PmPolicySteelGroup::getPolicyId));
        }

        // 赋值
        for (PmPolicy line : pageResult.getContent()) {
            // 内部钢种，多条拼接展示
            if (pmPolicySteelTypeMap.containsKey(line.getPolicyId())) {
                line.setStNo(pmPolicySteelTypeMap.get(line.getPolicyId()).stream().map(PmPolicySteelType::getStNo).collect(Collectors.joining(",")));
            }
            // 钢种组编码，多条拼接展示
            if (pmPolicySteelGroupMap.containsKey(line.getPolicyId())) {
                line.setSteelGroupCode(pmPolicySteelGroupMap.get(line.getPolicyId())
                        .stream().map(PmPolicySteelGroup::getSteelGroupCode).collect(Collectors.joining(",")));
            }
        }
        return pageResult;
    }

    @Override
    public PmPolicyResDTO listLine(Long policyId, Long tenantId) {
        return  getPolicyData(Arrays.asList(policyId), tenantId);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveData(PmPolicySaveDTO dto, Long tenantId) {
        // 校验数据
        if (CollectionUtils.isNotEmpty(dto.getPmPolicyResDTO().getPmPolicySlabMethodList())) {
            // 按照优先级分组
            Map<Long, Long> pmPolicySlabMethodMap = dto.getPmPolicyResDTO().getPmPolicySlabMethodList().
                    stream().collect(Collectors.groupingBy(PmPolicySlabMethod::getPriority, Collectors.counting()));
            pmPolicySlabMethodMap.forEach((key, value) -> {
                if (value > 1) {
                    throw new CommonException("pm.policy_slab_method_001", key);
                }
            });
            // 按组板方式分组
            Map<String, Long> policySlabMethodMap = dto.getPmPolicyResDTO().getPmPolicySlabMethodList().
                    stream().collect(Collectors.groupingBy(PmPolicySlabMethod::getComboSlabMethod, Collectors.counting()));
            policySlabMethodMap.forEach((key, value) -> {
                if (value > 1) {
                    throw new CommonException("pm.policy_slab_method_002", key);
                }
            });
        }

        if (CollectionUtils.isNotEmpty(dto.getPmPolicyResDTO().getPmPolicyVipList())) {
            // 按照优先级分组
            Map<Long, Long> pmPolicyVipMap = dto.getPmPolicyResDTO().getPmPolicyVipList().
                    stream().collect(Collectors.groupingBy(PmPolicyVip::getPriority, Collectors.counting()));
            pmPolicyVipMap.forEach((key, value) -> {
                if (value > 1) {
                    throw new CommonException("pm.policy_vip_001", key);
                }
            });
            // 按客户分组
            Map<String, Long> policyVipMap = dto.getPmPolicyResDTO().getPmPolicyVipList().
                    stream().collect(Collectors.groupingBy(PmPolicyVip::getCustomerCode, Collectors.counting()));
            policyVipMap.forEach((key, value) -> {
                if (value > 1) {
                    throw new CommonException("pm.policy_vip_002", key);
                }
            });
        }

        if (CollectionUtils.isNotEmpty(dto.getPmPolicyResDTO().getPmPolicySectionList())) {
            // 按照优先级分组
            Map<Long, Long> pmPolicySectionMap = dto.getPmPolicyResDTO().getPmPolicySectionList().
                    stream().collect(Collectors.groupingBy(PmPolicySection::getPriority, Collectors.counting()));
            pmPolicySectionMap.forEach((key, value) -> {
                if (value > 1) {
                    throw new CommonException("pm.policy_section_001", key);
                }
            });
            // 按规格分组
            Map<String, Long> policyVipMap = dto.getPmPolicyResDTO().getPmPolicySectionList().
                    stream().collect(Collectors.groupingBy(item -> item.getMoldThick() + "*" + item.getMoldWid(), Collectors.counting()));
            policyVipMap.forEach((key, value) -> {
                if (value > 1) {
                    throw new CommonException("pm.policy_section_002", key);
                }
            });
        }

        if (dto.getPmPolicy().getPolicyId() != null) {
            // 策略信息
            PmPolicyResDTO policyData = getPolicyData(Arrays.asList(dto.getPmPolicy().getPolicyId()), tenantId);

            // 钢种组
            List<PmPolicySteelGroup> pmPolicySteelGroupList = pmPolicySteelGroupRepository.selectByCondition(Condition.builder(PmPolicySteelGroup.class)
                            .andWhere(Sqls.custom().andEqualTo(PmPolicySteelGroup.FIELD_POLICY_ID,  dto.getPmPolicy().getPolicyId()))
                            .andWhere(Sqls.custom().andEqualTo(PmPolicySteelGroup.FIELD_TENANT_ID, tenantId))
                    .build());

            // 内部钢种
            List<PmPolicySteelType> pmPolicySteelTypeList = pmPolicySteelTypeRepository.selectByCondition(Condition.builder(PmPolicySteelType.class)
                    .andWhere(Sqls.custom().andEqualTo(PmPolicySteelType.FIELD_POLICY_ID, dto.getPmPolicy().getPolicyId()))
                    .andWhere(Sqls.custom().andEqualTo(PmPolicySteelType.FIELD_TENANT_ID, tenantId))
                    .build());
            // 更新策略表
            pmPolicyRepository.updateByPrimaryKey(dto.getPmPolicy());
            // 内部钢种
            pmPolicySteelTypeRepository.batchDeleteByPrimaryKey(pmPolicySteelTypeList);
            // 钢种组
            pmPolicySteelGroupRepository.batchDeleteByPrimaryKey(pmPolicySteelGroupList);

            // 删除table页
            if ("Y".equals(dto.getIsDelete())) {
                pmPolicySlabMethodRepository.batchDeleteByPrimaryKey(policyData.getPmPolicySlabMethodList());
                pmPolicyVipRepository.batchDeleteByPrimaryKey(policyData.getPmPolicyVipList());
                pmPolicySectionRepository.batchDeleteByPrimaryKey(policyData.getPmPolicySectionList());

            }
        } else {
            // 插入
            // 策略编码规则：轧制产线号(设计产线号后两位)+0001（四位流水），流水根据轧制产线号区分
            String policyCode = Strings.EMPTY;
            if ("10".equals(dto.getPmPolicy().getProdLineCode().substring(2,4))) {
                policyCode = codeRuleBuilder.generateCode(tenantId, Constants.CodeRule.POLICY_CODE,
                        CodeConstants.CodeRuleLevelCode.GLOBAL, Constants.CodeRule.GLOBAL_LEVEL_VALUE, null);
            }else if ("20".equals(dto.getPmPolicy().getProdLineCode().substring(2,4))) {
                policyCode = codeRuleBuilder.generateCode(tenantId, Constants.CodeRule.POLICY_CODE,
                        CodeConstants.CodeRuleLevelCode.CUSTOM, Constants.CodeRule.TYPE_20_LEVEL_CODE, null);
            }else if ("30".equals(dto.getPmPolicy().getProdLineCode().substring(2,4))) {
                policyCode = codeRuleBuilder.generateCode(tenantId, Constants.CodeRule.POLICY_CODE,
                        CodeConstants.CodeRuleLevelCode.CUSTOM, Constants.CodeRule.TYPE_30_LEVEL_CODE, null);
            } else {
                throw new CommonException("pm.policy_001",dto.getPmPolicy().getProdLineCode());
            }

            dto.getPmPolicy().setPolicyCode(dto.getPmPolicy().getProdLineCode().substring(2,4) +policyCode);
            dto.getPmPolicy().setTenantId(tenantId);
            pmPolicyRepository.insertSelective(dto.getPmPolicy());
        }

        // 组板方式
        if (CollectionUtils.isNotEmpty(dto.getPmPolicyResDTO().getPmPolicySlabMethodList())) {
            dto.getPmPolicyResDTO().getPmPolicySlabMethodList().forEach(item-> {
                item.setPolicySlabMethodId(null);
                item.setPolicyId(dto.getPmPolicy().getPolicyId());
                item.setTenantId(tenantId);
            });
            pmPolicySlabMethodRepository.batchInsertSelective(dto.getPmPolicyResDTO().getPmPolicySlabMethodList());
        }

        // 重要客户
        if (CollectionUtils.isNotEmpty(dto.getPmPolicyResDTO().getPmPolicyVipList())) {
            dto.getPmPolicyResDTO().getPmPolicyVipList().forEach(item-> {
                item.setPolicyVipCustId(null);
                item.setPolicyId(dto.getPmPolicy().getPolicyId());
                item.setTenantId(tenantId);
            });
            pmPolicyVipRepository.batchInsertSelective(dto.getPmPolicyResDTO().getPmPolicyVipList());
        }

        // 断面
        if (CollectionUtils.isNotEmpty(dto.getPmPolicyResDTO().getPmPolicySectionList())) {
            dto.getPmPolicyResDTO().getPmPolicySectionList().forEach(item-> {
                item.setPolicySectionId(null);
                item.setPolicyId(dto.getPmPolicy().getPolicyId());
                item.setTenantId(tenantId);
            });
            pmPolicySectionRepository.batchInsertSelective(dto.getPmPolicyResDTO().getPmPolicySectionList());
        }

        // 内部钢种
        if (StringUtils.isNotEmpty(dto.getPmPolicy().getStNo())) {
            List<PmPolicySteelType> insertPmPolicySteelTypeList = new ArrayList<>();
            for (String stNo : dto.getPmPolicy().getStNo().split(",")) {
                PmPolicySteelType pmPolicySteelType = new PmPolicySteelType();
                pmPolicySteelType.setPolicyId(dto.getPmPolicy().getPolicyId());
                pmPolicySteelType.setStNo(stNo);
                pmPolicySteelType.setTenantId(tenantId);
                insertPmPolicySteelTypeList.add(pmPolicySteelType);
            }
            pmPolicySteelTypeRepository.batchInsertSelective(insertPmPolicySteelTypeList);
        }

        // 钢种组
        if (StringUtils.isNotEmpty(dto.getPmPolicy().getSteelGroupCode())) {
            List<PmPolicySteelGroup> insertPmPolicySteelGroupList = new ArrayList<>();
            for (String groupCode : dto.getPmPolicy().getSteelGroupCode().split(",")) {
                PmPolicySteelGroup pmPolicySteelGroup = new PmPolicySteelGroup();
                pmPolicySteelGroup.setPolicyId(dto.getPmPolicy().getPolicyId());
                pmPolicySteelGroup.setSteelGroupCode(groupCode);
                pmPolicySteelGroup.setTenantId(tenantId);
                insertPmPolicySteelGroupList.add(pmPolicySteelGroup);
            }
            pmPolicySteelGroupRepository.batchInsertSelective(insertPmPolicySteelGroupList);
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteUi(List<PmPolicy> pmPolicyList) {
        List<Long> policyIdList = pmPolicyList.stream().map(PmPolicy::getPolicyId).collect(Collectors.toList());

        // 钢种组
        List<PmPolicySteelGroup> pmPolicySteelGroupList = pmPolicySteelGroupRepository.selectByCondition(Condition.builder(PmPolicySteelGroup.class)
                .andWhere(Sqls.custom().andIn(PmPolicySteelGroup.FIELD_POLICY_ID, policyIdList))
                .andWhere(Sqls.custom().andEqualTo(PmPolicySteelGroup.FIELD_TENANT_ID, pmPolicyList.get(0).getTenantId()))
                .build());

        // 内部钢种
        List<PmPolicySteelType> pmPolicySteelTypeList = pmPolicySteelTypeRepository.selectByCondition(Condition.builder(PmPolicySteelType.class)
                        .andWhere(Sqls.custom().andIn(PmPolicySteelType.FIELD_POLICY_ID, policyIdList))
                        .andWhere(Sqls.custom().andEqualTo(PmPolicySteelType.FIELD_TENANT_ID, pmPolicyList.get(0).getTenantId()))
                                .build());

        // 获取策略行信息
        PmPolicyResDTO pmPolicyResDTO = getPolicyData(policyIdList,pmPolicyList.get(0).getTenantId());

        // 策略
        pmPolicyRepository.batchDeleteByPrimaryKey(pmPolicyList);
        // 内部钢种
        pmPolicySteelTypeRepository.batchDeleteByPrimaryKey(pmPolicySteelTypeList);

        // 钢种组
        pmPolicySteelGroupRepository.batchDeleteByPrimaryKey(pmPolicySteelGroupList);

        // 删除table页
        pmPolicySectionRepository.batchDeleteByPrimaryKey(pmPolicyResDTO.getPmPolicySectionList());
        pmPolicySlabMethodRepository.batchDeleteByPrimaryKey(pmPolicyResDTO.getPmPolicySlabMethodList());
        pmPolicyVipRepository.batchDeleteByPrimaryKey(pmPolicyResDTO.getPmPolicyVipList());


    }

    private PmPolicyResDTO getPolicyData(List<Long> policyIdList, Long tenantId) {
        PmPolicyResDTO result = new PmPolicyResDTO();
//        }
        // 查询组板方式
        List<PmPolicySlabMethod> pmPolicySlabMethodList = pmPolicySlabMethodRepository.selectByCondition(Condition.builder(PmPolicySlabMethod.class)
                .andWhere(Sqls.custom().andIn(PmPolicySlabMethod.FIELD_POLICY_ID, policyIdList))
                .andWhere(Sqls.custom().andEqualTo(PmPolicySlabMethod.FIELD_TENANT_ID, tenantId))
                .orderBy(PmPolicySlabMethod.FIELD_PRIORITY)
                .build());
        result.setPmPolicySlabMethodList(pmPolicySlabMethodList);

        // 重要客户
        List<PmPolicyVip> pmPolicyVipList = pmPolicyVipRepository.selectByCondition(Condition.builder(PmPolicyVip.class)
                .andWhere(Sqls.custom().andIn(PmPolicyVip.FIELD_POLICY_ID, policyIdList))
                .andWhere(Sqls.custom().andEqualTo(PmPolicyVip.FIELD_TENANT_ID, tenantId))
                .orderBy(PmPolicyVip.FIELD_PRIORITY)
                .build());
        result.setPmPolicyVipList(pmPolicyVipList);

        // 指定断面
        List<PmPolicySection> pmPolicySectionList = pmPolicySectionRepository.selectByCondition(Condition.builder(PmPolicySection.class)
                .andWhere(Sqls.custom().andIn(PmPolicySection.FIELD_POLICY_ID, policyIdList))
                .andWhere(Sqls.custom().andEqualTo(PmPolicySection.FIELD_TENANT_ID, tenantId))
                .orderBy(PmPolicySection.FIELD_PRIORITY)
                .build());
        result.setPmPolicySectionList(pmPolicySectionList);

        return result;
    }

    @Override
    public PmPolicySaveDTO getLastData(Long tenantId, Long prodLineId) {
        PmPolicySaveDTO result = new PmPolicySaveDTO();
        // 获取最新一条数据
        List<PmPolicy> pmPolicieList = pmPolicyRepository.selectByCondition(Condition.builder(PmPolicy.class)
                .andWhere(Sqls.custom().andEqualTo(PmPolicy.FIELD_TENANT_ID, tenantId))
                .andWhere(Sqls.custom().andEqualTo(PmPolicy.FIELD_PROD_LINE_ID, prodLineId))
                .orderByDesc(PmPolicy.FIELD_CREATION_DATE)
                .build());
        if (CollectionUtils.isEmpty(pmPolicieList)) {
            return new PmPolicySaveDTO();
        }

        PmPolicy pmPolicy = pmPolicieList.get(0);
        // 内部钢种
        List<PmPolicySteelType> pmPolicySteelTypeList = pmPolicySteelTypeRepository.selectByCondition(Condition.builder(PmPolicySteelType.class)
                .andWhere(Sqls.custom().andEqualTo(PmPolicySteelType.FIELD_POLICY_ID, pmPolicy.getPolicyId()))
                .andWhere(Sqls.custom().andEqualTo(PmPolicySteelType.FIELD_TENANT_ID, tenantId))
                .build());

        if (CollectionUtils.isNotEmpty(pmPolicySteelTypeList)) {
            pmPolicy.setStNo(pmPolicySteelTypeList.stream().map(PmPolicySteelType::getStNo).collect(Collectors.joining(",")));
        }

        List<PmProductionLine> pmProductionLineList = pmProductionLineRepository.selectByCondition(Condition.builder(PmProductionLine.class)
                .andWhere(Sqls.custom().andEqualTo(PmProductionLine.FIELD_PROD_LINE_ID, prodLineId))
                .andWhere(Sqls.custom().andEqualTo(PmProductionLine.FIELD_TENANT_ID, tenantId))
                .build());
         if (CollectionUtils.isNotEmpty(pmProductionLineList)) {
            pmPolicy.setProdLineCode(pmProductionLineList.get(0).getProdLineCode());
        }

        // 钢种组
        List<PmPolicySteelGroup> pmPolicySteelGroupList = pmPolicySteelGroupRepository.selectByCondition(Condition.builder(PmPolicySteelGroup.class)
                .andWhere(Sqls.custom().andEqualTo(PmPolicySteelGroup.FIELD_POLICY_ID, pmPolicy.getPolicyId()))
                .andWhere(Sqls.custom().andEqualTo(PmPolicySteelGroup.FIELD_TENANT_ID, tenantId))
                .build());
        if (CollectionUtils.isNotEmpty(pmPolicySteelGroupList)) {
            pmPolicy.setSteelGroupCode(pmPolicySteelGroupList.stream().map(PmPolicySteelGroup::getSteelGroupCode).collect(Collectors.joining(",")));
        }

        result.setPmPolicy(pmPolicy);
        PmPolicyResDTO policyData = getPolicyData(Arrays.asList(pmPolicy.getPolicyId()), tenantId);
        result.setPmPolicyResDTO(policyData);
        return result;

    }
}

