DracChannelCodeFollowStaffService.java 5.54 KB
package cn.com.glsx.entmicro.service;

import cn.com.glsx.entmicro.constants.Constants;
import cn.com.glsx.entmicro.entity.DracChannelCodeFollowStaff;
import cn.com.glsx.entmicro.mapper.DracChannelCodeFollowStaffMapper;
import cn.com.glsx.entmicro.mapper.EntmicroStaffDepartRelationMapper;
import cn.com.glsx.entmicro.model.request.DracChannelCodeFollowStaffRequest;
import cn.com.glsx.entmicro.model.response.ChannelCodeStaffInfoResponse;
import cn.com.glsx.entmicro.model.response.DracChannelCodeFollowStaffResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

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

/**
 * @author Lichongyuan
 * @Description: 活码员工关联服务
 * @date 2021/11/12 11:32
 */
@Service
@Slf4j
public class DracChannelCodeFollowStaffService {

    @Resource
    private DracChannelCodeFollowStaffMapper dracChannelCodeFollowStaffMapper;

    @Resource
    private EntmicroStaffDepartRelationMapper entmicroStaffDepartRelationMapper;

    /**
     * 批量插入
     *
     * @param dracChannelCodeFollowStaffs
     */
    public void insertList(List<DracChannelCodeFollowStaff> dracChannelCodeFollowStaffs) {
        //TODO 校验重复
        dracChannelCodeFollowStaffMapper.insertList(dracChannelCodeFollowStaffs);
    }

    /**
     * 新增关联
     *
     * @param dracChannelCodeFollowStaff
     */
    public void insert(DracChannelCodeFollowStaff dracChannelCodeFollowStaff) {
        dracChannelCodeFollowStaffMapper.insert(dracChannelCodeFollowStaff);
    }

    /**
     * codeId和staffId查关联数据
     *
     * @param codeId
     * @param staffId
     * @return
     */
    public List<DracChannelCodeFollowStaff> findByCodeId(Integer codeId, List<Integer> staffId) {
        return dracChannelCodeFollowStaffMapper.findByCodeId(codeId, staffId);
    }

    /**
     * 更新关联
     */
    public void update(DracChannelCodeFollowStaffRequest dracChannelCodeFollowStaffRequest) {
        //查出本地活码和员工关联
        List<DracChannelCodeFollowStaff> oldList =findByCodeId(dracChannelCodeFollowStaffRequest.getCodeId());
        //根据入参的staffId查询本地员工信息
        List<ChannelCodeStaffInfoResponse> newStaffByIdList = entmicroStaffDepartRelationMapper.getStaffByIdList(dracChannelCodeFollowStaffRequest.getStaffIds(), dracChannelCodeFollowStaffRequest.getMerchantId());

        //入参参数转化
        List<DracChannelCodeFollowStaff> tranNewParam = newStaffByIdList.stream().map(index -> {
            DracChannelCodeFollowStaff dracChannelCodeFollowStaff = new DracChannelCodeFollowStaff();
            dracChannelCodeFollowStaff.setStaffId(index.getRelationId());
            dracChannelCodeFollowStaff.setStaffName(index.getStaffName());
            dracChannelCodeFollowStaff.setCodeId(dracChannelCodeFollowStaffRequest.getCodeId());
            dracChannelCodeFollowStaff.setUpdatedTime(new Date());
            dracChannelCodeFollowStaff.setDelFlag((int) Constants.NORMAL_FLAG);
            return dracChannelCodeFollowStaff;
        }).collect(Collectors.toList());
        //对比入参员工
        List<DracChannelCodeFollowStaff> addList = tranNewParam.stream().filter(index -> !oldList.contains(index)).collect(Collectors.toList());
        List<DracChannelCodeFollowStaff> deleteList = oldList.stream().filter(index -> !tranNewParam.contains(index)).collect(Collectors.toList());
        List<DracChannelCodeFollowStaff> updateList = oldList.stream().filter(index -> tranNewParam.contains(index)).collect(Collectors.toList());
        //新增活码员工关系
        if (!CollectionUtils.isEmpty(addList)) {
            addList.forEach(index -> {
                index.setDelFlag((int) Constants.NORMAL_FLAG);
                insert(index);
            });
        }
        //更新员工活码关系
        if (!CollectionUtils.isEmpty(updateList)) {
            updateList.forEach(index -> {
                index.setUpdatedTime(new Date());
                index.setUpdatedBy(dracChannelCodeFollowStaffRequest.getCreatedBy());
                dracChannelCodeFollowStaffMapper.updateByPrimaryKeySelective(index);
            });
        }
        //删除活码员工关系
        if (!CollectionUtils.isEmpty(deleteList)) {
            deleteList.forEach(index -> {
                deleteDracChannelCodeFollowStaff(index);
            });
        }
    }

    /**
     * codeId查询关联数据
     *
     * @param codeId
     * @return
     */
    public List<DracChannelCodeFollowStaff> findByCodeId(Integer codeId) {
        return dracChannelCodeFollowStaffMapper.findByCodeId(codeId, null);
    }

    /**
     * codeId查关联员工
     *
     * @param codeId
     * @return
     */
    public List<DracChannelCodeFollowStaffResponse> findStaffByCodeId(Set<Integer> codeId) {
        List<DracChannelCodeFollowStaffResponse> staffByCodeId = dracChannelCodeFollowStaffMapper.findStaffByCodeId(codeId);
        return staffByCodeId;
    }

    /**
     * 删除单条关联
     *
     * @param dracChannelCodeFollowStaff
     */
    public void deleteDracChannelCodeFollowStaff(DracChannelCodeFollowStaff dracChannelCodeFollowStaff) {
        dracChannelCodeFollowStaffMapper.deleteDracChannelCodeFollowStaff(dracChannelCodeFollowStaff.getId());
    }

    /**
     * 删除本地活码关联员工信息
     *
     * @param codeId
     */
    public void deleteByCodeId(Integer codeId) {
        dracChannelCodeFollowStaffMapper.deleteByCodeId(codeId);
    }
}