package com.fs.qw.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fs.common.utils.PubFun;
import com.fs.qw.vo.WxSopRuleTimeVO;
import com.fs.sop.domain.QwSop;
import com.fs.sop.domain.QwSopTemp;
import com.fs.sop.domain.SopUserLogsWx;
import com.fs.sop.mapper.QwSopMapper;
import com.fs.sop.mapper.QwSopTempMapper;
import com.fs.sop.service.ISopUserLogsWxService;
import com.fs.wxUser.domain.CompanyWxUser;
import com.fs.wxUser.mapper.CompanyWxUserMapper;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@Service
@AllArgsConstructor
public class AsyncWxSopService {

    private final QwSopMapper qwSopMapper;
    private final QwSopTempMapper qwSopTempMapper;
    private final CompanyWxUserMapper companyWxUserMapper;
    private final ISopUserLogsWxService sopUserLogsWxService;

    /**
     * 立即执行SOP任务
     */
    @Async("scheduledExecutorService")
    public void scheduledWxSopService(String[] ids) {
        try {
            List<WxSopRuleTimeVO> ruleTimeVOList = qwSopMapper.executeSopWxByIds(ids);
            List<QwSopTemp> tempList = qwSopTempMapper.selectListByIds(PubFun.listToNewList(ruleTimeVOList, WxSopRuleTimeVO::getTempId));
            Map<String, QwSopTemp> tempMap = PubFun.listToMapByGroupObject(tempList, QwSopTemp::getId);
            ruleTimeVOList = ruleTimeVOList.stream().filter(e -> tempMap.containsKey(e.getTempId())).collect(Collectors.toList());
            List<String> updateIdList = ruleTimeVOList.stream().filter(e -> "0".equals(tempMap.get(e.getTempId()).getStatus())).map(WxSopRuleTimeVO::getId).collect(Collectors.toList());
            if(!updateIdList.isEmpty()){
                QwSop qwSop = new QwSop();
                qwSop.setStatus(0L);
                qwSopMapper.update(qwSop, new QueryWrapper<QwSop>().in("id", updateIdList));
            }
            ruleTimeVOList.forEach(ruleTimeVO -> {
                QwSopTemp qwSopTemp = tempMap.get(ruleTimeVO.getTempId());
                if (!qwSopTemp.getStatus().equals("0")) {
                    processInternal(ruleTimeVO, qwSopTemp);
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
            log.error("立即执行执行失败", e);
        }
    }

    private void processInternal(WxSopRuleTimeVO timeVO, QwSopTemp temp) {
        List<CompanyWxUser> companyWxClients = companyWxUserMapper.listCompanyIds(timeVO.getQwUserIds().split(","));
        List<SopUserLogsWx> collect = companyWxClients.stream().map(e -> {
            SopUserLogsWx entity = new SopUserLogsWx();
            entity.setSopId(timeVO.getId());
            entity.setSopTempId(temp.getId());
            entity.setUserWxName(e.getNickName());
            entity.setAccountId(e.getAccountId());
            entity.setCompanyUserId(e.getCompanyUserId());
            entity.setStartTime(timeVO.getStartTime());
            return entity;
        }).collect(Collectors.toList());
        sopUserLogsWxService.insertList(collect);
    }

}
