CustomerApiServiceImpl.java 12.9 KB
package cn.fw.valhalla.controller.api;

import cn.fw.data.base.domain.common.Message;
import cn.fw.valhalla.domain.dto.CustomerDetailDto;
import cn.fw.valhalla.sdk.api.CustomerApiService;
import cn.fw.valhalla.sdk.param.ChangeAdviserReq;
import cn.fw.valhalla.sdk.param.ChangePlateNoReq;
import cn.fw.valhalla.sdk.param.CustomerParams;
import cn.fw.valhalla.sdk.result.CustomerContactDto;
import cn.fw.valhalla.sdk.result.CustomerInfoDto;
import cn.fw.valhalla.sdk.result.ReceptionResultDto;
import cn.fw.valhalla.service.bus.cust.ContactBizService;
import cn.fw.valhalla.service.bus.cust.CustomerBizService;
import cn.fw.valhalla.service.bus.cust.CustomerChangeBizService;
import cn.fw.valhalla.service.bus.cust.PickUpCustomerService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import static cn.fw.common.businessvalidator.Validator.BV;
import static cn.fw.common.web.util.ExceptionHandler.handleException;
import static cn.fw.common.web.util.ResultBuilder.failureWithMessage;
import static cn.fw.common.web.util.ResultBuilder.success;

/**
 * 保有客相关
 *
 * @author kurisu
 */
@Slf4j
@RestController
@RequestMapping("/api/valhalla/customer")
public class CustomerApiServiceImpl implements CustomerApiService {
    /**
     * 保有客数据服务
     */
    private final CustomerBizService customerBiz;
    private final CustomerChangeBizService changeBizService;
    private final ContactBizService contactBizService;
    private final PickUpCustomerService pickUpCustomerService;


    @Autowired
    CustomerApiServiceImpl(final CustomerBizService customerBiz,
                           final CustomerChangeBizService changeBizService,
                           final ContactBizService contactBizService,
                           final PickUpCustomerService pickUpCustomerService) {
        this.customerBiz = customerBiz;
        this.changeBizService = changeBizService;
        this.contactBizService = contactBizService;
        this.pickUpCustomerService = pickUpCustomerService;
    }

    @PostMapping("/save")
    @Override
    public Message<Boolean> addCustomer(@Valid @RequestBody CustomerParams customerParams) {
        final String msg = "新增保有客档案[addCustomer]";
        log.info("{}: param[{}]", msg, customerParams);
        try {
            Boolean added = customerBiz.save4api(customerParams);
            return success(added, data -> log.info("{} 成功, result={}", msg, data));
        } catch (Exception ex) {
            handleException(ex, e -> log.error("{}失败:param[{}]", msg, customerParams, e));
            return failureWithMessage("新增保有客档案失败");
        }
    }

    @Override
    @GetMapping("/query/by/id")
    public Message<CustomerInfoDto> queryById(@RequestParam("customerId") Long customerId) {
        final String msg = "查询保有客档案[queryById]";
        log.info("{}: param[{}]", msg, customerId);
        try {
            CustomerDetailDto detailDto = customerBiz.queryById(customerId);
            if (Objects.isNull(detailDto)) {
                log.info("{} 成功: 查无此档案", msg);
                return success();
            }
            CustomerInfoDto dto = new CustomerInfoDto();
            BeanUtils.copyProperties(detailDto, dto);
            dto.setConsultantId(detailDto.getAdviserId());
            dto.setConsultantName(detailDto.getAdviserName());
            return success(dto, data -> log.info("{} 成功:[{}]", msg, data));
        } catch (Exception ex) {
            handleException(ex, e -> log.error("{}失败:param[{}]", msg, customerId, e));
            return failureWithMessage("查询保有客档案");
        }
    }

    @Override
    @GetMapping("/query/by/mobile")
    public Message<List<CustomerInfoDto>> queryByMobile(@RequestParam("mobile") String mobile, Long groupId) {
        final String msg = "查询保有客档案列表[queryByMobile]";
        log.info("{}: param[{}]", msg, mobile);
        try {
            BV.isTrue(StringUtils.isNotBlank(mobile), "手机号不正确");
            List<CustomerDetailDto> list = customerBiz.queryByMobile(mobile, groupId);
            List<CustomerInfoDto> dtoList = new ArrayList<>();
            for (CustomerDetailDto customer : list) {
                CustomerInfoDto dto = new CustomerInfoDto();
                BeanUtils.copyProperties(customer, dto);
                dto.setConsultantId(customer.getAdviserId());
                dto.setConsultantName(customer.getAdviserName());
                dtoList.add(dto);
            }
            return success(dtoList, data -> log.info("{} 成功: {}", msg, data));
        } catch (Exception ex) {
            handleException(ex, e -> log.error("{}失败:param[{}]", msg, mobile, e));
            return failureWithMessage("查询保有客档案");
        }
    }

    @Override
    @GetMapping("/query/by/plateNo")
    public Message<CustomerInfoDto> queryByPlateNo(@RequestParam("plateNo") String plateNo, @RequestParam("groupId") Long groupId) {
        final String msg = "查询保有客档案列表[queryByPlateNo]";
        log.info("{}: param[{}]", msg, plateNo);
        try {
            BV.isTrue(StringUtils.isNotBlank(plateNo), "车牌号不正确");
            CustomerDetailDto detailDto = customerBiz.queryByPlateNo(plateNo, groupId);
            if (Objects.isNull(detailDto)) {
                log.info("{} 成功: 查无此档案", msg);
                return success();
            }
            CustomerInfoDto dto = new CustomerInfoDto();
            BeanUtils.copyProperties(detailDto, dto);
            dto.setConsultantId(detailDto.getAdviserId());
            dto.setConsultantName(detailDto.getAdviserName());
            return success(dto, data -> log.info("{} 成功: {}", msg, data));
        } catch (Exception ex) {
            handleException(ex, e -> log.error("{}失败:param[{}]", msg, plateNo, e));
            return failureWithMessage("查询保有客档案");
        }
    }

    @Override
    @GetMapping("/query/by/frameNo")
    public Message<CustomerInfoDto> queryByFrameNo(@RequestParam("frameNo") String frameNo, @RequestParam("groupId") Long groupId) {
        final String msg = "查询保有客档案列表[queryByFrameNo]";
        log.info("{}: param[{}]", msg, frameNo);
        try {
            BV.isTrue(StringUtils.isNotBlank(frameNo), "车架号不正确");
            CustomerDetailDto detailDto = customerBiz.queryByFrameNo(frameNo, groupId);
            if (Objects.isNull(detailDto)) {
                log.info("{} 成功: 查无此档案", msg);
                return success();
            }
            CustomerInfoDto dto = new CustomerInfoDto();
            BeanUtils.copyProperties(detailDto, dto);
            dto.setConsultantId(detailDto.getAdviserId());
            dto.setConsultantName(detailDto.getAdviserName());
            return success(dto, data -> log.info("{} 成功: {}", msg, data));
        } catch (Exception ex) {
            handleException(ex, e -> log.error("{}失败:param[{}]", msg, frameNo, e));
            return failureWithMessage("查询保有客档案");
        }
    }

    @GetMapping("/query/batch")
    @Override
    public Message<List<CustomerInfoDto>> queryBatch(@RequestParam("customerIds") final Long[] customerIds) {
        final String msg = "批量查询客户档案[queryBatch]";
        log.info("{}: param:{}", msg, customerIds);
        try {
            return success(customerBiz.queryBatch(customerIds), data -> log.info("{}:data[{}]", msg, data));
        } catch (Exception ex) {
            handleException(ex, e -> log.error("{}失败: param:{}", msg, customerIds, e));
            return failureWithMessage("批量查询客户档案失败");
        }

    }

    @PostMapping("/change/adviser")
    @Override
    public Message<Boolean> changeAdviser(@Valid @RequestBody final ChangeAdviserReq changeAdviserReq) {
        final String msg = "修改保有客专属顾问[changeAdviser]";
        log.info("{}: param:{}", msg, changeAdviserReq);
        try {
            return success(changeBizService.changeAdviser(changeAdviserReq),
                    data -> log.info("{}:data[{}]", msg, data));
        } catch (Exception ex) {
            handleException(ex, e -> log.error("{}失败: param:{}", msg, changeAdviserReq, e));
            return failureWithMessage("修改保有客专属顾问失败");
        }
    }

    @GetMapping("/dealers")
    @Override
    public Message<List<Long>> dealers(@RequestParam("groupId") final Long groupId, @RequestParam("memberId") final Long memberId) {
        final String msg = "查询会员档案所在商家[dealers]";
        log.info("{}: groupId:{},memberId:{}", msg, groupId, memberId);
        try {
            return success(customerBiz.getCustomerDealerId(groupId, memberId),
                    data -> log.info("{}:data[{}]", msg, data));
        } catch (Exception ex) {
            handleException(ex, e -> log.error("{}失败: groupId:{},memberId{}", msg, groupId, memberId, e));
            return failureWithMessage("查询会员档案所在商家失败");
        }
    }

    @Override
    @GetMapping("/receptioner")
    public Message<CustomerContactDto> queryReceptioner(@RequestParam("customerId") final Long customerId, @RequestParam("plateNo") final String plateNo, @RequestParam("shopId") final Long shopId) {
        final String msg = "查询接车送修人[receptioner]";
        log.info("{}: param:[plateNo:{}  groupId:{}]", msg, plateNo, shopId);
        try {
            return success(contactBizService.queryReceptioner(customerId, plateNo, shopId),
                    data -> log.info("{}:data[{}]", msg, data));
        } catch (Exception ex) {
            handleException(ex, e -> log.error("{}失败: param:[plateNo:{}]", msg, plateNo, e));
            return failureWithMessage("查询接车送修人失败");
        }
    }

    @Override
    @GetMapping("/contactInfo")
    public Message<CustomerContactDto> queryContactInfo(@RequestParam("customerId") Long customerId, @RequestParam("memberId") Long memberId) {
        final String msg = "查询联系人信息[contactInfo]";
        log.info("{}: param:[customerId:{}  memberId:{}]", msg, customerId, memberId);
        try {
            return success(contactBizService.queryContactInfo(customerId, memberId),
                    data -> log.info("{}:data[{}]", msg, data));
        } catch (Exception ex) {
            handleException(ex, e -> log.error("{}失败: param:[customerId:{}  memberId:{}]", msg, customerId, memberId, e));
            return failureWithMessage("查询联系人信息失败");
        }
    }

    @Override
    @GetMapping("/modifySpecCode")
    public Message<Boolean> modifySpecCode(@RequestParam("customerId") Long customerId, @RequestParam("specCode") String specCode) {
        final String msg = "修改档案车型代码[modifySpecCode]";
        log.info("{}: param:[customerId:{}  specCode:{}]", msg, customerId, specCode);
        try {
            return success(customerBiz.modifySpecCode(customerId, specCode),
                    data -> log.info("{}:data[{}]", msg, data));
        } catch (Exception ex) {
            handleException(ex, e -> log.error("{}失败: param:[customerId:{}  specCode:{}]", msg, customerId, specCode, e));
            return failureWithMessage("查询联系人信息失败");
        }
    }

    @Override
    @GetMapping("/queryReceivable")
    public Message<ReceptionResultDto> queryReceivable(@RequestParam("userId") Long userId, @RequestParam("plateNo") String plateNo) {
        final String msg = "查询能否接车[queryReceivable]";
        log.info("{}: param:[userId:{}  plateNo:{}]", msg, userId, plateNo);
        try {
            return success(customerBiz.queryReceivable(userId, plateNo),
                    data -> log.info("{}:data[{}]", msg, data));
        } catch (Exception ex) {
            handleException(ex, e -> log.error("{}失败: param:[userId:{}  plateNo:{}]", msg, userId, plateNo, e));
            return failureWithMessage("查询联系人信息失败");
        }
    }

    @Override
    @PostMapping("/updatePlateNo")
    public Message<Boolean> updatePlateNo(@Valid @RequestBody ChangePlateNoReq changePlateNoReq) {
        final String msg = "通过vin修改车牌号[updatePlateNo]";
        log.info("{}: param:[{}]", msg, changePlateNoReq);
        try {
            return success(pickUpCustomerService.fixPlateNo(changePlateNoReq.getFrameNo(), changePlateNoReq.getPlateNo(), changePlateNoReq.getGroupId()),
                    data -> log.info("{}:data[{}]", msg, data));
        } catch (Exception ex) {
            handleException(ex, e -> log.error("{}失败: param:[{}]", msg, changePlateNoReq, e));
            return failureWithMessage("通过vin修改车牌号失败");
        }
    }
}