PickUpCustomerService.java 4.9 KB
package cn.fw.valhalla.service.bus.cust;

import cn.fw.common.web.auth.LoginAuthBean;
import cn.fw.valhalla.common.constant.RoleCode;
import cn.fw.valhalla.domain.db.customer.Customer;
import cn.fw.valhalla.domain.dto.CustomerDetailDto;
import cn.fw.valhalla.domain.vo.customer.PickUpCustomerDetailVO;
import cn.fw.valhalla.rpc.angel.dto.InsuranceDTO;
import cn.fw.valhalla.rpc.cas.CasRpcService;
import cn.fw.valhalla.rpc.cas.dto.SubstituteDTO;
import cn.fw.valhalla.rpc.erp.dto.UserInfoDTO;
import cn.fw.valhalla.rpc.oop.dto.ShopDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Objects;
import java.util.Optional;

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

/**
 * @author : kurisu
 * @className : PickUpCustomerService
 * @description : 接车档案管理
 * @date: 2020-08-13 09:58
 */
@Service
@Slf4j
public class PickUpCustomerService extends AbstractCustomerService {
    /**
     * 售后RPC
     */
    private final CasRpcService casRpcService;

    @Autowired
    public PickUpCustomerService(final CasRpcService casRpcService) {
        this.casRpcService = casRpcService;
    }

    /**
     * 接车时查询档案信息
     *
     * @param currentUser
     * @param plateNo
     * @return
     */
    public PickUpCustomerDetailVO getDetailByPlateNo(LoginAuthBean currentUser, String plateNo) {
        Customer customer = customerService.queryByPlateNo(plateNo, currentUser.getGroupId());
        if (customer == null) {
            return null;
        }
        return with(customer, currentUser.getUserId());
    }

    /**
     * 修改车牌号
     *
     * @param frameNo
     * @param plateNo
     * @param currentUser
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public Long fixPlateNo(String frameNo, String plateNo, LoginAuthBean currentUser) {
        Customer customer = customerService.queryByFrameNo(frameNo, currentUser.getGroupId());
        BV.notNull(customer, () -> "车架号有误");
        customer.setPlateNo(plateNo);
        boolean bool = customerService.updateById(customer);
        BV.isTrue(bool, () -> "修改车牌号失败");
        return customer.getId();
    }

    /**
     * 修改车牌号
     *
     * @param frameNo
     * @param plateNo
     * @param groupId
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public Boolean fixPlateNo(String frameNo, String plateNo, Long groupId) {
        Customer customer = customerService.queryByFrameNo(frameNo, groupId);
        BV.notNull(customer, () -> "车架号有误");
        customer.setPlateNo(plateNo);
        return customerService.updateById(customer);
    }

    /**
     * 根据vin查询档案
     *
     * @param currentUser
     * @param frameNo
     * @return
     */
    public PickUpCustomerDetailVO getDetailByFrameNo(LoginAuthBean currentUser, String frameNo) {
        Customer customer = customerService.queryByFrameNo(frameNo, currentUser.getGroupId());
        if (customer == null) {
            return null;
        }
        return with(customer, currentUser.getUserId());
    }

    private PickUpCustomerDetailVO with(Customer customer, Long userId) {
        PickUpCustomerDetailVO detailVO = new PickUpCustomerDetailVO();
        CustomerDetailDto detailDto = renderDto(customer);
        BeanUtils.copyProperties(detailDto, detailVO);
        detailVO.setOrgCode(detailDto.getIdCode());
        detailVO.setIdNum(detailDto.getIdCode());
        Optional<InsuranceDTO> dtoOptional = queryInsuInfo(customer.getId());
        dtoOptional.ifPresent(t -> {
            detailVO.setInsurCompanyId(t.getInsurerId());
            detailVO.setInsurCompanyName(t.getInsurerName());
            detailVO.setInsurExpireDate(t.getExpiryDate());
        });

        boolean canReception = true;
        ShopDTO shop = Optional.ofNullable(queryDealId(userId, RoleCode.FWGW)).orElse(new ShopDTO());
        if (Objects.nonNull(customer.getShopId()) && customer.getShopId().equals(shop.getId()) && Objects.nonNull(customer.getAdviserId())) {
            UserInfoDTO user = userService.user(customer.getAdviserId());
            String userName = Optional.ofNullable(user).map(UserInfoDTO::getUserName).orElse("");
            detailVO.setAdviserName(userName);
            SubstituteDTO substitute = casRpcService.getSubstitute(customer.getShopId(), customer.getAdviserId());
            if (Objects.nonNull(substitute)) {
                detailVO.setSubstituteId(substitute.getSubstituteId());
                detailVO.setSubstitute(substitute.getSubstitute());
                canReception = userId.equals(customer.getAdviserId()) || userId.equals(substitute.getSubstituteId());
            }
        }
        detailVO.setCanReception(canReception);
        return detailVO;
    }
}