MassDetailStaffService.java 10.8 KB
package cn.com.glsx.entmicro.service;

import cn.com.glsx.entmicro.constants.Constants;
import cn.com.glsx.entmicro.constants.DracConstant;
import cn.com.glsx.entmicro.converter.MassDetailStaffConvert;
import cn.com.glsx.entmicro.entity.Mass;
import cn.com.glsx.entmicro.entity.MassDetailCustomer;
import cn.com.glsx.entmicro.entity.MassDetailStaff;
import cn.com.glsx.entmicro.entity.mass.MassStatisticItemEntity;
import cn.com.glsx.entmicro.mapper.MassDetailCustomerMapper;
import cn.com.glsx.entmicro.mapper.MassDetailStaffMapper;
import cn.com.glsx.entmicro.model.request.mass.MassStaffDetailListRpcRequest;
import cn.com.glsx.entmicro.model.response.MassDetailStaffResponse;
import cn.com.glsx.entmicro.model.response.MassStaffSendStatusResponse;
import cn.com.glsx.framework.core.enums.DefaultErrorEnum;
import cn.com.glsx.framework.core.exception.RpcServiceException;
import cn.com.glsx.framework.core.util.RpcAssert;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
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.*;
import java.util.stream.Collectors;

@Service
public class MassDetailStaffService {

    @Resource
    private MassDetailStaffMapper massDetailStaffMapper;
    @Resource
    private MassDetailCustomerMapper massDetailCustomerMapper;

    /**
     * 批量保存群发 员工列表
     * @param staffList
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {RpcServiceException.class})
    public Boolean insertMassDetailStaffList(List<MassDetailStaff> staffList) {
        if (CollectionUtils.isEmpty(staffList)) {
            return false;
        }
        massDetailStaffMapper.insertList(staffList);
        return true;
    }

    /**
     * 通过群发ID获取员工列表
     * @param massId
     * @return
     */
    public List<MassDetailStaffResponse> getStaffListByMassId(Integer massId) {
        Example selectCondition = new Example(MassDetailStaff.class);
        selectCondition.createCriteria().
                andEqualTo("massId", massId).
                andEqualTo("delFlag", DracConstant.DelFlag.OK);
        List<MassDetailStaff> massDetailList = massDetailStaffMapper.selectByExample(selectCondition);
        return MassDetailStaffConvert.convertStaffList(massDetailList);
    }

    /**
     * 更新微信消息-msgId
     * @param id
     * @param msgId
     */
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {RpcServiceException.class})
    public Boolean updateQyMessageSenderResult(Integer id, String msgId) {
        if (StringUtils.isEmpty(msgId) || id==null) {
            return false;
        }
        MassDetailStaff massDetailStaff = new MassDetailStaff();
        massDetailStaff.setId(id);
        massDetailStaff.setMsgId(msgId);
        massDetailStaffMapper.updateByPrimaryKeySelective(massDetailStaff);
        return true;
    }

    /**
     * 更新状态
     * @param id
     * @param status
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {RpcServiceException.class})
    public Boolean updateQyMessageSenderResultStatus(Integer id, Integer status) {
        if (status==null || id==null) {
            return false;
        }
        MassDetailStaff massDetailStaff = new MassDetailStaff();
        massDetailStaff.setId(id);
        massDetailStaff.setStatus(status);
        massDetailStaffMapper.updateByPrimaryKeySelective(massDetailStaff);
        //更新客户接收状态为失败
        MassDetailCustomer customer = new MassDetailCustomer();
        customer.setStatus(Constants.MASS_DETAIL_CUSTOMER_STATUS.SEND_ERROR_FOR_OTHER_MSG);
        Example example = new Example(MassDetailCustomer.class);
        example.createCriteria().andEqualTo("staffRelationId",id);
        massDetailCustomerMapper.updateByExampleSelective(customer,example);
        return true;
    }

    /**
     * 根据massId更新员工发送状态
     * @param massId
     * @param status
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {RpcServiceException.class})
    public Boolean updateQyMessageSenderStatusByMassId(Integer massId, Integer status) {
        if (status==null || massId==null) {
            return false;
        }
        MassDetailStaff massDetailStaff = new MassDetailStaff();
        massDetailStaff.setStatus(status);
        Example massDetailStaffExample = new Example(MassDetailStaff.class);
        massDetailStaffExample.createCriteria().andEqualTo("massId",massId);
        List<MassDetailStaff> massDetailStaffs = massDetailStaffMapper.selectByExample(massDetailStaffExample);
        List<Integer> massDetailStaffIds = massDetailStaffs.stream().map(MassDetailStaff::getId).collect(Collectors.toList());
        massDetailStaffMapper.updateByExampleSelective(massDetailStaff,massDetailStaffExample);
        //更新客户接收状态为失败
        MassDetailCustomer customer = new MassDetailCustomer();
        customer.setStatus(Constants.MASS_DETAIL_CUSTOMER_STATUS.SEND_ERROR_FOR_OTHER_MSG);
        Example example = new Example(MassDetailCustomer.class);
        example.createCriteria().andIn("staffRelationId",massDetailStaffIds);
        massDetailCustomerMapper.updateByExampleSelective(customer,example);
        return true;
    }

    /**
     * 根据msgId更新员工发送状态
     * @param msgId
     * @param status
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {RpcServiceException.class})
    public Boolean updateQyMessageSenderResultStatusByMsgId(String msgId, Integer status) {
        if (status==null || msgId==null) {
            return false;
        }
        Example example = new Example(MassDetailStaff.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("msgId",msgId);
        MassDetailStaff massDetailStaff = new MassDetailStaff();
        massDetailStaff.setStatus(status);
        massDetailStaffMapper.updateByExampleSelective(massDetailStaff,example);
        //更新客户接收状态为失败-3
        massDetailCustomerMapper.updateMassDetailCustomerSendStatus(msgId);
        return true;
    }

    public Map<Integer, Integer> countStaffSender(Integer massId) {
        List<MassStatisticItemEntity> massStatisticItemEntityList = massDetailStaffMapper.countStaffSender(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<MassDetailStaffResponse> getStaffListByMassIdAndStatus(Integer massId, Integer status) {
        Example selectCondition = new Example(MassDetailStaff.class);
        Example.Criteria criteria = selectCondition.createCriteria();
        criteria.andEqualTo("massId", massId).
                andEqualTo("delFlag", DracConstant.DelFlag.OK);
        if (status!=null) {
            criteria.andEqualTo("status", status);
        }
        List<MassDetailStaff> massDetailList = massDetailStaffMapper.selectByExample(selectCondition);
        // 组装客户数
        buildCustomerCount(massDetailList, massId, status);
        return MassDetailStaffConvert.convertStaffList(massDetailList);
    }

    private void buildCustomerCount(List<MassDetailStaff> massDetailList, Integer massId, Integer status) {
        if (CollectionUtils.isEmpty(massDetailList)) {
            return ;
        }

        // 补充客户数量
        List<MassStatisticItemEntity> massStaffCustomerCountList = massDetailStaffMapper.getStaffCustomerCount(massId, status);
        if (CollectionUtils.isEmpty(massStaffCustomerCountList)) {
            return;
        }

        Map<Integer, Integer> customerCountMapper = massStaffCustomerCountList.stream().collect(Collectors.toMap(MassStatisticItemEntity::getDataType, MassStatisticItemEntity::getDataCount, (item1, item2) -> item1));

        for (MassDetailStaff staff:massDetailList) {
            staff.setCustomerNum(customerCountMapper.get(staff.getStaffId()));
        }
    }

    public void updateQyMessageSenderStatus(Integer msgId, Integer status, Date date) {
        MassDetailStaff massDetailStaff = new MassDetailStaff();
        massDetailStaff.setId(msgId);
        massDetailStaff.setStatus(status);
        massDetailStaff.setSendTime(date);
        massDetailStaffMapper.updateByPrimaryKeySelective(massDetailStaff);
    }

    /**
     * 通过主键获取群发员工记录信息
     * @param id
     * @return
     */
    public MassDetailStaff getStaffDetailById(Integer id) {
        if (id==null) {
            return null;
        }
        MassDetailStaff selectCondition = new MassDetailStaff();
        selectCondition.setId(id);
        MassDetailStaff massDetailStaff = massDetailStaffMapper.selectByPrimaryKey(selectCondition);
        return massDetailStaff;
    }

    /**
     * 根据massId判断是否创建任务中心任务
     * @param massId
     * @return
     */
    public List<MassDetailStaff> canAddTask(Integer massId) {
        Example example = new Example(MassDetailStaff.class);
        example.createCriteria()
                .andEqualTo("massId",massId)
                .andIn("status", Arrays.asList(Constants.MASS_DETAIL_STAFF_STATUS.UNSEND,Constants.MASS_DETAIL_STAFF_STATUS.SENDED))
                .andIsNotNull("msgId")
                .andEqualTo("delFlag",Constants.NORMAL_FLAG);

        List<MassDetailStaff> massDetailStaffs = massDetailStaffMapper.selectByExample(example);
        return massDetailStaffs;
    }

    public List<Integer> getSucessStaffListByMass(Mass mass) {
        if (mass==null||mass.getId()==null){
            return null;
        }
        List<Integer> list = massDetailStaffMapper.getSucessStaffListByMass(mass.getId());
        return list;
    }

    public MassStaffSendStatusResponse selectMassStaffDetail(MassStaffDetailListRpcRequest request){
        RpcAssert.assertNotNull(request, DefaultErrorEnum.PARAMETER_NULL,"参数不能为空!");
        RpcAssert.assertNotNull(request.getMassId(), DefaultErrorEnum.PARAMETER_NULL,"massId 参数不能为空!");
        RpcAssert.assertNotNull(request.getStaffId(), DefaultErrorEnum.PARAMETER_NULL,"staffId 参数不能为空!");
        List<MassStaffSendStatusResponse> massDetailStaffs = massDetailStaffMapper.selectMassStaffDetail(request);
        if (massDetailStaffs!=null && !massDetailStaffs.isEmpty()){
            return massDetailStaffs.get(0);
        }
        return null;
    }
}