AccountService.java 18 KB

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