AccountService.java 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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. count = Long.parseLong(object.toString());
  75. }
  76. accountDO1.setNowTotal(count);
  77. String activitySql1 =" SELECT " +
  78. " SUM(ptpd.total) AS total FROM wlyy_health_bank_task_patient_detail ptpd " +
  79. " WHERE " +" task_id = '" + accountDO.getTaskId() + "' " +
  80. "and patient_id = '"+accountDO1.getPatientId()+"'";
  81. List<Map<String,Object>> rstotal6 = jdbcTemplate.queryForList(activitySql1);
  82. Long activityIntegrate = 0L;
  83. if(rstotal6!=null&&rstotal6.size()>0){
  84. Object object = rstotal6.get(0).get("total");
  85. activityIntegrate = Long.parseLong(object.toString());
  86. }
  87. accountDO1.setActivityIntegrate(activityIntegrate);
  88. String activitySql = "SELECT COUNT(*) AS total FROM ( SELECT * FROM " +
  89. "wlyy_health_bank_task_patient_detail " +
  90. " WHERE patient_id = '" + accountDO1.getPatientId()+
  91. "'AND activity_id != '' GROUP BY activity_id ) btpd1";
  92. List<Map<String,Object>> rstotal1 = jdbcTemplate.queryForList(activitySql);
  93. Long activityCount = 0L;
  94. if(rstotal1!=null&&rstotal1.size()>0){
  95. Object object = rstotal1.get(0).get("total");
  96. activityCount = Long.parseLong(object.toString());
  97. }
  98. accountDO1.setActivityTotal(activityCount);
  99. String taskSql = "SELECT COUNT(*) AS total FROM ( SELECT * FROM " +
  100. "wlyy_health_bank_task_patient_detail " +
  101. " WHERE patient_id = '" + accountDO1.getPatientId()+
  102. "' GROUP BY task_id ) btpd1";
  103. List<Map<String,Object>> rstotal2 = jdbcTemplate.queryForList(taskSql);
  104. Long taskCount = 0L;
  105. if(rstotal2!=null&&rstotal2.size()>0){
  106. Object object = rstotal2.get(0).get("total");
  107. taskCount = Long.parseLong(object.toString());
  108. }
  109. accountDO1.setTaskTotal(taskCount);
  110. if (accountDO.getPatientIds() != null && accountDO.getPatientIds().size() != 0){
  111. StringBuffer buffer = new StringBuffer();
  112. buffer.append(" (");
  113. for (int i=0;i<accountDO.getPatientIds().size();i++){
  114. buffer.append("'"+accountDO.getPatientIds().get(i)+"'").append(",");
  115. }
  116. buffer.deleteCharAt(buffer.length()-1);
  117. buffer.append(") ");
  118. String accountSql = "select * from wlyy_health_bank_account where patient_id in "+buffer;
  119. List<AccountDO> accountDOSList = jdbcTemplate.query(accountSql,new BeanPropertyRowMapper<>(AccountDO.class));
  120. if (accountDOSList != null && accountDOSList.size() !=0){
  121. String sql2 = " SELECT " +
  122. " ba.total + COALESCE (bacd1.sum, 0) AS total " +
  123. " FROM " +
  124. " wlyy_health_bank_account ba " +
  125. " LEFT JOIN ( " +
  126. " SELECT " +
  127. " COALESCE (SUM(bacd.integrate), 0) AS sum, " +
  128. " bacd.account_id " +
  129. " FROM " +
  130. " wlyy_health_bank_credits_detail bacd " +
  131. " WHERE " +
  132. " bacd.trade_direction = - 1 " +
  133. " GROUP BY " +
  134. " bacd.account_id ) bacd1 ON ba.id = bacd1.account_id " +
  135. " WHERE " +
  136. " ba.id = '"+accountDO1.getId()+"'";
  137. List<Map<String,Object>> rstotal3 = jdbcTemplate.queryForList(sql2);
  138. Long Count = 0L;
  139. if(rstotal3!=null&&rstotal3.size()>0){
  140. Object object = rstotal3.get(0).get("total");
  141. Count = Long.parseLong(object.toString());
  142. }
  143. accountDO1.setSum(Count);
  144. StringBuffer buffer1 = new StringBuffer();
  145. buffer1.append(" (");
  146. for (int i=0;i<accountDOSList.size();i++){
  147. buffer1.append("'"+accountDOSList.get(i).getId()+"'").append(",");
  148. }
  149. buffer1.deleteCharAt(buffer1.length()-1);
  150. buffer1.append(") ");
  151. String sql3 = "SELECT " +
  152. " COUNT(1) + 1 AS total " +
  153. "FROM " +
  154. " ( " +
  155. " SELECT " +
  156. " ba.id AS id, " +
  157. " ba.total + COALESCE (bacd1.sum, 0) AS sum " +
  158. " FROM " +
  159. " wlyy_health_bank_account ba " +
  160. " LEFT JOIN ( " +
  161. " SELECT " +
  162. " COALESCE (SUM(bacd.integrate), 0) AS sum, " +
  163. " bacd.account_id " +
  164. " FROM " +
  165. " wlyy_health_bank_credits_detail bacd " +
  166. " WHERE " +
  167. " bacd.trade_direction = - 1 " +
  168. " GROUP BY " +
  169. " bacd.account_id ) bacd1 ON ba.id = bacd1.account_id " +
  170. " WHERE " +
  171. " ba.id IN " +buffer1+
  172. " )ba1 WHERE ba1.sum > "+accountDO1.getSum();
  173. List<Map<String,Object>> rstotal4 = jdbcTemplate.queryForList(sql3);
  174. Integer Count1 = 0;
  175. if(rstotal4!=null&&rstotal4.size()>0){
  176. Object object = rstotal4.get(0).get("total");
  177. Count1 = Integer.parseInt(object.toString());
  178. }
  179. accountDO1.setTeamRanking(Count1);
  180. String sql4 = "SELECT " +
  181. " COUNT(1) + 1 AS total " +
  182. "FROM " +
  183. " ( " +
  184. " SELECT " +
  185. " ba.id AS id, " +
  186. " ba.total + COALESCE (bacd1.sum, 0) AS sum " +
  187. " FROM " +
  188. " wlyy_health_bank_account ba " +
  189. " LEFT JOIN ( " +
  190. " SELECT " +
  191. " COALESCE (SUM(bacd.integrate), 0) AS sum, " +
  192. " bacd.account_id " +
  193. " FROM " +
  194. " wlyy_health_bank_credits_detail bacd " +
  195. " WHERE " +
  196. " bacd.trade_direction = - 1 " +
  197. " GROUP BY " +
  198. " bacd.account_id ) bacd1 ON ba.id = bacd1.account_id " +
  199. " )ba1 WHERE ba1.sum > "+accountDO1.getSum();
  200. List<Map<String,Object>> rstotal5 = jdbcTemplate.queryForList(sql4);
  201. Integer Count2 = 0;
  202. if(rstotal5!=null&&rstotal5.size()>0){
  203. Object object = rstotal5.get(0).get("total");
  204. Count2 = Integer.parseInt(object.toString());
  205. }
  206. accountDO1.setCityRanking(Count2);
  207. }
  208. }
  209. }
  210. String sqlcount = new ISqlUtils().getSql(accountDO,0,0,"count");
  211. List<Map<String,Object>> rstotal = jdbcTemplate.queryForList(sqlcount);
  212. Long count = 0L;
  213. if(rstotal!=null&&rstotal.size()>0){
  214. count = (Long) rstotal.get(0).get("total");
  215. }
  216. return Envelop.getSuccessListWithPage(HealthBankMapping.api_success,accountDOS,page,size,count);
  217. }
  218. /**
  219. * 根据条件活动用户信息
  220. *
  221. * @param page 页码
  222. *
  223. * @param size 分页大小
  224. * @return
  225. */
  226. public Envelop<AccountDO> findByCondition1(JSONArray patients, String ruleId, Integer page, Integer size){
  227. for (int i=0;i<patients.size();i++){
  228. String patientId = patients.getJSONObject(i).getString("code");
  229. String sql = "select * from wlyy_health_bank_account where patient_id = '"+patientId+"'";
  230. List<AccountDO> accountDOS = jdbcTemplate.query(sql,new BeanPropertyRowMapper(AccountDO.class));
  231. if (accountDOS == null || accountDOS.size() == 0) {
  232. AccountDO accountDO1 = new AccountDO();
  233. accountDO1.setPatientId(patientId);
  234. accountDO1.setTotal(0);
  235. accountDO1.setAccountName(patients.getJSONObject(i).getString("name"));
  236. accountDO1.setCardNumber(patients.getJSONObject(i).getString("idcard"));
  237. accountDO1.setHospital("350205");
  238. accountDO1.setPassword("321321312321");
  239. accountDO1.setHospitalName("海沧区");
  240. accountDO1.setCreateTime(new Date());
  241. accountDO1.setUpdateTime(new Date());
  242. accountDao.save(accountDO1);
  243. }
  244. }
  245. TaskRuleDO taskRuleDO = taskRuleDao.findOne(ruleId);
  246. String taskSql = "select * from wlyy_health_bank_task where transaction_id = '"+taskRuleDO.getId()+"'";
  247. List<TaskDO> taskDOList = jdbcTemplate.query(taskSql,new BeanPropertyRowMapper(TaskDO.class));
  248. List<String> ids = new ArrayList<>();
  249. if(taskDOList != null && taskDOList.size() != 0){
  250. if (taskRuleDO.getPeriod() == 1){
  251. for (int i = 0;i<patients.size();i++){
  252. String patientId = patients.getJSONObject(i).getString("code");
  253. String sql = "select * from wlyy_health_bank_task_patient_detail where patient_id = '"+patientId+"' AND task_id = '"+taskDOList.get(0).getId()+"'";
  254. List<TaskPatientDetailDO> taskPatientDetailDOS = jdbcTemplate.query(sql,new BeanPropertyRowMapper(TaskPatientDetailDO.class));
  255. if (taskPatientDetailDOS ==null || taskPatientDetailDOS.size() == 0){
  256. ids.add(patientId);
  257. }
  258. }
  259. }else if (taskRuleDO.getPeriod() == 0){
  260. for (int i = 0;i<patients.size();i++){
  261. String patientId = patients.getJSONObject(i).getString("code");
  262. SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
  263. Date date = new Date();
  264. String date1 = dateFormat.format(date);
  265. String begin = DateUtils.getMinMonthDate(date1);
  266. String end = DateUtils.getMaxMonthDate(date1);
  267. 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+"'";
  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. }
  274. }else {
  275. for (int i = 0;i<patients.size();i++){
  276. String patientId = patients.getJSONObject(i).getString("code");
  277. ids.add(patientId);
  278. }
  279. }
  280. StringBuffer buffer = new StringBuffer();
  281. buffer.append("(");
  282. if (ids == null || ids.size() == 0){
  283. buffer.append("''");
  284. }else {
  285. for (int i=0;i<ids.size();i++){
  286. buffer.append("'"+ids.get(i)+"'").append(",");
  287. }
  288. buffer.deleteCharAt(buffer.length()-1);
  289. }
  290. buffer.append(") ");
  291. /* StringBuffer buffer1 = new StringBuffer();//设备类型
  292. buffer1.append("(");
  293. if (deviceTypes == null || deviceTypes.size() == 0){
  294. buffer1.append("''");
  295. }else {
  296. for (int i=0;i<deviceTypes.size();i++){
  297. buffer1.append("'"+deviceTypes.get(i)+"'").append(",");
  298. }
  299. buffer1.deleteCharAt(buffer1.length()-1);
  300. }
  301. buffer1.append(")");
  302. String sql1 = null ;
  303. if (bindStatus == -1){
  304. sql1 = "( SELECT btpd.patient_id AS patient_id FROM " +
  305. " wlyy_health_bank_task_patient_detail btpd " +
  306. " LEFT JOIN wlyy_health_bank_task bt ON bt.id = btpd.task_id WHERE " +
  307. " bt.task_code NOT IN "+buffer1+" and "+buffer+")";
  308. }else if (bindStatus == 1){
  309. sql1 = "( SELECT btpd.patient_id AS patient_id FROM " +
  310. " wlyy_health_bank_task_patient_detail btpd " +
  311. " LEFT JOIN wlyy_health_bank_task bt ON bt.id = btpd.task_id WHERE " +
  312. " bt.task_code IN "+buffer1 +" and "+buffer+")";
  313. }*/
  314. String sql =
  315. "SELECT ba1.patient_id AS patient_id," +
  316. "ba1.account_name AS account_name," +
  317. "ba1.hospital AS hospital," +
  318. "ba1.total AS total," +
  319. "ba1.create_time AS create_time," +
  320. "ba1.sum AS sum" +
  321. " FROM" +
  322. "( SELECT " +
  323. "ba.patient_id AS patient_id," +
  324. "ba.account_name AS account_name," +
  325. "ba.hospital AS hospital," +
  326. "ba.total AS total," +
  327. "ba.create_time AS create_time," +
  328. "(ba.total +COALESCE((cd1.total),0)) AS sum" +
  329. " FROM" +
  330. " wlyy_health_bank_account ba" +
  331. " LEFT JOIN ( " +
  332. "SELECT" +
  333. " SUM(cd.integrate) AS total," +
  334. " cd.patient_id AS patient_id" +
  335. " FROM" +
  336. " wlyy_health_bank_credits_detail cd" +
  337. " WHERE " +
  338. "cd.trade_direction = - 1" +
  339. " GROUP BY " +
  340. " cd.patient_id ) cd1 ON cd1.patient_id = ba.patient_id " +
  341. " WHERE ba.patient_id IN " + buffer +
  342. " ORDER BY" +
  343. " ba.create_time DESC " +
  344. "LIMIT "+(page-1)*size+","+size +")ba1" +
  345. " ORDER BY " +
  346. " ba1.sum DESC";
  347. List<AccountDO> accountDOS = jdbcTemplate.query(sql,new BeanPropertyRowMapper(AccountDO.class));
  348. String sqlCount = "SELECT count(1) AS total"+
  349. " FROM " +
  350. " wlyy_health_bank_account ba LEFT JOIN " +
  351. " ( " +
  352. " SELECT " +
  353. " SUM(cd.integrate) AS total, " +
  354. " cd.patient_id AS patient_id " +
  355. " FROM " +
  356. " wlyy_health_bank_credits_detail cd " +
  357. " WHERE " +
  358. " cd.trade_direction = - 1 " +
  359. " GROUP BY " +
  360. " cd.patient_id " +
  361. " ) cd1 ON cd1.patient_id = ba.patient_id " +
  362. "WHERE ba.patient_id IN " + buffer +
  363. " ORDER BY " +
  364. " ba.create_time, " +
  365. " (ba.total + COALESCE(cd1.total,0)) DESC ";
  366. List<Map<String,Object>> rstotal = jdbcTemplate.queryForList(sqlCount);
  367. Long count = 0L;
  368. if(rstotal!=null&&rstotal.size()>0){
  369. count = (Long) rstotal.get(0).get("total");
  370. }
  371. return Envelop.getSuccessListWithPage(HealthBankMapping.api_success, accountDOS,page,size,count);
  372. }
  373. }