package com.yihu.jw.service;/** * Created by nature of king on 2018/5/10. */ import com.alibaba.fastjson.JSONArray; import com.yihu.base.mysql.query.BaseJpaService; import com.yihu.jw.dao.ActivityDao; import com.yihu.jw.dao.TaskDao; import com.yihu.jw.dao.TaskPatientDetailDao; import com.yihu.jw.entity.health.bank.ActivityDO; import com.yihu.jw.entity.health.bank.TaskDO; import com.yihu.jw.entity.health.bank.TaskPatientDetailDO; import com.yihu.jw.restmodel.common.Envelop; import com.yihu.jw.rm.health.bank.HealthBankMapping; import com.yihu.jw.util.DateUtils; import com.yihu.jw.util.ISqlUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.BeanPropertyRowMapper; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.stereotype.Service; import javax.transaction.Transactional; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.Map; /** * @author wangzhinan * @create 2018-05-10 13:45 * @desc task service **/ @Service @Transactional public class TaskService extends BaseJpaService{ @Autowired private TaskDao taskDao; @Autowired private JdbcTemplate jdbcTemplate; @Autowired private TaskPatientDetailDao taskPatientDetailDao; @Autowired private ActivityDao activityDao; /** * 添加任务 * * @param taskDO 任务对象 * @return */ public Envelop insert(TaskDO taskDO){ taskDO.setCreateTime(new Date()); taskDO.setUpdateTime(new Date()); taskDao.save(taskDO); Envelop envelop = new Envelop<>(); envelop.setObj(true); return envelop; } /** * 更新任务 * * @param taskDO * @return */ public Envelop update(TaskDO taskDO){ String sql = ISqlUtils.getUpdateSql(taskDO); jdbcTemplate.update(sql); Envelop envelop = new Envelop<>(); envelop.setObj(true); return envelop; } /* public Envelop findByCondition(TaskDO taskDO, Integer page, Integer size) throws ParseException { String sql = new ISqlUtils().getSql(taskDO,page,size,"*"); List creditsDetailDOS = jdbcTemplate.query(sql,new BeanPropertyRowMapper(CreditsDetailDO.class)); String sqlcount = new ISqlUtils().getSql(creditsDetailDO,0,0,"count"); List> rstotal = jdbcTemplate.queryForList(sqlcount); Long count = 0L; if(rstotal!=null&&rstotal.size()>0){ count = (Long) rstotal.get(0).get("total"); } return Envelop.getSuccessListWithPage(HealthBankMapping.api_success, creditsDetailDOS,page,size,count); }*/ /** * 查找数据 * * @param taskDO 任务对象 * @param page 页码 * @param size 分页大小 * @return */ public Envelop selectByCondition(TaskDO taskDO,Integer page,Integer size){ String sql = new ISqlUtils().getSql(taskDO,page,size,"*"); List taskDOS = jdbcTemplate.query(sql,new BeanPropertyRowMapper(TaskDO.class)); for (TaskDO taskDO1:taskDOS){ if (taskDO1.getType().equalsIgnoreCase("ACTIVITY_TASK")){ ActivityDO activityDO = activityDao.findOne(taskDO1.getTransactionId()); taskDO1.setActivityDO(activityDO); } //参与活动的详情 String taskSql = "select * from wlyy_health_bank_task_patient_detail where task_id = '"+taskDO1.getId()+ "' and patient_openid = '"+taskDO.getOpenId()+"'"; List taskPatientDetailDOS = jdbcTemplate.query(taskSql,new BeanPropertyRowMapper(TaskPatientDetailDO.class)); taskDO1.setTaskPatientDetailDOS(taskPatientDetailDOS); //参与人数 String taskSql1 = "select count(1) as total from wlyy_health_bank_task_patient_detail where task_id = '"+taskDO1.getId()+"'"; List> rstotal = jdbcTemplate.queryForList(taskSql1); Long count = 0L; if(rstotal!=null&&rstotal.size()>0){ count = (Long) rstotal.get(0).get("total"); } taskDO1.setTotal(count); taskDO1.setPatientId(taskDO.getPatientId()); } String sqlcount = new ISqlUtils().getSql(taskDO,0,0,"count"); List> rstotal = jdbcTemplate.queryForList(sqlcount); Long count = 0L; if(rstotal!=null&&rstotal.size()>0){ count = (Long) rstotal.get(0).get("total"); } return Envelop.getSuccessListWithPage(HealthBankMapping.api_success, taskDOS,page,size,count); } /** * 查看当前的任务 * @param array taskCode集合 * @param patientId 居民id * @param page 页码 * @param size 分页大小 * @return */ public Envelop selectByTask(JSONArray array,String patientId,Integer page,Integer size){ StringBuffer buffer = new StringBuffer(); List taskCodes = new ArrayList<>(); for (int i=0;i taskDOList = jdbcTemplate.query(sql,new BeanPropertyRowMapper(TaskDO.class)); for (TaskDO taskDO:taskDOList){ if (taskDO.getType().equalsIgnoreCase("ACTIVITY_TASK")){ ActivityDO activityDO = activityDao.findOne(taskDO.getTransactionId()); taskDO.setActivityDO(activityDO); } if (taskDO.getPeriod() == 1){ String taskSql = "select * from wlyy_health_bank_task_patient_detail where task_id = '"+taskDO.getId()+ "' and patient_id = '"+patientId+"'"; List taskPatientDetailDOS = jdbcTemplate.query(taskSql,new BeanPropertyRowMapper(TaskPatientDetailDO.class)); taskDO.setTaskPatientDetailDOS(taskPatientDetailDOS); }else if (taskDO.getPeriod() == 0){ String taskSql = "select * from wlyy_health_bank_task_patient_detail where task_id = '"+taskDO.getId()+ "' and patient_id = '"+patientId+"' and create_time > '" + DateUtils.getDayBegin() +"' and create_time < '"+ DateUtils.getDayEnd() +"'"; List taskPatientDetailDOS = jdbcTemplate.query(taskSql,new BeanPropertyRowMapper(TaskPatientDetailDO.class)); taskDO.setTaskPatientDetailDOS(taskPatientDetailDOS); } taskDO.setPatientId(patientId); } String sqlcount = "select COUNT(1) AS total from wlyy_health_bank_task bt where "+buffer ; List> rstotal = jdbcTemplate.queryForList(sqlcount); Long count = 0L; if(rstotal!=null&&rstotal.size()>0){ count = (Long) rstotal.get(0).get("total"); } return Envelop.getSuccessListWithPage(HealthBankMapping.api_success, taskDOList,page,size,count); } /* public List getTasks(String patientId){ List taskDOList = new ArrayList<>(); TaskDO taskDO = new TaskDO(); *//*taskDO.setPatientId(patientId);*//* taskDO.setTaskCode("BIND"); taskDO.setPeriod(1); taskDO.setTaskTitle("首次绑定"); taskDO.setTaskContent("(使用社区发放的已关联您身份信息的设备,登录厦门i健康绑定设备)"); taskDO.setTradeType("activity"); taskDO.setTransactionId("402885e96324a409016324c0a45a0006"); taskDO.setCreateTime(new Date()); taskDO.setUpdateTime(new Date()); taskDOList.add(taskDO); TaskDO taskDO1 = new TaskDO(); *//* taskDO1.setPatientId(patientId);*//* taskDO1.setTaskCode("MEASURE"); taskDO1.setPeriod(0); taskDO1.setTaskTitle("每日测量"); taskDO1.setTaskContent("(使用社区发放的已关联您身份信息的设备,绑定后每天完成测量)"); taskDO1.setTradeType("activity"); taskDO1.setTransactionId("402885e96324a409016324c0a45a0006"); taskDO1.setCreateTime(new Date()); taskDO1.setUpdateTime(new Date()); taskDOList.add(taskDO1); return taskDOList; }*/ }