TaskPatientDtailService.java 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. package com.yihu.jw.service;/**
  2. * Created by nature of king on 2018/6/8.
  3. */
  4. import com.yihu.jw.dao.AccountDao;
  5. import com.yihu.jw.dao.ActivityDao;
  6. import com.yihu.jw.dao.TaskDao;
  7. import com.yihu.jw.dao.TaskPatientDetailDao;
  8. import com.yihu.jw.entity.health.bank.*;
  9. import com.yihu.jw.entity.util.AesEncryptUtils;
  10. import com.yihu.jw.restmodel.web.MixEnvelop;
  11. import com.yihu.jw.rm.health.bank.HealthBankMapping;
  12. import com.yihu.jw.util.ISqlUtils;
  13. import com.yihu.mysql.query.BaseJpaService;
  14. import org.slf4j.Logger;
  15. import org.slf4j.LoggerFactory;
  16. import org.springframework.beans.factory.annotation.Autowired;
  17. import org.springframework.jdbc.core.BeanPropertyRowMapper;
  18. import org.springframework.jdbc.core.JdbcTemplate;
  19. import org.springframework.stereotype.Service;
  20. import javax.transaction.Transactional;
  21. import java.util.Date;
  22. import java.util.List;
  23. import java.util.Map;
  24. /**
  25. * @author wangzhinan
  26. * @create 2018-06-08 15:43
  27. * @desc 任务参与 Service
  28. **/
  29. @Service
  30. @Transactional
  31. public class TaskPatientDtailService extends BaseJpaService<TaskPatientDetailDO,TaskPatientDetailDao> {
  32. private Logger logger = LoggerFactory.getLogger(TaskPatientDtailService.class);
  33. @Autowired
  34. private TaskPatientDetailDao taskPatientDetailDao;
  35. @Autowired
  36. private JdbcTemplate jdbcTemplate;
  37. @Autowired
  38. private AccountDao accountDao;
  39. @Autowired
  40. private TaskDao taskDao;
  41. @Autowired
  42. private ActivityDao activityDao;
  43. /**
  44. * 查看任务参与情况
  45. *
  46. * @param taskPatientDetailDO 任务参与对象
  47. *
  48. * @param page 页码
  49. * @param size 分页大小
  50. * @return
  51. */
  52. public MixEnvelop<TaskRangDO, TaskRangDO> selectByCondition(TaskPatientDetailDO taskPatientDetailDO, Integer page, Integer size){
  53. String sql = new ISqlUtils().getSql(taskPatientDetailDO,page,size,"*");
  54. List<TaskPatientDetailDO> taskPatientDetailDOS = jdbcTemplate.query(sql,new BeanPropertyRowMapper(TaskPatientDetailDO.class));
  55. String sqlcount = new ISqlUtils().getSql(taskPatientDetailDO,0,0,"count");
  56. List<Map<String,Object>> rstotal = jdbcTemplate.queryForList(sqlcount);
  57. Long count = 0L;
  58. if(rstotal!=null&&rstotal.size()>0){
  59. count = (Long) rstotal.get(0).get("total");
  60. }
  61. return MixEnvelop.getSuccessListWithPage(HealthBankMapping.api_success, taskPatientDetailDOS,page,size,count);
  62. }
  63. /**
  64. * 添加任务参与
  65. *
  66. * @param taskPatientDetailDO 任务参与对象
  67. * @return
  68. */
  69. public MixEnvelop<Boolean, Boolean> insert(TaskPatientDetailDO taskPatientDetailDO) throws Exception{
  70. MixEnvelop<Boolean, Boolean> envelop = new MixEnvelop<>();
  71. taskPatientDetailDO.setCreateTime(new Date());
  72. taskPatientDetailDO.setUpdateTime(new Date());
  73. taskPatientDetailDO.setStatus(Integer.parseInt("0"));
  74. String accountSql = "select * from wlyy_health_bank_account where patient_id = '"+taskPatientDetailDO.getPatientId()+"'";
  75. List<AccountDO> accountDOS = jdbcTemplate.query(accountSql,new BeanPropertyRowMapper(AccountDO.class));
  76. if (accountDOS == null || accountDOS.size() ==0){
  77. AccountDO accountDO = new AccountDO();
  78. accountDO.setSaasId("dev");
  79. accountDO.setStatus(1);
  80. if(taskPatientDetailDO.getPatientIdcard().length()>=4){// 判断是否长度大于等于4
  81. String cardNumber=taskPatientDetailDO.getPatientIdcard().substring(taskPatientDetailDO.getPatientIdcard().length()- 4,taskPatientDetailDO.getPatientIdcard().length());//截取两个数字之间的部分
  82. int random = (int)((Math.random()*9+1)*100000);
  83. accountDO.setCardNumber(cardNumber+Integer.toString(random));
  84. }
  85. accountDO.setAccountName(taskPatientDetailDO.getName());
  86. accountDO.setTotal(0);
  87. accountDO.setCouponTotal(0L);
  88. accountDO.setPatientId(taskPatientDetailDO.getPatientId());
  89. accountDO.setCardNumber(taskPatientDetailDO.getPatientIdcard());
  90. accountDO.setHospital(taskPatientDetailDO.getHospital());
  91. accountDO.setHospitalName(taskPatientDetailDO.getHospitalName());
  92. accountDO.setCreateTime(new Date());
  93. accountDO.setUpdateTime(new Date());
  94. accountDao.save(accountDO);
  95. }
  96. String activitySql = "select * from wlyy_health_bank_task where transaction_id = '" + taskPatientDetailDO.getActivityId() +"'";
  97. List<TaskDO> taskDOList = jdbcTemplate.query(activitySql,new BeanPropertyRowMapper(TaskDO.class));
  98. StringBuffer buffer = new StringBuffer();
  99. buffer.append(" and task_id IN (");
  100. for (TaskDO taskDO : taskDOList){
  101. buffer.append("'"+taskDO.getId()+"'").append(",");
  102. }
  103. buffer.deleteCharAt(buffer.length()-1);
  104. buffer.append(")");
  105. String sql = "select * from wlyy_health_bank_task_patient_detail where patient_openid = '"+taskPatientDetailDO.getPatientOpenid()+"'"+buffer;
  106. List<TaskPatientDetailDO> taskPatientDetailDOList = jdbcTemplate.query(sql,new BeanPropertyRowMapper(TaskPatientDetailDO.class));
  107. if (taskPatientDetailDOList != null && taskPatientDetailDOList.size() !=0){
  108. throw new Exception("该微信账号已报名过!");
  109. }
  110. for (TaskDO taskDO : taskDOList){
  111. taskPatientDetailDO.setTotal(Long.parseLong("0"));
  112. taskPatientDetailDO.setTaskId(taskDO.getId());
  113. taskPatientDetailDO.setCouponTotal(0L);
  114. taskPatientDetailDao.save(taskPatientDetailDO);
  115. }
  116. envelop.setObj(true);
  117. return envelop;
  118. }
  119. /**
  120. * 更新任务参与
  121. *
  122. * @param taskPatientDetailDO 任务参与对象
  123. * @return
  124. */
  125. public MixEnvelop<Boolean, Boolean> update(TaskPatientDetailDO taskPatientDetailDO){
  126. MixEnvelop<Boolean, Boolean> envelop = new MixEnvelop<>();
  127. String sql = ISqlUtils.getUpdateSql(taskPatientDetailDO);
  128. jdbcTemplate.update(sql);
  129. envelop.setObj(true);
  130. return envelop;
  131. }
  132. /**
  133. * 获取参与活动信息
  134. *
  135. * @param openId 微信id
  136. * @param idCard 身份证号
  137. * @param unionId
  138. * @param taskCode 任务标识
  139. * @return
  140. */
  141. public TaskPatientDetailDO selectByPatientId(String openId,String idCard,String unionId,String taskCode){
  142. logger.info("openId:"+openId+"idCard:"+idCard+"unionId:"+unionId+"taskCode:"+taskCode);
  143. String sql ="select * from wlyy_health_bank_task_patient_detail where " +
  144. " (patient_idcard = '"+ AesEncryptUtils.encrypt(idCard)+"' OR union_id ='"+unionId+"') AND task_id IN " +
  145. "(select id from wlyy_health_bank_task where task_code = '"+taskCode+"' )";
  146. List<TaskPatientDetailDO> taskPatientDetailDOS = jdbcTemplate.query(sql,new BeanPropertyRowMapper(TaskPatientDetailDO.class));
  147. logger.info("参与信息:"+taskPatientDetailDOS);
  148. if (taskPatientDetailDOS!=null&&taskPatientDetailDOS.size()!=0){
  149. return taskPatientDetailDOS.get(0);
  150. }else {
  151. return null;
  152. }
  153. }
  154. public TaskPatientDetailDO selectByPatientIdAndTaskId(String openId,String idCard,String unionId,String taskId){
  155. logger.info("openId:"+openId+"idCard:"+idCard+"unionId:"+unionId+"taskCode:"+taskId);
  156. String sql ="select * from wlyy_health_bank_task_patient_detail where " +
  157. " (patient_idcard = '"+AesEncryptUtils.encrypt(idCard)+"' OR union_id ='"+unionId+"') AND task_id = '"+taskId+"'" ;
  158. List<TaskPatientDetailDO> taskPatientDetailDOS = jdbcTemplate.query(sql,new BeanPropertyRowMapper(TaskPatientDetailDO.class));
  159. logger.info("参与信息:"+taskPatientDetailDOS);
  160. return taskPatientDetailDOS.get(0);
  161. }
  162. /**
  163. * 获取参与详情
  164. *
  165. * @param page
  166. * @param size
  167. * @return
  168. */
  169. public List<TaskPatientDetailDO> select(Integer page,Integer size){
  170. String sql ="SELECT * FROM `wlyy_health_bank_task_patient_detail` btpd " +
  171. "GROUP BY btpd.activity_id, btpd.patient_id ORDER BY btpd.create_time limit "+(page-1)*size+","+size;
  172. List<TaskPatientDetailDO> taskPatientDetailDOS = jdbcTemplate.query(sql,new BeanPropertyRowMapper<>(TaskPatientDetailDO.class));
  173. for (TaskPatientDetailDO taskPatientDetailDO : taskPatientDetailDOS){
  174. ActivityDO activityDO = activityDao.selectById(taskPatientDetailDO.getActivityId());
  175. taskPatientDetailDO.setActivityDO(activityDO);
  176. }
  177. return taskPatientDetailDOS;
  178. }
  179. }