package com.bzd.quartz.task;

import com.bzd.common.config.dao.DaoSupport;
import com.bzd.common.config.dao.PageData;
import com.bzd.common.constant.ScheduleConstants;
import com.bzd.common.utils.DateUtils;
import com.bzd.quartz.domain.SysJob;
import com.bzd.quartz.service.ISysJobService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.bzd.common.utils.StringUtils;

import javax.annotation.Resource;
import java.sql.Date;
import java.util.List;

import static com.bzd.common.utils.ShiroUtils.getSysUser;

/**
 * 定时任务调度测试
 *
 * @author LiXiagnFei
 */
@Component("ryTask")
public class RyTask
{
    @Resource(name = "daoSupport")
    private DaoSupport daoSupport;
    @Autowired
    ISysJobService jobService;
    public void ryMultipleParams(String s, Boolean b, Long l, Double d, Integer i)
    {
        System.out.println(StringUtils.format("执行多参方法： 字符串类型{}，布尔类型{}，长整型{}，浮点型{}，整形{}", s, b, l, d, i));
    }

    public void ryParams(String params)
    {
        System.out.println("执行有参方法：" + params);
    }

    public void ryNoParams()
    {
        System.out.println("执行无参方法");
    }

    //购药处方登记成功后如果是随访品 触发计划和执行任务
    //触发计划
    public void triggerPLan(String action,String mdmCode, String storeId, String patientId, String templateId, Integer planId) throws Exception {
        // 方法实现
        System.out.println("触发计划了=========>执行有参方法:triggerPLan with parameters: " + mdmCode + "," + action + ", " + storeId + ", " + patientId + ", " + templateId + ", " + planId);
        // 这里添加具体的业务逻辑
        System.out.println("执行时间:"+DateUtils.getTime());
        PageData plan=new PageData();
        plan.put("planId",planId);
        plan.put("storeId",storeId);
        plan.put("patientId",patientId);
        plan.put("status",1);//0=关闭 1=开启/进行中，2=默认/已创建待开启
        plan.put("updatedTime",DateUtils.getTime());
        plan.put("updatedBy","系统触发");
        int planResult= daoSupport.update("followTaskMapper.openPlanByPlanId", plan);
        System.out.println("触发计划:更新方法<openPlanByPlanId(plan)>：plan="+plan+"结果:"+planResult);
    }
    //触发任务
    public void triggerTask(String appointmentDate, String nodeFlag, String action, String mdmCode, String storeId , String patientId , String templateId , Integer planId , Integer taskId)throws Exception {
        System.out.println("触发任务了=========>执行有参方法:triggerTask parameters"+nodeFlag+action+mdmCode+storeId+patientId+templateId+planId+taskId);
        System.out.println("执行时间:"+DateUtils.getTime());
        //        ST 时间条件+单次节点
        //        PT 时间条件+周期节点
        //        SE 事件条件+单次节点
        //        PE 事件条件+周期节点
        //        生成任务 action=create
        //        执行任务 action=execute
        if("create".equals(action)){
            if("PT".equals(nodeFlag) || "PE".equals(nodeFlag)){
                updateJobStatusByTaskId(taskId,patientId,storeId);
            }
            PageData    task=new PageData();
            task.put("planId",planId);
            task.put("taskId",taskId);
            task.put("storeId",storeId);
            task.put("patientId",patientId);
            task.put("taskStatus","待执行");//待执行,已下发，已完成，未完成，未下发，已取消
            task.put("updatedAt",DateUtils.getTime());
            task.put("operator","系统触发");
            task.put("appointmentDate",appointmentDate);//预约日期
            System.out.println("触发任务:更新方法<openTaskByTaskId(task)>：预约日期="+appointmentDate);
            int taskResult= daoSupport.update("followTaskMapper.openTaskByTaskId", task);
            System.out.println("触发任务:更新方法<openTaskByTaskId(task)>：task="+task+"结果:"+taskResult);
        }if("execute".equals(action)){
            System.out.println("执行时间:发短信了,你好:小爱同学,需要什么帮助吗,你的药吃完了没?"+DateUtils.getTime()+"古币啊");
        }

    }
    /**
     * 更新任务状态并递减剩余执行次数。
     *
     * @param taskId 任务ID
     */
    public void updateJobStatusByTaskId(Integer taskId, String storeId, String patientId) throws Exception {
        PageData    task=new PageData();
        task.put("taskId",taskId);
        task.put("storeId",storeId);
        task.put("patientId",patientId);

        PageData    jobResult= (PageData)daoSupport.findForObject("followTaskMapper.getJobByTaskId", task);

        if (StringUtils.isNotNull(jobResult)) {
         int getRemainingExecutions=(int)   jobResult.get("remainingExecutions");
         int currentExecutionCount=(int)   jobResult.get("currentExecutionCount");
            if (getRemainingExecutions>0) {
                jobResult.put("remainingExecutions",getRemainingExecutions - 1);
                jobResult.put("currentExecutionCount",currentExecutionCount + 1);
                jobResult.put("updateTime",DateUtils.getTime());
                jobResult.put("updateBy","SYSTEM");
                jobResult.put("taskId",taskId);
                jobResult.put("storeId",storeId);
                jobResult.put("patientId",patientId);
                int ret= daoSupport.update("followTaskMapper.updateJobStatusByTaskId", jobResult);
                System.out.println("执行更行方法:更新任务状态并递减剩余执行次数<updateJobStatusByTaskId(String taskId)>：taskId="+taskId+"结果:"+ret);
            }
        }

    }
}
