package com.cyksj.service.corp.kf.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.cyksj.common.exception.BusinessRuntimeException;
import com.cyksj.common.util.Jsons;
import com.cyksj.common.util.StringUtil;
import com.cyksj.config.corp.YHLXWxCorpConfig;
import com.cyksj.mapper.corp.CorpFollowMapper;
import com.cyksj.mapper.corp.kf.*;
import com.cyksj.mapper.manage.cms.CmsUserMapper;
import com.cyksj.model.dto.CorpKfChangeFollowLogDto;
import com.cyksj.model.entity.*;
import com.cyksj.model.kf.*;
import com.cyksj.model.request.corp.kf.CorpKfDutyReq;
import com.cyksj.model.response.corp.CorpKfServicerStateViewResp;
import com.cyksj.model.views.CorpKfDutyServicersStatusView;
import com.cyksj.model.views.SysCorpFollowView;
import com.cyksj.service.corp.WxCorpOps;
import com.cyksj.service.corp.kf.CorpKfService;
import com.cyksj.service.mange.cms.service.CmsUserService;
import com.ejlchina.searcher.BeanSearcher;
import com.ejlchina.searcher.util.MapUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 项目名: yhlxj2
 * 文件名: CorpKfServiceImpl
 * 创建者: JavaZou
 * 创建时间:2024/12/30 18:10
 */
@Service
@RequiredArgsConstructor
public class CorpKfServiceImpl implements CorpKfService {
	private final CorpKfAccountMapper corpKfAccountMapper;

	private final WxCorpOps wxCorpOps;

	private final YHLXWxCorpConfig yhlxWxCorpConfig;

	private final CorpKfAccountFollowMapper corpKfAccountFollowMapper;

	private final CorpKfAccountSessionConfigMapper corpKfAccountSessionConfigMapper;

	private final CmsUserService cmsUserService;

	private final CorpFollowMapper corpFollowMapper;

	private final CorpKfServicerSessionRecordMapper corpKfServicerSessionRecordMapper;

	private final CmsUserMapper cmsUserMapper;

	private final BeanSearcher beanSearcher;

	private final CorpKfFollowChangeRecordMapper corpKfFollowChangeRecordMapper;

	@Override
	public void addKfAccount(CorpKfAccount corpKfAccount) throws Exception {
		WxCpKfAccountAdd wxCpKfAccountAdd = new WxCpKfAccountAdd();
		wxCpKfAccountAdd.setName(corpKfAccount.getName());
		wxCpKfAccountAdd.setMediaId(wxCorpOps.getMediaIdByFileName("image", corpKfAccount.getImg(), yhlxWxCorpConfig.getCorpId()));
		WxCpKfAccountAddResp resp = wxCorpOps.addAccount(yhlxWxCorpConfig.getCorpId(), wxCpKfAccountAdd);
		corpKfAccount.setOpenKfId(resp.getOpenKfid());
		corpKfAccountMapper.insert(corpKfAccount);
	}

	@Override
	public void delKfAccount(Long id) throws Exception {
		CorpKfAccount corpKfAccount = corpKfAccountMapper.selectById(id);
		WxCpKfAccountDel wxCpKfAccountDel = new WxCpKfAccountDel();
		wxCpKfAccountDel.setOpenKfid(corpKfAccount.getOpenKfId());
		wxCorpOps.delAccount(yhlxWxCorpConfig.getCorpId(),wxCpKfAccountDel);
		corpKfAccountMapper.deleteById(id);
		//删除人员
		corpKfAccountFollowMapper.delete(Wrappers.lambdaQuery(CorpKfAccountFollow.class).eq(CorpKfAccountFollow::getOpenKfId, corpKfAccount.getOpenKfId()));
		//删除session设置
		corpKfAccountSessionConfigMapper.delete(Wrappers.lambdaQuery(CorpKfAccountSessionConfig.class).eq(CorpKfAccountSessionConfig::getOpenKfId, corpKfAccount.getOpenKfId()));
	}

	@Override
	public void updateKfAccount(CorpKfAccount corpKfAccount) throws Exception {
		if(StringUtils.isNotBlank(corpKfAccount.getName()) && StringUtils.isNotBlank(corpKfAccount.getImg())){
			CorpKfAccount kfAccount = corpKfAccountMapper.selectById(corpKfAccount.getId());
			WxCpKfAccountUpd wxCpKfAccountUpd = new WxCpKfAccountUpd();
			wxCpKfAccountUpd.setOpenKfid(kfAccount.getOpenKfId());
			if (StringUtils.isNotBlank(corpKfAccount.getName())) {
				wxCpKfAccountUpd.setName(corpKfAccount.getName());
			}
			if (StringUtils.isNotBlank(corpKfAccount.getImg())) {
				wxCpKfAccountUpd.setMediaId(wxCorpOps.getMediaIdByFileName("image", corpKfAccount.getImg(), yhlxWxCorpConfig.getCorpId()));
			}
			wxCorpOps.updAccount(yhlxWxCorpConfig.getCorpId(),wxCpKfAccountUpd);
		}
		corpKfAccountMapper.updateById(corpKfAccount);
	}

	@Override
	public void setKfAccountRuleOrAutoReply(CorpKfAccountSessionConfig corpKfAccountSessionConfig) {
		CorpKfAccountSessionConfig db = corpKfAccountSessionConfigMapper.selectOne(Wrappers.lambdaQuery(CorpKfAccountSessionConfig.class)
				.eq(CorpKfAccountSessionConfig::getOpenKfId, corpKfAccountSessionConfig.getOpenKfId())
				.last("limit 1"));
		if (db != null) {
			corpKfAccountSessionConfig.setId(db.getId());
		}
		Long id = corpKfAccountSessionConfig.getId();
		if (id == null) {
			corpKfAccountSessionConfigMapper.insert(corpKfAccountSessionConfig);
			return;
		}
		corpKfAccountSessionConfigMapper.updateById(corpKfAccountSessionConfig);
	}

	@Override
	public void setKfDuty(CorpKfDutyReq dutyReq) throws Exception {
		String openKfId = dutyReq.getOpenKfId();
		//记录变更记录
		List<CorpKfAccountFollow> before = corpKfAccountFollowMapper.selectList(Wrappers.lambdaQuery(CorpKfAccountFollow.class)
				.eq(CorpKfAccountFollow::getOpenKfId, openKfId));
		List<CorpKfAccountFollow> dutyList = Optional.ofNullable(dutyReq.getDutyList()).orElse(new ArrayList<>());
		//非单个 删除其他设置
		if (!dutyReq.getIsUpdateSingle()) {
			List<Long> exists = dutyList.stream().filter(c -> c.getId() != null).map(CorpKfAccountFollow::getId).collect(Collectors.toList());
			corpKfAccountFollowMapper.delete(Wrappers.lambdaQuery(CorpKfAccountFollow.class)
					.eq(CorpKfAccountFollow::getOpenKfId, openKfId)
					.notIn(!exists.isEmpty(), CorpKfAccountFollow::getId, exists));
		}

		dutyList.forEach(duty -> {
			if (duty.getId() != null) {
				corpKfAccountFollowMapper.updateById(duty);
				return;
			}
			duty.setOpenKfId(openKfId);
			corpKfAccountFollowMapper.insert(duty);
		});

		//需要删除的值班
		if (CollUtil.isNotEmpty(dutyReq.getDelIds())) {
			corpKfAccountFollowMapper.deleteBatchIds(dutyReq.getDelIds());
		}

		//将账号下所有值班人员重新设置
		List<CorpKfAccountFollow> corpKfAccountFollows = corpKfAccountFollowMapper.selectList(Wrappers.lambdaQuery(CorpKfAccountFollow.class)
				.eq(CorpKfAccountFollow::getOpenKfId, openKfId));
		//无需值班
		if (corpKfAccountFollows.isEmpty()) {
			return;
		}
		//值班的客服
		Set<String> thisDutyFollows = new HashSet<>();
		for (CorpKfAccountFollow corpKfAccountFollow : corpKfAccountFollows) {
			thisDutyFollows.addAll(Jsons.parseList(corpKfAccountFollow.getUserIdList(), String.class));
		}
		//接待人员列表
		CorpKfServicerResp corpKfServicerResp = wxCorpOps.getKfServicerList(yhlxWxCorpConfig.getCorpId(), openKfId);
		List<CorpKfServicerResp.CorpKfServicer> servicerList = corpKfServicerResp.getServicerList();
		List<String> delDutyFollowIds = new ArrayList<>();
		for (CorpKfServicerResp.CorpKfServicer corpKfServicer : servicerList) {
			String userid = corpKfServicer.getUserid();
			if (userid != null) {
				if (!thisDutyFollows.contains(userid)) {
					delDutyFollowIds.add(userid);
				}
			}
		}
		if (thisDutyFollows.size() > 0) {
			wxCorpOps.addCorpKfServicer(openKfId, thisDutyFollows);
		}

		if (delDutyFollowIds.size() > 0) {
			wxCorpOps.delCorpKfServicer(openKfId, delDutyFollowIds);
		}
		List<CorpKfAccountFollow> after = corpKfAccountFollowMapper.selectList(Wrappers.lambdaQuery(CorpKfAccountFollow.class)
				.eq(CorpKfAccountFollow::getOpenKfId, openKfId));
		CmsUser cmsUser = cmsUserMapper.selectById(dutyReq.getAdminId());
		recordChangeKfFollows(openKfId, cmsUser, before, after);
	}

	private void recordChangeKfFollows(String openKfId, CmsUser cmsUser, List<CorpKfAccountFollow> before, List<CorpKfAccountFollow> after) throws Exception {
		String operator = null;
		if (cmsUser != null) {
			operator = cmsUser.getNickname();
		}
		//客服关联
		List<SysCorpFollowView> sysCorpFollowViews = beanSearcher.searchAll(SysCorpFollowView.class, MapUtils.builder().build());
		Map<String, List<SysCorpFollowView>> followMap = sysCorpFollowViews.stream().collect(Collectors.groupingBy(SysCorpFollowView::getFollowId));
		Map<Integer, List<CorpKfAccountFollow>> bfDutyWeekFollowMap = before.stream().collect(Collectors.groupingBy(CorpKfAccountFollow::getDutyWeek));
		Map<Integer, List<CorpKfAccountFollow>> afDutyWeekFollowMap = after.stream().collect(Collectors.groupingBy(CorpKfAccountFollow::getDutyWeek));

		StringBuilder changeStr = new StringBuilder();
		for (Integer dutyWeek : bfDutyWeekFollowMap.keySet()) {
			List<CorpKfAccountFollow> bfFollow = bfDutyWeekFollowMap.get(dutyWeek);
			List<CorpKfAccountFollow> afFollow = Optional.ofNullable(afDutyWeekFollowMap.get(dutyWeek)).orElse(new ArrayList<>());
			CorpKfChangeFollowLogDto corpKfWeekChange = getCorpKfWeekChange(bfFollow, afFollow, dutyWeek, followMap);
			if (corpKfWeekChange != null) {
				if (changeStr.length() > 0) {
					changeStr.append("；");
				}
				changeStr.append(corpKfWeekChange.toChangeString());
			}
		}
		for (Integer dutyWeek : afDutyWeekFollowMap.keySet()) {
			List<CorpKfAccountFollow> afFollow = afDutyWeekFollowMap.get(dutyWeek);
			List<CorpKfAccountFollow> bfFollow = Optional.ofNullable(bfDutyWeekFollowMap.get(dutyWeek)).orElse(new ArrayList<>());
			if (bfFollow.size() > 0) {
				continue;
			}
			CorpKfChangeFollowLogDto corpKfWeekChange = getCorpKfWeekChange(bfFollow, afFollow, dutyWeek, followMap);
			if (corpKfWeekChange != null) {
				if (changeStr.length() > 0) {
					changeStr.append("；");
				}
				changeStr.append(corpKfWeekChange.toChangeString());
			}
		}
		if (changeStr.length() > 0) {
			CorpKfFollowChangeRecord corpKfFollowChangeRecord = new CorpKfFollowChangeRecord();
			corpKfFollowChangeRecord.setOpenKfId(openKfId);
			corpKfFollowChangeRecord.setPreValue(Jsons.toJson(before));
			corpKfFollowChangeRecord.setBackValue(Jsons.toJson(after));
			corpKfFollowChangeRecord.setOperator(operator);
			corpKfFollowChangeRecord.setChangeStr(changeStr.toString());
			corpKfFollowChangeRecordMapper.insert(corpKfFollowChangeRecord);
		}
	}

	@Override
	public void servicerCommute(long cmsUserId, String openKfId, Boolean status) throws Exception {
		CorpKfServicerResp corpKfServicerResp = wxCorpOps.getKfServicerList(yhlxWxCorpConfig.getCorpId(), openKfId);
		List<String> followIds = corpFollowMapper.selectList(Wrappers.lambdaQuery(CorpFollow.class)
				.eq(CorpFollow::getCmsUserId, cmsUserId)).stream().map(CorpFollow::getUserid).collect(Collectors.toList());
		if (followIds.isEmpty()) {
			throw BusinessRuntimeException.getInstance("未关联企业微信成员");
		}
		//接待人员列表
		List<CorpKfServicerResp.CorpKfServicer> servicerList = corpKfServicerResp.getServicerList();
		List<String> delDutyFollowId = new ArrayList<>();
		//下班
		if (!status) {
			for (CorpKfServicerResp.CorpKfServicer servicer : servicerList) {
				if (servicer.getUserid() != null) {
					if (followIds.contains(servicer.getUserid())) {
						delDutyFollowId.add(servicer.getUserid());
					}
				}
			}
			if (!delDutyFollowId.isEmpty()) {
				wxCorpOps.delCorpKfServicer(openKfId, delDutyFollowId);
			}
			return;
		}
		//上班
		DateTime now = DateTime.now();
		String thisHourDate = DateUtil.format(now, "HH:mm:ss");
		//星期天为1
		int dayOfWeek = DateUtil.dayOfWeek(now);
		//是否值班期
		List<CorpKfAccountFollow> corpKfAccountFollows = corpKfAccountFollowMapper.selectList(Wrappers.lambdaQuery(CorpKfAccountFollow.class)
				.eq(CorpKfAccountFollow::getOpenKfId, openKfId)
				.le(CorpKfAccountFollow::getStartTime, thisHourDate)
				.ge(CorpKfAccountFollow::getEndTime, thisHourDate)
				.eq(CorpKfAccountFollow::getDutyWeek, dayOfWeek));
		//无需值班
		if (corpKfAccountFollows.isEmpty()) {
			throw BusinessRuntimeException.getInstance("非值班时间");
		}
		//目前时间段需要值班的客服
		Set<String> thisHourDutyFollows = new HashSet<>();
		for (CorpKfAccountFollow corpKfAccountFollow : corpKfAccountFollows) {
			thisHourDutyFollows.addAll(Jsons.parseList(corpKfAccountFollow.getUserIdList(), String.class));
		}
		String dutyFollowId = null;
		for (String followId : followIds) {
			if (thisHourDutyFollows.contains(followId)) {
				dutyFollowId = followId;
				break;
			}
		}
		if (dutyFollowId == null) {
			throw BusinessRuntimeException.getInstance("无关联企业成员");
		}
		Set<String> dutyFollowIds = new HashSet<>();
		for (CorpKfServicerResp.CorpKfServicer corpKfServicer : servicerList) {
			String userid = corpKfServicer.getUserid();
			if (userid != null) {
				dutyFollowIds.add(userid);
			}
		}
		dutyFollowIds.add(dutyFollowId);
		wxCorpOps.addCorpKfServicer(openKfId, dutyFollowIds);
	}

	@Override
	public List<CorpKfDutyServicersStatusView> getDutyFollowByOpenKfId(long cmsUserId, String openKfId) throws Exception {
		Boolean isRoot = cmsUserService.isRootRole(cmsUserId);
		//目前时间段需要值班的客服
		Set<String> dutyFollowIds = getThisHourDutyFollowIds(openKfId);
		if (dutyFollowIds.isEmpty()) {
			return new ArrayList<>();
		}
		CorpKfServicerResp corpKfServicerResp = wxCorpOps.getKfServicerList(yhlxWxCorpConfig.getCorpId(), openKfId);
		List<CorpKfServicerResp.CorpKfServicer> servicerList = corpKfServicerResp.getServicerList();
		//目前值班客服id
		List<String> corpKfDutyServiceIds = new ArrayList<>();
		for (CorpKfServicerResp.CorpKfServicer servicer : servicerList) {
			if (servicer.getUserid() != null && servicer.getStatus() == 0) {
				corpKfDutyServiceIds.add(servicer.getUserid());
			}
		}
		List<CorpKfDutyServicersStatusView> views = new ArrayList<>();
		CorpKfDutyServicersStatusView corpKfDutyServicersStatusView = null;
		//查看目前时间段值班人员情况
		for (String thisHourDutyFollow : dutyFollowIds) {
			LambdaQueryWrapper<CorpFollow> wrapper = Wrappers.lambdaQuery(CorpFollow.class)
					.eq(CorpFollow::getUserid, thisHourDutyFollow);
			//非root 只查询当前用户当前时间段值班情况
			if (!isRoot) {
				wrapper.eq(CorpFollow::getCmsUserId, cmsUserId);
			}
			CorpFollow corpFollow = corpFollowMapper.selectOne(wrapper
					.last("limit 1"));
			if (corpFollow == null) {
				continue;
			}
			corpKfDutyServicersStatusView = new CorpKfDutyServicersStatusView();
			Long relateUserId = corpFollow.getCmsUserId();
			CmsUser cmsUser = cmsUserMapper.selectById(relateUserId);
			if (cmsUser != null) {
				corpKfDutyServicersStatusView.setName(cmsUser.getNickname());
			}
			corpKfDutyServicersStatusView.setFollowName(corpFollow.getName());
			corpKfDutyServicersStatusView.setCmsUserId(corpFollow.getCmsUserId());
			if (!corpKfDutyServiceIds.contains(thisHourDutyFollow)) {
				//未值班
				corpKfDutyServicersStatusView.setStatus(0);
			} else {
				corpKfDutyServicersStatusView.setStatus(1);
			}
			views.add(corpKfDutyServicersStatusView);
		}
		return views;
	}

	@Override
	public String getUrl(String openKfId, String scene) throws Exception {
		return wxCorpOps.getCorpKfUrl(openKfId, scene);
	}

	/**
	 * 转接客服
	 */
	@Override
	public void transCustomer(String followId, String toFollowId, Integer transNum, List<String> transUserIds, String openKfId) throws Exception {
		List<CorpKfServicerSessionRecord> corpKfServicerSessionRecords = corpKfServicerSessionRecordMapper.selectSessionByCondition(openKfId, followId, transUserIds, transNum);
		String corpId = yhlxWxCorpConfig.getCorpId();
		for (CorpKfServicerSessionRecord corpKfServicerSessionRecord : corpKfServicerSessionRecords) {
			//调用微信客服接口 变更会话状态
			try {
				wxCorpOps.transCorpKfSessionToServicer(corpId, corpKfServicerSessionRecord.getOpenKfId(), toFollowId, corpKfServicerSessionRecord.getExternalUserid(), 3);
				corpKfServicerSessionRecord.setServiceState(2);
				corpKfServicerSessionRecord.setCloseTime(DateTime.now());
				corpKfServicerSessionRecordMapper.updateById(corpKfServicerSessionRecord);

				CorpKfServicerSessionRecord newRecord = new CorpKfServicerSessionRecord();
				newRecord.setOpenKfId(openKfId);
				newRecord.setFromUserid(followId);
				newRecord.setChangeType(2);
				newRecord.setServicerUserid(toFollowId);
				newRecord.setExternalUserid(corpKfServicerSessionRecord.getExternalUserid());
				newRecord.setStartTime(corpKfServicerSessionRecord.getStartTime());
				newRecord.setMsgid(RandomUtil.randomString(16));
				newRecord.setServiceState(0);
				corpKfServicerSessionRecordMapper.insert(newRecord);
			} catch (Exception e) {
				String errorText = StringUtil.getErrorText(e);
				if (errorText.contains("conversation end")) {
					corpKfServicerSessionRecord.setServiceState(1);
					corpKfServicerSessionRecordMapper.updateById(corpKfServicerSessionRecord);
				}
			}
		}
	}

	@Override
	public List<CorpKfAccount> getCorpKfAccountByUId(long cmsUserId) {
		List<CorpKfAccount> corpKfAccounts = corpKfAccountMapper.selectList(null);
		List<String> followIds = corpFollowMapper.selectList(Wrappers.lambdaQuery(CorpFollow.class)
				.eq(CorpFollow::getCmsUserId, cmsUserId)).stream().map(CorpFollow::getUserid).collect(Collectors.toList());

		return corpKfAccounts.stream().filter(account -> {
			String openKfId = account.getOpenKfId();
			for (String followId : followIds) {
				Integer selectCount = corpKfAccountFollowMapper.selectCount(Wrappers.lambdaQuery(CorpKfAccountFollow.class)
						.eq(CorpKfAccountFollow::getOpenKfId, openKfId)
						.apply(String.format("JSON_CONTAINS(user_id_list,'\"%s\"')", followId)));
				if (selectCount > 0) {
					return true;
				}
			}
			return false;
		}).collect(Collectors.toList());
	}

	@Override
	public List<CorpKfServicerStateViewResp> getServicerStateList(String openKfId) throws Exception {
		List<CorpKfServicerStateViewResp> corpKfServicerStateViewResps = new ArrayList<>();
		CorpKfServicerResp corpKfServicerResp = wxCorpOps.getKfServicerList(yhlxWxCorpConfig.getCorpId(), openKfId);
		CorpKfServicerStateViewResp corpKfServicerStateViewResp = null;
		if (CollUtil.isNotEmpty(corpKfServicerResp.getServicerList())) {
			for (CorpKfServicerResp.CorpKfServicer corpKfServicer : corpKfServicerResp.getServicerList()) {
				corpKfServicerStateViewResp = new CorpKfServicerStateViewResp();
				String userid = corpKfServicer.getUserid();
				Integer status = corpKfServicer.getStatus();
				Integer stopType = corpKfServicer.getStop_type();
				corpKfServicerStateViewResp.setServicerUserid(userid);
				//接待状态
				Integer state = 0;
				//停止接待
				if (status == 1) {
					//暂时挂起
					if (stopType == 1) {
						state = 2;
					} else state = 1;
				}
				corpKfServicerStateViewResp.setState(state);
				corpKfServicerStateViewResps.add(corpKfServicerStateViewResp);
			}
		}
		return corpKfServicerStateViewResps;
	}

	/**
	 * 获取当前值班客服
	 */
	public Set<String> getThisHourDutyFollowIds(String openKfId) throws Exception {
		DateTime now = DateTime.now();
		String thisHourDate = DateUtil.format(now, "HH:mm");
		//星期天为1
		int dayOfWeek = DateUtil.dayOfWeek(now);
		//是否值班期
		List<CorpKfAccountFollow> corpKfAccountFollows = corpKfAccountFollowMapper.selectList(Wrappers.lambdaQuery(CorpKfAccountFollow.class)
				.eq(CorpKfAccountFollow::getOpenKfId, openKfId)
				.le(CorpKfAccountFollow::getStartTime, thisHourDate)
				.ge(CorpKfAccountFollow::getEndTime, thisHourDate)
				.eq(CorpKfAccountFollow::getDutyWeek, dayOfWeek));

		//目前时间段需要值班的客服
		Set<String> thisHourDutyFollows = new HashSet<>();
		for (CorpKfAccountFollow corpKfAccountFollow : corpKfAccountFollows) {
			thisHourDutyFollows.addAll(Jsons.parseList(corpKfAccountFollow.getUserIdList(), String.class));
		}
		return thisHourDutyFollows;
	}

	public CorpKfChangeFollowLogDto getCorpKfWeekChange(List<CorpKfAccountFollow> bfFollow, List<CorpKfAccountFollow> afFollow, Integer dutyWeek, Map<String, List<SysCorpFollowView>> followMap) {
		List<String> beforeSelect = new ArrayList<>();
		List<String> afterSelect = new ArrayList<>();

		Set<String> bfFollowSet = new HashSet<>();
		bfFollow.forEach(bf -> {
			try {
				bfFollowSet.addAll(Jsons.parseList(bf.getUserIdList(), String.class));
			} catch (Exception e) {
			}
		});

		Set<String> afFollowSet = new HashSet<>();
		afFollow.forEach(af -> {
			try {
				afFollowSet.addAll(Jsons.parseList(af.getUserIdList(), String.class));
			} catch (Exception e) {
			}
		});
		//移除
		for (String bfUserId : bfFollowSet) {
			if (!afFollowSet.contains(bfUserId)) {
				SysCorpFollowView sysCorpFollowView = followMap.get(bfUserId).get(0);
				beforeSelect.add(String.format("%s(%s)", sysCorpFollowView.getName(), sysCorpFollowView.getFollowName()));
			}
		}
		//增加
		for (String afUserId : afFollowSet) {
			if (!bfFollowSet.contains(afUserId)) {
				SysCorpFollowView sysCorpFollowView = followMap.get(afUserId).get(0);
				afterSelect.add(String.format("%s(%s)", sysCorpFollowView.getName(), sysCorpFollowView.getFollowName()));
			}
		}
		if (beforeSelect.size() > 0 || afterSelect.size() > 0) {
			CorpKfChangeFollowLogDto corpKfChangeFollowLogDto = new CorpKfChangeFollowLogDto();
			corpKfChangeFollowLogDto.setDutyWeek(dutyWeek);
			corpKfChangeFollowLogDto.setBeforeSelect(beforeSelect);
			corpKfChangeFollowLogDto.setAfterSelect(afterSelect);
			return corpKfChangeFollowLogDto;
		}
		return null;
	}
}
