CreditsDetailService.java 7.8 KB

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