package com.rongwei.bscommon.sys.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ArrayUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rongwei.bscommon.sys.dao.SrmIssuesListDao;
import com.rongwei.bscommon.sys.service.EmailService;
import com.rongwei.bsentity.domin.SrmIssuesListDo;
import com.rongwei.commonservice.service.RedisService;
import com.rongwei.rwadmincommon.system.vo.SysUserVo;
import com.rongwei.wfentity.sys.vo.RunTaskVo;
import com.rongwei.wfserver.wfcommon.sys.service.RunTaskService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * @author ：trj
 * @description： 邮件业务配置服务
 * @date ：2023/3/29 15:08
 */
@Service("EmailService")
@Slf4j
public class EmailServiceImpl extends ServiceImpl<SrmIssuesListDao, SrmIssuesListDo> implements EmailService {

    @Autowired
    private RunTaskService runTaskService;
    @Autowired
    private RedisService redisService;



    @Override
    public void taskSpaAudit(String id) {

        List<RunTaskVo> taskByProInstId = runTaskService.getTaskByProInstId(id);

        AtomicBoolean flag = new AtomicBoolean(false);

        taskByProInstId.forEach(task -> {
            // 获取 当前任务中的待办节点
            if (task.getTaskstatus().equals("20")) {
                // 待办节点 ==》spa跟踪
                if (task.getTaskcode().equals("userTask___1678415436338")) {
                    flag.set(true);
                }
            }
        });

        if (flag.get()) {

            HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            String token = request.getHeader("token");
            SysUserVo currUser =  redisService.getLoginUser(token);

            UpdateWrapper<SrmIssuesListDo> update = new UpdateWrapper();
            update.set("TASKSPASUBMISSIONTIME", new Date())
                    .set("MODIFYDATE", new Date())
                    .set("MODIFYUSERID", currUser.getId())
                    .set("MODIFYUSERNAME", currUser.getName())
                    .eq("PROCESSINSTID", id);
            baseMapper.update(new SrmIssuesListDo(), update);
        }
    }

    @Override
    public void taskSPATrack() {
        // 查询已走到 SPA跟踪 节点的问题list
        List<SrmIssuesListDo> proIds = baseMapper.getProblemByTaskRun();

        List<String> ids = new ArrayList<>();

        proIds.forEach(item -> {
            Date taskDate = DateUtil.offsetDay(item.getTASKSPASUBMISSIONTIME(), Integer.parseInt(item.getFollowupdays()));

            //判断当前时间 是否和 跟踪执行时间（任务SPA时间+跟踪天数） 相等
            if (DateUtil.betweenDay(taskDate, new Date(), true) == 0) {
                //已经到期
                ids.add(item.getId());
            }
        });

        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String token = request.getHeader("token");
        SysUserVo currUser = redisService.getLoginUser(token);

        if(ArrayUtil.isNotEmpty(ids)){
            UpdateWrapper<SrmIssuesListDo> update = new UpdateWrapper<>();
            update.set("ISMATURITY", 200)
                    .set("DAYSMATURITYTIME", new Date())
                    .set("MODIFYDATE", new Date())
                    .set("MODIFYUSERID", currUser.getId())
                    .set("MODIFYUSERNAME", currUser.getName())
                    .in("ID", ids);
            baseMapper.update(new SrmIssuesListDo(), update);
        }


    }

    @Override
    public void planSaveSendMail(String id) {
        // 查询流程节点
    }
}
