CreditsDetailService.java 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829
  1. package com.yihu.jw.service;/**
  2. * Created by nature of king on 2018/4/27.
  3. */
  4. import com.alibaba.fastjson.JSONArray;
  5. import com.alibaba.fastjson.JSONObject;
  6. import com.yihu.base.mysql.query.BaseJpaService;
  7. import com.yihu.jw.dao.*;
  8. import com.yihu.jw.entity.health.bank.*;
  9. import com.yihu.jw.restmodel.common.Envelop;
  10. import com.yihu.jw.rm.health.bank.HealthBankMapping;
  11. import com.yihu.jw.util.DateUtils;
  12. import com.yihu.jw.util.ISqlUtils;
  13. import org.slf4j.Logger;
  14. import org.slf4j.LoggerFactory;
  15. import org.springframework.beans.factory.annotation.Autowired;
  16. import org.springframework.jdbc.core.BeanPropertyRowMapper;
  17. import org.springframework.jdbc.core.JdbcTemplate;
  18. import org.springframework.stereotype.Service;
  19. import javax.transaction.Transactional;
  20. import java.text.ParseException;
  21. import java.text.SimpleDateFormat;
  22. import java.util.ArrayList;
  23. import java.util.Date;
  24. import java.util.List;
  25. import java.util.Map;
  26. /**
  27. * @author wangzhinan
  28. * @create 2018-04-27 16:53
  29. * @desc credits log info Service
  30. **/
  31. @Service
  32. @Transactional
  33. public class CreditsDetailService extends BaseJpaService<CreditsDetailDO,CredittsLogDetailDao> {
  34. private Logger logger = LoggerFactory.getLogger(CreditsDetailService.class);
  35. @Autowired
  36. private CredittsLogDetailDao credittsLogDetailDao;
  37. @Autowired
  38. private TaskDao taskDao;
  39. @Autowired
  40. private ActivityDao activityDao;
  41. @Autowired
  42. private JdbcTemplate jdbcTemplate;
  43. @Autowired
  44. private AccountDao accountDao;
  45. @Autowired
  46. private TaskPatientDetailDao taskPatientDetailDao;
  47. @Autowired
  48. private TaskRuleDao taskRuleDao;
  49. /**
  50. * find creditsLogInfo
  51. *
  52. * @return
  53. * @throws ParseException
  54. */
  55. public Envelop<CreditsDetailDO> findByCondition(CreditsDetailDO creditsDetailDO, Integer page, Integer size) throws ParseException {
  56. String sql = new ISqlUtils().getSql(creditsDetailDO,page,size,"*");
  57. List<CreditsDetailDO> creditsDetailDOS = jdbcTemplate.query(sql,new BeanPropertyRowMapper(CreditsDetailDO.class));
  58. for (CreditsDetailDO creditsDetailDO1 : creditsDetailDOS){
  59. if (creditsDetailDO1.getTradeType() != null && creditsDetailDO1.getTradeType().equalsIgnoreCase("HEALTH_TASK")){
  60. TaskDO taskDO = taskDao.findOne(creditsDetailDO1.getTransactionId());
  61. creditsDetailDO1.setTaskDO(taskDO);
  62. }
  63. }
  64. String sqlcount = new ISqlUtils().getSql(creditsDetailDO,0,0,"count");
  65. List<Map<String,Object>> rstotal = jdbcTemplate.queryForList(sqlcount);
  66. Long count = 0L;
  67. if(rstotal!=null&&rstotal.size()>0){
  68. count = (Long) rstotal.get(0).get("total");
  69. }
  70. return Envelop.getSuccessListWithPage(HealthBankMapping.api_success, creditsDetailDOS,page,size,count);
  71. }
  72. /**
  73. * 获取账户信息
  74. *
  75. * @param creditsDetailDO
  76. * @return
  77. */
  78. public Envelop<AccountDO> findByTradeDirection(CreditsDetailDO creditsDetailDO){
  79. AccountDO accountDO1 = new AccountDO();
  80. accountDO1.setPatientId(creditsDetailDO.getPatientId());
  81. String sql1 = ISqlUtils.getAllSql(accountDO1);
  82. List<AccountDO> accountDOS = jdbcTemplate.query(sql1,new BeanPropertyRowMapper(AccountDO.class));
  83. if (accountDOS == null || accountDOS.size() == 0){
  84. accountDO1.setTotal(0);
  85. accountDO1.setAccountName(creditsDetailDO.getName());
  86. accountDO1.setCardNumber("jw");
  87. accountDO1.setHospital("海沧区");
  88. accountDO1.setPassword("321321312321");
  89. accountDO1.setHospitalName("haichan");
  90. accountDO1.setCreateTime(new Date());
  91. accountDO1.setUpdateTime(new Date());
  92. accountDao.save(accountDO1);
  93. }
  94. List<AccountDO> accountDOS1 = jdbcTemplate.query(sql1,new BeanPropertyRowMapper(AccountDO.class));
  95. AccountDO accountDO = accountDOS1.get(0);
  96. String sql = "SELECT SUM(cd.integrate) as total FROM wlyy_health_bank_credits_detail cd where cd.trade_direction = "+creditsDetailDO.getTradeDirection() +" AND cd.patient_id = '" +creditsDetailDO.getPatientId()+"'";
  97. List<Map<String,Object>> rstotal = jdbcTemplate.queryForList(sql);
  98. if (rstotal!= null && rstotal.size()>0){
  99. if (rstotal.get(0).get("total") == null){
  100. accountDO.setUsedTotal(0);
  101. }else {
  102. accountDO.setUsedTotal(Integer.parseInt(rstotal.get(0).get("total").toString()));
  103. }
  104. }
  105. return Envelop.getSuccess(HealthBankMapping.api_success,accountDO);
  106. }
  107. /*
  108. public Envelop<Boolean> exchangeGoods(GoodsDO goodsDO){
  109. CreditsDetailDO creditsLogDetailDO = new CreditsDetailDO();
  110. Envelop<Boolean> envelop = new Envelop<>();
  111. envelop.setObj(true);
  112. return envelop;
  113. }
  114. */
  115. public Envelop<AccountDO> selectByRanking(List<String> patientIds, Integer page, Integer size){
  116. StringBuffer buffer = new StringBuffer();
  117. buffer.append(" ba.patient_id in(");
  118. if (patientIds == null || patientIds.size() == 0){
  119. buffer.append("''");
  120. }else {
  121. for (int i=0;i<patientIds.size();i++){
  122. buffer.append("'"+patientIds.get(i)+"'").append(",");
  123. }
  124. buffer.deleteCharAt(buffer.length()-1);
  125. }
  126. buffer.append(") ");
  127. String sql =
  128. "SELECT ba1.patient_id AS patient_id," +
  129. "ba1.account_name AS account_name," +
  130. "ba1.hospital AS hospital," +
  131. "ba1.total AS total," +
  132. "ba1.create_time AS create_time," +
  133. "ba1.sum AS sum" +
  134. " FROM" +
  135. "( SELECT " +
  136. "ba.patient_id AS patient_id," +
  137. "ba.account_name AS account_name," +
  138. "ba.hospital AS hospital," +
  139. "ba.total AS total," +
  140. "ba.create_time AS create_time," +
  141. " if(ba.total=0,ba.total,(ba.total +COALESCE((cd1.total),0))) AS sum" +
  142. " FROM" +
  143. " wlyy_health_bank_account ba" +
  144. " LEFT JOIN ( " +
  145. "SELECT" +
  146. " SUM(cd.integrate) AS total," +
  147. " cd.patient_id AS patient_id" +
  148. " FROM" +
  149. " wlyy_health_bank_credits_detail cd" +
  150. " WHERE " +
  151. "cd.trade_direction = - 1" +
  152. " GROUP BY " +
  153. " cd.patient_id ) cd1 ON cd1.patient_id = ba.patient_id " +
  154. " WHERE " + buffer +
  155. " ORDER BY" +
  156. " ba.create_time DESC " +
  157. "LIMIT "+(page-1)*size+","+size +")ba1" +
  158. " ORDER BY " +
  159. " ba1.sum DESC";
  160. List<AccountDO> accountDOS = jdbcTemplate.query(sql,new BeanPropertyRowMapper(AccountDO.class));
  161. String sqlCount = "SELECT count(1) AS total"+
  162. " FROM " +
  163. " wlyy_health_bank_account ba LEFT JOIN " +
  164. " ( " +
  165. " SELECT " +
  166. " SUM(cd.integrate) AS total, " +
  167. " cd.patient_id AS patient_id " +
  168. " FROM " +
  169. " wlyy_health_bank_credits_detail cd " +
  170. " WHERE " +
  171. " cd.trade_direction = - 1 " +
  172. " GROUP BY " +
  173. " cd.patient_id " +
  174. " ) cd1 ON cd1.patient_id = ba.patient_id " +
  175. "WHERE " + buffer +
  176. " ORDER BY " +
  177. " ba.create_time, " +
  178. " (ba.total + COALESCE(cd1.total,0)) DESC ";
  179. List<Map<String,Object>> rstotal = jdbcTemplate.queryForList(sqlCount);
  180. Long count = 0L;
  181. if(rstotal!=null&&rstotal.size()>0){
  182. count = (Long) rstotal.get(0).get("total");
  183. }
  184. return Envelop.getSuccessListWithPage(HealthBankMapping.api_success, accountDOS,page,size,count);
  185. }
  186. /**
  187. * 添加积分
  188. *
  189. * @param creditsDetailDO
  190. * @return
  191. */
  192. public Envelop<CreditsDetailDO> insert(CreditsDetailDO creditsDetailDO){
  193. try {
  194. synchronized (creditsDetailDO.getPatientId()){
  195. String sqlAccount = "select * from wlyy_health_bank_account ba where ba.patient_id = '"+creditsDetailDO.getPatientId() +"'";
  196. List<AccountDO> accountDOList = jdbcTemplate.query(sqlAccount,new BeanPropertyRowMapper(AccountDO.class));
  197. if (accountDOList != null && accountDOList.size() != 0){
  198. creditsDetailDO.setAccountId(accountDOList.get(0).getId());
  199. }else {
  200. AccountDO accountDO1 = new AccountDO();
  201. accountDO1.setPatientId(creditsDetailDO.getPatientId());
  202. accountDO1.setTotal(0);
  203. accountDO1.setAccountName(creditsDetailDO.getName());
  204. accountDO1.setCardNumber(creditsDetailDO.getIdCard());
  205. accountDO1.setHospital("350205");
  206. accountDO1.setPassword("321321312321");
  207. accountDO1.setHospitalName("海沧区");
  208. accountDO1.setCreateTime(new Date());
  209. accountDO1.setUpdateTime(new Date());
  210. accountDao.save(accountDO1);
  211. List<AccountDO> accountDOS = jdbcTemplate.query(sqlAccount,new BeanPropertyRowMapper(AccountDO.class));
  212. creditsDetailDO.setAccountId(accountDOS.get(0).getId());
  213. }
  214. TaskDO taskDO = new TaskDO();
  215. taskDO.setTaskCode(creditsDetailDO.getFlag());
  216. taskDO.setId(creditsDetailDO.getTransactionId());
  217. /*taskDO.setPatientId(creditsDetailDO.getPatientId());*/
  218. String sql = ISqlUtils.getSql(taskDO,1,1,"*");
  219. List<TaskDO> taskDOList = jdbcTemplate.query(sql,new BeanPropertyRowMapper(TaskDO.class));
  220. creditsDetailDO.setTransactionId(taskDOList.get(0).getId());
  221. String ruleSql = "SELECT * FROM wlyy_health_bank_task_rule WHERE id= '"+taskDOList.get(0).getRuleCode()+"'";
  222. List<TaskRuleDO> taskRuleDOS = jdbcTemplate.query(ruleSql,new BeanPropertyRowMapper(TaskRuleDO.class));
  223. TaskRuleDO taskRuleDO = taskRuleDOS.get(0);
  224. creditsDetailDO.setIntegrate(taskRuleDO.getIntegrate());
  225. creditsDetailDO.setTradeDirection(taskRuleDO.getTradeDirection());
  226. String taskSql = "select * from wlyy_health_bank_task_patient_detail where task_id = '"+taskDOList.get(0).getId()+"' and patient_id = '" + creditsDetailDO.getPatientId() +"'";
  227. List<TaskPatientDetailDO> taskPatientDetailDOS = jdbcTemplate.query(taskSql,new BeanPropertyRowMapper(TaskPatientDetailDO.class));
  228. if (taskPatientDetailDOS == null || taskPatientDetailDOS.size() ==0){
  229. TaskPatientDetailDO taskPatientDetailDO = new TaskPatientDetailDO();
  230. taskPatientDetailDO.setTaskId(creditsDetailDO.getTransactionId());
  231. taskPatientDetailDO.setSaasId(creditsDetailDO.getSaasId());
  232. taskPatientDetailDO.setPatientId(creditsDetailDO.getPatientId());
  233. taskPatientDetailDO.setPatientIdcard(creditsDetailDO.getIdCard());
  234. taskPatientDetailDO.setPatientOpenid(creditsDetailDO.getOpenId());
  235. taskPatientDetailDO.setUnionId(creditsDetailDO.getUnionId());
  236. taskPatientDetailDO.setStatus(Integer.parseInt("0"));
  237. taskPatientDetailDO.setCreateTime(new Date());
  238. taskPatientDetailDO.setUpdateTime(new Date());
  239. taskPatientDetailDO.setActivityId(taskDO.getTransactionId());
  240. taskPatientDetailDO.setTotal(Long.parseLong("0"));
  241. taskPatientDetailDao.save(taskPatientDetailDO);
  242. }else if (taskPatientDetailDOS != null && taskDOList.get(0).getPeriod() == 0){
  243. String taskSql1 = "select * from wlyy_health_bank_task_patient_detail where task_id = '"+taskDOList.get(0).getId()+
  244. "' and patient_id = '"+creditsDetailDO.getPatientId()+"' and create_time > '" + DateUtils.getDayBegin() +"' and create_time < '"+ DateUtils.getDayEnd() +"'";
  245. List<TaskPatientDetailDO> taskPatientDetailDOS1 = jdbcTemplate.query(taskSql1,new BeanPropertyRowMapper(TaskPatientDetailDO.class));
  246. if (taskPatientDetailDOS1 == null || taskPatientDetailDOS1.size() == 0){
  247. TaskPatientDetailDO taskPatientDetailDO = new TaskPatientDetailDO();
  248. taskPatientDetailDO.setTaskId(creditsDetailDO.getTransactionId());
  249. taskPatientDetailDO.setSaasId(creditsDetailDO.getSaasId());
  250. taskPatientDetailDO.setPatientId(creditsDetailDO.getPatientId());
  251. taskPatientDetailDO.setPatientIdcard(creditsDetailDO.getIdCard());
  252. taskPatientDetailDO.setPatientOpenid(creditsDetailDO.getOpenId());
  253. taskPatientDetailDO.setActivityId(taskDO.getTransactionId());
  254. taskPatientDetailDO.setStatus(Integer.parseInt("0"));
  255. taskPatientDetailDO.setCreateTime(new Date());
  256. taskPatientDetailDO.setUpdateTime(new Date());
  257. taskPatientDetailDO.setTotal(Long.parseLong("0"));
  258. taskPatientDetailDao.save(taskPatientDetailDO);
  259. }
  260. }
  261. creditsDetailDO.setCreateTime(new Date());
  262. creditsDetailDO.setUpdateTime(new Date());
  263. CreditsDetailDO creditsDetailDO1 =credittsLogDetailDao.save(creditsDetailDO);
  264. creditsDetailDO1.setFlag(creditsDetailDO.getFlag());
  265. List<CreditsDetailDO> creditsDetailDOList = new ArrayList<>();
  266. creditsDetailDOList.add(creditsDetailDO1);
  267. TaskPatientDetailDO taskPatientDetailDO = new TaskPatientDetailDO();
  268. taskPatientDetailDO.setPatientId(creditsDetailDO1.getPatientId());
  269. taskPatientDetailDO.setTaskId(creditsDetailDO1.getTransactionId());
  270. String taskSql1 = ISqlUtils.getAllSql(taskPatientDetailDO);
  271. List<TaskPatientDetailDO> taskPatientDetailDOS1 = jdbcTemplate.query(taskSql1,new BeanPropertyRowMapper(TaskPatientDetailDO.class));
  272. TaskPatientDetailDO taskPatientDetailDO1 = taskPatientDetailDOS1.get(0);
  273. if (creditsDetailDO1.getTradeDirection() == 1){
  274. taskPatientDetailDO1.setTotal(taskPatientDetailDO1.getTotal()+creditsDetailDO1.getIntegrate());
  275. }else if (creditsDetailDO.getTradeDirection() == -1){
  276. taskPatientDetailDO1.setTotal(taskPatientDetailDO1.getTotal()-creditsDetailDO1.getIntegrate());
  277. }
  278. taskPatientDetailDao.save(taskPatientDetailDO1);
  279. AccountDO accountDO = accountDao.findOne(creditsDetailDO1.getAccountId());
  280. if (creditsDetailDO1.getTradeDirection() == 1){
  281. accountDO.setTotal(accountDO.getTotal()+creditsDetailDO1.getIntegrate());
  282. }else if (creditsDetailDO.getTradeDirection() == -1){
  283. accountDO.setTotal(accountDO.getTotal()-creditsDetailDO1.getIntegrate());
  284. }
  285. AccountDO accountDO1 = accountDao.save(accountDO);
  286. List<CreditsDetailDO> creditsDetailDOS = new ArrayList<>();
  287. for (CreditsDetailDO creditsDetailDO2:creditsDetailDOList){
  288. creditsDetailDO2.setTotal(accountDO1.getTotal());
  289. creditsDetailDOS.add(creditsDetailDO2);
  290. }
  291. Envelop<CreditsDetailDO> envelop = new Envelop<>();
  292. envelop.setDetailModelList(creditsDetailDOS);
  293. return envelop;
  294. }
  295. }catch (Exception e){
  296. e.printStackTrace();
  297. Envelop<CreditsDetailDO> envelop = new Envelop<>();
  298. return envelop;
  299. }
  300. }
  301. /**
  302. * 活动排名
  303. *
  304. * @param activityId 活动id
  305. * @param ids 微信编码
  306. *
  307. * @param page 页码
  308. *
  309. * @param size 分页大小
  310. * @return
  311. */
  312. public Envelop<TaskPatientDetailDO> selectByActivityRanking(String activityId,List<String> ids,Integer page,Integer size){
  313. StringBuffer buffer = new StringBuffer();
  314. buffer.append("(");
  315. if (ids == null || ids.size() == 0){
  316. buffer.append("''");
  317. }else {
  318. for (int i=0;i<ids.size();i++){
  319. buffer.append("'"+ids.get(i)+"'").append(",");
  320. }
  321. buffer.deleteCharAt(buffer.length()-1);
  322. }
  323. buffer.append(") ");
  324. String sql = "SELECT " +
  325. " * " +
  326. "FROM " +
  327. " ( " +
  328. " SELECT " +
  329. " SUM(ptpd.total) AS total, " +
  330. " ptpd.patient_openid AS patient_openid, " +
  331. " ptpd.task_id AS task_id, " +
  332. " ptpd.activity_id AS activity_id, " +
  333. " ptpd.create_time as create_time, " +
  334. " ptpd.patient_id AS patient_id " +
  335. " FROM " +
  336. " wlyy_health_bank_task_patient_detail ptpd " +
  337. " WHERE " +
  338. " activity_id = '" + activityId +
  339. "' GROUP BY " +
  340. " patient_id " +
  341. " ORDER BY ptpd.create_time DESC " +
  342. " )btpd1 " +
  343. " WHERE patient_id IN "+buffer+
  344. " ORDER BY btpd1.total DESC "+" LIMIT " + (page-1)*size+","+size;
  345. List<TaskPatientDetailDO> taskPatientDetailDOS = jdbcTemplate.query(sql,new BeanPropertyRowMapper(TaskPatientDetailDO.class));
  346. for (TaskPatientDetailDO taskPatientDetailDO : taskPatientDetailDOS){
  347. String accountSql = "select * from wlyy_health_bank_account where patient_id = '"+taskPatientDetailDO.getPatientId()+"'";
  348. List<AccountDO> accountDOS = jdbcTemplate.query(accountSql,new BeanPropertyRowMapper(AccountDO.class));
  349. taskPatientDetailDO.setAccountDO(accountDOS.get(0));
  350. }
  351. String sqlCount = "SELECT " +
  352. " count(1) AS total " +
  353. "FROM " +
  354. " ( " +
  355. " SELECT " +
  356. " SUM(ptpd.total) AS total, " +
  357. " ptpd.patient_openid AS patient_openid, " +
  358. " ptpd.task_id AS task_id, " +
  359. " ptpd.activity_id AS activity_id, " +
  360. " ptpd.create_time as create_time, " +
  361. " ptpd.patient_id AS patient_id " +
  362. " FROM " +
  363. " wlyy_health_bank_task_patient_detail ptpd " +
  364. " WHERE " +
  365. " activity_id = '" + activityId+
  366. "' GROUP BY " +
  367. " patient_openid " +
  368. " ORDER BY ptpd.create_time DESC " +
  369. " )btpd1 " +
  370. "WHERE patient_openid IN "+buffer+
  371. " ORDER BY btpd1.total DESC ";
  372. List<Map<String,Object>> rstotal = jdbcTemplate.queryForList(sqlCount);
  373. Long count = 0L;
  374. if(rstotal!=null&&rstotal.size()>0){
  375. count = (Long) rstotal.get(0).get("total");
  376. }
  377. return Envelop.getSuccessListWithPage(HealthBankMapping.api_success, taskPatientDetailDOS,page,size,count);
  378. }
  379. /**
  380. * 根据活动查询积分
  381. *
  382. * @param activityId 活动id
  383. *
  384. * @param patientId 居民id
  385. *
  386. * @param page 页码
  387. *
  388. * @param size 分页大小
  389. * @return
  390. */
  391. public Envelop<CreditsDetailDO> selectByActivity(String activityId,String patientId,Integer page,Integer size){
  392. String sql="SELECT * " +
  393. "FROM " +
  394. " wlyy_health_bank_credits_detail " +
  395. "WHERE" +
  396. " transaction_id IN ( " +
  397. " SELECT " +
  398. " bt.id " +
  399. " FROM " +
  400. " wlyy_health_bank_task bt " +
  401. " WHERE " +
  402. " transaction_id = '"+activityId +"' " +
  403. " ) " +
  404. " and patient_id = '" +patientId+
  405. "' LIMIT "+(page-1)*size +","+size;
  406. List<CreditsDetailDO> creditsDetailDOS = jdbcTemplate.query(sql,new BeanPropertyRowMapper(CreditsDetailDO.class));
  407. for (CreditsDetailDO creditsDetailDO : creditsDetailDOS){
  408. TaskDO taskDO = taskDao.findOne(creditsDetailDO.getTransactionId());
  409. creditsDetailDO.setTaskDO(taskDO);
  410. }
  411. String sqlcount = "SELECT count(1) AS" +
  412. " total FROM " +
  413. " wlyy_health_bank_credits_detail " +
  414. "WHERE" +
  415. " transaction_id IN ( " +
  416. " SELECT " +
  417. " bt.id " +
  418. " FROM " +
  419. " wlyy_health_bank_task bt " +
  420. " WHERE " +
  421. " transaction_id = '"+activityId +"' " +
  422. " ) " +
  423. " and patient_id = '" +patientId+
  424. "'";
  425. List<Map<String,Object>> rstotal = jdbcTemplate.queryForList(sqlcount);
  426. Long count = 0L;
  427. if(rstotal!=null&&rstotal.size()>0){
  428. count = (Long) rstotal.get(0).get("total");
  429. }
  430. return Envelop.getSuccessListWithPage(HealthBankMapping.api_success,creditsDetailDOS,page,size,count);
  431. }
  432. /**
  433. * 根据活动查找全部排行
  434. *
  435. * @param activityId 活动id
  436. * @param page 页码
  437. * @param size 分页大小
  438. * @return
  439. */
  440. public Envelop<TaskPatientDetailDO> selectByActivityRanking1(String activityId,String patientId,Integer page,Integer size){
  441. String sql = "SELECT " +
  442. " * " +
  443. "FROM " +
  444. " ( " +
  445. " SELECT " +
  446. " SUM(ptpd.total) AS total, " +
  447. " ptpd.patient_openid AS patient_openid, " +
  448. " ptpd.task_id AS task_id, " +
  449. " ptpd.activity_id AS activity_id, " +
  450. " ptpd.create_time as create_time, " +
  451. " ptpd.patient_id AS patient_id " +
  452. " FROM " +
  453. " wlyy_health_bank_task_patient_detail ptpd " +
  454. " WHERE " +
  455. " activity_id = '" + activityId +
  456. "' GROUP BY " +
  457. " patient_openid " +
  458. " ORDER BY ptpd.create_time DESC " +
  459. " )btpd1 " +
  460. " ORDER BY btpd1.total DESC "+" LIMIT " + (page-1)*size+","+size;
  461. List<TaskPatientDetailDO> taskPatientDetailDOS = jdbcTemplate.query(sql,new BeanPropertyRowMapper(TaskPatientDetailDO.class));
  462. logger.info(taskPatientDetailDOS.toString()+""+taskPatientDetailDOS.size());
  463. for (int i = 0;taskPatientDetailDOS != null&&taskPatientDetailDOS.size()!=0 && i<taskPatientDetailDOS.size();i++){
  464. TaskPatientDetailDO taskPatientDetailDO = taskPatientDetailDOS.get(i);
  465. String accountSql = "select * from wlyy_health_bank_account where patient_id = '"+taskPatientDetailDO.getPatientId()+"'";
  466. List<AccountDO> accountDOS = jdbcTemplate.query(accountSql,new BeanPropertyRowMapper(AccountDO.class));
  467. if (taskPatientDetailDOS.get(i).getPatientId().equalsIgnoreCase(patientId)){
  468. /*String taskSql = "select count(1)+1 as total from (" +
  469. "select * from (SELECT " +
  470. " SUM(ptpd.total) AS total, " +
  471. " ptpd.patient_openid AS patient_openid, " +
  472. " ptpd.task_id AS task_id, " +
  473. " ptpd.activity_id AS activity_id, " +
  474. " ptpd.create_time as create_time, " +
  475. " ptpd.patient_id AS patient_id " +
  476. " FROM " +
  477. " wlyy_health_bank_task_patient_detail ptpd " +
  478. " WHERE " +
  479. " activity_id = '" + activityId + "')ptpd1 where" +
  480. " ptpd1.patient_id = '"+patientId+"' AND ptpd1.total > "+taskPatientDetailDOS.get(i).getTotal()+") ptpd2";
  481. List<Map<String,Object>> rstotal1 = jdbcTemplate.queryForList(taskSql);
  482. Long count = 0L;
  483. if(rstotal1!=null&&rstotal1.size()>0){
  484. count = (Long) rstotal1.get(0).get("total");
  485. }
  486. accountDOS.get(0).setActivityRanking(count);*/
  487. taskPatientDetailDO.setIsFlag(1);
  488. }else {
  489. taskPatientDetailDO.setIsFlag(0);
  490. }
  491. taskPatientDetailDO.setAccountDO(accountDOS.get(0));
  492. }
  493. String sqlCount = "SELECT " +
  494. " count(1) AS total " +
  495. "FROM " +
  496. " ( " +
  497. " SELECT " +
  498. " SUM(ptpd.total) AS total, " +
  499. " ptpd.patient_openid AS patient_openid, " +
  500. " ptpd.task_id AS task_id, " +
  501. " ptpd.activity_id AS activity_id, " +
  502. " ptpd.create_time as create_time, " +
  503. " ptpd.patient_id AS patient_id " +
  504. " FROM " +
  505. " wlyy_health_bank_task_patient_detail ptpd " +
  506. " WHERE " +
  507. " activity_id = '" + activityId+
  508. "' GROUP BY " +
  509. " patient_openid " +
  510. " ORDER BY ptpd.create_time DESC " +
  511. " )btpd1 " +
  512. " ORDER BY btpd1.total DESC ";
  513. List<Map<String,Object>> rstotal = jdbcTemplate.queryForList(sqlCount);
  514. Long count = 0L;
  515. if(rstotal!=null&&rstotal.size()>0){
  516. count = (Long) rstotal.get(0).get("total");
  517. }
  518. return Envelop.getSuccessListWithPage(HealthBankMapping.api_success, taskPatientDetailDOS,page,size,count);
  519. }
  520. /**
  521. * 医生主动加分
  522. *
  523. * @param array 居民信息集合
  524. *
  525. * @param ruleId 规则id
  526. * @return
  527. */
  528. public Envelop<Boolean> doctorAddIntegrate(JSONArray array, String ruleId, String description) throws Exception {
  529. Envelop<Boolean> envelop = new Envelop<>();
  530. for (int i=0;i<array.size();i++){
  531. TaskRuleDO taskRuleDO = taskRuleDao.findOne(ruleId);
  532. JSONObject object = array.getJSONObject(i);
  533. String patientId = object.getString("code");
  534. String idCard = object.getString("idcard");
  535. String unionId = object.getString("unionid");
  536. String openId = object.getString("openid");
  537. String taskSql = "select * from wlyy_health_bank_task bt where type = 'RULE_TASK' AND transaction_id = '"+ruleId +"'";
  538. List<TaskDO> taskDOList = jdbcTemplate.query(taskSql,new BeanPropertyRowMapper(TaskDO.class));
  539. TaskDO taskDO1 = new TaskDO();
  540. if (taskDOList.isEmpty() && taskDOList.size() == 0){
  541. TaskDO taskDO = new TaskDO();
  542. taskDO.setTransactionId(taskRuleDO.getId());
  543. taskDO.setTaskCode("RULE");
  544. taskDO.setStatus(1);
  545. taskDO.setRuleCode(taskRuleDO.getId());
  546. taskDO.setType("RULE_TASK");
  547. taskDO.setCreateTime(new Date());
  548. taskDO.setUpdateTime(new Date());
  549. taskDO.setPeriod(taskRuleDO.getPeriod());
  550. taskDO.setSaasId("dev");
  551. taskDO.setTitle(taskRuleDO.getDescription());
  552. taskDO.setContent(taskRuleDO.getDescription());
  553. taskDO1 = taskDao.save(taskDO);
  554. }else {
  555. taskDO1 = taskDOList.get(0);
  556. }
  557. if (taskRuleDO.getPeriod() == 1){
  558. String sql = "select * from wlyy_health_bank_task_patient_detail where patient_id = '"+patientId+"'AND task_id ='"+taskDO1.getId()+"'";
  559. List<TaskPatientDetailDO> taskPatientDetailDOS = jdbcTemplate.query(sql,new BeanPropertyRowMapper(TaskPatientDetailDO.class));
  560. if (taskPatientDetailDOS != null && taskPatientDetailDOS.size() != 0){
  561. throw new Exception("已奖励过");
  562. }
  563. }else if (taskRuleDO.getPeriod() == 0){
  564. SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
  565. Date date = new Date();
  566. String date1 = dateFormat.format(date);
  567. String begin = DateUtils.getMinMonthDate(date1);
  568. String end = DateUtils.getMaxMonthDate(date1);
  569. String sql = "select * from wlyy_health_bank_task_patient_detail where patient_id = '"+patientId+"'AND task_id ='"+taskDO1.getId()+"' AND create_time > '"+begin+"' AND create_time < '"+end+"'";
  570. List<TaskPatientDetailDO> taskPatientDetailDOS = jdbcTemplate.query(sql,new BeanPropertyRowMapper(TaskPatientDetailDO.class));
  571. if (taskPatientDetailDOS != null && taskPatientDetailDOS.size() != 0){
  572. throw new Exception("已奖励过");
  573. }
  574. }
  575. String sql1 = "select * from wlyy_health_bank_task_patient_detail where task_id = '"+taskDO1.getId()+"' " +
  576. "AND patient_idcard = '"+idCard+"' AND patient_openid = '"+openId+"' AND union_id = '"+unionId+"'";
  577. List<TaskPatientDetailDO> taskPatientDetailDOS = jdbcTemplate.query(sql1,new BeanPropertyRowMapper(TaskPatientDetailDO.class));
  578. TaskPatientDetailDO taskPatientDetailDO1 = new TaskPatientDetailDO();
  579. if (taskPatientDetailDOS.isEmpty() && taskPatientDetailDOS.size() == 0){
  580. TaskPatientDetailDO taskPatientDetailDO = new TaskPatientDetailDO();
  581. taskPatientDetailDO.setStatus(1);
  582. taskPatientDetailDO.setTotal(Long.parseLong("0"));
  583. taskPatientDetailDO.setPatientId(patientId);
  584. taskPatientDetailDO.setPatientOpenid(openId);
  585. taskPatientDetailDO.setPatientIdcard(idCard);
  586. taskPatientDetailDO.setUnionId(unionId);
  587. taskPatientDetailDO.setSaasId("dev");
  588. taskPatientDetailDO.setCreateTime(new Date());
  589. taskPatientDetailDO.setUpdateTime(new Date());
  590. taskPatientDetailDO.setTaskId(taskDO1.getId());
  591. taskPatientDetailDO1 = taskPatientDetailDao.save(taskPatientDetailDO);
  592. }else {
  593. taskPatientDetailDO1 = taskPatientDetailDOS.get(0);
  594. }
  595. String sql = "select * from wlyy_health_bank_account where patient_id = '"+patientId+"'";
  596. List<AccountDO> accountDOS = jdbcTemplate.query(sql,new BeanPropertyRowMapper(AccountDO.class));
  597. if (taskRuleDO.getTradeDirection() == -1 && taskRuleDO.getIntegrate() == 0){
  598. String integrateSql = "select * from wlyy_health_bank_credits_detail where patient_id = '"+patientId+"'";
  599. List<CreditsDetailDO> creditsDetailDOS = jdbcTemplate.query(integrateSql,new BeanPropertyRowMapper(CreditsDetailDO.class));
  600. for (CreditsDetailDO creditsDetailDO:creditsDetailDOS){
  601. creditsDetailDO.setStatus(0);
  602. creditsDetailDO.setDescription(description);
  603. credittsLogDetailDao.save(creditsDetailDO);
  604. AccountDO accountDO = accountDOS.get(0);
  605. accountDO.setTotal(0);
  606. accountDao.save(accountDO);
  607. }
  608. CreditsDetailDO creditsDetailDO1 = new CreditsDetailDO();
  609. creditsDetailDO1.setStatus(1);
  610. creditsDetailDO1.setAccountId(accountDOS.get(0).getId());
  611. creditsDetailDO1.setHospital("350205");
  612. creditsDetailDO1.setPatientId(patientId);
  613. creditsDetailDO1.setIntegrate(taskRuleDO.getIntegrate());
  614. creditsDetailDO1.setTradeDirection(taskRuleDO.getTradeDirection());
  615. creditsDetailDO1.setDescription(description);
  616. creditsDetailDO1.setCreateTime(new Date());
  617. creditsDetailDO1.setUpdateTime(new Date());
  618. creditsDetailDO1.setTransactionId(taskDO1.getId());
  619. creditsDetailDO1.setTradeType("HEALTH_TASK");
  620. credittsLogDetailDao.save(creditsDetailDO1);
  621. }else{
  622. CreditsDetailDO creditsDetailDO = new CreditsDetailDO();
  623. creditsDetailDO.setStatus(1);
  624. creditsDetailDO.setAccountId(accountDOS.get(0).getId());
  625. creditsDetailDO.setHospital("350205");
  626. creditsDetailDO.setPatientId(patientId);
  627. creditsDetailDO.setIntegrate(taskRuleDO.getIntegrate());
  628. creditsDetailDO.setTradeDirection(taskRuleDO.getTradeDirection());
  629. creditsDetailDO.setDescription(description);
  630. creditsDetailDO.setCreateTime(new Date());
  631. creditsDetailDO.setUpdateTime(new Date());
  632. creditsDetailDO.setTransactionId(taskDO1.getId());
  633. creditsDetailDO.setTradeType("HEALTH_TASK");
  634. credittsLogDetailDao.save(creditsDetailDO);
  635. if (taskRuleDO.getTradeDirection() == -1){
  636. AccountDO accountDO = accountDOS.get(0);
  637. int total = accountDO.getTotal() - taskRuleDO.getIntegrate();
  638. if (total<0){
  639. accountDO.setTotal(0);
  640. }else {
  641. accountDO.setTotal(total);
  642. }
  643. accountDao.save(accountDO);
  644. }else if (taskRuleDO.getTradeDirection() == 1){
  645. AccountDO accountDO = accountDOS.get(0);
  646. accountDO.setTotal(accountDO.getTotal() + taskRuleDO.getIntegrate());
  647. accountDao.save(accountDO);
  648. }
  649. }
  650. }
  651. envelop.setObj(true);
  652. return envelop;
  653. }
  654. /**
  655. * 固定数据
  656. *
  657. * @param
  658. * @return
  659. *//*
  660. public List<TaskDO> getTasks(String patientId){
  661. List<TaskDO> taskDOList = new ArrayList<>();
  662. TaskDO taskDO = new TaskDO();
  663. *//*taskDO.setPatientId(patientId);*//*
  664. taskDO.setTaskCode("BIND");
  665. taskDO.setPeriod(1);
  666. taskDO.setTaskTitle("首次绑定");
  667. taskDO.setTaskContent("(绑定社区登记发放的设备,可获得各类型设备的首绑奖励。)");
  668. taskDO.setTradeType("activity");
  669. taskDO.setTransactionId("402885e96324a409016324c0a45a0006");
  670. taskDO.setCreateTime(new Date());
  671. taskDO.setUpdateTime(new Date());
  672. taskDOList.add(taskDO);
  673. TaskDO taskDO1 = new TaskDO();
  674. *//*taskDO1.setPatientId(patientId);*//*
  675. taskDO1.setTaskCode("MEASURE");
  676. taskDO1.setPeriod(0);
  677. taskDO1.setTaskTitle("每日测量");
  678. taskDO1.setTaskContent("(使用社区发放的已关联您身份信息的设备,绑定后每天完成测量)");
  679. taskDO1.setTradeType("activity");
  680. taskDO1.setTransactionId("402885e96324a409016324c0a45a0006");
  681. taskDO1.setCreateTime(new Date());
  682. taskDO1.setUpdateTime(new Date());
  683. taskDOList.add(taskDO1);
  684. return taskDOList;
  685. }
  686. */
  687. public Envelop<CreditsDetailDO> stepAddIntegrate(CreditsDetailDO creditsDetailDO){
  688. try {
  689. synchronized (creditsDetailDO.getPatientId()){
  690. String sqlAccount = "select * from wlyy_health_bank_account ba where ba.patient_id = '"+creditsDetailDO.getPatientId() +"'";
  691. List<AccountDO> accountDOList = jdbcTemplate.query(sqlAccount,new BeanPropertyRowMapper(AccountDO.class));
  692. if (accountDOList != null && accountDOList.size() != 0){
  693. creditsDetailDO.setAccountId(accountDOList.get(0).getId());
  694. }else {
  695. AccountDO accountDO1 = new AccountDO();
  696. accountDO1.setPatientId(creditsDetailDO.getPatientId());
  697. accountDO1.setTotal(0);
  698. accountDO1.setAccountName(creditsDetailDO.getName());
  699. accountDO1.setCardNumber("jw");
  700. accountDO1.setHospital("350205");
  701. accountDO1.setPassword("321321312321");
  702. accountDO1.setHospitalName("海沧区");
  703. accountDO1.setCreateTime(new Date());
  704. accountDO1.setUpdateTime(new Date());
  705. accountDao.save(accountDO1);
  706. List<AccountDO> accountDOS = jdbcTemplate.query(sqlAccount,new BeanPropertyRowMapper(AccountDO.class));
  707. creditsDetailDO.setAccountId(accountDOS.get(0).getId());
  708. }
  709. TaskDO taskDO = taskDao.findOne(creditsDetailDO.getTransactionId());
  710. String sql1 = "select * from wlyy_health_bank_task_patient_detail where patient_openid = '"+creditsDetailDO.getOpenId()+"' " +
  711. "AND patient_idcard = '"+creditsDetailDO.getIdCard()+"' AND union_id = '"+creditsDetailDO.getUnionId()+"' AND task_id = '"+creditsDetailDO.getTransactionId()+"'";
  712. List<TaskPatientDetailDO> taskPatientDetailDOList = jdbcTemplate.query(sql1,new BeanPropertyRowMapper(TaskPatientDetailDO.class));
  713. TaskPatientDetailDO taskPatientDetailDO = taskPatientDetailDOList.get(0);
  714. String sql = "select * from wlyy_health_bank_credits_detail where patient_id = '"+creditsDetailDO.getPatientId()+"' AND " +
  715. "transaction_id = '"+creditsDetailDO.getTransactionId()+"' AND create_time > '"+DateUtils.getDayBegin() +"' AND" +
  716. " create_time < '"+DateUtils.getDayEnd()+"'";
  717. List<CreditsDetailDO> creditsDetailDOS = jdbcTemplate.query(sql,new BeanPropertyRowMapper<>(CreditsDetailDO.class));
  718. if (creditsDetailDOS != null && creditsDetailDOS.size() != 0){
  719. CreditsDetailDO creditsDetailDO1 = creditsDetailDOS.get(0);
  720. TaskRuleDO taskRuleDO = taskRuleDao.findOne(taskDO.getRuleCode());
  721. if (creditsDetailDO.getStepNumber() == 50){
  722. creditsDetailDO1.setIntegrate(1);
  723. creditsDetailDO1.setTradeDirection(1);
  724. CreditsDetailDO creditsDetailDO2 = credittsLogDetailDao.save(creditsDetailDO1);
  725. AccountDO accountDO = accountDao.findOne(creditsDetailDO2.getAccountId());
  726. accountDO.setTotal(accountDO.getTotal()+creditsDetailDO2.getIntegrate());
  727. AccountDO accountDO1 = accountDao.save(accountDO);
  728. creditsDetailDO2.setTotal(accountDO1.getTotal());
  729. taskPatientDetailDO.setTotal(taskPatientDetailDO.getTotal()+creditsDetailDO2.getIntegrate());
  730. taskPatientDetailDao.save(taskPatientDetailDO);
  731. creditsDetailDOS.clear();
  732. creditsDetailDOS.add(creditsDetailDO2);
  733. }else if (creditsDetailDO.getStepNumber() == 100){
  734. creditsDetailDO1.setIntegrate(creditsDetailDO1.getIntegrate()+2);
  735. creditsDetailDO1.setTradeDirection(1);
  736. CreditsDetailDO creditsDetailDO2 = credittsLogDetailDao.save(creditsDetailDO1);
  737. AccountDO accountDO = accountDao.findOne(creditsDetailDO2.getAccountId());
  738. accountDO.setTotal(accountDO.getTotal()+(creditsDetailDO2.getIntegrate()-1));
  739. AccountDO accountDO1 = accountDao.save(accountDO);
  740. creditsDetailDO2.setTotal(accountDO1.getTotal());
  741. taskPatientDetailDO.setTotal(taskPatientDetailDO.getTotal()+(creditsDetailDO2.getIntegrate()-1));
  742. taskPatientDetailDao.save(taskPatientDetailDO);
  743. creditsDetailDOS.clear();
  744. creditsDetailDOS.add(creditsDetailDO2);
  745. }else if (creditsDetailDO.getStepNumber() == 500){
  746. if (creditsDetailDO1.getIntegrate() == 1){
  747. creditsDetailDO1.setIntegrate(creditsDetailDO1.getIntegrate()+7);
  748. }else if(creditsDetailDO1.getIntegrate() == 3){
  749. creditsDetailDO1.setIntegrate(creditsDetailDO1.getIntegrate()+5);
  750. }
  751. creditsDetailDO1.setTradeDirection(1);
  752. CreditsDetailDO creditsDetailDO2 = credittsLogDetailDao.save(creditsDetailDO1);
  753. AccountDO accountDO = accountDao.findOne(creditsDetailDO2.getAccountId());
  754. accountDO.setTotal(accountDO.getTotal()+(creditsDetailDO2.getIntegrate()-3));
  755. AccountDO accountDO1 = accountDao.save(accountDO);
  756. creditsDetailDO2.setTotal(accountDO1.getTotal());
  757. taskPatientDetailDO.setTotal(taskPatientDetailDO.getTotal()+(creditsDetailDO2.getIntegrate()-3));
  758. creditsDetailDOS.clear();
  759. creditsDetailDOS.add(creditsDetailDO2);
  760. }
  761. }else{
  762. CreditsDetailDO creditsDetailDO1 = new CreditsDetailDO();
  763. if (creditsDetailDO.getStepNumber() == 50){
  764. creditsDetailDO1.setIntegrate(1);
  765. creditsDetailDO1.setTradeDirection(1);
  766. }else if (creditsDetailDO.getStepNumber() == 100){
  767. creditsDetailDO1.setIntegrate(3);
  768. creditsDetailDO1.setTradeDirection(1);
  769. }else if (creditsDetailDO.getStepNumber() == 500){
  770. creditsDetailDO1.setIntegrate(8);
  771. creditsDetailDO1.setTradeDirection(1);
  772. }
  773. creditsDetailDO1.setSaasId("dev");
  774. creditsDetailDO1.setTradeType("HEALTH_TASK");
  775. creditsDetailDO1.setPatientId(creditsDetailDO.getPatientId());
  776. creditsDetailDO1.setHospital("350205");
  777. creditsDetailDO1.setAccountId(creditsDetailDO.getAccountId());
  778. creditsDetailDO1.setStatus(1);
  779. creditsDetailDO1.setCreateTime(new Date());
  780. creditsDetailDO1.setUpdateTime(new Date());
  781. creditsDetailDO1.setTransactionId(taskPatientDetailDO.getTaskId());
  782. CreditsDetailDO creditsDetailDO2 = credittsLogDetailDao.save(creditsDetailDO1);
  783. AccountDO accountDO = accountDao.findOne(creditsDetailDO2.getAccountId());
  784. accountDO.setTotal(accountDO.getTotal()+creditsDetailDO2.getIntegrate());
  785. taskPatientDetailDO.setTotal(taskPatientDetailDO.getTotal()+creditsDetailDO2.getIntegrate());
  786. taskPatientDetailDao.save(taskPatientDetailDO);
  787. creditsDetailDO2.setTotal(accountDO.getTotal());
  788. creditsDetailDOS.add(creditsDetailDO2);
  789. }
  790. Envelop<CreditsDetailDO> envelop = new Envelop<>();
  791. envelop.setDetailModelList(creditsDetailDOS);
  792. return envelop;
  793. }
  794. }catch (Exception e){
  795. e.printStackTrace();
  796. Envelop<CreditsDetailDO> envelop = new Envelop<>();
  797. return envelop;
  798. }
  799. }
  800. }