MassDetailCustomerService.java 4.94 KB
package cn.com.glsx.entmicro.service;

import cn.com.glsx.entmicro.constants.Constants;
import cn.com.glsx.entmicro.entity.MassChatDetail;
import cn.com.glsx.entmicro.entity.MassDetailCustomer;
import cn.com.glsx.entmicro.entity.mass.MassCustomerItemEntity;
import cn.com.glsx.entmicro.entity.mass.MassStatisticItemEntity;
import cn.com.glsx.entmicro.mapper.MassDetailCustomerMapper;
import cn.com.glsx.entmicro.model.response.MassDetailStaffResponse;
import cn.com.glsx.framework.core.exception.RpcServiceException;
import cn.com.glsx.scrm.wechat.dto.workwechat.externalmsg.response.GetWorkWechatExternalContactGroupMsgSendResultDTO;
import com.github.pagehelper.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@Service
public class MassDetailCustomerService {

    @Resource
    private MassDetailCustomerMapper massDetailCustomerMapper;

    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {RpcServiceException.class})
    public Boolean batchSaveMassDetailCustomer(List<MassDetailCustomer> massDetailCustomerList) {
        massDetailCustomerMapper.insertList(massDetailCustomerList);
        return true;
    }

    /**
     * 获取 群发中的客户列表
     * @param relationId
     * @return
     */
    public List<MassDetailCustomer> getMassDetailCustomerListByRelationId(Integer relationId) {
        Example selectCondition = new Example(MassDetailCustomer.class);
        selectCondition.createCriteria().andEqualTo("staffRelationId", relationId);
        List<MassDetailCustomer> customerList = massDetailCustomerMapper.selectByExample(selectCondition);
        return customerList;
    }

    public Map<Integer, Integer> countCustomerSender(Integer massId) {
        List<MassStatisticItemEntity> massStatisticItemEntityList = massDetailCustomerMapper.countCustomerSender(massId);
        if (CollectionUtils.isEmpty(massStatisticItemEntityList)) {
            return new HashMap<>();
        }
        Map<Integer, Integer> result = massStatisticItemEntityList.stream().collect(Collectors.toMap(MassStatisticItemEntity::getDataType, MassStatisticItemEntity::getDataCount, (item1, item2) -> item1));
        return result;
    }

    public List<MassCustomerItemEntity> getCustomerListByMassIdAndStatus(Integer massId, Integer status) {
        List<MassCustomerItemEntity> result = massDetailCustomerMapper.getCustomerListByMassIdAndStatus(massId, status);
        return result;
    }

    public Page<MassCustomerItemEntity> getCustomerListByMassIdAndStatusPage(Integer massId, Integer status) {
        Page<MassCustomerItemEntity> result = massDetailCustomerMapper.getCustomerListByMassIdAndStatusPage(massId, status);
        return result;
    }

    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {RpcServiceException.class})
    public void updateMassDetailCustomerSendResult(Integer staffRelationId, Integer customerId, GetWorkWechatExternalContactGroupMsgSendResultDTO msgSendResultDTO) {
        Date sendTime = (msgSendResultDTO.getSendTime()!=null)?new Date(msgSendResultDTO.getSendTime()*1000L):null;
        massDetailCustomerMapper.updateMassDetailCustomerSendResult(staffRelationId, customerId, msgSendResultDTO.getStatus(), sendTime);
    }

    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {RpcServiceException.class})
    public void updateMassDetailCustomerSendResult(Integer staffRelationId, String external_user_id, Integer status) {
        massDetailCustomerMapper.updateMassDetailCustomerStatusByExternalUserId(staffRelationId, external_user_id, status);
    }

    /**
     * 根据massId统计客户数量
     * @param massId
     * @return
     */
    public Integer countCustomers(Integer massId) {
        if (massId==null){
            return null;
        }
        Integer num = massDetailCustomerMapper.countCustomers(massId);
        return num;
    }

    @Async
    public void updateFailListCustomer(List<String> failList, MassDetailStaffResponse staffResponse) {
        try {
            if (failList!=null&&!failList.isEmpty()){
                for (String s : failList) {
                    updateMassDetailCustomerSendResult(staffResponse.getId(),s, Constants.MASS_DETAIL_CUSTOMER_STATUS.SEND_ERROR_FOR_UNFRIEND);
                }
            }
        } catch (Exception e) {
            log.info("更新客户接收失败异常:{}",e);
        }
    }

    public List<Integer> getSendSuccessCustomerIdList(Integer massId){
        if (massId==null){return null;}
        return massDetailCustomerMapper.getSendSuccessCustomerIdList(massId);
    }
}