CreditsDetailService.java 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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.*;
  6. import com.yihu.jw.entity.health.bank.*;
  7. import com.yihu.jw.restmodel.common.Envelop;
  8. import com.yihu.jw.rm.health.bank.HealthBankMapping;
  9. import com.yihu.jw.util.ISqlUtils;
  10. import org.springframework.beans.factory.annotation.Autowired;
  11. import org.springframework.jdbc.core.BeanPropertyRowMapper;
  12. import org.springframework.jdbc.core.JdbcTemplate;
  13. import org.springframework.stereotype.Service;
  14. import javax.transaction.Transactional;
  15. import java.text.ParseException;
  16. import java.util.ArrayList;
  17. import java.util.List;
  18. import java.util.Map;
  19. /**
  20. * @author wangzhinan
  21. * @create 2018-04-27 16:53
  22. * @desc credits log info Service
  23. **/
  24. @Service
  25. @Transactional
  26. public class CreditsDetailService extends BaseJpaService<CreditsDetailDO,CredittsLogDetailDao> {
  27. @Autowired
  28. private CredittsLogDetailDao credittsLogDetailDao;
  29. @Autowired
  30. private TaskDao taskDao;
  31. @Autowired
  32. private ActivityDao activityDao;
  33. @Autowired
  34. private JdbcTemplate jdbcTemplate;
  35. @Autowired
  36. private AccountDao accountDao;
  37. @Autowired
  38. private TaskDetailDao taskDetailDao;
  39. /**
  40. * find creditsLogInfo
  41. *
  42. * @return
  43. * @throws ParseException
  44. */
  45. public Envelop<CreditsDetailDO> findByCondition(CreditsDetailDO creditsDetailDO, Integer page, Integer size) throws ParseException {
  46. String sql = new ISqlUtils().getSql(creditsDetailDO,page,size,"*");
  47. List<CreditsDetailDO> creditsDetailDOS = jdbcTemplate.query(sql,new BeanPropertyRowMapper(CreditsDetailDO.class));
  48. for (CreditsDetailDO creditsDetailDO1 : creditsDetailDOS){
  49. if (creditsDetailDO1.getTradeType().equalsIgnoreCase("HEALTH_TASK")){
  50. TaskDO taskDO = taskDao.findOne(creditsDetailDO1.getTransactionId());
  51. creditsDetailDO1.setTaskDO(taskDO);
  52. }else if(creditsDetailDO1.getTradeType().equalsIgnoreCase("HEALTH_ACTIVITY")){
  53. ActivityDO activityDO = activityDao.findOne(creditsDetailDO1.getTransactionId());
  54. creditsDetailDO1.setActivityDO(activityDO);
  55. }
  56. }
  57. String sqlcount = new ISqlUtils().getSql(creditsDetailDO,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 Envelop.getSuccessListWithPage(HealthBankMapping.api_success, creditsDetailDOS,page,size,count);
  64. }
  65. /**
  66. * 获取账户信息
  67. *
  68. * @param creditsDetailDO
  69. * @return
  70. */
  71. public Envelop<AccountDO> findByTradeDirection(CreditsDetailDO creditsDetailDO){
  72. AccountDO accountDO = accountDao.findOne(creditsDetailDO.getAccountId());
  73. String sql = "SELECT SUM(cd.integrate) as total FROM wlyy_health_bank_credits_detail cd where cd.trade_direction = "+creditsDetailDO.getTradeDirection();
  74. List<Map<String,Object>> rstotal = jdbcTemplate.queryForList(sql);
  75. if (rstotal!= null && rstotal.size()>0){
  76. if (rstotal.get(0).get("total") == null){
  77. accountDO.setUsedTotal(0);
  78. }else {
  79. accountDO.setUsedTotal(Integer.parseInt(rstotal.get(0).get("total").toString()));
  80. }
  81. }
  82. return Envelop.getSuccess(HealthBankMapping.api_success,accountDO);
  83. }
  84. /*
  85. public Envelop<Boolean> exchangeGoods(GoodsDO goodsDO){
  86. CreditsDetailDO creditsLogDetailDO = new CreditsDetailDO();
  87. Envelop<Boolean> envelop = new Envelop<>();
  88. envelop.setObj(true);
  89. return envelop;
  90. }
  91. */
  92. public Envelop<AccountDO> selectByRanking(List<String> patientIds, Integer page, Integer size){
  93. StringBuffer buffer = new StringBuffer();
  94. if (patientIds !=null && patientIds.size()!=0){
  95. buffer.append(" ba.patient_id in(");
  96. for (int i=0;i<patientIds.size();i++){
  97. buffer.append("'"+patientIds.get(i)+"'").append(",");
  98. }
  99. buffer.append(")");
  100. buffer.deleteCharAt(buffer.length()-2);
  101. }
  102. String sql = "SELECT" +
  103. " ba.patient_id AS patient_id, " +
  104. " ba.account_name AS account_name," +
  105. " ba.hospital AS hospital, " +
  106. " ba.total AS total, " +
  107. " ba.create_time AS create_time, " +
  108. " (ba.total +(cd1.total)) AS sum " +
  109. " FROM " +
  110. " wlyy_health_bank_account ba, " +
  111. " ( " +
  112. " SELECT " +
  113. " SUM(cd.integrate) AS total, " +
  114. " cd.patient_id AS patient_id " +
  115. " FROM " +
  116. " wlyy_health_bank_credits_detail cd " +
  117. " WHERE " +
  118. " cd.trade_direction = - 1 " +
  119. " GROUP BY " +
  120. " cd.patient_id " +
  121. " ) cd1 " +
  122. "WHERE " + buffer +
  123. "AND cd1.patient_id = ba.patient_id " +
  124. "ORDER BY " +
  125. " ba.create_time, " +
  126. " (ba.total + cd1.total) DESC " +
  127. "LIMIT "+(page-1)*size+","+size;
  128. List<AccountDO> accountDOS = jdbcTemplate.query(sql,new BeanPropertyRowMapper(AccountDO.class));
  129. String sqlCount = "SELECT count(1) AS total"+
  130. " FROM " +
  131. " wlyy_health_bank_account ba, " +
  132. " ( " +
  133. " SELECT " +
  134. " SUM(cd.integrate) AS total, " +
  135. " cd.patient_id AS patient_id " +
  136. " FROM " +
  137. " wlyy_health_bank_credits_detail cd " +
  138. " WHERE " +
  139. " cd.trade_direction = - 1 " +
  140. " GROUP BY " +
  141. " cd.patient_id " +
  142. " ) cd1 " +
  143. "WHERE " + buffer +
  144. "AND cd1.patient_id = ba.patient_id " +
  145. "ORDER BY " +
  146. " ba.create_time, " +
  147. " (ba.total + cd1.total) DESC ";
  148. List<Map<String,Object>> rstotal = jdbcTemplate.queryForList(sqlCount);
  149. Long count = 0L;
  150. if(rstotal!=null&&rstotal.size()>0){
  151. count = (Long) rstotal.get(0).get("total");
  152. }
  153. return Envelop.getSuccessListWithPage(HealthBankMapping.api_success, accountDOS,page,size,count);
  154. }
  155. /**
  156. * 添加积分
  157. *
  158. * @param creditsDetailDO
  159. * @return
  160. */
  161. public Envelop<CreditsDetailDO> insert(CreditsDetailDO creditsDetailDO){
  162. TaskDO taskDO = new TaskDO();
  163. taskDO.setTaskCode(creditsDetailDO.getFlag());
  164. taskDO.setPatientId(creditsDetailDO.getPatientId());
  165. String sql = ISqlUtils.getSql(taskDO,1,1,"*");
  166. List<TaskDO> taskDOList = jdbcTemplate.query(sql,new BeanPropertyRowMapper(TaskDO.class));
  167. if (taskDOList != null && taskDOList.size() != 0){
  168. creditsDetailDO.setTransactionId(taskDOList.get(0).getId());
  169. }
  170. String sqlAccount = "select * from wlyy_health_bank_account ba where ba.patient_id = '"+creditsDetailDO.getPatientId() +"'";
  171. List<AccountDO> accountDOList = jdbcTemplate.query(sqlAccount,new BeanPropertyRowMapper(AccountDO.class));
  172. if (accountDOList != null && accountDOList.size() != 0){
  173. creditsDetailDO.setAccountId(accountDOList.get(0).getId());
  174. }
  175. if (creditsDetailDO.getTradeDirection() == 1){
  176. if (creditsDetailDO.getTradeType().equals("HEALTH_TASK")){
  177. TaskDetailDO taskDetailDO = new TaskDetailDO();
  178. taskDetailDO.setIntegrate(creditsDetailDO.getIntegrate());
  179. taskDetailDO.setTaskId(creditsDetailDO.getTransactionId());
  180. taskDetailDO.setSaasId(creditsDetailDO.getSaasId());
  181. taskDetailDO.setPatientId(creditsDetailDO.getPatientId());
  182. taskDetailDO.setTradeDirection(creditsDetailDO.getTradeDirection());
  183. taskDetailDO.setStatus("1");
  184. taskDetailDao.save(taskDetailDO);
  185. }
  186. }
  187. CreditsDetailDO creditsDetailDO1 =credittsLogDetailDao.save(creditsDetailDO);
  188. creditsDetailDO1.setFlag(creditsDetailDO.getFlag());
  189. List<CreditsDetailDO> creditsDetailDOList = new ArrayList<>();
  190. creditsDetailDOList.add(creditsDetailDO1);
  191. AccountDO accountDO = accountDao.findOne(creditsDetailDO1.getAccountId());
  192. if (creditsDetailDO1.getTradeDirection() == 1){
  193. accountDO.setTotal(accountDO.getTotal()+creditsDetailDO1.getIntegrate());
  194. }else if (creditsDetailDO.getTradeDirection() == -1){
  195. accountDO.setTotal(accountDO.getTotal()-creditsDetailDO1.getIntegrate());
  196. }
  197. accountDao.save(accountDO);
  198. Envelop<CreditsDetailDO> envelop = new Envelop<>();
  199. envelop.setDetailModelList(creditsDetailDOList);
  200. return envelop;
  201. }
  202. }