CustomerController.java 13.6 KB
package cn.fw.valhalla.controller.app;

import cn.fw.common.page.AppPage;
import cn.fw.common.web.auth.LoginAuthBean;
import cn.fw.common.web.auth.annotation.CurrentUser;
import cn.fw.data.base.domain.common.Message;
import cn.fw.security.auth.client.annotation.Authorization;
import cn.fw.security.auth.client.annotation.IgnoreAuth;
import cn.fw.security.auth.client.enums.AuthType;
import cn.fw.valhalla.domain.dto.CustomerChangeDto;
import cn.fw.valhalla.domain.query.CustomerQueryVO;
import cn.fw.valhalla.domain.vo.customer.*;
import cn.fw.valhalla.service.bus.CommonService;
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 com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.List;

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;
import static cn.fw.valhalla.common.constant.MessageStr.QUERY_FAILURE;
import static cn.fw.valhalla.common.constant.MessageStr.SAVE_FAILURE;

/**
 * @author : kurisu
 * @className : CustomerController
 * @description : 档案控制器
 * @date: 2020-08-12 15:30
 */
@Slf4j
@Validated
@RestController
@Authorization(AuthType.APP)
@RequestMapping("/app/customer")
public class CustomerController {
    private final CustomerBizService customerBizService;
    private final PickUpCustomerService pickUpCustomerService;
    private final CustomerChangeBizService customerChangeBiz;
    private final ContactBizService contactBizService;
    private final CommonService commonService;


    @Autowired
    public CustomerController(final CustomerBizService customerBizService,
                              final PickUpCustomerService pickUpCustomerService,
                              final CustomerChangeBizService customerChangeBiz,
                              final ContactBizService contactBizService,
                              final CommonService commonService) {
        this.customerBizService = customerBizService;
        this.pickUpCustomerService = pickUpCustomerService;
        this.customerChangeBiz = customerChangeBiz;
        this.contactBizService = contactBizService;
        this.commonService = commonService;
    }

    /**
     * 分页查询客户信息列表 (管理人员)
     *
     * @param queryVO 查询条件
     */
    @GetMapping("/page")
    @IgnoreAuth
    public Message<AppPage<CustomerListVO>> customerList(@CurrentUser LoginAuthBean currentUser,
                                                         final CustomerQueryVO queryVO) {
        final String msg = "分页查询 客户列表(管理人员)[page]";
        try {
            log.info("{}: param[{}]", msg, queryVO);
            AppPage<CustomerListVO> page = customerBizService.getList(currentUser, queryVO);
            return success(page, data -> log.info("dataSize: {}", CollectionUtils.isEmpty(data.getData()) ? 0 : data.getData().size()));
        } catch (Exception ex) {
            handleException(ex, e -> log.error("{}失败:param[{}]", msg, queryVO, e));
            return failureWithMessage(QUERY_FAILURE);
        }
    }

    /**
     * 分页查询客户信息列表 (服务顾问自己的)
     *
     * @param currentUser
     * @param queryVO
     * @return
     */
    @GetMapping("/page/own")
    public Message<AppPage<CustomerListVO>> customerOwnList(@CurrentUser LoginAuthBean currentUser,
                                                            final CustomerQueryVO queryVO) {
        final String msg = "分页查询 客户列表(自己的)[page/own]";
        try {
            queryVO.setConsultantId(currentUser.getUserId());
            log.info("{}: param[{}]", msg, queryVO);
            AppPage<CustomerListVO> page = customerBizService.getList(currentUser, queryVO);
            return success(page, data -> log.info("dataSize: {}", CollectionUtils.isEmpty(data.getData()) ? 0 : data.getData().size()));
        } catch (Exception ex) {
            handleException(ex, e -> log.error("{}失败:param[{}]", msg, queryVO, e));
            return failureWithMessage(QUERY_FAILURE);
        }
    }

    /**
     * 根据ID查询档案信息
     *
     * @param cusId 档案ID
     */
    @GetMapping("/detailById")
    @IgnoreAuth
    public Message<CustomerDetailVO> getDetailById(@NotNull(message = "档案ID不能为空") final Long cusId) {
        final String msg = "根据ID查询档案信息[detail]";
        log.info("{}: param[{}]", msg, cusId);
        try {
            return success(customerBizService.getDetailById(cusId));
        } catch (Exception ex) {
            handleException(ex, e -> log.error("{}失败:param[{}]", msg, cusId, e));
            return failureWithMessage(QUERY_FAILURE);
        }
    }


    /**
     * 根据车牌号查询档案信息
     *
     * @param plateNo 车牌号
     */
    @GetMapping("/detailByPlate")
    @IgnoreAuth
    public Message<PickUpCustomerDetailVO> getDetailByPlate(@CurrentUser LoginAuthBean currentUser, @NotBlank(message = "车牌号不能为空") final String plateNo) {
        final String msg = "根据车牌号查询档案信息[detail]";
        log.info("{}: param[{}]", msg, plateNo);
        try {
            return success(pickUpCustomerService.getDetailByPlateNo(currentUser, plateNo));
        } catch (Exception ex) {
            handleException(ex, e -> log.error("{}失败:param[{}]", msg, plateNo, e));
            return failureWithMessage(QUERY_FAILURE);
        }
    }

    /**
     * 通过智能通话拨打电话
     *
     * @param currentUser
     * @param id
     * @param accidentCar
     * @return
     */
    @GetMapping("/call")
    public Message<String> call(@CurrentUser LoginAuthBean currentUser,
                                @NotNull(message = "档案ID不能为空") final Long id,
                                final Boolean accidentCar
    ) {
        final String msg = "通过智能通话拨打电话[call]";
        log.info("{}: param[cusId:{}  accidentCar:{}]", msg, id, accidentCar);
        try {
            return success(commonService.call(currentUser.getUserId(), id, accidentCar));
        } catch (Exception ex) {
            handleException(ex, e -> log.error("{}失败:param[{} accidentCar:{}]", msg, id, accidentCar, e));
            return failureWithMessage(QUERY_FAILURE);
        }
    }

    /**
     * 根据车架号查询档案信息
     * 如果发现档案是临牌则修改车牌为现在的
     *
     * @param frameNo 车架号
     */
    @GetMapping("/detailByFrameNo")
    public Message<PickUpCustomerDetailVO> getDetailByFrameNo(@CurrentUser LoginAuthBean currentUser, @NotBlank(message = "车架号不能为空") final String frameNo) {
        final String msg = "根据车架号号查询档案信息[detail]";
        log.info("{}: param[{}]", msg, frameNo);
        try {
            return success(pickUpCustomerService.getDetailByFrameNo(currentUser, frameNo));
        } catch (Exception ex) {
            handleException(ex, e -> log.error("{}失败:param[{}]", msg, frameNo, e));
            return failureWithMessage(QUERY_FAILURE);
        }
    }

    /**
     * 根据车架号修改档案车牌号
     *
     * @param frameNo
     * @param plateNo
     * @return
     */
    @PostMapping("/fixPlateNo")
    public Message<Long> fixPlateNo(@CurrentUser LoginAuthBean currentUser, @NotBlank(message = "车架号不能为空") final String frameNo, @NotBlank(message = "车牌号不能为空") final String plateNo) {
        final String msg = "根据车架号修改档案车牌号[fixPlateNo]";
        log.info("{}: param[{}  {}]", msg, frameNo, plateNo);
        try {
            return success(pickUpCustomerService.fixPlateNo(frameNo, plateNo, currentUser));
        } catch (Exception ex) {
            handleException(ex, e -> log.error("{}失败:param[{}]", msg, frameNo, e));
            return failureWithMessage(SAVE_FAILURE);
        }
    }

    /**
     * 根据ID查询客户信息
     *
     * @param cusId 客户ID
     */
    @GetMapping("/vehicle")
    public Message<VehicleRecordVO> getCarInfoById(@NotNull(message = "档案ID不能为空") final Long cusId) {
        final String msg = "根据ID查询车辆信息[vehicle]";
        log.info("{}: param[{}]", msg, cusId);
        try {
            return success(customerBizService.getCarInfoById(cusId));
        } catch (Exception ex) {
            handleException(ex, e -> log.error("{}失败:param[{}]", msg, cusId, e));
            return failureWithMessage(QUERY_FAILURE);
        }
    }

    /**
     * 根据ID查询客户信息
     *
     * @param cusId 客户ID
     */
    @GetMapping("/owner")
    public Message<OwnerVO> getOwnerInfo(@NotNull(message = "档案ID不能为空") final Long cusId) {
        final String msg = "根据ID查询所有人信息[owner]";
        log.info("{}: param[{}]", msg, cusId);
        try {
            return success(customerBizService.getOwnerInfo(cusId));
        } catch (Exception ex) {
            handleException(ex, e -> log.error("{}失败:param[{}]", msg, cusId, e));
            return failureWithMessage(QUERY_FAILURE);
        }
    }

    /**
     * 根据ID查询客户信息
     *
     * @param cusId 客户ID
     */
    @GetMapping("/getMore")
    public Message<List<MoreVehicleVO>> getMore(@NotNull(message = "客户ID不能为空") final Long cusId, @CurrentUser LoginAuthBean currentUser) {
        final String msg = "根据ID查询档案信息[getMore]";
        log.info("{}: param[{}]", msg, cusId);
        try {
            return success(customerBizService.getMoreArchive(cusId, currentUser.getGroupId()));
        } catch (Exception ex) {
            handleException(ex, e -> log.error("{}失败:param[{}]", msg, cusId, e));
            return failureWithMessage(QUERY_FAILURE);
        }
    }


    /**
     * 修改档案标签
     *
     * @param tags 标签枚举
     */
    @PostMapping("/updateTagById")
    public Message<Integer> updateTagById(@NotBlank(message = "标签不能为空") final String tags,
                                          @NotNull(message = "id不能为空") final Long cusId) {
        final String msg = "修改档案标签[updateTagById]";
        log.info("{}: param[{}、{}]", msg, tags, cusId);
        try {
            return success(customerChangeBiz.updateTagById(cusId, tags));
        } catch (Exception ex) {
            handleException(ex, e -> log.error("{}失败:param[{}、{}]", msg, tags, cusId, e));
            return failureWithMessage(SAVE_FAILURE);
        }
    }


    /**
     * 查询送修人、同行人等信息
     *
     * @param cusId 档案id
     */
    @GetMapping("/contact/list")
    public Message<List<CustomerContactVO>> contactList(@NotNull(message = "档案ID不能为空") final Long cusId) {
        final String msg = "查询送修人、同行人等信息[save]";
        log.info("{}: param[{}]", msg, cusId);
        try {
            return success(contactBizService.contactList(cusId));
        } catch (Exception ex) {
            handleException(ex, e -> log.error("{}失败:param[{}]", msg, cusId, e));
            return failureWithMessage(QUERY_FAILURE);
        }
    }

    /**
     * 获取档案变更二维码
     *
     * @param currentUser
     * @param customerChangeDto
     * @return
     */
    @PostMapping("/change/qrcode")
    public Message<CustomerChangeQrCodeVO> getQrCode(@CurrentUser LoginAuthBean currentUser,
                                                     @Validated @RequestBody CustomerChangeDto customerChangeDto) {
        final String msg = "获取档案变更二维码";
        log.info("{}: param[{}]", msg, customerChangeDto);
        try {
            CustomerChangeQrCodeVO qrCode = customerChangeBiz.createQrCode(currentUser, customerChangeDto);
            log.info("生成成功");
            return success(qrCode);
        } catch (Exception ex) {
            handleException(ex, e -> log.error("{}失败:param[{}]", msg, customerChangeDto, e));
            return failureWithMessage(SAVE_FAILURE);
        }
    }

    /**
     * 主动放弃
     *
     * @param cusId
     */
    @PostMapping("/abandon")
    public Message<Void> abandon(@NotNull(message = "id不能为空") final Long cusId, final String reason) {
        final String msg = "主动放弃客户[abandon]";
        log.info("{}: param[{}、{}]", msg, cusId, reason);
        try {
            customerBizService.abandon(cusId, reason);
            return success();
        } catch (Exception ex) {
            handleException(ex, e -> log.error("{}失败:param[{}、{}]", msg, cusId, reason, e));
            return failureWithMessage(SAVE_FAILURE);
        }
    }

    /**
     * 继续跟进
     *
     * @param cusId
     */
    @PostMapping("/continue")
    public Message<Void> continueFollow(@CurrentUser LoginAuthBean currentUser, @NotNull(message = "id不能为空") final Long cusId) {
        final String msg = "继续跟进客户[continue]";
        log.info("{}: param[{}]", msg, cusId);
        try {
            customerBizService.continueFollow(currentUser, cusId);
            return success();
        } catch (Exception ex) {
            handleException(ex, e -> log.error("{}失败:param[{}、{}]", msg, cusId, e));
            return failureWithMessage(SAVE_FAILURE);
        }
    }
}