package com.xisc.pm.infra.repository.impl;

import com.google.common.collect.Lists;
import com.xisc.pm.api.dto.PmPolicyGroupReqDTO;
import com.xisc.pm.app.service.autoboard.dto.PmOrderGroupDTO;
import com.xisc.pm.app.service.autoboard.dto.PmOrderGroupDemension;
import com.xisc.pm.domain.entity.PmPolicyGroup;
import com.xisc.pm.domain.repository.PmPolicyGroupRepository;
import com.xisc.pm.infra.mapper.PmPolicyGroupMapper;

import org.apache.commons.lang.StringUtils;
import org.hzero.mybatis.base.impl.BaseRepositoryImpl;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 策略组表(PmPolicyGroup)资源库
 *
 * @author 纪文桃 wentao.ji01@hand-china.com
 * @since 2024-11-08 09:38:30
 */
@Component
public class PmPolicyGroupRepositoryImpl extends BaseRepositoryImpl<PmPolicyGroup> implements PmPolicyGroupRepository {
    @Resource
    private PmPolicyGroupMapper pmPolicyGroupMapper;

    @Override
    public List<PmPolicyGroup> selectList(PmPolicyGroup pmPolicyGroup) {
        return pmPolicyGroupMapper.selectList(pmPolicyGroup);
    }

    @Override
    public PmPolicyGroup selectByPrimary(Long policyGroupId) {
        PmPolicyGroup pmPolicyGroup = new PmPolicyGroup();
        pmPolicyGroup.setPolicyGroupId(policyGroupId);
        List<PmPolicyGroup> pmPolicyGroups = pmPolicyGroupMapper.selectList(pmPolicyGroup);
        if (pmPolicyGroups.size() == 0) {
            return null;
        }
        return pmPolicyGroups.get(0);
    }

    @Override
    public List<PmPolicyGroup> queryList(PmPolicyGroupReqDTO dto, Long tenantId, List<String> policyIdList) {
        return pmPolicyGroupMapper.queryList(dto, tenantId, policyIdList);
    }

    @Override
    public List<PmOrderGroupDTO> selectContGroupConditionByGroupId(Long policyGroupId, Long tenantId) {
        List<PmOrderGroupDemension> pmOrderGroupDemensions = pmPolicyGroupMapper.selectContGroupConditionByGroupId(policyGroupId, tenantId);
        List<PmOrderGroupDTO> pmOrderGroupDTOs = new ArrayList<PmOrderGroupDTO>();
        for (PmOrderGroupDemension pmOrderGroupDemension : pmOrderGroupDemensions) {
            PmOrderGroupDTO pmOrderGroupDTO = new PmOrderGroupDTO();
            if(StringUtils.isNotBlank(pmOrderGroupDemension.getGroupDimenSion())) {
                pmOrderGroupDTO.setGroupDimenSion(Lists.newArrayList(pmOrderGroupDemension.getGroupDimenSion().split(",")));
            } else {
                pmOrderGroupDTO.setGroupDimenSion(new ArrayList<>());
            }
            if(StringUtils.isNotBlank(pmOrderGroupDemension.getGroupSeq())) {
                pmOrderGroupDTO.setGroupSeq(Lists.newArrayList(pmOrderGroupDemension.getGroupSeq().split(",")));
            } else {
                pmOrderGroupDTO.setGroupSeq(new ArrayList<>());
            }
            pmOrderGroupDTO.setGroupId(policyGroupId);
            pmOrderGroupDTO.setPriority(pmOrderGroupDemension.getPriority());
            
            pmOrderGroupDTOs.add(pmOrderGroupDTO);
        }
        return pmOrderGroupDTOs;
    }
}
