FollowApiServiceImpl.java 7.9 KB
package cn.fw.shirasawa.server.controller.api;

import cn.fw.common.web.annotation.ControllerMethod;
import cn.fw.data.base.domain.common.Message;
import cn.fw.shirasawa.common.utils.MessageFormatUtil;
import cn.fw.shirasawa.domain.dto.FollowTaskCompleteDTO;
import cn.fw.shirasawa.domain.dto.FollowTerminationDTO;
import cn.fw.shirasawa.domain.dto.OriginalDataDTO;
import cn.fw.shirasawa.domain.enums.FollowTypeEnum;
import cn.fw.shirasawa.sdk.api.FollowApiService;
import cn.fw.shirasawa.sdk.enums.BusinessTypeEnum;
import cn.fw.shirasawa.sdk.enums.DataTypeEnum;
import cn.fw.shirasawa.sdk.param.*;
import cn.fw.shirasawa.sdk.result.SucessFollowRecordVo;
import cn.fw.shirasawa.service.bus.follow.FollowBizService;
import cn.fw.shirasawa.service.bus.follow.OriginalDataBizService;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;

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

/**
 * @author : kurisu
 * @className : FollowApiServiceImpl
 * @description : 跟进api
 * @date: 2020-08-14 17:53
 */
@Slf4j
@RestController
@RequestMapping("/api/shirasawa/follow")
public class FollowApiServiceImpl implements FollowApiService {
    @Value("${follow.max-size}")
    @Getter
    private int maxSize;

    private final OriginalDataBizService originalDataBizService;
    private final FollowBizService followBizService;

    @Autowired
    public FollowApiServiceImpl(final OriginalDataBizService originalDataBizService,
                                final FollowBizService followBizService) {
        this.originalDataBizService = originalDataBizService;
        this.followBizService = followBizService;
    }

    @Override
    @PostMapping("/generate")
    @ControllerMethod("生成跟进任务")
    public Message<Void> generate(@Valid @RequestBody FollowGenerateDTO generateDTO) {
        if (BusinessTypeEnum.PS.equals(generateDTO.getBusinessType())) {
            BV.notNull(generateDTO.getMemberId(), () -> "会员号不能为空");
            BV.notNull(generateDTO.getCustomerId(), () -> "档案id不能为空");
        }
        if (DataTypeEnum.AC.equals(generateDTO.getType())) {
            BV.isNotBlank(generateDTO.getPlateNo(), () -> "车牌号不能为空");
            BV.isNotBlank(generateDTO.getContacts(), () -> "联系方式不能为空");
        }
        if (BusinessTypeEnum.AS.equals(generateDTO.getBusinessType()) && !DataTypeEnum.AC.equals(generateDTO.getType())) {
            BV.isNotBlank(generateDTO.getFrameNo(), () -> "车架号不能为空");
        }
        OriginalDataDTO dataDTO = new OriginalDataDTO();
        BeanUtils.copyProperties(generateDTO, dataDTO);
        DataTypeEnum type = generateDTO.getType();
        dataDTO.setDataType(type.getValue());
        dataDTO.setBizType(generateDTO.getBusinessType());
        originalDataBizService.saveOrigin(dataDTO);
        return success();
    }

    @Override
    @PostMapping("/generate_batch")
    @ControllerMethod("批量生成跟进任务")
    public Message<Void> generateBatch(@Valid @RequestBody CustomList<FollowGenerateDTO> paramList) {
        BV.isNotEmpty(paramList, () -> "列表不能为空");

        BV.isTrue(paramList.size() <= getMaxSize(), () -> MessageFormatUtil.MessageFormatTransfer("最多支持一次性生成{0}条任务", getMaxSize()));
        List<OriginalDataDTO> list = new ArrayList<>();
        for (FollowGenerateDTO param : paramList) {
            if (BusinessTypeEnum.PS.equals(param.getBusinessType())) {
                BV.notNull(param.getCustomerId(), () -> "档案id不能为空");
            }
            if (DataTypeEnum.AC.equals(param.getType())) {
                BV.isNotBlank(param.getPlateNo(), () -> "车牌号不能为空");
                BV.isNotBlank(param.getContacts(), () -> "联系方式不能为空");
            }
            if (BusinessTypeEnum.AS.equals(param.getBusinessType()) && !DataTypeEnum.AC.equals(param.getType())) {
                BV.isNotBlank(param.getFrameNo(), () -> "车架号不能为空");
            }
            OriginalDataDTO dataDTO = new OriginalDataDTO();
            BeanUtils.copyProperties(param, dataDTO);
            dataDTO.setDataType(param.getType().getValue());
            dataDTO.setBizType(param.getBusinessType());
            list.add(dataDTO);
        }
        originalDataBizService.saveBatchOrigin(list);
        return success();
    }

    @Override
    @PutMapping("/complete/record")
    @ControllerMethod("完成跟进待办")
    public Message<Void> completeFollow(@RequestParam("recordId") Long recordId, @RequestParam("userId") Long userId) {
        followBizService.completeRecord(recordId, userId);
        return success();
    }

    @Override
    @PostMapping("/create/record")
    @ControllerMethod("生成下一次待办")
    public Message<Boolean> createNextRecord(@Valid @RequestBody NewRecordDTO newRecordDTO) {
        return success(followBizService.createNewRecord(newRecordDTO));
    }

    @Override
    @PostMapping("/complete/task")
    @ControllerMethod("完成跟进任务")
    public Message<Void> completeTask(@Valid @RequestBody TaskCompleteDTO taskCompleteDTO) {
        FollowTaskCompleteDTO dto = new FollowTaskCompleteDTO();
        BeanUtils.copyProperties(taskCompleteDTO, dto);
        dto.setFollowType(FollowTypeEnum.ofValue(taskCompleteDTO.getType().getValue()));
        dto.setBizType(taskCompleteDTO.getBusinessType());
        followBizService.completeTask(dto);
        return success();
    }

    @Override
    @PostMapping("/termination/task")
    @ControllerMethod("终止跟进任务")
    public Message<Void> terminationTask(@Valid @RequestBody TerminationDTO terminationDTO) {
        FollowTerminationDTO followTerminationDTO = new FollowTerminationDTO();
        BeanUtils.copyProperties(terminationDTO, followTerminationDTO);
        followTerminationDTO.setFollowType(FollowTypeEnum.ofValue(terminationDTO.getType().getValue()));
        followTerminationDTO.setBizType(terminationDTO.getBusinessType());
        followBizService.terminationTask(followTerminationDTO);
        return success();
    }

    @Override
    @PostMapping("/termination_batch")
    @ControllerMethod("批量终止跟进任务")
    public Message<Void> terminationBatch(@Valid @RequestBody CustomList<TerminationDTO> paramList) {
        BV.isNotEmpty(paramList, () -> "列表不能为空");
        BV.isTrue(paramList.size() <= getMaxSize(), () -> MessageFormatUtil.MessageFormatTransfer("最多支持一次性结束{0}条任务", getMaxSize()));
        List<FollowTerminationDTO> list = new ArrayList<>();
        for (TerminationDTO dto : paramList) {
            FollowTerminationDTO followTerminationDTO = new FollowTerminationDTO();
            BeanUtils.copyProperties(dto, followTerminationDTO);
            followTerminationDTO.setFollowType(FollowTypeEnum.ofValue(dto.getType().getValue()));
            followTerminationDTO.setBizType(dto.getBusinessType());
            list.add(followTerminationDTO);
        }
        followBizService.terminationTaskBatch(list);
        return success();
    }

    @Override
    @GetMapping("/getCompletedFollowRecord")
    @ControllerMethod("获取已完成的跟进记录")
    public Message<List<SucessFollowRecordVo>> getCompletedFollowRecord(@RequestParam("bizType") @NotNull(message = "业务类型不能为空!") Integer bizType,
                                                                        @RequestParam("customerId") @NotEmpty(message = "档案id不能为空!") List<Long> customerId) {
        return success(followBizService.getCompletedFollowRecord(bizType, customerId));
    }
}