FollowPoolBizService.java 6.21 KB
package cn.fw.valhalla.service.bus.follow;

import cn.fw.common.page.AppPage;
import cn.fw.common.web.auth.LoginAuthBean;
import cn.fw.valhalla.common.enums.DefeatTypeEnum;
import cn.fw.valhalla.common.utils.DateUtil;
import cn.fw.valhalla.common.utils.StringUtils;
import cn.fw.valhalla.domain.db.PublicPool;
import cn.fw.valhalla.domain.dto.FollowPoolDTO;
import cn.fw.valhalla.domain.enums.TaskStateEnum;
import cn.fw.valhalla.domain.query.FollowPoolQueryVO;
import cn.fw.valhalla.domain.vo.customer.PublicPoolVO;
import cn.fw.valhalla.domain.vo.follow.FollowPoolListVO;
import cn.fw.valhalla.rpc.erp.UserService;
import cn.fw.valhalla.rpc.erp.dto.UserInfoDTO;
import cn.fw.valhalla.rpc.oop.OopService;
import cn.fw.valhalla.rpc.oop.dto.ShopDTO;
import cn.fw.valhalla.service.data.FollowTaskService;
import cn.fw.valhalla.service.data.PublicPoolService;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import static cn.fw.common.businessvalidator.Validator.BV;

/**
 * @author : kurisu
 * @className : FollowPoolBizService
 * @description : 跟进池服务
 * @date: 2020-09-23 15:30
 */
@Slf4j
@Service
public class FollowPoolBizService {
    private final FollowTaskService followTaskService;
    private final PublicPoolService publicPoolService;
    private final OopService oopService;
    private final UserService userService;
    private final static Integer MONTH_DAY = 31;

    @Autowired
    public FollowPoolBizService(final FollowTaskService followTaskService,
                                final PublicPoolService publicPoolService,
                                final OopService oopService,
                                final UserService userService) {
        this.followTaskService = followTaskService;
        this.publicPoolService = publicPoolService;
        this.oopService = oopService;
        this.userService = userService;
    }

    /**
     * 跟进池
     *
     * @param queryVO
     * @return
     */
    public AppPage<FollowPoolListVO> followList(LoginAuthBean user, FollowPoolQueryVO queryVO) {
        prepareParams(user, queryVO);
        List<FollowPoolDTO> list = followTaskService.followList(queryVO);
        List<FollowPoolListVO> followList = new ArrayList<>();
        for (FollowPoolDTO followPoolDTO : list) {
            FollowPoolListVO vo = trans2Pool(followPoolDTO);
            followList.add(vo);
        }
        AppPage<FollowPoolListVO> page = AppPage.empty(queryVO);
        page.setData(followList);
        return page;
    }

    /**
     * 公共池
     *
     * @param queryVO
     * @return
     */
    public AppPage<PublicPoolVO> publicList(FollowPoolQueryVO queryVO) {
        Page<PublicPool> opage = new Page<>(queryVO.getCurrent(), queryVO.getPageSize());
        opage = publicPoolService.page(opage, Wrappers.<PublicPool>lambdaQuery()
                .eq(StringUtils.isValid(queryVO.getPlateNo()), PublicPool::getPlateNo, "%" + queryVO.getPlateNo() + "%")
                .eq(Objects.nonNull(queryVO.getType()), PublicPool::getType, queryVO.getType())
                .eq(Objects.nonNull(queryVO.getGroupId()), PublicPool::getGroupId, queryVO.getGroupId())
        );
        AppPage<PublicPoolVO> page = AppPage.empty(queryVO);
        List<PublicPool> list = opage.getRecords();
        if (CollectionUtils.isEmpty(list)) {
            return page;
        }
        List<PublicPoolVO> publicList = new ArrayList<>();
        for (PublicPool pool : list) {
            PublicPoolVO vo = new PublicPoolVO();
            BeanUtils.copyProperties(pool, vo);
            vo.setTypeDesc(pool.getType().getName());
            publicList.add(vo);
        }
        page.setData(publicList);
        return page;
    }


    private FollowPoolListVO trans2Pool(FollowPoolDTO poolDTO) {
        FollowPoolListVO vo = new FollowPoolListVO();
        BeanUtils.copyProperties(poolDTO, vo);
        UserInfoDTO user = userService.user(poolDTO.getUserId());
        if (Objects.nonNull(user)) {
            vo.setFollower(user.getUserName());
        }
        ShopDTO shop = oopService.shop(poolDTO.getShopId());
        if (Objects.nonNull(shop)) {
            vo.setShopName(shop.getShopName());
        }
        DefeatTypeEnum defeatTypeEnum = DefeatTypeEnum.ofValue(poolDTO.getInitiative());
        if (Objects.nonNull(defeatTypeEnum) && poolDTO.getState() == 3) {
            vo.setDefeatType(defeatTypeEnum.getName());
        }

        if (Boolean.TRUE.equals(poolDTO.getFinished()) && !poolDTO.getOriginShop().equals(poolDTO.getFinishShop())) {
            vo.setDefeatDesc("集团内战败");
        }
        if (poolDTO.getState() == 3 && DefeatTypeEnum.B.equals(defeatTypeEnum)) {
            vo.setDefeatDesc("系统划走");
        }

        return vo;
    }

    private void prepareParams(LoginAuthBean user, FollowPoolQueryVO queryVO) {
        if (Objects.nonNull(user)) {
            queryVO.setUserId(user.getUserId());
            queryVO.setGroupId(user.getGroupId());
        } else {
            BV.isFalse(Objects.isNull(queryVO.getShopId()) && Objects.isNull(queryVO.getUserId()), () -> "请选择服务站或者人员");
        }
        boolean inSection = DateUtil.sub(queryVO.getEndTime(), queryVO.getStartTime(), "d") <= MONTH_DAY;
        BV.isTrue(inSection, () -> "暂不支持查询超过一个月区间的数据");
        boolean d = DateUtil.sub(queryVO.getEndTime(), queryVO.getStartTime(), "d") >= 0;
        BV.isTrue(d, () -> "截止时间必须大于开始时间");
        if (Objects.nonNull(queryVO.getOrder()) && StringUtils.isValid(queryVO.getOrderAtt())) {
            StringBuilder sb = new StringBuilder(" order by ");
            sb.append(StringUtils.toColumnName(queryVO.getOrderAtt()).toLowerCase());
            if (queryVO.getOrder() == 1) {
                sb.append(" asc ");
            } else {
                sb.append(" desc ");
            }
            queryVO.setOrderString(sb.toString());
        }
    }
}