AccountService.java 19 KB

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