package com.hzs.bonus.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzs.bonus.base.service.ICuMemberAgentService;
import com.hzs.common.core.constant.MemberFieldConstants;
import com.hzs.common.core.enums.EAgentType;
import com.hzs.common.core.utils.ComputeUtil;
import com.hzs.common.domain.member.base.CuMemberAgent;
import com.hzs.bonus.base.mapper.CuMemberAgentMapper;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 会员信息-代理商信息 服务实现类
 * </p>
 *
 * @author hzs
 * @since 2022-08-31
 */
@Service
public class CuMemberAgentServiceImpl extends ServiceImpl<CuMemberAgentMapper, CuMemberAgent> implements ICuMemberAgentService {

    @Override
    public Map<String,List<CuMemberAgent>> queryCuMemberAgent() {
        QueryWrapper<CuMemberAgent> queryWrapper = new QueryWrapper<>();
        queryWrapper.select(" pk_member,agent_province,agent_city,agent_county,agent_grade,pk_country ")
                .eq(MemberFieldConstants.AGENT_TYPE, EAgentType.AGENT.getValue());
        List<CuMemberAgent> cuMemberAgentList = baseMapper.selectList(queryWrapper);
        Map<String,List<CuMemberAgent>> cuMemberAgentMap = new HashMap<>(ComputeUtil.mapInitCapacity(cuMemberAgentList.size()));
        cuMemberAgentList.forEach(cuMemberAgent -> {
            String key = cuMemberAgent.getPkCountry().toString() + cuMemberAgent.getAgentProvince().toString() + cuMemberAgent.getAgentCity().toString();
            if(cuMemberAgentMap.containsKey(key)){
                cuMemberAgentMap.get(key).add(cuMemberAgent);
            }else{
                List<CuMemberAgent> cuMemberAgents = new ArrayList<>();
                cuMemberAgents.add(cuMemberAgent);
                cuMemberAgentMap.put(key, cuMemberAgents);
            }
        });
        return cuMemberAgentMap;
    }
}
