package com.yihu.wlyy.statistics.job.message;

import com.yihu.wlyy.statistics.dao.MessageDao;
import com.yihu.wlyy.statistics.model.signfamily.Message;
import com.yihu.wlyy.statistics.task.PushMsgTask;
import com.yihu.wlyy.statistics.util.DateUtil;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.context.support.SpringBeanAutowiringSupport;

import javax.transaction.Transactional;
import java.util.*;


/**
 * Created by hzp on 2017.1.4.
 */
@Component
@Scope("prototype")
public class FollowupPlanJob implements Job {
    @Autowired
    JdbcTemplate jdbcTemplate;

    @Value("${systemConfig.msg_push_server}")
    private String url;

    @Autowired
    private MessageDao messageDao;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
        System.out.print("follow plan message sending...");

        String date = DateUtil.dateToStrShort(new Date());
        //发送随访计划消息
        sendMessage(date);

        System.out.print("follow plan message send over.");
    }

    /**
     * 每日发送随访计划提醒消息
     */
    @Transactional
    public void sendMessage(String date)
    {
        try{
            String start = date +" 00:00:00";
            String end = date +" 23:59:59";

            String sql = "select doctor_code,count(1) count from wlyy_followup where status not in ('0','1') and followup_plan_date>='"+start+"' and followup_plan_date<='"+end+"' group by doctor_code";
            //获取所有未执行随访计划
            List<Map<String,Object>> followupToday = jdbcTemplate.queryForList(sql);

            if(followupToday!=null)
            {
                List<Message> list = new ArrayList<>();
                for(Map<String,Object> map:followupToday)
                {
                    String doctor = String.valueOf(map.get("doctor_code"));
                    String count = String.valueOf(map.get("count"));

                    // 添加签约消息
                    String title = "随访计划提醒";
                    String content = "您今日有" +count+"个随访计划待处理";
                    Message message = new Message();
                    message.setCode(UUID.randomUUID().toString());
                    message.setCzrq(new Date());
                    message.setCreateTime(new Date());
                    message.setContent(content);
                    message.setRead(1);//设置未读
                    message.setReceiver(doctor);//设置接受医生的code
                    message.setSender("system");//设置发送的用户
                    message.setTitle(title);
                    message.setType(4);//随访计划提醒
                    message.setReadonly(1);//是否只读消息
                    list.add(message);
                    // 推送消息给医生
                    PushMsgTask.url= url;
                    // 推送消息给医生
                    PushMsgTask.getInstance().put(doctor,"4",title,content,"");
                }

                messageDao.save(list);
            }


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