TaskPatientDtailService.java 7.0 KB

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