FollowController.java 7.57 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.FollowAttachmentDTO;
import cn.fw.valhalla.domain.enums.FollowTypeEnum;
import cn.fw.valhalla.domain.query.FollowQueryVO;
import cn.fw.valhalla.domain.vo.follow.FollowDetailVO;
import cn.fw.valhalla.domain.vo.follow.FollowRecordVO;
import cn.fw.valhalla.domain.vo.follow.FollowTodoListVO;
import cn.fw.valhalla.service.bus.follow.FollowBizService;
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.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;

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

/**
 * @author : kurisu
 * @className : FollowController
 * @description : 跟进控制器
 * @date: 2020-08-15 16:23
 */
@Slf4j
@RestController
@Authorization(AuthType.APP)
@Validated
@RequestMapping("/app/follow")
public class FollowController {
    private final FollowBizService followBizService;

    @Autowired
    public FollowController(final FollowBizService followBizService) {
        this.followBizService = followBizService;
    }


    @GetMapping("/todo/list")
    public Message<AppPage<FollowTodoListVO>> todoList(@CurrentUser LoginAuthBean currentUser, final FollowQueryVO queryVO) {
        final String msg = "分页查询 跟进待办列表[follow/todo/list]";
        try {
            log.info("{}: param[{}]", msg, queryVO.getType());
            BV.notNull(queryVO.getType(), "跟进类型不正确");
            AppPage<FollowTodoListVO> page = followBizService.todoList(queryVO, currentUser);
            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.getType(), e));
            return failureWithMessage(QUERY_FAILURE);
        }
    }

    @GetMapping("/todo/detail")
    public Message<FollowDetailVO> todoDetail(@NotNull(message = "跟进记录id不能为空") final Long id,
                                              @NotNull(message = "跟进类型不能为空") final Integer type) {
        final String msg = "查询跟进待办详情[follow/todo/detail]";
        try {
            log.info("{}: param[{}  {}]", msg, id, type);
            FollowTypeEnum typeEnum = FollowTypeEnum.ofValue(type);
            BV.notNull(typeEnum, "跟进类型不正确");
            FollowDetailVO detailVO = followBizService.todoDetail(id, typeEnum);
            return success(detailVO, data -> log.info("{}", data));
        } catch (Exception ex) {
            handleException(ex, e -> log.error("{}失败:param[{}  {}]", msg, id, type, e));
            return failureWithMessage(QUERY_FAILURE);
        }
    }

    @GetMapping("/approve/detail")
    @IgnoreAuth
    public Message<FollowDetailVO> approveDetail(@NotNull(message = "跟进记录id不能为空") final Long recordId,
                                                 @NotNull(message = "跟进类型不能为空") final Integer type) {
        final String msg = "查询跟进战败审批详情[follow/approve/detail]";
        try {
            log.info("{}: param[{}  {}]", msg, recordId, type);
            FollowTypeEnum typeEnum = FollowTypeEnum.ofValue(type);
            BV.notNull(typeEnum, "跟进类型不正确");
            FollowDetailVO detailVO = followBizService.approveDetail(recordId, typeEnum);
            return success(detailVO, data -> log.info("{}", data));
        } catch (Exception ex) {
            handleException(ex, e -> log.error("{}失败:param[{}  {}]", msg, recordId, type, e));
            return failureWithMessage(QUERY_FAILURE);
        }
    }

    @GetMapping("/todo/record")
    @IgnoreAuth
    public Message<List<FollowRecordVO>> todoRecord(@NotNull(message = "跟进任务id不能为空") final Long taskId,
                                                    @NotNull(message = "跟进类型不能为空") final Integer type) {
        final String msg = "查询跟进历史记录[follow/todo/record]";
        try {
            log.info("{}: param[{}  {}]", msg, taskId, type);
            FollowTypeEnum typeEnum = FollowTypeEnum.ofValue(type);
            BV.notNull(typeEnum, "跟进类型不正确");
            return success(followBizService.todoRecord(taskId, typeEnum), data -> log.info("{}", data));
        } catch (Exception ex) {
            handleException(ex, e -> log.error("{}失败:param[{}  {}]", msg, taskId, type, e));
            return failureWithMessage(QUERY_FAILURE);
        }
    }

    @PostMapping("/defeat")
    public Message<Void> defeat(@CurrentUser LoginAuthBean currentUser,
                                @NotNull(message = "跟进记录id不能为空") final Long id,
                                final String reason) {
        final String msg = "主动战败[follow/defeat]";
        try {
            log.info("{}: param[recordId:{}   reason:{}]", msg, id, reason);
            followBizService.defeat(currentUser, reason, id);
            log.info("操作成功");
            return success();
        } catch (Exception ex) {
            handleException(ex, e -> log.error("{}失败:param[{}  {}]", msg, id, reason, e));
            return failureWithMessage(QUERY_FAILURE);
        }
    }

    @PostMapping("/todo/complete")
    public Message<Void> complete(@CurrentUser LoginAuthBean currentUser,
                                  @NotNull(message = "跟进记录id不能为空") final Long id,
                                  @NotNull(message = "跟进类型不能为空") final Integer type) {
        final String msg = "完成跟进待办[follow/todo/complete]";
        try {
            log.info("{}: param[{}  {}]", msg, id, type);
            FollowTypeEnum typeEnum = FollowTypeEnum.ofValue(type);
            BV.notNull(typeEnum, "跟进类型不正确");
            followBizService.completeRecord(id, typeEnum, currentUser);
            log.info("操作成功");
            return success();
        } catch (Exception ex) {
            handleException(ex, e -> log.error("{}失败:param[{}  {}]", msg, id, type, e));
            return failureWithMessage(QUERY_FAILURE);
        }
    }

    @PostMapping("/todo/upload")
    public Message<Void> upload(@CurrentUser LoginAuthBean currentUser, @RequestBody @Valid final FollowAttachmentDTO dto) {
        final String msg = "上传跟进凭证[follow/todo/upload]";
        try {
            log.info("{}: param[{}]", msg, dto);
            BV.notNull(dto.getFollowType(), "跟进类型不正确");
            followBizService.uploadAtt(dto, currentUser);
            log.info("操作成功");
            return success();
        } catch (Exception ex) {
            handleException(ex, e -> log.error("{}失败:param[{}]", msg, dto, e));
            return failureWithMessage(QUERY_FAILURE);
        }
    }
}