EntmicroFollowerStaffService.java 4.99 KB
package cn.com.glsx.entmicro.service;

import cn.com.glsx.entmicro.mapper.EntmicroStaffMapper;
import cn.com.glsx.entmicro.model.request.EntmicroFollowerStaffRequest;
import cn.com.glsx.entmicro.model.response.EntmicroFollowerStaffResponse;
import cn.com.glsx.entmicro.util.EmojiUtils;
import cn.com.glsx.framework.core.beans.rpc.RpcPagination;
import cn.com.glsx.framework.core.beans.rpc.RpcResponse;
import cn.com.glsx.framework.core.exception.RpcServiceException;
import cn.com.glsx.scrm.shopmall.remote.WeappNoticeConfigRemote;
import com.alibaba.fastjson.JSONArray;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.glsx.plat.common.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

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


/**
 * @author Lichongyuan
 * @Description:
 * @date 2021/9/18 11:56
 */
@Service
@Slf4j
public class EntmicroFollowerStaffService {

    @Resource
    private EntmicroStaffMapper entmicroStaffMapper;

    @Resource
    private WeappNoticeConfigRemote weappNoticeConfigRemote;

    @Autowired
    private Environment environment;


    /**
     * 添加跟进员工
     *
     * @param entmicroFollowerStaffRequest
     * @return
     */
    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = RpcServiceException.class)
    public void saveFollowerStaff(EntmicroFollowerStaffRequest entmicroFollowerStaffRequest) {
        entmicroFollowerStaffRequest.setFollowerCreatedTime(new Date());
        entmicroStaffMapper.saveEntmicroFollowerStaff(entmicroFollowerStaffRequest);
    }

    /**
     * 删除跟进员工
     *
     * @param entmicroFollowerStaffRequest
     * @return
     */
    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = RpcServiceException.class)
    public void deleteFollowerStaff(EntmicroFollowerStaffRequest entmicroFollowerStaffRequest) {
        entmicroStaffMapper.deleteEntmicroFollowerStaff(entmicroFollowerStaffRequest);
        //删除商城跟进员
        log.info("删除商城跟进员入参: {}", entmicroFollowerStaffRequest);
        RpcResponse<Boolean> booleanRpcResponse = weappNoticeConfigRemote.deleteCustomerServer(entmicroFollowerStaffRequest.getMerchantId(), entmicroFollowerStaffRequest.getUserOneId());
        if (booleanRpcResponse.isFailed()) {
            log.info("商城跟进员未能成功移除: {}", JSONArray.toJSONString(booleanRpcResponse));
        }
    }

    /**
     * 查询跟进员工列表
     *
     * @param pagination
     * @return
     */
    public Page<EntmicroFollowerStaffResponse> listFollowerStaffByPage(RpcPagination<EntmicroFollowerStaffRequest> pagination) {
        PageHelper.startPage(pagination.getPageNum(), pagination.getPageSize());
        EntmicroFollowerStaffRequest condition = pagination.getCondition();
        condition.setStaffName(StringUtils.isNotEmpty(condition.getStaffName()) ? EmojiUtils.parseToHtmlHexadecimal(condition.getStaffName()) : condition.getStaffName());
        Page<EntmicroFollowerStaffResponse> result = entmicroStaffMapper.listEntmicroFollowerStaffByPage(condition);
        if (!CollectionUtils.isEmpty(result.getResult())) {
            List<Integer> collect = result.stream().map(EntmicroFollowerStaffResponse::getId).collect(Collectors.toList());
            Map<Integer, Map> departMap = entmicroStaffMapper.findDepartByMerIdAndId(collect, condition.getMerchantId());
            result.forEach(index -> {
                Map map = departMap.get(index.getId());
                if (!map.isEmpty()) {
                    String departName = map.get("department_name") + "";
                    String departmentName = departName != null ? EmojiUtils.parseToUnicode(departName) : null;
                    index.setDepartmentName(departmentName);
                }
                String staffName = index.getStaffName() != null ? EmojiUtils.parseToUnicode(index.getStaffName()) : null;
                index.setStaffName(staffName);
            });
        }
        return result;
    }



    /**
     * 查询指定跟进类型的员工
     * @param entmicroFollowerStaffRequest
     * @return
     */
    public List<EntmicroFollowerStaffResponse> findEntFollowStaffSpec(EntmicroFollowerStaffRequest entmicroFollowerStaffRequest) {
        String followTypeName = environment.getProperty("enmicro.notice.followTypeName");
        List<String> followTypeNameList = Arrays.asList(followTypeName.split(","));
        entmicroFollowerStaffRequest.setFollowerTypeNameList(followTypeNameList);
        return entmicroStaffMapper.listEntmicroFollower(entmicroFollowerStaffRequest);
    }
}