AbstractCustomerService.java 11.8 KB
package cn.fw.valhalla.service.bus.cust;

import cn.fw.oop.sdk.enums.BizTypeEnum;
import cn.fw.third.push.sdk.api.ImSendMessage;
import cn.fw.valhalla.common.utils.DateUtil;
import cn.fw.valhalla.component.CustomerChangeProducer;
import cn.fw.valhalla.domain.db.customer.Customer;
import cn.fw.valhalla.domain.db.customer.CustomerBaseInfo;
import cn.fw.valhalla.domain.db.customer.CustomerLoanInfo;
import cn.fw.valhalla.domain.db.pool.StammkundePool;
import cn.fw.valhalla.domain.dto.CustomerDetailDto;
import cn.fw.valhalla.domain.dto.StammkundeDto;
import cn.fw.valhalla.domain.enums.DefeatReasonEnum;
import cn.fw.valhalla.domain.enums.StammkundeSourcesEnum;
import cn.fw.valhalla.domain.enums.StammkundeStatusEnum;
import cn.fw.valhalla.rpc.angel.InsurerRpcService;
import cn.fw.valhalla.rpc.angel.dto.InsuranceDTO;
import cn.fw.valhalla.rpc.erp.UserService;
import cn.fw.valhalla.rpc.erp.dto.UserInfoDTO;
import cn.fw.valhalla.rpc.erp.dto.UserRoleDataRangeDTO;
import cn.fw.valhalla.rpc.oop.OopService;
import cn.fw.valhalla.rpc.oop.dto.ShopDTO;
import cn.fw.valhalla.rpc.oop.dto.SpecDTO;
import cn.fw.valhalla.sdk.enums.SourceTypeEnum;
import cn.fw.valhalla.sdk.result.CustomerInfoDto;
import cn.fw.valhalla.service.data.CustomerBaseInfoService;
import cn.fw.valhalla.service.data.CustomerLoanInfoService;
import cn.fw.valhalla.service.data.CustomerService;
import cn.fw.valhalla.service.data.StammkundePoolService;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;

import javax.validation.constraints.NotNull;
import java.time.LocalDate;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

import static cn.fw.common.businessvalidator.Validator.BV;

/**
 * @author : kurisu
 * @className : CustomerService
 * @description : 档案抽象类
 * @date: 2020-08-13 09:51
 */
public abstract class AbstractCustomerService {
    @Autowired
    protected UserService userService;
    @Autowired
    protected OopService oopService;
    @Autowired
    protected CustomerService customerService;
    @Autowired
    protected CustomerBaseInfoService customerBaseInfoService;
    @Autowired
    protected InsurerRpcService insurerRpcService;
    @Autowired
    protected CustomerChangeProducer customerChangeProducer;
    @Autowired
    protected ImSendMessage imSendMessage;
    @Autowired
    protected CustomerLoanInfoService customerLoanInfoService;
    @Autowired
    protected StammkundePoolService stammkundePoolService;

    /**
     * 组合车辆档案和人员档案信息
     *
     * @param customer
     * @return
     */
    protected CustomerDetailDto renderDto(@NotNull Customer customer) {
        CustomerBaseInfo baseInfo = customerBaseInfoService.queryById(customer.getBaseId());
        return renderDto(customer, baseInfo);
    }

    /**
     * 组合车辆档案和人员档案信息
     *
     * @param customer
     * @param baseInfo
     * @return
     */
    protected CustomerDetailDto renderDto(@NotNull Customer customer, CustomerBaseInfo baseInfo) {
        CustomerDetailDto dto = new CustomerDetailDto();
        if (Objects.nonNull(baseInfo)) {
            dto.setMemberId(baseInfo.getMemberId());
            dto.setName(baseInfo.getName());
            dto.setCompanyName(baseInfo.getCompanyName());
            dto.setMobile(baseInfo.getMobile());
            dto.setRegion(baseInfo.getCityName());
            dto.setCusType(Objects.isNull(baseInfo.getCusType()) ? 0 : baseInfo.getCusType().getValue());
            dto.setIdCode(baseInfo.getIdCode());
            dto.setGender(baseInfo.getGender().getValue());
            dto.setContact(baseInfo.getContact());
            dto.setBirthday(baseInfo.getBirthday());
            dto.setAddress(baseInfo.getAddress());
            dto.setLat(baseInfo.getLat());
            dto.setLng(baseInfo.getLng());
            dto.setHobby(baseInfo.getHobby());
            dto.setCityCode(baseInfo.getCityCode());
            dto.setCityName(baseInfo.getCityName());
        }
        CustomerLoanInfo loanInfo = customerLoanInfoService.getOne(Wrappers.<CustomerLoanInfo>lambdaQuery()
                .eq(CustomerLoanInfo::getGroupId, customer.getGroupId())
                .eq(CustomerLoanInfo::getFrameNo, customer.getFrameNo())
                .last(" limit 1")
        );
        if (Objects.nonNull(loanInfo)) {
            dto.setLoanCustomer(LocalDate.now().isBefore(DateUtil.date2LocalDate(loanInfo.getExpires())));
            dto.setExpires(loanInfo.getExpires());
            dto.setPeriods(loanInfo.getPeriods());
        }
        dto.setId(customer.getId());
        dto.setPlateNo(customer.getPlateNo());
        dto.setBrandId(customer.getBrandId());
        dto.setBrandName(customer.getBrandName());
        dto.setSeriesId(customer.getSeriesId());
        dto.setSeriesName(customer.getSeriesName());
        dto.setSpecId(customer.getSpecId());
        dto.setFrameNo(customer.getFrameNo());
        dto.setSpecName(customer.getSpecName());
        dto.setCarImage(customer.getCarImage());
        dto.setTags(customer.getTags());
        dto.setCusLevel(customer.getCusLevel());
        dto.setCurrentMileage(customer.getCurrentMileage());
        dto.setInsuranceExpires(customer.getInsuranceExpires());
        dto.setArrivalTime(customer.getArrivalTime());
        dto.setAdviserId(customer.getAdviserId());
        dto.setArrivalCount(customer.getArrivalCount());
        UserInfoDTO user = userService.user(customer.getAdviserId());
        if (Objects.nonNull(user)) {
            dto.setAdviserName(user.getUserName());
        }
        dto.setEngineNo(customer.getEngineNo());
        dto.setRegDate(customer.getRegDate());
        dto.setSpecCode(customer.getSpecCode());
        dto.setBuyDate(customer.getBuyDate());
        dto.setUseType(Objects.isNull(customer.getUseType()) ? null : customer.getUseType().getValue());
        dto.setCreateTime(customer.getCreateTime());
        dto.setGroupId(customer.getGroupId());
        dto.setVehicleLicense(customer.getVehicleLicense());
        dto.setShopId(customer.getShopId());
        dto.setBuyPrice(customer.getBuyPrice());
        dto.setSource(Objects.isNull(customer.getSource()) ? null : customer.getSource().getValue());
        dto.setYn(customer.getYn());
        return dto;
    }

    /**
     * 根据userid和角色码查询商家id
     *
     * @param userId
     * @param roleCOde
     * @return
     */
    protected ShopDTO queryDealId(Long userId, String roleCOde) {
        List<UserRoleDataRangeDTO> dataRange = userService.getUserRoleDataRange(userId, roleCOde);
        if (CollectionUtils.isEmpty(dataRange)) {
            return null;
        }
        Long shopId = dataRange.get(0).getRangeValue();
        ShopDTO shop = oopService.shop(shopId);
        BV.isTrue(shop != null && BizTypeEnum.AFTER_SALE.getValue().equals(shop.getBizType()), "角色门店信息不正确");
        return shop;
    }

    /**
     * 查询档案正在生效的保险信息
     *
     * @param customerId
     * @return
     */
    protected Optional<InsuranceDTO> queryInsuInfo(Long customerId) {
        if (Objects.isNull(customerId)) {
            return Optional.empty();
        }
        InsuranceDTO insuranceDTO = insurerRpcService.queryInsByCustId(customerId);
        return Optional.ofNullable(insuranceDTO);
    }

    /**
     * 档案发送改变后
     *
     * @param customer 变更后的档案
     */
    protected void customerChangeHandler(Customer customer) {
        Long memberId = Optional.ofNullable(customerBaseInfoService.queryById(customer.getBaseId())).map(CustomerBaseInfo::getMemberId).orElse(null);
        final CustomerInfoDto customerInfoDto = new CustomerInfoDto();
        BeanUtils.copyProperties(customer, customerInfoDto);
        customerInfoDto.setId(customer.getId());
        customerInfoDto.setConsultantId(customer.getAdviserId());
        customerInfoDto.setMemberId(memberId);
        customerChangeProducer.send(customerInfoDto);
    }

    /**
     * 创建新档案
     *
     * @param customer
     * @param baseInfo
     * @return
     */
    protected Long create(Customer customer, CustomerBaseInfo baseInfo) {
        final SpecDTO spec = oopService.spec(customer.getSpecId());
        customer.setCarImage(Optional.ofNullable(spec).orElse(new SpecDTO()).getThumbnail());
        final String plateNo = customer.getPlateNo();
        final int count = customerService.count(Wrappers.<Customer>lambdaQuery()
                .eq(Customer::getYn, Boolean.TRUE)
                .eq(Customer::getPlateNo, plateNo)
                .eq(Customer::getFrameNo, customer.getFrameNo())
        );
        BV.isTrue(count == 0, "车辆档案已存在已存在");

        CustomerBaseInfo info = customerBaseInfoService.queryByIdCode(baseInfo.getIdCode(), baseInfo.getGroupId());
        if (Objects.isNull(info)) {
            info = customerBaseInfoService.queryByMobile(baseInfo.getMobile(), baseInfo.getGroupId());
        }
        if (Objects.nonNull(info)) {
            baseInfo.setId(info.getId());
            baseInfo.setCreateTime(null);
        }
        customerBaseInfoService.saveOrUpdate(baseInfo);

        customer.setBaseId(baseInfo.getId());
        customer.setCreateTime(new Date());
        customer.setYn(Boolean.TRUE);
        customer.setUpdateTime(new Date());
        customer.setSource(SourceTypeEnum.TO_SHOP);
        if (customer.getBuyDate() == null) {
            customer.setBuyDate(customer.getRegDate());
        }
        boolean saved = customerService.save(customer);
        if (saved) {
            StammkundePool simpleInfo = createSimpleInfo(customer, customer.getAdviserId());
            if (plateNo.contains("临")) {
                simpleInfo.setSources(StammkundeSourcesEnum.ORDER);
            } else {
                simpleInfo.setSources(StammkundeSourcesEnum.RECEPTION);
            }
            simpleInfo.setPoolStatus(StammkundeStatusEnum.KUNDE);
            simpleInfo.setAktiv(Boolean.TRUE);
            simpleInfo.setActivationTime(new Date());
            stammkundePoolService.save(simpleInfo);
            final CustomerInfoDto customerInfoDto = new CustomerInfoDto();
            BeanUtils.copyProperties(customer, customerInfoDto);
            customerInfoDto.setConsultantId(customer.getAdviserId());
            customerInfoDto.setMemberId(baseInfo.getMemberId());
            this.customerChangeProducer.send(customerInfoDto);
        }
        return customer.getId();
    }

    protected StammkundePool rejectAndNew(StammkundeDto dto) {
        stammkundePoolService.reject(dto);
        StammkundePool stammkundePool = new StammkundePool();
        stammkundePool.setCustomerId(dto.getCustomerId());
        stammkundePool.setShopId(dto.getShopId());
        stammkundePool.setGroupId(dto.getGroupId());
        stammkundePool.setAdviserId(dto.getNewUserId());
        stammkundePool.setAdviserName(dto.getNewUserName());
        return stammkundePool;
    }

    protected StammkundePool createSimpleInfo(Customer customer, Long userId) {
        StammkundePool stammkundePool = createSimpleInfo(customer);
        if (Objects.nonNull(userId)) {
            stammkundePool.setAdviserId(userId);
            UserInfoDTO user = userService.user(userId);
            if (Objects.nonNull(user)) {
                stammkundePool.setAdviserName(user.getUserName());
            }
        }
        return stammkundePool;
    }

    protected StammkundePool createSimpleInfo(Customer customer) {
        StammkundePool stammkundePool = new StammkundePool();
        stammkundePool.setCustomerId(customer.getId());
        stammkundePool.setShopId(customer.getShopId());
        stammkundePool.setGroupId(customer.getGroupId());
        stammkundePool.setAktiv(Boolean.FALSE);
        stammkundePool.setCreateTime(new Date());
        return stammkundePool;
    }
}