FollowRecordServiceImpl.java 4.57 KB
package cn.fw.shirasawa.service.data.impl;

import cn.fw.shirasawa.dao.mapper.FollowRecordMapper;
import cn.fw.shirasawa.domain.db.follow.FollowRecord;
import cn.fw.shirasawa.domain.db.follow.SucessFollowRecord;
import cn.fw.shirasawa.domain.dto.FollowRecordPoolDTO;
import cn.fw.shirasawa.domain.enums.OutTimeEnum;
import cn.fw.shirasawa.domain.query.FollowRecordPoolQuery;
import cn.fw.shirasawa.sdk.enums.BusinessTypeEnum;
import cn.fw.shirasawa.service.data.FollowRecordService;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * @author : kurisu
 * @className : FollowRecordImpl
 * @description : 跟进记录
 * @date: 2020-08-12 11:10
 */
@Slf4j
@Service
public class FollowRecordServiceImpl extends ServiceImpl<FollowRecordMapper, FollowRecord> implements FollowRecordService {

    /**
     * 查询跟进记录
     *
     * @param taskId
     * @return
     */
    @Override
    public List<FollowRecord> getRecordList(final Long taskId) {
        return this.list(Wrappers.<FollowRecord>lambdaQuery()
                .eq(FollowRecord::getTaskId, taskId)
                .eq(FollowRecord::getAddTodo, Boolean.TRUE)
                .eq(FollowRecord::getYn, Boolean.TRUE)
                .orderByAsc(FollowRecord::getId)
        );
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateById(FollowRecord entity) {
        return super.updateById(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean overdue(final Long id) {
        return this.update(Wrappers.<FollowRecord>lambdaUpdate()
                .set(FollowRecord::getOutTime, OutTimeEnum.BE_OVERDUE)
                .eq(FollowRecord::getId, id)
        );
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean addTodoBatchById(List<Long> list) {
        if (CollectionUtils.isEmpty(list)) {
            return false;
        }
        return this.update(Wrappers.<FollowRecord>lambdaUpdate()
                .set(FollowRecord::getAddTodo, Boolean.TRUE)
                .in(FollowRecord::getId, list)
        );
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeByTaskId(Long taskId) {
        return this.remove(Wrappers.<FollowRecord>lambdaQuery()
                .eq(FollowRecord::getTaskId, taskId)
                .eq(FollowRecord::getOutTime, OutTimeEnum.ONGOING)
                .eq(FollowRecord::getAddTodo, Boolean.FALSE)
                .isNull(FollowRecord::getFollowTime)
        );
    }

    @Override
    public boolean checkCompelCall(Long taskId, Long userId, int times) {
        List<Long> recordIdList = this.getBaseMapper().getLastRecordAttType(taskId, userId, times);
        if (!CollectionUtils.isEmpty(recordIdList)) {
            return recordIdList.size() >= times;
        }
        return false;
    }

    @Override
    public List<SucessFollowRecord> getCompletedFollowRecord(Integer bizType, List<Long> customerIdList) {
        return this.getBaseMapper().getCompletedFollowRecord(bizType, customerIdList);
    }

    @Override
    public long recordPoolCount(FollowRecordPoolQuery query) {
        return Optional.ofNullable(this.getBaseMapper().recordPoolCount(query)).orElse(0L);
    }

    @Override
    public List<FollowRecordPoolDTO> recordPoolList(FollowRecordPoolQuery query) {
        Integer current = query.getCurrent();
        Integer pageSize = query.getPageSize();
        Integer startIndex = (current - 1) * pageSize;
        List<FollowRecordPoolDTO> list = this.getBaseMapper().recordPoolList(startIndex, pageSize, query);
        return Optional.ofNullable(list).orElse(new ArrayList<>());
    }

    @Override
    public long recordRemaining(Long userId, BusinessTypeEnum bizType, @Nullable Long recordId) {
        return count(Wrappers.<FollowRecord>lambdaQuery()
                .eq(FollowRecord::getUserId, userId)
                .eq(FollowRecord::getBizType, bizType)
                .eq(FollowRecord::getOutTime, OutTimeEnum.ONGOING)
                .eq(FollowRecord::getYn, Boolean.TRUE)
                .eq(FollowRecord::getAddTodo, Boolean.TRUE)
                .ne(Objects.nonNull(recordId), FollowRecord::getId, recordId)
        );
    }
}