package com.ruoyi.ibs.grid.service.impl;

import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.ibs.grid.domain.vo.RegionCustVO;
import com.ruoyi.ibs.grid.mapper.AnchorCustMapper;
import com.ruoyi.ibs.grid.mapper.CustInfoMapper;
import com.ruoyi.ibs.grid.service.RegionCustService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author 吴凯程
 * @Date 2024/11/14
 **/
@Service
public class RegionCustServiceImpl implements RegionCustService {

    @Resource
    private CustInfoMapper custInfoMapper;

    @Resource
    private AnchorCustMapper anchorCustMapper;

    /**
     * @param code
     * @return
     */
    @Override
    public List<RegionCustVO> getRegionCustList(String code, String custType) {

        List<RegionCustVO> regionCustVOS = null;
        if (custType.equals("0")) {
            regionCustVOS = custInfoMapper.selectRegionRetailByCode(code);
        } else if (custType.equals("1")) {
            regionCustVOS = custInfoMapper.selectRegionMerchantByCode(code);
        } else if (custType.equals("2")) {
            regionCustVOS = custInfoMapper.selectRegionBusinessByCode(code);
        }
        List<String> custIds = regionCustVOS.stream().map(RegionCustVO::getCustId).collect(Collectors.toList());
        if (custIds.isEmpty()) {
            return null;
        }
        List<RegionCustVO> regionCustAnchors = anchorCustMapper.selectAnchor(custIds);
        regionCustVOS.forEach(regionCustVO -> {
            regionCustAnchors.forEach(regionCustAnchor -> {
                if (regionCustAnchor.getCustId().equals(regionCustVO.getCustId())) {
                    regionCustAnchor.setCustName(regionCustVO.getCustName());
                    regionCustAnchor.setCustType(regionCustVO.getCustType());
                    BeanUtils.copyProperties(regionCustAnchor, regionCustVO);
                }
            });
        });
        return regionCustVOS;
    }
}
