AccountService.java 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. package com.yihu.jw.service;/**
  2. * Created by nature of king on 2018/5/10.
  3. */
  4. import com.alibaba.fastjson.JSONArray;
  5. import com.yihu.jw.dao.AccountDao;
  6. import com.yihu.jw.dao.TaskRuleDao;
  7. import com.yihu.jw.entity.health.bank.AccountDO;
  8. import com.yihu.jw.entity.health.bank.TaskDO;
  9. import com.yihu.jw.entity.health.bank.TaskPatientDetailDO;
  10. import com.yihu.jw.entity.health.bank.TaskRuleDO;
  11. import com.yihu.jw.restmodel.web.MixEnvelop;
  12. import com.yihu.jw.rm.health.bank.HealthBankMapping;
  13. import com.yihu.jw.util.DateUtils;
  14. import com.yihu.jw.util.ISqlUtils;
  15. import com.yihu.mysql.query.BaseJpaService;
  16. import org.springframework.beans.factory.annotation.Autowired;
  17. import org.springframework.jdbc.core.BeanPropertyRowMapper;
  18. import org.springframework.jdbc.core.JdbcTemplate;
  19. import org.springframework.stereotype.Service;
  20. import javax.transaction.Transactional;
  21. import java.text.ParseException;
  22. import java.text.SimpleDateFormat;
  23. import java.util.ArrayList;
  24. import java.util.Date;
  25. import java.util.List;
  26. import java.util.Map;
  27. /**
  28. * @author wangzhinan
  29. * @create 2018-05-10 11:26
  30. * @desc account service
  31. **/
  32. @Service
  33. @Transactional
  34. public class AccountService extends BaseJpaService<AccountDO,AccountDao> {
  35. @Autowired
  36. private AccountDao accountDao;
  37. @Autowired
  38. private JdbcTemplate jdbcTemplate;
  39. @Autowired
  40. private TaskRuleDao taskRuleDao;
  41. /**
  42. * 添加银行账户信息
  43. *
  44. * @param accountDO 银行账户对象
  45. * @return
  46. */
  47. public MixEnvelop<Boolean, Boolean> insert(AccountDO accountDO){
  48. accountDao.save(accountDO);
  49. MixEnvelop<Boolean, Boolean> envelop = new MixEnvelop<>();
  50. envelop.setObj(true);
  51. return envelop;
  52. }
  53. /**
  54. * 获取银行账户信息
  55. *
  56. * @param accountDO 银行账户对象
  57. * @param page 页码
  58. * @param size 每页大小
  59. * @return
  60. * @throws ParseException
  61. */
  62. public MixEnvelop<AccountDO, AccountDO> findByCondition(AccountDO accountDO, Integer page, Integer size) throws ParseException {
  63. String sql = new ISqlUtils().getSql(accountDO,page,size,"*");
  64. List<AccountDO> accountDOS = jdbcTemplate.query(sql,new BeanPropertyRowMapper(AccountDO.class));
  65. for (AccountDO accountDO1:accountDOS){
  66. String sql1 = "select COALESCE(sum(bcd.integrate),0) as total from wlyy_health_bank_credits_detail bcd where bcd.trade_direction = 1" +
  67. " AND bcd.create_time > '"+ DateUtils.getDayBegin()+"' AND bcd.create_time < '"+DateUtils.getDayEnd()+"' AND bcd.patient_id = '" + accountDO1.getPatientId() +"'"
  68. +"AND transaction_id = '"+accountDO.getTaskId()+"'";
  69. List<Map<String,Object>> rstotal = jdbcTemplate.queryForList(sql1);
  70. Long count = 0L;
  71. if(rstotal!=null&&rstotal.size()>0){
  72. Object object = rstotal.get(0).get("total");
  73. if (object != null){
  74. count = Long.parseLong(object.toString());
  75. }
  76. }
  77. accountDO1.setNowTotal(count);
  78. String activitySql1 =" SELECT " +
  79. " SUM(ptpd.total) AS total FROM wlyy_health_bank_task_patient_detail ptpd " +
  80. " WHERE " +" task_id = '" + accountDO.getTaskId() + "' " +
  81. "and patient_id = '"+accountDO1.getPatientId()+"'";
  82. List<Map<String,Object>> rstotal6 = jdbcTemplate.queryForList(activitySql1);
  83. Long activityIntegrate = 0L;
  84. if(rstotal6!=null&&rstotal6.size()>0){
  85. Object object = rstotal6.get(0).get("total");
  86. if (object!=null){
  87. activityIntegrate = Long.parseLong(object.toString());
  88. }
  89. }
  90. accountDO1.setActivityIntegrate(activityIntegrate);
  91. String activitySql = "SELECT COUNT(*) AS total FROM ( SELECT * FROM " +
  92. "wlyy_health_bank_task_patient_detail " +
  93. " WHERE patient_id = '" + accountDO1.getPatientId()+
  94. "'AND activity_id != '' GROUP BY activity_id ) btpd1";
  95. List<Map<String,Object>> rstotal1 = jdbcTemplate.queryForList(activitySql);
  96. Long activityCount = 0L;
  97. if(rstotal1!=null&&rstotal1.size()>0){
  98. Object object = rstotal1.get(0).get("total");
  99. if (object != null){
  100. activityCount = Long.parseLong(object.toString());
  101. }
  102. }
  103. accountDO1.setActivityTotal(activityCount);
  104. String taskSql = "SELECT COUNT(*) AS total FROM ( SELECT * FROM " +
  105. "wlyy_health_bank_task_patient_detail " +
  106. " WHERE patient_id = '" + accountDO1.getPatientId()+
  107. "' GROUP BY task_id ) btpd1";
  108. List<Map<String,Object>> rstotal2 = jdbcTemplate.queryForList(taskSql);
  109. Long taskCount = 0L;
  110. if(rstotal2!=null&&rstotal2.size()>0){
  111. Object object = rstotal2.get(0).get("total");
  112. if (object != null ){
  113. taskCount = Long.parseLong(object.toString());
  114. }
  115. }
  116. accountDO1.setTaskTotal(taskCount);
  117. if (accountDO.getPatientIds() != null && accountDO.getPatientIds().size() != 0){
  118. StringBuffer buffer = new StringBuffer();
  119. buffer.append(" (");
  120. for (int i=0;i<accountDO.getPatientIds().size();i++){
  121. buffer.append("'"+accountDO.getPatientIds().get(i)+"'").append(",");
  122. }
  123. buffer.deleteCharAt(buffer.length()-1);
  124. buffer.append(") ");
  125. String accountSql = "select * from wlyy_health_bank_account where patient_id in "+buffer;
  126. List<AccountDO> accountDOSList = jdbcTemplate.query(accountSql,new BeanPropertyRowMapper<>(AccountDO.class));
  127. if (accountDOSList != null && accountDOSList.size() !=0){
  128. String sql2 = " SELECT " +
  129. " ba.total + COALESCE (bacd1.sum, 0) AS total " +
  130. " FROM " +
  131. " wlyy_health_bank_account ba " +
  132. " LEFT JOIN ( " +
  133. " SELECT " +
  134. " COALESCE (SUM(bacd.integrate), 0) AS sum, " +
  135. " bacd.account_id " +
  136. " FROM " +
  137. " wlyy_health_bank_credits_detail bacd " +
  138. " WHERE " +
  139. " bacd.trade_direction = - 1 " +
  140. " GROUP BY " +
  141. " bacd.account_id ) bacd1 ON ba.id = bacd1.account_id " +
  142. " WHERE " +
  143. " ba.id = '"+accountDO1.getId()+"'";
  144. List<Map<String,Object>> rstotal3 = jdbcTemplate.queryForList(sql2);
  145. Long Count = 0L;
  146. if(rstotal3!=null&&rstotal3.size()>0){
  147. Object object = rstotal3.get(0).get("total");
  148. if (object!=null){
  149. Count = Long.parseLong(object.toString());
  150. }
  151. }
  152. accountDO1.setSum(Count);
  153. StringBuffer buffer1 = new StringBuffer();
  154. buffer1.append(" (");
  155. for (int i=0;i<accountDOSList.size();i++){
  156. buffer1.append("'"+accountDOSList.get(i).getId()+"'").append(",");
  157. }
  158. buffer1.deleteCharAt(buffer1.length()-1);
  159. buffer1.append(") ");
  160. String sql3 = "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. " WHERE " +
  180. " ba.id IN " +buffer1+
  181. " )ba1 WHERE ba1.sum > "+accountDO1.getSum();
  182. List<Map<String,Object>> rstotal4 = jdbcTemplate.queryForList(sql3);
  183. Integer Count1 = 0;
  184. if(rstotal4!=null&&rstotal4.size()>0){
  185. Object object = rstotal4.get(0).get("total");
  186. if (object != null){
  187. Count1 = Integer.parseInt(object.toString());
  188. }
  189. }
  190. accountDO1.setTeamRanking(Count1);
  191. String sql4 = "SELECT " +
  192. " COUNT(1) + 1 AS total " +
  193. "FROM " +
  194. " ( " +
  195. " SELECT " +
  196. " ba.id AS id, " +
  197. " ba.total /*+ COALESCE (bacd1.sum, 0)*/ AS sum " +
  198. " FROM " +
  199. " wlyy_health_bank_account ba " +
  200. /*" LEFT JOIN ( " +
  201. " SELECT " +
  202. " COALESCE (SUM(bacd.integrate), 0) AS sum, " +
  203. " bacd.account_id " +
  204. " FROM " +
  205. " wlyy_health_bank_credits_detail bacd " +
  206. " WHERE " +
  207. " bacd.trade_direction = - 1 " +
  208. " GROUP BY " +
  209. " bacd.account_id ) bacd1 ON ba.id = bacd1.account_id " +*/
  210. " )ba1 WHERE ba1.sum > "+accountDO1.getSum();
  211. List<Map<String,Object>> rstotal5 = jdbcTemplate.queryForList(sql4);
  212. Integer Count2 = 0;
  213. if(rstotal5!=null&&rstotal5.size()>0){
  214. Object object = rstotal5.get(0).get("total");
  215. if (object != null){
  216. Count2 = Integer.parseInt(object.toString());
  217. }
  218. }
  219. accountDO1.setCityRanking(Count2);
  220. }
  221. }
  222. }
  223. String sqlcount = new ISqlUtils().getSql(accountDO,0,0,"count");
  224. List<Map<String,Object>> rstotal = jdbcTemplate.queryForList(sqlcount);
  225. Long count = 0L;
  226. if(rstotal!=null&&rstotal.size()>0){
  227. count = (Long) rstotal.get(0).get("total");
  228. }
  229. return MixEnvelop.getSuccessListWithPage(HealthBankMapping.api_success,accountDOS,page,size,count);
  230. }
  231. /**
  232. * 根据条件活动用户信息
  233. *
  234. * @param page 页码
  235. *
  236. * @param size 分页大小
  237. * @return
  238. */
  239. public MixEnvelop<AccountDO, AccountDO> findByCondition1(JSONArray patients, String ruleId, Integer page, Integer size){
  240. for (int i=0;i<patients.size();i++){
  241. String patientId = patients.getJSONObject(i).getString("code");
  242. String sql = "select * from wlyy_health_bank_account where patient_id = '"+patientId+"'";
  243. List<AccountDO> accountDOS = jdbcTemplate.query(sql,new BeanPropertyRowMapper(AccountDO.class));
  244. if (accountDOS == null || accountDOS.size() == 0) {
  245. AccountDO accountDO1 = new AccountDO();
  246. accountDO1.setPatientId(patientId);
  247. accountDO1.setTotal(0);
  248. accountDO1.setAccountName(patients.getJSONObject(i).getString("name"));
  249. accountDO1.setCardNumber(patients.getJSONObject(i).getString("idcard"));
  250. accountDO1.setHospital("350205");
  251. accountDO1.setPassword("321321312321");
  252. accountDO1.setHospitalName("海沧区");
  253. accountDO1.setCreateTime(new Date());
  254. accountDO1.setUpdateTime(new Date());
  255. accountDao.save(accountDO1);
  256. }
  257. }
  258. TaskRuleDO taskRuleDO = taskRuleDao.findOne(ruleId);
  259. String taskSql = "select * from wlyy_health_bank_task where transaction_id = '"+taskRuleDO.getId()+"'";
  260. List<TaskDO> taskDOList = jdbcTemplate.query(taskSql,new BeanPropertyRowMapper(TaskDO.class));
  261. List<String> ids = new ArrayList<>();
  262. if(taskDOList != null && taskDOList.size() != 0){
  263. if (taskRuleDO.getPeriod() == 1){
  264. for (int i = 0;i<patients.size();i++){
  265. String patientId = patients.getJSONObject(i).getString("code");
  266. String sql = "select * from wlyy_health_bank_task_patient_detail where patient_id = '"+patientId+"' AND task_id = '"+taskDOList.get(0).getId()+"'";
  267. List<TaskPatientDetailDO> taskPatientDetailDOS = jdbcTemplate.query(sql,new BeanPropertyRowMapper(TaskPatientDetailDO.class));
  268. if (taskPatientDetailDOS ==null || taskPatientDetailDOS.size() == 0){
  269. ids.add(patientId);
  270. }
  271. }
  272. }else if (taskRuleDO.getPeriod() == 0){
  273. for (int i = 0;i<patients.size();i++){
  274. String patientId = patients.getJSONObject(i).getString("code");
  275. SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
  276. Date date = new Date();
  277. String date1 = dateFormat.format(date);
  278. String begin = DateUtils.getMinMonthDate(date1);
  279. String end = DateUtils.getMaxMonthDate(date1);
  280. String sql = "select * from wlyy_health_bank_task_patient_detail where patient_id = '"+patientId+"' AND task_id = '"+taskDOList.get(0).getId()+"' AND create_time > '"+begin+"' AND create_time < '"+end+"'";
  281. List<TaskPatientDetailDO> taskPatientDetailDOS = jdbcTemplate.query(sql,new BeanPropertyRowMapper(TaskPatientDetailDO.class));
  282. if (taskPatientDetailDOS ==null || taskPatientDetailDOS.size() == 0){
  283. ids.add(patientId);
  284. }
  285. }
  286. }
  287. }else {
  288. for (int i = 0;i<patients.size();i++){
  289. String patientId = patients.getJSONObject(i).getString("code");
  290. ids.add(patientId);
  291. }
  292. }
  293. StringBuffer buffer = new StringBuffer();
  294. buffer.append("(");
  295. if (ids == null || ids.size() == 0){
  296. buffer.append("''");
  297. }else {
  298. for (int i=0;i<ids.size();i++){
  299. buffer.append("'"+ids.get(i)+"'").append(",");
  300. }
  301. buffer.deleteCharAt(buffer.length()-1);
  302. }
  303. buffer.append(") ");
  304. /* StringBuffer buffer1 = new StringBuffer();//设备类型
  305. buffer1.append("(");
  306. if (deviceTypes == null || deviceTypes.size() == 0){
  307. buffer1.append("''");
  308. }else {
  309. for (int i=0;i<deviceTypes.size();i++){
  310. buffer1.append("'"+deviceTypes.get(i)+"'").append(",");
  311. }
  312. buffer1.deleteCharAt(buffer1.length()-1);
  313. }
  314. buffer1.append(")");
  315. String sql1 = null ;
  316. if (bindStatus == -1){
  317. sql1 = "( SELECT btpd.patient_id AS patient_id FROM " +
  318. " wlyy_health_bank_task_patient_detail btpd " +
  319. " LEFT JOIN wlyy_health_bank_task bt ON bt.id = btpd.task_id WHERE " +
  320. " bt.task_code NOT IN "+buffer1+" and "+buffer+")";
  321. }else if (bindStatus == 1){
  322. sql1 = "( SELECT btpd.patient_id AS patient_id FROM " +
  323. " wlyy_health_bank_task_patient_detail btpd " +
  324. " LEFT JOIN wlyy_health_bank_task bt ON bt.id = btpd.task_id WHERE " +
  325. " bt.task_code IN "+buffer1 +" and "+buffer+")";
  326. }*/
  327. String sql =
  328. "SELECT ba1.patient_id AS patient_id," +
  329. "ba1.account_name AS account_name," +
  330. "ba1.hospital AS hospital," +
  331. "ba1.total AS total," +
  332. "ba1.create_time AS create_time," +
  333. "ba1.sum AS sum" +
  334. " FROM" +
  335. "( SELECT " +
  336. "ba.patient_id AS patient_id," +
  337. "ba.account_name AS account_name," +
  338. "ba.hospital AS hospital," +
  339. "ba.total AS total," +
  340. "ba.create_time AS create_time," +
  341. "(ba.total +COALESCE((cd1.total),0)) AS sum" +
  342. " FROM" +
  343. " wlyy_health_bank_account ba" +
  344. " LEFT JOIN ( " +
  345. "SELECT" +
  346. " SUM(cd.integrate) AS total," +
  347. " cd.patient_id AS patient_id" +
  348. " FROM" +
  349. " wlyy_health_bank_credits_detail cd" +
  350. " WHERE " +
  351. "cd.trade_direction = - 1" +
  352. " GROUP BY " +
  353. " cd.patient_id ) cd1 ON cd1.patient_id = ba.patient_id " +
  354. " WHERE ba.patient_id IN " + buffer +
  355. " ORDER BY" +
  356. " ba.create_time DESC " +
  357. "LIMIT "+(page-1)*size+","+size +")ba1" +
  358. " ORDER BY " +
  359. " ba1.sum DESC";
  360. List<AccountDO> accountDOS = jdbcTemplate.query(sql,new BeanPropertyRowMapper(AccountDO.class));
  361. String sqlCount = "SELECT count(1) AS total"+
  362. " FROM " +
  363. " wlyy_health_bank_account ba LEFT JOIN " +
  364. " ( " +
  365. " SELECT " +
  366. " SUM(cd.integrate) AS total, " +
  367. " cd.patient_id AS patient_id " +
  368. " FROM " +
  369. " wlyy_health_bank_credits_detail cd " +
  370. " WHERE " +
  371. " cd.trade_direction = - 1 " +
  372. " GROUP BY " +
  373. " cd.patient_id " +
  374. " ) cd1 ON cd1.patient_id = ba.patient_id " +
  375. "WHERE ba.patient_id IN " + buffer +
  376. " ORDER BY " +
  377. " ba.create_time, " +
  378. " (ba.total + COALESCE(cd1.total,0)) DESC ";
  379. List<Map<String,Object>> rstotal = jdbcTemplate.queryForList(sqlCount);
  380. Long count = 0L;
  381. if(rstotal!=null&&rstotal.size()>0){
  382. count = (Long) rstotal.get(0).get("total");
  383. }
  384. return MixEnvelop.getSuccessListWithPage(HealthBankMapping.api_success, accountDOS,page,size,count);
  385. }
  386. /**
  387. * 根据居民code获取积分
  388. *
  389. * @param patient
  390. * @return
  391. */
  392. public MixEnvelop<AccountDO, AccountDO> selectByPatient(String patient){
  393. MixEnvelop<AccountDO,AccountDO> envelop = new MixEnvelop<>();
  394. String sql = "select * from wlyy_health_bank_account where status = 1 and patient_id = '"+patient+"'";
  395. List<AccountDO> accountDOS = jdbcTemplate.query(sql,new BeanPropertyRowMapper(AccountDO.class));
  396. envelop.setObj(accountDOS.get(0));
  397. return envelop;
  398. }
  399. }