浏览代码

代码提交

chenweida 8 年之前
父节点
当前提交
11b348f010

+ 16 - 0
patient-co-statistics/src/main/java/com/yihu/wlyy/statistics/controller/JobController.java

@ -451,4 +451,20 @@ public class JobController extends BaseController {
            return invalidUserException(e, -1, "启动失败:" + e.getMessage());
        }
    }
    /**
     * 启动通知任务
     *
     * @return
     */
    @ApiOperation(value = "停止通知任务")
    @RequestMapping(value = "stopNoticeJob", method = RequestMethod.GET)
    public String stopNoticeJob() {
        try {
            jobService.stopNoticeJob();
            return success("启动成功!");
        } catch (Exception e) {
            error(e);
            return invalidUserException(e, -1, "启动失败:" + e.getMessage());
        }
    }
}

+ 0 - 17
patient-co-statistics/src/main/java/com/yihu/wlyy/statistics/dao/NoticeDao.java

@ -1,17 +0,0 @@
package com.yihu.wlyy.statistics.dao;
import com.yihu.wlyy.statistics.model.message.Notice;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;
import java.util.List;
/**
 * Created by Administrator on 2016/11/7.
 */
public interface NoticeDao extends PagingAndSortingRepository<Notice, Long>, JpaSpecificationExecutor<Notice> {
    @Query("select p from Notice p where p.toType=1 and p.status=1")
    List<Notice> findAllDoctorNoRead();
}

+ 44 - 70
patient-co-statistics/src/main/java/com/yihu/wlyy/statistics/job/message/NoticeJob.java

@ -1,14 +1,12 @@
package com.yihu.wlyy.statistics.job.message;
import com.yihu.wlyy.statistics.dao.NoticeDao;
import com.yihu.wlyy.statistics.dao.DoctorDao;
import com.yihu.wlyy.statistics.dao.QuartzJobLogDao;
import com.yihu.wlyy.statistics.etl.storage.DBStorage;
import com.yihu.wlyy.statistics.job.business.QuartzHelper;
import com.yihu.wlyy.statistics.model.doctor.Doctor;
import com.yihu.wlyy.statistics.model.job.QuartzJobLog;
import com.yihu.wlyy.statistics.model.message.Notice;
import com.yihu.wlyy.statistics.service.DoctorWorkTimeService;
import com.yihu.wlyy.statistics.task.PushMsgTask;
import com.yihu.wlyy.statistics.util.JsonUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.quartz.Job;
@ -19,7 +17,6 @@ import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
@ -33,17 +30,17 @@ import java.util.Map;
@Scope("prototype")
public class NoticeJob implements Job {
    public static String jobKey = "Notice_JOB";
    public static String jobCron = "0 0 9,15,20 * * ?"; //每天9点,15点,20 点执行一次
    private static String[] noticeKey = {"no", "NO_ZX", "NO_QY", "NO_JKZD"};
    private static String[] noticeTitle = {"no", "咨询消息", "签约消息", "健康指导消息"};
    private static String[] noticeContent = {"no", "你有[SIZE]条未读的咨询消息", "你有[SIZE]条未读的签约消息", "你有[SIZE]条未读的健康指导消息"};
    @Autowired
    private NoticeDao noticeDao;
    private static String[] noticeTitle = {"no", "咨询消息", "签约消息", "体征指标"};
    private static String[] noticeContent = {"no", "您有[SIZE]条新的咨询消息", "您有[SIZE]条新的签约消息", "您有[SIZE]条新的体征指标"};
    @Autowired
    private DoctorWorkTimeService doctorWorkTimeService;
    @Autowired
    private QuartzHelper quartzHelper;
    @Autowired
    private QuartzJobLogDao dbStorage;
    @Autowired
    private DoctorDao doctorDao;
    @Transactional
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
@ -53,38 +50,25 @@ public class NoticeJob implements Job {
        quartzJobLog.setJobId(jobKey);
        quartzJobLog.setJobName(jobKey);
        try {
            Date nextStartTime = null;
            List<Doctor> doctors = doctorDao.findAllDoctors();
            //得到所有未发送的医生的通知
            List<Notice> noticeList = noticeDao.findAllDoctorNoRead();
            Map<String, Map<String, Integer>> maps = new HashMap<>();
            //发送通知
            for (Notice notice : noticeList) {
            for (Doctor doctor : doctors) {
                //判断医生是不是在工作时间内
                if ("1".equals(doctorWorkTimeService.isDoctorWorking(notice.getToUid(), -1).get("status"))) {
                    //医生在工作时间内
                    Map<String, Integer> size = maps.get(notice.getToUid());
                    if (size == null) {
                        size = new HashMap<>();
                        size.put(notice.getType(), 1);
                    } else {
                        Integer num = size.get(notice.getType());
                        if (num == null) {
                            size.put(notice.getType(), 1);
                        } else {
                            size.put(notice.getType(), num++);
                        }
                    }
                    maps.put(notice.getToUid(), size);
                    notice.setStatus("0");
                } else {
                    //得到这个医生的下次工作时间
                    Date workTime = doctorWorkTimeService.getDoctorNextWork(notice.getToUid());
                    if(nextStartTime==null){
                        nextStartTime=workTime;
                    }
                    if (workTime.before(nextStartTime)) {
                        //如果医生的工作时间在下次工作之前,那就把医生的工作时间作为下次的执行时间
                        nextStartTime = workTime;
                if ("1".equals(doctorWorkTimeService.isDoctorWorking(doctor.getCode(), -1).get("status"))) {
                    Map<String,Integer> map=new HashMap<>();
                    //得到医生多少条咨询消息
                    Integer size=getDoctorMesssageCount(doctor.getCode(),1);
                    addData(map, size, "1");
                    //得到医生多少条签约消息
                    size=getDoctorMesssageCount(doctor.getCode(),2);
                    addData(map, size, "2");
                    //得到医生多少条体征消息
                    size=getDoctorMesssageCount(doctor.getCode(),3);
                    addData(map, size, "3");
                    if(map.size()>0){
                        maps.put(doctor.getCode(),map);
                    }
                }
            }
@ -104,46 +88,36 @@ public class NoticeJob implements Job {
            }
            // 推送消息给医生
            PushMsgTask.getInstance().put(jsonArray);
            //判断是否任务在执行
            if (noticeList.size() != 0) {
                //判断下次任务执行时间
                if (nextStartTime!=null&&nextStartTime.before(new Date())) {
                    //如果在现在之前 就立马执行
                    quartzHelper.startNow(NoticeJob.class, NoticeJob.jobKey+new Date().getTime(), new HashMap<>());
                } else {
                    //如果在现在之前 就立马执行
                    quartzHelper.startAt(nextStartTime, NoticeJob.class, NoticeJob.jobKey+nextStartTime.getTime(), new HashMap<>());
                }
            } else {
                //如果没有数据延迟半小时
                long curren = System.currentTimeMillis();
                curren += 30 * 60 * 1000;
                Date da = new Date(curren);
                quartzHelper.startAt(da, NoticeJob.class, NoticeJob.jobKey+curren, new HashMap<>());
            }
            //保存日志
            quartzJobLog.setJobEndTime(new Date());
            quartzJobLog.setJobContent("通知成功");
            quartzJobLog.setJobType("1");
            dbStorage.save(quartzJobLog);
        } catch (Exception e) {
            try {
                //保存日志
                quartzJobLog.setJobEndTime(new Date());
                quartzJobLog.setJobContent("通知失败");
                quartzJobLog.setJobType("0");
                dbStorage.save(quartzJobLog);
                //如果失败的话延迟10分钟执行
                long curren = System.currentTimeMillis();
                curren += 10 * 60 * 1000;
                Date da = new Date(curren);
                quartzHelper.startAt(da, NoticeJob.class, NoticeJob.jobKey+curren, new HashMap<>());
            } catch (Exception e1) {
                e1.printStackTrace();
            }
            //保存日志
            quartzJobLog.setJobEndTime(new Date());
            quartzJobLog.setJobContent("通知失败");
            quartzJobLog.setJobType("0");
            dbStorage.save(quartzJobLog);
            e.printStackTrace();
        }
    }
    private void addData(Map<String, Integer> map, Integer size, String key) {
        if (size > 0) {
            map.put(key, size);
        }
    }
    /**
     * 得到医生的未读数目
     * @param code 医生code
     * @param type 1是咨询 2个签约 3是体征
     * @return
     */
    private Integer getDoctorMesssageCount(String code, int type) {
        return 0;
    }
}

+ 0 - 90
patient-co-statistics/src/main/java/com/yihu/wlyy/statistics/model/message/Notice.java

@ -1,90 +0,0 @@
package com.yihu.wlyy.statistics.model.message;
import com.yihu.wlyy.statistics.model.IdEntity;
import javax.persistence.Entity;
import javax.persistence.Table;
import java.util.Date;
/**
 * Created by Administrator on 2016/11/7.
 * 系通通知
 */
@Entity
@Table(name = "wlyy_notice")
public class Notice extends IdEntity {
    private String toUid;//接受人code
    private String content;//通知内容
    private String title;//消息标题
    private String data;//消息数据
    private String type;//通知类型 1咨询 2签约 3健康指导
    private Date createTime;//创建时间
    private String status;//是否发送 1 否 0是
    private String toType;//接受人类型 1:医生 2患者
    public String getToUid() {
        return toUid;
    }
    public void setToUid(String toUid) {
        this.toUid = toUid;
    }
    public String getContent() {
        return content;
    }
    public void setContent(String content) {
        this.content = content;
    }
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public String getData() {
        return data;
    }
    public void setData(String data) {
        this.data = data;
    }
    public String getType() {
        return type;
    }
    public void setType(String type) {
        this.type = type;
    }
    public Date getCreateTime() {
        return createTime;
    }
    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }
    public String getStatus() {
        return status;
    }
    public void setStatus(String status) {
        this.status = status;
    }
    public String getToType() {
        return toType;
    }
    public void setToType(String toType) {
        this.toType = toType;
    }
}

+ 1 - 1
patient-co-statistics/src/main/java/com/yihu/wlyy/statistics/service/JobService.java

@ -459,7 +459,7 @@ public class JobService {
    public void startNoticeJob() throws Exception {
        if(!quartzHelper.isExistJob(NoticeJob.jobKey)){
            quartzHelper.startNow(NoticeJob.class,NoticeJob.jobKey,new HashMap<>());
            quartzHelper.addJob(NoticeJob.class,NoticeJob.jobCron,NoticeJob.jobKey,new HashMap<>());
        }else{
            throw new Exception("已经启动");
        }