package com.jpsoft.shinestar.scheduled;

import cn.hutool.core.date.DateUtil;
import com.jpsoft.shinestar.config.WxConfig;
import com.jpsoft.shinestar.modules.base.entity.PersonInfo;
import com.jpsoft.shinestar.modules.base.entity.PersonPopedom;
import com.jpsoft.shinestar.modules.base.service.PersonInfoService;
import com.jpsoft.shinestar.modules.base.service.PersonPopedomService;
import com.jpsoft.shinestar.modules.business.dao.RequestForLeaveInfoDAO;
import com.jpsoft.shinestar.modules.business.dto.ExaminePersonDTO;
import com.jpsoft.shinestar.modules.business.entity.RequestForLeaveInfo;
import com.jpsoft.shinestar.modules.business.service.RequestForLeaveInfoService;
import com.jpsoft.shinestar.modules.common.dto.Sort;
import com.jpsoft.shinestar.modules.common.utils.JwtUtil2;
import com.jpsoft.shinestar.modules.common.utils.WechatMessageUtil;
import com.jpsoft.shinestar.modules.workflow.entity.ProcessTemplate;
import com.jpsoft.shinestar.modules.workflow.entity.TemplateDepartment;
import com.jpsoft.shinestar.modules.workflow.service.ProcessSetService;
import com.jpsoft.shinestar.modules.workflow.service.ProcessTemplateService;
import com.jpsoft.shinestar.modules.workflow.service.TemplateDepartmentService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @author 墨鱼_mo
 * @date 2021-6-16 14:27
 */
@Component
@Slf4j
@Transactional
public class CancelLeaveTask {

    @Value("${jwt.secret}")
    private String jwtSecret;

    @Autowired
    private WxConfig wxConfig;

    @Autowired
    private PersonPopedomService personPopedomService;

    @Autowired
    private TemplateDepartmentService templateDepartmentService;

    @Autowired
    private ProcessTemplateService processTemplateService;

    @Autowired
    private ProcessSetService processSetService;

    @Autowired
    private RequestForLeaveInfoService requestForLeaveInfoService;

    @Autowired
    private RequestForLeaveInfoDAO requestForLeaveInfoDAO;

    @Autowired
    private PersonInfoService personInfoService;

    /**
     * 每天中午12点检查需要销假但未销假的记录
     */
    @Scheduled(cron = "0 00 12 ? * *")
    public  void run() {

        Map<String,Object> searchParams = new HashMap<>();
        searchParams.put("status","1");
        searchParams.put("endTime1", DateUtil.offsetDay(new Date(),-1));
        searchParams.put("endTime2",new Date());
        searchParams.put("leaveTypeSt","2,5,6");
        searchParams.put("cancelStatus","5");
        searchParams.put("enableCancel",true);
        List<Sort> sortList = new ArrayList<>();
        sortList.add(new Sort("create_time","desc"));
        List<RequestForLeaveInfo> requestForLeaveInfoList = requestForLeaveInfoDAO.search(searchParams,sortList);
        String names = "";
        if (requestForLeaveInfoList.size()>0){
            for (RequestForLeaveInfo requestForLeaveInfo : requestForLeaveInfoList){

                PersonPopedom personPopedom = personPopedomService.get(requestForLeaveInfo.getPersonPopedomId());

                PersonInfo personInfo = personInfoService.get(requestForLeaveInfo.getPersonId());
                if (StringUtils.isNotBlank(names)){
                    names = names + "," + personInfo.getName();
                }else{
                    names = personInfo.getName();
                }

                try{
                    String accessToken = JwtUtil2.createToken(jwtSecret, personInfo.getId() + "", DateTime.now().plusHours(30 * 24).toDate());
                    WechatMessageUtil.sendEvenMessage(personInfo.getOpenId(), "有一条请假信息待销假", personInfo.getName(), com.jpsoft.shinestar.modules.common.utils.StringUtils.getApprovalNo(), "请前往处理", wxConfig.getPortalUrl() + "xsy/approval/infosp?id=" + requestForLeaveInfo.getId() + "&procinstActUserId=" + requestForLeaveInfo.getId() + "&formId=" + "leaveCancelWork" + "&token=" + accessToken, wxConfig.getAppId(), wxConfig.getAppSecret());
                }catch (Exception ex){
                    ex.printStackTrace();
                }


                try{
                    //向审核人发送消息

                    TemplateDepartment templateDepartment = templateDepartmentService.findByFormId("leaveCancelWork");
                    if (templateDepartment != null) {
                        ProcessTemplate processTemplate = processTemplateService.get(templateDepartment.getProcessTemplateId());

                        ExaminePersonDTO examinePersonDTO = processSetService.getExaminePerson(null, processTemplate.getId(), personPopedom, 1,null);

                        List<String> personIdList = Arrays.asList(examinePersonDTO.getPersonIds().split(","));
                        if (personIdList.size() > 0) {
                        //    new Thread(()->{
                                for (String personId : personIdList) {
                                    try{
                                        PersonInfo personInfo0 = personInfoService.get(Long.parseLong(personId));
                                        //提交人
                                        PersonInfo personInfo1 = personInfoService.get(personPopedom.getPersonId());
                                        String accessToken = JwtUtil2.createToken(jwtSecret, personInfo0.getId() + "", DateTime.now().plusHours(30 * 24).toDate());
                                        WechatMessageUtil.sendEvenMessage(personInfo0.getOpenId(), "待销假信息提醒", personInfo1.getName(), com.jpsoft.shinestar.modules.common.utils.StringUtils.getApprovalNo(), "请登录电脑端处理", wxConfig.getPortalUrl() + "xsy/approval/infosp?id=" + requestForLeaveInfo.getId() + "&procinstActUserId=" + requestForLeaveInfo.getId() + "&formId=" + "leaveCancelWork" + "&token=" + accessToken, wxConfig.getAppId(), wxConfig.getAppSecret());
                                    }catch (Exception ex){
                                        ex.printStackTrace();
                                    }

                                }
                       //     }).start();

                        }
                    }
                }catch (Exception ex){
                    ex.printStackTrace();
                }



            }




        }

    }
}
