CommonController.java 6.31 KB
package cn.fw.valhalla.controller.app;

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.common.utils.DateUtil;
import cn.fw.valhalla.domain.vo.PostUserVO;
import cn.fw.valhalla.service.bus.CommonService;
import cn.fw.valhalla.service.bus.CustomerImportBizService;
import cn.fw.valhalla.service.bus.CustomerRetentionRatioBizService;
import cn.fw.valhalla.service.bus.LeaveNeedDoBizService;
import cn.fw.valhalla.service.data.FollowRecordService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
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.SAVE_FAILURE;

/**
 * @author : kurisu
 * @className : CommonController
 * @description : 公共接口
 * @date: 2020-10-22 11:06
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@Authorization(AuthType.APP)
@Validated
@RequestMapping("/app/common")
public class CommonController {
    private final CommonService commonService;
    private final LeaveNeedDoBizService leaveNeedDoBizService;
    private final CustomerImportBizService customerImportBizService;
    private final CustomerRetentionRatioBizService customerRetentionRatioBizService;


    @GetMapping("/staff/list")
    @IgnoreAuth
    public Message<List<PostUserVO>> list(@NotNull(message = "服务站ID不能为空") final Long shopId) {
        final String msg = "查询人员[app/common/staff/list]";
        try {
            log.info("{}: param[shopId: {}]", msg, shopId);
            List<PostUserVO> list = commonService.getUsers(shopId);
            return success(list, data -> log.info("{}", data));
        } catch (Exception ex) {
            handleException(ex, e -> log.error("{}失败:param[shopId: {}]", msg, shopId, e));
            return failureWithMessage(SAVE_FAILURE);
        }
    }

    @GetMapping("/leave/add")
    @Authorization(AuthType.NONE)
    public Message<Void> add(@NotNull(message = "服务站ID不能为空") final Long shopId,
                             @NotNull(message = "用户ID不能为空") final Long userId) {
        final String msg = "添加离职待分配数据[app/common/leave/add]";
        try {
            log.info("{}: param[shopId: {} userId: {}]", msg, shopId, userId);
            leaveNeedDoBizService.add(userId, shopId);
            return success();
        } catch (Exception ex) {
            handleException(ex, e -> log.error("{}失败:param[shopId: {} userId: {}]", msg, shopId, userId, e));
            return failureWithMessage(SAVE_FAILURE);
        }
    }

    @GetMapping("/import/deal")
    @Authorization(AuthType.NONE)
    public Message<Void> dealImport() {
        final String msg = "处理导入的档案数据[app/common/import/deal]";
        try {
            log.info("{}  开始", msg);
            customerImportBizService.integrated();
            log.info("{}  结束", msg);
            return success();
        } catch (Exception ex) {
            handleException(ex, e -> log.error("{}  失败", msg, e));
            return failureWithMessage(SAVE_FAILURE);
        }
    }

    @GetMapping("/import/apportion")
    @Authorization(AuthType.NONE)
    public Message<Void> apportion() {
        final String msg = "导入的档案分配顾问[app/common/import/apportion]";
        try {
            log.info("{}  开始", msg);
            customerImportBizService.apportion();
            log.info("{}  结束", msg);
            return success();
        } catch (Exception ex) {
            handleException(ex, e -> log.error("{}  失败", msg, e));
            return failureWithMessage(SAVE_FAILURE);
        }
    }

    @GetMapping("/crr/extracting")
    @Authorization(AuthType.NONE)
    public Message<Void> crrExtracting(String date) {
        final String msg = "抽取保有客保持率数据[app/common/crr/extracting]";
        try {
            log.info("{}  开始", msg);
            Date nowDate = new Date();
            if (StringUtils.isNotBlank(date) && NumberUtils.isDigits(date)) {
                LocalDateTime localDateTime = Instant.ofEpochMilli(NumberUtils.toLong(date)).atZone(ZoneId.systemDefault()).toLocalDateTime();
                nowDate = DateUtil.localDateTime2Date(localDateTime);
            }
            customerRetentionRatioBizService.extracting(DateUtil.startDate(nowDate));
            log.info("{}  结束", msg);
            return success();
        } catch (Exception ex) {
            handleException(ex, e -> log.error("{}  失败", msg, e));
            return failureWithMessage(SAVE_FAILURE);
        }
    }

    /**
     * 交车后取消订单时回滚档案
     *
     * @param vin
     * @return
     */
    @GetMapping("/customer/rollback")
    @Authorization(AuthType.NONE)
    public Message<Void> customerRollback(@NotBlank(message = "车架号不能为空") String vin,
                                          @RequestParam(defaultValue = "2", required = false, name = "groupId") Long groupId) {
        final String msg = "交车后取消订单时回滚档案[app/common/customer/rollback]";
        try {
            log.info("{}  param[vin: {} groupId: {}]", msg, vin, groupId);
            commonService.rollBack(vin, groupId);
            return success();
        } catch (Exception ex) {
            handleException(ex, e -> log.error("{}  失败 param[vin: {} groupId: {}]", msg, vin, groupId, e));
            return failureWithMessage(SAVE_FAILURE);
        }
    }
}