AsyncMassService.java 6.24 KB
package cn.com.glsx.entmicro.service;

import cn.com.glsx.entmicro.constants.Constants;
import cn.com.glsx.entmicro.manager.MassServiceComponent;
import cn.com.glsx.entmicro.model.request.CustListSearchRequest;
import cn.com.glsx.entmicro.model.response.CustomerInfoResponse;
import cn.com.glsx.entmicro.model.response.EntmicroStaffResponse;
import cn.com.glsx.entmicro.model.response.MassResponse;
import cn.com.glsx.entmicro.util.EmojiUtils;
import cn.com.glsx.marketing.model.request.activity.ActivityCustomerSendStatusUpdateRequest;
import cn.com.glsx.marketing.remote.activity.SolicitRemote;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Future;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @Author yelq
 * @Version 1.0
 * @Description:
 * @Date 2022/6/4 18:34
 */
@Slf4j
@Service
public class AsyncMassService {

    @Autowired
    private MassService massService;

    @Autowired
    private MassServiceComponent massServiceComponent;
    @Autowired
    private SolicitRemote solicitRemote;
    @Resource
    private MassDetailStaffService massDetailStaffService;
    @Autowired
    private MassDetailCustomerService massDetailCustomerService;
    @Autowired
    private EntmicroStaffService entmicroStaffService;
    @Autowired
    private CustomerService customerService;


    @Async
    public Future<Map<Integer, CustomerInfoResponse>> getCustomerInfoResponseMapper(List<Integer> customerIds, Integer merchantId) {
        try {
            CustListSearchRequest custSearchRequest = new CustListSearchRequest();
            custSearchRequest.setCustIds(customerIds);
            custSearchRequest.setMerchantId(merchantId);
            List<CustomerInfoResponse> customerInfoResponseList = customerService.findCustomerByCondition(custSearchRequest);
            customerInfoResponseList.forEach(index -> {
                index.setCustomerName(com.glsx.plat.common.utils.StringUtils.isNotEmpty(index.getCustomerName()) ? EmojiUtils.parseToUnicode(index.getCustomerName()) : null);
            });
            if (CollectionUtils.isEmpty(customerInfoResponseList)) {
                return new AsyncResult<>(new HashMap<>());
            }
            Map<Integer, CustomerInfoResponse> customerMapper = customerInfoResponseList.stream().collect(Collectors.toMap(CustomerInfoResponse::getId, Function.identity(), (item1, item2) -> item1));
            return new AsyncResult<>(customerMapper);
        } catch (Exception e) {
            log.info("异步查询异常:{}",e);
        }
        return new AsyncResult<>(new HashMap<>());
    }

    @Async
    public Future<Map<Integer, EntmicroStaffResponse>> getEntmicroStaffResponseMapper(List<Integer> staffIdLsit) {
        try {
            List<EntmicroStaffResponse> staffResponseList = entmicroStaffService.findStaffByStaffIds(staffIdLsit);
            staffResponseList.forEach(index -> {
                //Emoji转化
                index.setStaffName(com.glsx.plat.common.utils.StringUtils.isNotEmpty(index.getStaffName()) ? EmojiUtils.parseToUnicode(index.getStaffName()) : null);
                index.setEntmicroName(com.glsx.plat.common.utils.StringUtils.isNotEmpty(index.getEntmicroName()) ? EmojiUtils.parseToUnicode(index.getEntmicroName()) : null);
            });
            if (CollectionUtils.isEmpty(staffResponseList)) {
                return new AsyncResult<>(new HashMap<>());
            }
            Map<Integer, EntmicroStaffResponse> staffMapper = staffResponseList.stream().collect(Collectors.toMap(EntmicroStaffResponse::getId, Function.identity(), (item1, item2) -> item1));
            return new AsyncResult<>(staffMapper);
        } catch (Exception e) {
            log.info("异步查询异常:{}",e);
        }
        return new AsyncResult<>(new HashMap<>());
    }

    @Async
    public void writeBackActivityCustomerSendStatus(Integer id) {
        try {
            MassResponse mass = massService.getDetailSingle(id);
            if (mass != null && mass.getType() == 1 && mass.getSelectType() == Constants.MASS_ONECLICK_CONDITION && mass.getStatus() == 1) {
                List<Integer> list = massService.getSendSuccessCustomerIdList(id);
                List<Integer> mergeIdList = massServiceComponent.getMergeIdList(list, mass.getMerchantId());
                ActivityCustomerSendStatusUpdateRequest request = new ActivityCustomerSendStatusUpdateRequest();
                request.setCustomerIds(mergeIdList);
                request.setMassId(id);
                solicitRemote.updateCustomerSendStatus(request);
            }
        } catch (Exception e) {
            log.info("一键营销客户接收状态回写失败:{}",e);
        }
    }

    @Async
    public Future<Map<Integer, Integer>> countStaffSender(Integer massId) {
        try {
            Map<Integer, Integer> massStaffCountMapper = massDetailStaffService.countStaffSender(massId);
            return new AsyncResult<>(massStaffCountMapper);
        } catch (Exception e) {
            log.info("异步查询异常:{}",e);
        }
        return new AsyncResult<>(new HashMap<>());
    }

    @Async
    public Future<Map<Integer, Integer>> countCustomerSender(Integer massId) {
        try {
            Map<Integer, Integer> massCustomerCountMapper = massDetailCustomerService.countCustomerSender(massId);
            return new AsyncResult<>(massCustomerCountMapper);
        } catch (Exception e) {
            log.info("异步查询异常:{}",e);
        }
        return new AsyncResult<>(new HashMap<>());
    }

    @Async
    public Future<Map<Integer, Integer>> countCustomerChatSender(Integer massId) {
        try {
            Map<Integer, Integer> massCustomerCountMapper = massDetailStaffService.countStaffSender(massId);
            return new AsyncResult<>(massCustomerCountMapper);
        } catch (Exception e) {
            log.info("异步查询异常:{}",e);
        }
        return new AsyncResult<>(new HashMap<>());
    }


}