ActivityService.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. package com.yihu.jw.service;/**
  2. * Created by nature of king on 2018/4/27.
  3. */
  4. import com.yihu.base.mysql.query.BaseJpaService;
  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.ActivityDO;
  9. import com.yihu.jw.entity.health.bank.TaskDO;
  10. import com.yihu.jw.entity.health.bank.TaskPatientDetailDO;
  11. import com.yihu.jw.restmodel.common.Envelop;
  12. import com.yihu.jw.rm.health.bank.HealthBankMapping;
  13. import com.yihu.jw.util.ISqlUtils;
  14. import org.springframework.beans.factory.annotation.Autowired;
  15. import org.springframework.boot.autoconfigure.couchbase.CouchbaseProperties;
  16. import org.springframework.jdbc.core.BeanPropertyRowMapper;
  17. import org.springframework.jdbc.core.JdbcTemplate;
  18. import org.springframework.stereotype.Service;
  19. import javax.transaction.Transactional;
  20. import java.text.ParseException;
  21. import java.util.ArrayList;
  22. import java.util.Date;
  23. import java.util.List;
  24. import java.util.Map;
  25. /**
  26. * @author wangzhinan
  27. * @create 2018-04-27 14:38
  28. * @desc health activity Service
  29. **/
  30. @Service
  31. @Transactional
  32. public class ActivityService extends BaseJpaService<ActivityDO,ActivityDao> {
  33. @Autowired
  34. private ActivityDao activityDao;
  35. @Autowired
  36. private TaskPatientDetailDao taskPatientDetailDao;
  37. @Autowired
  38. private TaskDao taskDao;
  39. @Autowired
  40. private JdbcTemplate jdbcTemplate;
  41. /**
  42. * insert activityDO
  43. *
  44. * @param activityDO 活动参数对象
  45. * @return
  46. */
  47. public Envelop<Boolean> insert(ActivityDO activityDO){
  48. activityDO.setCreateTime(new Date());
  49. activityDO.setUpdateTime(new Date());
  50. activityDao.save(activityDO);
  51. Envelop<Boolean> envelop = new Envelop<>();
  52. envelop.setObj(true);
  53. return envelop;
  54. }
  55. /**
  56. * find by condition
  57. *
  58. * @param activityDO 活动参数对象
  59. * @param page 页码
  60. * @param size 每页大小
  61. * @return
  62. * @throws ParseException
  63. */
  64. public Envelop<ActivityDO> findByCondition(ActivityDO activityDO,Integer page, Integer size) throws ParseException {
  65. String sql = new ISqlUtils().getSql(activityDO,page,size,"*");
  66. List<ActivityDO> activityDOS = jdbcTemplate.query(sql,new BeanPropertyRowMapper(ActivityDO.class));
  67. for (ActivityDO activityDO1:activityDOS){
  68. String taskSql = "SELECT" +
  69. " COUNT(1) AS total1 " +
  70. "FROM " +
  71. " ( " +
  72. " SELECT DISTINCT " +
  73. " (btpd.patient_openid) " +
  74. " FROM " +
  75. " wlyy_health_bank_task_patient_detail btpd " +
  76. " WHERE " +
  77. " activity_id = '" +activityDO1.getId()+
  78. "' ) btpd1";
  79. List<Map<String,Object>> rstotal = jdbcTemplate.queryForList(taskSql);
  80. Long count = 0L;
  81. if(rstotal!=null&&rstotal.size()>0){
  82. count = (Long) rstotal.get(0).get("total1");
  83. }
  84. activityDO1.setTotal(count);
  85. String taskSql1 = "select * from wlyy_health_bank_task_patient_detail btpd where activity_id = '"+activityDO1.getId()
  86. +"' and (patient_idcard = '"+activityDO.getPatientIdcard()+"' OR union_id = '"+activityDO.getUnionId()+"')";
  87. List<TaskPatientDetailDO> taskPatientDetailDOS = jdbcTemplate.query(taskSql1,new BeanPropertyRowMapper(TaskPatientDetailDO.class));
  88. activityDO1.setTaskPatientDetailDOS(taskPatientDetailDOS);
  89. String tasksql = "select * from wlyy_health_bank_task where transaction_id = '"+activityDO1.getId()+"'";
  90. List<TaskDO> taskDOList = jdbcTemplate.query(tasksql,new BeanPropertyRowMapper(TaskDO.class));
  91. activityDO1.setTaskDOS(taskDOList);
  92. }
  93. String sqlcount = new ISqlUtils().getSql(activityDO,0,0,"count");
  94. List<Map<String,Object>> rstotal = jdbcTemplate.queryForList(sqlcount);
  95. Long count = 0L;
  96. if(rstotal!=null&&rstotal.size()>0){
  97. count = (Long) rstotal.get(0).get("total");
  98. }
  99. return Envelop.getSuccessListWithPage(HealthBankMapping.api_success,activityDOS,page,size,count);
  100. }
  101. /**
  102. * update activityDO
  103. *
  104. * @param activityDO 活动参数对象
  105. * @return
  106. */
  107. public Envelop<Boolean> update(ActivityDO activityDO){
  108. activityDao.save(activityDO);
  109. Envelop<Boolean> envelop = new Envelop<>();
  110. envelop.setObj(true);
  111. return envelop;
  112. }
  113. /**
  114. * 获取参与的活动
  115. *
  116. * @param activityDO 活动对象
  117. * @param page 页码
  118. * @param size 分页大小
  119. * @return
  120. */
  121. public Envelop<ActivityDO> selectByPatient(ActivityDO activityDO,Integer page,Integer size){
  122. String sql ="SELECT * " +
  123. " FROM wlyy_health_bank_activity " +
  124. "WHERE " +
  125. "id IN ( " +
  126. "SELECT bt.transaction_id " +
  127. "FROM wlyy_health_bank_task bt " +
  128. "WHERE id IN (" +
  129. " SELECT task_id FROM " +
  130. "wlyy_health_bank_task_patient_detail" +
  131. " WHERE " +
  132. " patient_openid = '"+activityDO.getOpenId()+ "' AND patient_idcard = '"+activityDO.getPatientIdcard()+"' AND union_id = '"+ activityDO.getUnionId()+"')" +
  133. " )" +
  134. " LIMIT "+(page-1)*size +","+size;
  135. List<ActivityDO> activityDOS = jdbcTemplate.query(sql,new BeanPropertyRowMapper(ActivityDO.class));
  136. for (ActivityDO activityDO1:activityDOS){
  137. String activitySql ="SELECT btpd1.sum AS total FROM (SELECT " +
  138. " SUM(total) AS sum , " +
  139. " patient_id, " +
  140. " patient_openid, " +
  141. " patient_idcard, " +
  142. " activity_id," +
  143. " union_id " +
  144. " FROM " +
  145. " wlyy_health_bank_task_patient_detail " +
  146. "GROUP BY patient_openid,patient_idcard,union_id)btpd1 " +
  147. "WHERE " +
  148. " btpd1.activity_id = '"+activityDO1.getId() +"' AND patient_openid = '"+activityDO.getOpenId()+ "' AND patient_idcard = '"+activityDO.getPatientIdcard()+"' AND union_id = '"+ activityDO.getUnionId()+"'";
  149. List<Map<String,Object>> rstotal = jdbcTemplate.queryForList(activitySql);
  150. Long count = 0L;
  151. if(rstotal!=null&&rstotal.size()>0){
  152. Object object = rstotal.get(0).get("total");
  153. count = Long.parseLong(object.toString());
  154. }
  155. activityDO1.setSum(count);
  156. String rankingSql = "SELECT count(1)+1 AS total FROM (SELECT " +
  157. " SUM(total) AS sum , " +
  158. " patient_id, " +
  159. " patient_openid, " +
  160. " patient_idcard, " +
  161. " activity_id " +
  162. "FROM " +
  163. " wlyy_health_bank_task_patient_detail " +
  164. "GROUP BY patient_openid,patient_idcard,union_id)btpd1 " +
  165. "WHERE " +
  166. " btpd1.activity_id = '"+activityDO1.getId()+"' AND btpd1.sum >" +activityDO1.getSum() ;
  167. List<Map<String,Object>> rstotal1 = jdbcTemplate.queryForList(rankingSql);
  168. Integer count1 = 0;
  169. if(rstotal1!=null&&rstotal1.size()>0){
  170. Object object = rstotal1.get(0).get("total");
  171. count1 = Integer.parseInt(object.toString());
  172. }
  173. activityDO1.setActivityRanking(count1);
  174. String taskSql = "SELECT" +
  175. " COUNT(1) AS total1 " +
  176. "FROM " +
  177. " ( " +
  178. " SELECT * " +
  179. " FROM " +
  180. " wlyy_health_bank_task_patient_detail btpd " +
  181. " WHERE " +
  182. " activity_id = '" +activityDO1.getId()+
  183. "' GROUP BY patient_openid,patient_idcard,union_id) btpd1";
  184. List<Map<String,Object>> rstotal2 = jdbcTemplate.queryForList(taskSql);
  185. Long count2 = 0L;
  186. if(rstotal2!=null&&rstotal2.size()>0){
  187. count2 = (Long) rstotal2.get(0).get("total1");
  188. }
  189. activityDO1.setTotal(count2);
  190. }
  191. String sqlcount = "SELECT count(1) AS total" +
  192. " FROM wlyy_health_bank_activity " +
  193. "WHERE " +
  194. "id IN ( " +
  195. "SELECT bt.transaction_id " +
  196. "FROM wlyy_health_bank_task bt " +
  197. "WHERE id IN (" +
  198. " SELECT task_id FROM " +
  199. "wlyy_health_bank_task_patient_detail" +
  200. " WHERE " +
  201. " patient_openid = '"+activityDO.getOpenId()+ "' AND patient_idcard = '"+activityDO.getPatientIdcard()+"' AND union_id = '"+ activityDO.getUnionId()+"' )" +
  202. " )";
  203. List<Map<String,Object>> rstotal = jdbcTemplate.queryForList(sqlcount);
  204. Long count = 0L;
  205. if(rstotal!=null&&rstotal.size()>0){
  206. count = (Long) rstotal.get(0).get("total");
  207. }
  208. return Envelop.getSuccessListWithPage(HealthBankMapping.api_success,activityDOS,page,size,count);
  209. }
  210. /**
  211. * 批量删除活动
  212. *
  213. * @param ids 活动id集合
  214. * @return
  215. */
  216. public Envelop<Boolean> batchDelete(List<String> ids){
  217. Envelop<Boolean> envelop = new Envelop<>();
  218. for (int i =0;i<ids.size();i++){
  219. List<TaskDO> taskDOList = taskDao.selectByActivityId(ids.get(i));
  220. for (TaskDO taskDO:taskDOList){
  221. taskDO.setStatus(0);
  222. taskDO.setCreateTime(new Date());
  223. taskDO.setUpdateTime(new Date());
  224. taskDao.save(taskDO);
  225. }
  226. List<TaskPatientDetailDO> taskPatientDetailDOS = taskPatientDetailDao.selectByActivityId(ids.get(i));
  227. for(TaskPatientDetailDO taskPatientDetailDO:taskPatientDetailDOS){
  228. taskPatientDetailDO.setStatus(-1);
  229. taskPatientDetailDO.setCreateTime(new Date());
  230. taskPatientDetailDO.setUpdateTime(new Date());
  231. taskPatientDetailDao.save(taskPatientDetailDO);
  232. }
  233. ActivityDO activityDO = activityDao.findOne(ids.get(i));
  234. activityDO.setStatus(-1);
  235. activityDO.setCreateTime(new Date());
  236. activityDO.setUpdateTime(new Date());
  237. activityDao.save(activityDO);
  238. }
  239. return envelop;
  240. }
  241. }