CreditsDetailService.java 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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 accountDO1 = new AccountDO();
  73. accountDO1.setPatientId(creditsDetailDO.getPatientId());
  74. String sql1 = ISqlUtils.getAllSql(accountDO1);
  75. List<AccountDO> accountDOS = jdbcTemplate.query(sql1,new BeanPropertyRowMapper(AccountDO.class));
  76. if (accountDOS == null || accountDOS.size() == 0){
  77. accountDO1.setTotal(0);
  78. accountDO1.setAccountName("jw");
  79. accountDO1.setCardNumber("jw");
  80. accountDO1.setHospital("海沧区");
  81. accountDO1.setPassword("321321312321");
  82. accountDO1.setHospitalName("haichan");
  83. accountDao.save(accountDO1);
  84. }
  85. List<AccountDO> accountDOS1 = jdbcTemplate.query(sql1,new BeanPropertyRowMapper(AccountDO.class));
  86. AccountDO accountDO = accountDOS1.get(0);
  87. String sql = "SELECT SUM(cd.integrate) as total FROM wlyy_health_bank_credits_detail cd where cd.trade_direction = "+creditsDetailDO.getTradeDirection();
  88. List<Map<String,Object>> rstotal = jdbcTemplate.queryForList(sql);
  89. if (rstotal!= null && rstotal.size()>0){
  90. if (rstotal.get(0).get("total") == null){
  91. accountDO.setUsedTotal(0);
  92. }else {
  93. accountDO.setUsedTotal(Integer.parseInt(rstotal.get(0).get("total").toString()));
  94. }
  95. }
  96. return Envelop.getSuccess(HealthBankMapping.api_success,accountDO);
  97. }
  98. /*
  99. public Envelop<Boolean> exchangeGoods(GoodsDO goodsDO){
  100. CreditsDetailDO creditsLogDetailDO = new CreditsDetailDO();
  101. Envelop<Boolean> envelop = new Envelop<>();
  102. envelop.setObj(true);
  103. return envelop;
  104. }
  105. */
  106. public Envelop<AccountDO> selectByRanking(List<String> patientIds, Integer page, Integer size){
  107. StringBuffer buffer = new StringBuffer();
  108. buffer.append(" ba.patient_id in(");
  109. if (patientIds == null || patientIds.size() == 0){
  110. buffer.append("''");
  111. }else {
  112. for (int i=0;i<patientIds.size();i++){
  113. buffer.append("'"+patientIds.get(i)+"'").append(",");
  114. }
  115. buffer.deleteCharAt(buffer.length()-1);
  116. }
  117. buffer.append(") AND");
  118. String sql = "SELECT" +
  119. " ba.patient_id AS patient_id, " +
  120. " ba.account_name AS account_name," +
  121. " ba.hospital AS hospital, " +
  122. " ba.total AS total, " +
  123. " ba.create_time AS create_time, " +
  124. " (ba.total +(cd1.total)) AS sum " +
  125. " FROM " +
  126. " wlyy_health_bank_account ba, " +
  127. " ( " +
  128. " SELECT " +
  129. " SUM(cd.integrate) AS total, " +
  130. " cd.patient_id AS patient_id " +
  131. " FROM " +
  132. " wlyy_health_bank_credits_detail cd " +
  133. " WHERE " +
  134. " cd.trade_direction = - 1 " +
  135. " GROUP BY " +
  136. " cd.patient_id " +
  137. " ) cd1 " +
  138. "WHERE " + buffer +
  139. " cd1.patient_id = ba.patient_id " +
  140. "ORDER BY " +
  141. " ba.create_time, " +
  142. " (ba.total + cd1.total) DESC " +
  143. "LIMIT "+(page-1)*size+","+size;
  144. List<AccountDO> accountDOS = jdbcTemplate.query(sql,new BeanPropertyRowMapper(AccountDO.class));
  145. String sqlCount = "SELECT count(1) AS total"+
  146. " FROM " +
  147. " wlyy_health_bank_account ba, " +
  148. " ( " +
  149. " SELECT " +
  150. " SUM(cd.integrate) AS total, " +
  151. " cd.patient_id AS patient_id " +
  152. " FROM " +
  153. " wlyy_health_bank_credits_detail cd " +
  154. " WHERE " +
  155. " cd.trade_direction = - 1 " +
  156. " GROUP BY " +
  157. " cd.patient_id " +
  158. " ) cd1 " +
  159. "WHERE " + buffer +
  160. " cd1.patient_id = ba.patient_id " +
  161. "ORDER BY " +
  162. " ba.create_time, " +
  163. " (ba.total + cd1.total) DESC ";
  164. List<Map<String,Object>> rstotal = jdbcTemplate.queryForList(sqlCount);
  165. Long count = 0L;
  166. if(rstotal!=null&&rstotal.size()>0){
  167. count = (Long) rstotal.get(0).get("total");
  168. }
  169. return Envelop.getSuccessListWithPage(HealthBankMapping.api_success, accountDOS,page,size,count);
  170. }
  171. /**
  172. * 添加积分
  173. *
  174. * @param creditsDetailDO
  175. * @return
  176. */
  177. public Envelop<CreditsDetailDO> insert(CreditsDetailDO creditsDetailDO){
  178. TaskDO taskDO = new TaskDO();
  179. taskDO.setTaskCode(creditsDetailDO.getFlag());
  180. taskDO.setPatientId(creditsDetailDO.getPatientId());
  181. String sql = ISqlUtils.getSql(taskDO,1,1,"*");
  182. List<TaskDO> taskDOList = jdbcTemplate.query(sql,new BeanPropertyRowMapper(TaskDO.class));
  183. if (taskDOList != null && taskDOList.size() != 0){
  184. creditsDetailDO.setTransactionId(taskDOList.get(0).getId());
  185. }
  186. String sqlAccount = "select * from wlyy_health_bank_account ba where ba.patient_id = '"+creditsDetailDO.getPatientId() +"'";
  187. List<AccountDO> accountDOList = jdbcTemplate.query(sqlAccount,new BeanPropertyRowMapper(AccountDO.class));
  188. if (accountDOList != null && accountDOList.size() != 0){
  189. creditsDetailDO.setAccountId(accountDOList.get(0).getId());
  190. }
  191. if (creditsDetailDO.getTradeDirection() == 1){
  192. if (creditsDetailDO.getTradeType().equals("HEALTH_TASK")){
  193. TaskDetailDO taskDetailDO = new TaskDetailDO();
  194. taskDetailDO.setIntegrate(creditsDetailDO.getIntegrate());
  195. taskDetailDO.setTaskId(creditsDetailDO.getTransactionId());
  196. taskDetailDO.setSaasId(creditsDetailDO.getSaasId());
  197. taskDetailDO.setPatientId(creditsDetailDO.getPatientId());
  198. taskDetailDO.setTradeDirection(creditsDetailDO.getTradeDirection());
  199. taskDetailDO.setStatus("1");
  200. taskDetailDao.save(taskDetailDO);
  201. }
  202. }
  203. CreditsDetailDO creditsDetailDO1 =credittsLogDetailDao.save(creditsDetailDO);
  204. creditsDetailDO1.setFlag(creditsDetailDO.getFlag());
  205. List<CreditsDetailDO> creditsDetailDOList = new ArrayList<>();
  206. creditsDetailDOList.add(creditsDetailDO1);
  207. AccountDO accountDO = accountDao.findOne(creditsDetailDO1.getAccountId());
  208. if (creditsDetailDO1.getTradeDirection() == 1){
  209. accountDO.setTotal(accountDO.getTotal()+creditsDetailDO1.getIntegrate());
  210. }else if (creditsDetailDO.getTradeDirection() == -1){
  211. accountDO.setTotal(accountDO.getTotal()-creditsDetailDO1.getIntegrate());
  212. }
  213. accountDao.save(accountDO);
  214. Envelop<CreditsDetailDO> envelop = new Envelop<>();
  215. envelop.setDetailModelList(creditsDetailDOList);
  216. return envelop;
  217. }
  218. }