package com.qmth.sop.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qmth.sop.business.bean.dto.AreaDto;
import com.qmth.sop.business.bean.result.ServiceRegionDetailResult;
import com.qmth.sop.business.entity.TBCrm;
import com.qmth.sop.business.entity.TBServiceRegion;
import com.qmth.sop.business.entity.TBServiceRegionDetail;
import com.qmth.sop.business.mapper.TBServiceRegionDetailMapper;
import com.qmth.sop.business.service.TBCrmService;
import com.qmth.sop.business.service.TBServiceRegionDetailService;
import com.qmth.sop.business.service.TBServiceRegionService;
import com.qmth.sop.common.contant.SystemConstant;
import com.qmth.sop.common.enums.ExceptionResultEnum;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * <p>
 * 服务区域规划明细表 服务实现类
 * </p>
 *
 * @author wangliang
 * @since 2023-08-01
 */
@Service
public class TBServiceRegionDetailServiceImpl extends ServiceImpl<TBServiceRegionDetailMapper, TBServiceRegionDetail> implements TBServiceRegionDetailService {
    @Resource
    private TBCrmService tbCrmService;
    @Resource
    private TBServiceRegionService tbServiceRegionService;

    @Transactional
    @Override
    public void bindServiceRegionAreas(Long serviceRegionId, List<AreaDto> areaDtoList) {
        TBServiceRegion db = tbServiceRegionService.getById(serviceRegionId);
        Long serviceUnitId = db.getServiceId();
        Long leaderId = db.getLeadId();

        // 删除之前绑定的区域
        this.remove(new QueryWrapper<TBServiceRegionDetail>().lambda().eq(TBServiceRegionDetail::getServiceRegionId, serviceRegionId));

        List<TBCrm> willUpdateCrmList = new ArrayList<>();
        // 添加新绑定区域
        List<TBServiceRegionDetail> tbServiceRegionDetailList = areaDtoList.stream().flatMap(e -> {
            // 批量更新相关的派单的大区经理
            String province = e.getProvince();
            String city = e.getCity();
            List<TBCrm> tbCrmList = tbCrmService.findByTBCrmListByServiceUnitIdAndAddress(serviceUnitId, province, city);
            willUpdateCrmList.addAll(tbCrmList);

            TBServiceRegionDetail detail = new TBServiceRegionDetail();
            detail.setId(SystemConstant.getDbUuid());
            detail.setServiceRegionId(serviceRegionId);
            detail.setCountry(e.getCountry());
            detail.setProvince(province);
            detail.setCity(city);
            detail.setArea(e.getArea());
            detail.setAddress(e.getAddress());
            return Stream.of(detail);
        }).collect(Collectors.toList());
        this.saveBatch(tbServiceRegionDetailList);

        if (CollectionUtils.isNotEmpty(willUpdateCrmList)) {
            for (TBCrm tbCrm : willUpdateCrmList) {
                ServiceRegionDetailResult result = this.findRegionDetail(tbCrm.getServiceId(), tbCrm.getCrmNo());
                tbCrm.setLeadId(result.getLeadId());
                tbCrm.setRegionId(result.getServiceRegionId());
                tbCrm.setRegionDetailId(result.getServiceRegionDetailId());
            }
            tbCrmService.updateBatchById(willUpdateCrmList);
        }
    }

    @Override
    public ServiceRegionDetailResult findRegionDetail(Long serviceUnitId, String crmNo) {
        ServiceRegionDetailResult result = null;
        List<ServiceRegionDetailResult> resultList = this.baseMapper.findRegionDetailByServiceUnitIdAndCrmNo(serviceUnitId, crmNo);
        if (CollectionUtils.isNotEmpty(resultList)) {
            if (resultList.size() > 1) {
                throw ExceptionResultEnum.ERROR.exception(String.format("派单号为[%s]的派单可匹配多个大区详细信息", crmNo));
            }
            result = resultList.get(0);
        }
        return result;
    }
}
