AccountService.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. package com.yihu.jw.service;/**
  2. * Created by nature of king on 2018/5/10.
  3. */
  4. import com.yihu.base.mysql.query.BaseJpaService;
  5. import com.yihu.jw.dao.AccountDao;
  6. import com.yihu.jw.entity.health.bank.AccountDO;
  7. import com.yihu.jw.entity.health.bank.CreditsDetailDO;
  8. import com.yihu.jw.restmodel.common.Envelop;
  9. import com.yihu.jw.rm.health.bank.HealthBankMapping;
  10. import com.yihu.jw.util.DateUtils;
  11. import com.yihu.jw.util.ISqlUtils;
  12. import org.springframework.beans.factory.annotation.Autowired;
  13. import org.springframework.jdbc.core.BeanPropertyRowMapper;
  14. import org.springframework.jdbc.core.JdbcTemplate;
  15. import org.springframework.stereotype.Service;
  16. import javax.transaction.Transactional;
  17. import java.text.ParseException;
  18. import java.util.ArrayList;
  19. import java.util.List;
  20. import java.util.Map;
  21. /**
  22. * @author wangzhinan
  23. * @create 2018-05-10 11:26
  24. * @desc account service
  25. **/
  26. @Service
  27. @Transactional
  28. public class AccountService extends BaseJpaService<AccountDO,AccountDao> {
  29. @Autowired
  30. private AccountDao accountDao;
  31. @Autowired
  32. private JdbcTemplate jdbcTemplate;
  33. /**
  34. * 添加银行账户信息
  35. *
  36. * @param accountDO 银行账户对象
  37. * @return
  38. */
  39. public Envelop<Boolean> insert(AccountDO accountDO){
  40. accountDao.save(accountDO);
  41. Envelop<Boolean> envelop = new Envelop<>();
  42. envelop.setObj(true);
  43. return envelop;
  44. }
  45. /**
  46. * 获取银行账户信息
  47. *
  48. * @param accountDO 银行账户对象
  49. * @param page 页码
  50. * @param size 每页大小
  51. * @return
  52. * @throws ParseException
  53. */
  54. public Envelop<AccountDO> findByCondition(AccountDO accountDO, Integer page, Integer size) throws ParseException {
  55. String sql = new ISqlUtils().getSql(accountDO,page,size,"*");
  56. List<AccountDO> accountDOS = jdbcTemplate.query(sql,new BeanPropertyRowMapper(AccountDO.class));
  57. for (AccountDO accountDO1:accountDOS){
  58. String sql1 = "select COALESCE(sum(bcd.integrate),0) as total from wlyy_health_bank_credits_detail bcd where bcd.trade_direction = 1" +
  59. " AND bcd.create_time > '"+ DateUtils.getDayBegin()+"' AND bcd.create_time < '"+DateUtils.getDayEnd()+"' AND bcd.patient_id = " +
  60. " '"+ accountDO1.getPatientId() +"'";
  61. List<Map<String,Object>> rstotal = jdbcTemplate.queryForList(sql1);
  62. Long count = 0L;
  63. if(rstotal!=null&&rstotal.size()>0){
  64. Object object = rstotal.get(0).get("total");
  65. count = Long.parseLong(object.toString());
  66. }
  67. accountDO1.setNowTotal(count);
  68. String activitySql = "SELECT COUNT(*) AS total FROM ( SELECT * FROM " +
  69. "wlyy_health_bank_task_patient_detail " +
  70. " WHERE patient_id = '" + accountDO1.getPatientId()+
  71. "'AND activity_id != '' GROUP BY activity_id ) btpd1";
  72. List<Map<String,Object>> rstotal1 = jdbcTemplate.queryForList(activitySql);
  73. Long activityCount = 0L;
  74. if(rstotal1!=null&&rstotal1.size()>0){
  75. Object object = rstotal1.get(0).get("total");
  76. activityCount = Long.parseLong(object.toString());
  77. }
  78. accountDO1.setActivityTotal(activityCount);
  79. String taskSql = "SELECT COUNT(*) AS total FROM ( SELECT * FROM " +
  80. "wlyy_health_bank_task_patient_detail " +
  81. " WHERE patient_id = '" + accountDO1.getPatientId()+
  82. "' GROUP BY task_id ) btpd1";
  83. List<Map<String,Object>> rstotal2 = jdbcTemplate.queryForList(taskSql);
  84. Long taskCount = 0L;
  85. if(rstotal2!=null&&rstotal2.size()>0){
  86. Object object = rstotal2.get(0).get("total");
  87. taskCount = Long.parseLong(object.toString());
  88. }
  89. accountDO1.setTaskTotal(taskCount);
  90. if (accountDO.getPatientIds() != null && accountDO.getPatientIds().size() != 0){
  91. StringBuffer buffer = new StringBuffer();
  92. buffer.append(" (");
  93. for (int i=0;i<accountDO.getPatientIds().size();i++){
  94. buffer.append("'"+accountDO.getPatientIds().get(i)+"'").append(",");
  95. }
  96. buffer.deleteCharAt(buffer.length()-1);
  97. buffer.append(") ");
  98. String accountSql = "select * from wlyy_health_bank_account where patient_id in "+buffer;
  99. List<AccountDO> accountDOS1 = jdbcTemplate.query(sql,new BeanPropertyRowMapper<>(AccountDO.class));
  100. if (accountDOS1 != null && accountDOS1.size() !=0){
  101. String sql2 = " SELECT " +
  102. " ba.total + COALESCE (bacd1.sum, 0) AS total " +
  103. " FROM " +
  104. " wlyy_health_bank_account ba " +
  105. " LEFT JOIN ( " +
  106. " SELECT " +
  107. " COALESCE (SUM(bacd.integrate), 0) AS sum, " +
  108. " bacd.account_id " +
  109. " FROM " +
  110. " wlyy_health_bank_credits_detail bacd " +
  111. " WHERE " +
  112. " bacd.trade_direction = - 1 " +
  113. " GROUP BY " +
  114. " bacd.account_id ) bacd1 ON ba.id = bacd1.account_id " +
  115. " WHERE " +
  116. " ba.id = '"+accountDO1.getId()+"'";
  117. List<Map<String,Object>> rstotal3 = jdbcTemplate.queryForList(sql2);
  118. Long Count = 0L;
  119. if(rstotal3!=null&&rstotal3.size()>0){
  120. Object object = rstotal3.get(0).get("total");
  121. Count = Long.parseLong(object.toString());
  122. }
  123. accountDO1.setSum(Count);
  124. StringBuffer buffer1 = new StringBuffer();
  125. buffer1.append(" (");
  126. for (int i=0;i<accountDOS1.size();i++){
  127. buffer1.append("'"+accountDOS1.get(i).getId()+"'").append(",");
  128. }
  129. buffer1.deleteCharAt(buffer1.length()-1);
  130. buffer1.append(") ");
  131. String sql3 = "SELECT " +
  132. " COUNT(1) + 1 AS total " +
  133. "FROM " +
  134. " ( " +
  135. " SELECT " +
  136. " ba.id AS id, " +
  137. " ba.total + COALESCE (bacd1.sum, 0) AS sum " +
  138. " FROM " +
  139. " wlyy_health_bank_account ba " +
  140. " LEFT JOIN ( " +
  141. " SELECT " +
  142. " COALESCE (SUM(bacd.integrate), 0) AS sum, " +
  143. " bacd.account_id " +
  144. " FROM " +
  145. " wlyy_health_bank_credits_detail bacd " +
  146. " WHERE " +
  147. " bacd.trade_direction = - 1 " +
  148. " GROUP BY " +
  149. " bacd.account_id ) bacd1 ON ba.id = bacd1.account_id " +
  150. " WHERE " +
  151. " ba.id IN " +buffer1+
  152. " )ba1 WHERE ba1.sum > "+accountDO1.getSum();
  153. List<Map<String,Object>> rstotal4 = jdbcTemplate.queryForList(sql3);
  154. Integer Count1 = 0;
  155. if(rstotal4!=null&&rstotal4.size()>0){
  156. Object object = rstotal4.get(0).get("total");
  157. Count1 = Integer.parseInt(object.toString());
  158. }
  159. accountDO1.setTeamRanking(Count1);
  160. String sql4 = "SELECT " +
  161. " COUNT(1) + 1 AS total " +
  162. "FROM " +
  163. " ( " +
  164. " SELECT " +
  165. " ba.id AS id, " +
  166. " ba.total + COALESCE (bacd1.sum, 0) AS sum " +
  167. " FROM " +
  168. " wlyy_health_bank_account ba " +
  169. " LEFT JOIN ( " +
  170. " SELECT " +
  171. " COALESCE (SUM(bacd.integrate), 0) AS sum, " +
  172. " bacd.account_id " +
  173. " FROM " +
  174. " wlyy_health_bank_credits_detail bacd " +
  175. " WHERE " +
  176. " bacd.trade_direction = - 1 " +
  177. " GROUP BY " +
  178. " bacd.account_id ) bacd1 ON ba.id = bacd1.account_id " +
  179. " )ba1 WHERE ba1.sum > "+accountDO1.getSum();
  180. List<Map<String,Object>> rstotal5 = jdbcTemplate.queryForList(sql4);
  181. Integer Count2 = 0;
  182. if(rstotal5!=null&&rstotal5.size()>0){
  183. Object object = rstotal5.get(0).get("total");
  184. Count2 = Integer.parseInt(object.toString());
  185. }
  186. accountDO1.setCityRanking(Count2);
  187. }
  188. }
  189. }
  190. String sqlcount = new ISqlUtils().getSql(accountDO,0,0,"count");
  191. List<Map<String,Object>> rstotal = jdbcTemplate.queryForList(sqlcount);
  192. Long count = 0L;
  193. if(rstotal!=null&&rstotal.size()>0){
  194. count = (Long) rstotal.get(0).get("total");
  195. }
  196. return Envelop.getSuccessListWithPage(HealthBankMapping.api_success,accountDOS,page,size,count);
  197. }
  198. /**
  199. * 根据条件活动用户信息
  200. *
  201. * @param patientIds 居民id
  202. * @param bindStatus 绑定状态
  203. *
  204. * @param deviceTypes 设备类型
  205. *
  206. * @param page 页码
  207. *
  208. * @param size 分页大小
  209. * @return
  210. */
  211. public Envelop<AccountDO> findByCondition1(List<String> patientIds ,int bindStatus,List<String> deviceTypes,Integer page,Integer size){
  212. StringBuffer buffer = new StringBuffer();
  213. buffer.append(" btpd.patient_id in(");
  214. if (patientIds == null || patientIds.size() == 0){
  215. buffer.append("''");
  216. }else {
  217. for (int i=0;i<patientIds.size();i++){
  218. buffer.append("'"+patientIds.get(i)+"'").append(",");
  219. }
  220. buffer.deleteCharAt(buffer.length()-1);
  221. }
  222. buffer.append(") ");
  223. StringBuffer buffer1 = new StringBuffer();
  224. buffer1.append("(");
  225. if (deviceTypes == null || deviceTypes.size() == 0){
  226. buffer1.append("''");
  227. }else {
  228. for (int i=0;i<deviceTypes.size();i++){
  229. buffer1.append("'"+deviceTypes.get(i)+"'").append(",");
  230. }
  231. buffer1.deleteCharAt(buffer1.length()-1);
  232. }
  233. buffer1.append(")");
  234. String sql1 = null ;
  235. if (bindStatus == -1){
  236. sql1 = "( SELECT btpd.patient_id AS patient_id FROM " +
  237. " wlyy_health_bank_task_patient_detail btpd " +
  238. " LEFT JOIN wlyy_health_bank_task bt ON bt.id = btpd.task_id WHERE " +
  239. " bt.task_code NOT IN "+buffer1+" and "+buffer+")";
  240. }else if (bindStatus == 1){
  241. sql1 = "( SELECT btpd.patient_id AS patient_id FROM " +
  242. " wlyy_health_bank_task_patient_detail btpd " +
  243. " LEFT JOIN wlyy_health_bank_task bt ON bt.id = btpd.task_id WHERE " +
  244. " bt.task_code IN "+buffer1 +" and "+buffer+")";
  245. }
  246. String sql =
  247. "SELECT ba1.patient_id AS patient_id," +
  248. "ba1.account_name AS account_name," +
  249. "ba1.hospital AS hospital," +
  250. "ba1.total AS total," +
  251. "ba1.create_time AS create_time," +
  252. "ba1.sum AS sum" +
  253. " FROM" +
  254. "( SELECT " +
  255. "ba.patient_id AS patient_id," +
  256. "ba.account_name AS account_name," +
  257. "ba.hospital AS hospital," +
  258. "ba.total AS total," +
  259. "ba.create_time AS create_time," +
  260. "(ba.total +COALESCE((cd1.total),0)) AS sum" +
  261. " FROM" +
  262. " wlyy_health_bank_account ba" +
  263. " LEFT JOIN ( " +
  264. "SELECT" +
  265. " SUM(cd.integrate) AS total," +
  266. " cd.patient_id AS patient_id" +
  267. " FROM" +
  268. " wlyy_health_bank_credits_detail cd" +
  269. " WHERE " +
  270. "cd.trade_direction = - 1" +
  271. " GROUP BY " +
  272. " cd.patient_id ) cd1 ON cd1.patient_id = ba.patient_id " +
  273. " WHERE ba.patient_id IN " + sql1 +
  274. " ORDER BY" +
  275. " ba.create_time DESC " +
  276. "LIMIT "+(page-1)*size+","+size +")ba1" +
  277. " ORDER BY " +
  278. " ba1.sum DESC";
  279. List<AccountDO> accountDOS = jdbcTemplate.query(sql,new BeanPropertyRowMapper(AccountDO.class));
  280. String sqlCount = "SELECT count(1) AS total"+
  281. " FROM " +
  282. " wlyy_health_bank_account ba LEFT JOIN " +
  283. " ( " +
  284. " SELECT " +
  285. " SUM(cd.integrate) AS total, " +
  286. " cd.patient_id AS patient_id " +
  287. " FROM " +
  288. " wlyy_health_bank_credits_detail cd " +
  289. " WHERE " +
  290. " cd.trade_direction = - 1 " +
  291. " GROUP BY " +
  292. " cd.patient_id " +
  293. " ) cd1 ON cd1.patient_id = ba.patient_id " +
  294. "WHERE ba.patient_id IN " + sql1 +
  295. " ORDER BY " +
  296. " ba.create_time, " +
  297. " (ba.total + COALESCE(cd1.total,0)) DESC ";
  298. List<Map<String,Object>> rstotal = jdbcTemplate.queryForList(sqlCount);
  299. Long count = 0L;
  300. if(rstotal!=null&&rstotal.size()>0){
  301. count = (Long) rstotal.get(0).get("total");
  302. }
  303. return Envelop.getSuccessListWithPage(HealthBankMapping.api_success, accountDOS,page,size,count);
  304. }
  305. }