CreditsDetailService.java 77 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393
  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.jw.dao.*;
  7. import com.yihu.jw.entity.health.bank.*;
  8. import com.yihu.jw.restmodel.web.MixEnvelop;
  9. import com.yihu.jw.rm.health.bank.HealthBankMapping;
  10. import com.yihu.jw.util.DateUtils;
  11. import com.yihu.jw.util.ISqlUtils;
  12. import com.yihu.jw.util.date.DateUtil;
  13. import com.yihu.mysql.query.BaseJpaService;
  14. import org.slf4j.Logger;
  15. import org.slf4j.LoggerFactory;
  16. import org.springframework.beans.factory.annotation.Autowired;
  17. import org.springframework.data.redis.core.StringRedisTemplate;
  18. import org.springframework.jdbc.core.BeanPropertyRowMapper;
  19. import org.springframework.jdbc.core.JdbcTemplate;
  20. import org.springframework.stereotype.Service;
  21. import org.springframework.util.StringUtils;
  22. import javax.print.DocFlavor;
  23. import javax.transaction.Transactional;
  24. import java.text.ParseException;
  25. import java.text.SimpleDateFormat;
  26. import java.util.*;
  27. /**
  28. * @author wangzhinan
  29. * @create 2018-04-27 16:53
  30. * @desc credits log info Service
  31. **/
  32. @Service
  33. @Transactional
  34. public class CreditsDetailService extends BaseJpaService<CreditsDetailDO,CredittsLogDetailDao> {
  35. private Logger logger = LoggerFactory.getLogger(CreditsDetailService.class);
  36. private static String STEP = "health:blank:step";
  37. @Autowired
  38. private CredittsLogDetailDao credittsLogDetailDao;
  39. @Autowired
  40. private TaskDao taskDao;
  41. @Autowired
  42. private ActivityDao activityDao;
  43. @Autowired
  44. private JdbcTemplate jdbcTemplate;
  45. @Autowired
  46. private AccountDao accountDao;
  47. @Autowired
  48. private TaskPatientDetailDao taskPatientDetailDao;
  49. @Autowired
  50. private TaskRuleDao taskRuleDao;
  51. @Autowired
  52. private StringRedisTemplate redisTemplate;
  53. @Autowired
  54. private ActiveRecordService activeRecordService;
  55. @Autowired
  56. private ActivityRuleDao activityRuleDao;
  57. @Autowired
  58. private ExchangeGoodsDao exchangeGoodsDao;
  59. @Autowired
  60. private TaskGoodsDao taskGoodsDao;
  61. /**
  62. * find creditsLogInfo
  63. *
  64. * @return
  65. * @throws ParseException
  66. */
  67. public MixEnvelop<CreditsDetailDO, CreditsDetailDO> findByCondition(CreditsDetailDO creditsDetailDO, Integer page, Integer size) throws ParseException {
  68. String sql = new ISqlUtils().getSql(creditsDetailDO,page,size,"*");
  69. List<CreditsDetailDO> creditsDetailDOS = jdbcTemplate.query(sql,new BeanPropertyRowMapper(CreditsDetailDO.class));
  70. for (CreditsDetailDO creditsDetailDO1 : creditsDetailDOS){
  71. if (creditsDetailDO1.getTradeType() != null && creditsDetailDO1.getTradeType().equalsIgnoreCase("ACTIVITY_TASK")){
  72. TaskDO taskDO = taskDao.findOne(creditsDetailDO1.getTransactionId());
  73. creditsDetailDO1.setTaskDO(taskDO);
  74. }else if (creditsDetailDO1.getTradeType()!=null&&creditsDetailDO1.getTradeType().equals("EXCHANGE_GOODS")){
  75. ExchangeGoodsDO exchangeGoodsDO =exchangeGoodsDao.findOne(creditsDetailDO1.getTransactionId());
  76. TaskGoodsDO taskGoodsDO = taskGoodsDao.selectById(exchangeGoodsDO.getActivityGoodsId());
  77. exchangeGoodsDO.setTaskGoodsDO(taskGoodsDO);
  78. creditsDetailDO1.setExchangeGoodsDO(exchangeGoodsDO);
  79. }
  80. }
  81. String sqlcount = new ISqlUtils().getSql(creditsDetailDO,0,0,"count");
  82. List<Map<String,Object>> rstotal = jdbcTemplate.queryForList(sqlcount);
  83. Long count = 0L;
  84. if(rstotal!=null&&rstotal.size()>0){
  85. count = (Long) rstotal.get(0).get("total");
  86. }
  87. return MixEnvelop.getSuccessListWithPage(HealthBankMapping.api_success, creditsDetailDOS,page,size,count);
  88. }
  89. /**
  90. * 获取账户信息
  91. *
  92. * @param creditsDetailDO
  93. * @return
  94. */
  95. public MixEnvelop<AccountDO, AccountDO> findByTradeDirection(CreditsDetailDO creditsDetailDO){
  96. AccountDO accountDO1 = new AccountDO();
  97. accountDO1.setPatientId(creditsDetailDO.getPatientId());
  98. String sql1 = ISqlUtils.getAllSql(accountDO1);
  99. List<AccountDO> accountDOS = jdbcTemplate.query(sql1,new BeanPropertyRowMapper(AccountDO.class));
  100. if (accountDOS == null || accountDOS.size() == 0){
  101. accountDO1.setTotal(0);
  102. accountDO1.setAccountName(creditsDetailDO.getName());
  103. if(creditsDetailDO.getIdCard().length()>=4){// 判断是否长度大于等于4
  104. String cardNumber=creditsDetailDO.getIdCard().substring(creditsDetailDO.getIdCard().length()- 4,creditsDetailDO.getIdCard().length());//截取两个数字之间的部分
  105. int random = (int)((Math.random()*9+1)*100000);
  106. accountDO1.setCardNumber(cardNumber+Integer.toString(random));
  107. }
  108. accountDO1.setHospital(creditsDetailDO.getHospital());
  109. accountDO1.setPassword("123456");
  110. accountDO1.setHospitalName(creditsDetailDO.getHospitalName());
  111. accountDO1.setStatus(1);
  112. accountDO1.setSaasId("dev");
  113. accountDO1.setCreateTime(new Date());
  114. accountDO1.setUpdateTime(new Date());
  115. accountDao.save(accountDO1);
  116. }
  117. List<AccountDO> accountDOS1 = jdbcTemplate.query(sql1,new BeanPropertyRowMapper(AccountDO.class));
  118. AccountDO accountDO = accountDOS1.get(0);
  119. 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()+"'";
  120. List<Map<String,Object>> rstotal = jdbcTemplate.queryForList(sql);
  121. if (rstotal!= null && rstotal.size()>0){
  122. if (rstotal.get(0).get("total") == null){
  123. accountDO.setUsedTotal(0);
  124. }else {
  125. accountDO.setUsedTotal(Integer.parseInt(rstotal.get(0).get("total").toString()));
  126. }
  127. }
  128. return MixEnvelop.getSuccess(HealthBankMapping.api_success,accountDO);
  129. }
  130. /*
  131. public Envelop<Boolean> exchangeGoods(GoodsDO goodsDO){
  132. CreditsDetailDO creditsLogDetailDO = new CreditsDetailDO();
  133. Envelop<Boolean> envelop = new Envelop<>();
  134. envelop.setObj(true);
  135. return envelop;
  136. }
  137. */
  138. public MixEnvelop<AccountDO, AccountDO> selectByRanking(List<String> patientIds, Integer page, Integer size){
  139. StringBuffer buffer = new StringBuffer();
  140. buffer.append(" ba.patient_id in(");
  141. if (patientIds == null || patientIds.size() == 0){
  142. buffer.append("''");
  143. }else {
  144. for (int i=0;i<patientIds.size();i++){
  145. buffer.append("'"+patientIds.get(i)+"'").append(",");
  146. }
  147. buffer.deleteCharAt(buffer.length()-1);
  148. }
  149. buffer.append(") ");
  150. String sql =
  151. "SELECT ba1.patient_id AS patient_id," +
  152. "ba1.account_name AS account_name," +
  153. "ba1.hospital AS hospital," +
  154. "ba1.total AS total," +
  155. "ba1.create_time AS create_time," +
  156. "ba1.sum AS sum" +
  157. " FROM" +
  158. "( SELECT " +
  159. "ba.patient_id AS patient_id," +
  160. "ba.account_name AS account_name," +
  161. "ba.hospital AS hospital," +
  162. "ba.total AS total," +
  163. "ba.create_time AS create_time," +
  164. " if(ba.total=0,ba.total,(ba.total +COALESCE((cd1.total),0))) AS sum" +
  165. " FROM" +
  166. " wlyy_health_bank_account ba" +
  167. " LEFT JOIN ( " +
  168. "SELECT" +
  169. " SUM(cd.integrate) AS total," +
  170. " cd.patient_id AS patient_id" +
  171. " FROM" +
  172. " wlyy_health_bank_credits_detail cd" +
  173. " WHERE " +
  174. "cd.trade_direction = - 1" +
  175. " GROUP BY " +
  176. " cd.patient_id ) cd1 ON cd1.patient_id = ba.patient_id " +
  177. " WHERE " + buffer +
  178. " ORDER BY" +
  179. " ba.create_time DESC )ba1" +
  180. " ORDER BY " +
  181. " ba1.total DESC"+
  182. " LIMIT "+(page-1)*size+","+size ;
  183. List<AccountDO> accountDOS = jdbcTemplate.query(sql,new BeanPropertyRowMapper(AccountDO.class));
  184. String sqlCount = "SELECT count(1) AS total"+
  185. " FROM " +
  186. " wlyy_health_bank_account ba LEFT JOIN " +
  187. " ( " +
  188. " SELECT " +
  189. " SUM(cd.integrate) AS total, " +
  190. " cd.patient_id AS patient_id " +
  191. " FROM " +
  192. " wlyy_health_bank_credits_detail cd " +
  193. " WHERE " +
  194. " cd.trade_direction = - 1 " +
  195. " GROUP BY " +
  196. " cd.patient_id " +
  197. " ) cd1 ON cd1.patient_id = ba.patient_id " +
  198. "WHERE " + buffer +
  199. " ORDER BY " +
  200. " ba.create_time, " +
  201. " (ba.total + COALESCE(cd1.total,0)) DESC ";
  202. List<Map<String,Object>> rstotal = jdbcTemplate.queryForList(sqlCount);
  203. Long count = 0L;
  204. if(rstotal!=null&&rstotal.size()>0){
  205. count = (Long) rstotal.get(0).get("total");
  206. }
  207. return MixEnvelop.getSuccessListWithPage(HealthBankMapping.api_success, accountDOS,page,size,count);
  208. }
  209. /**
  210. * 添加积分
  211. *
  212. * @param creditsDetailDO
  213. * @return
  214. */
  215. public MixEnvelop<CreditsDetailDO, CreditsDetailDO> insert(CreditsDetailDO creditsDetailDO){
  216. try {
  217. synchronized (creditsDetailDO.getPatientId()){
  218. String sqlAccount = "select * from wlyy_health_bank_account ba where ba.patient_id = '"+creditsDetailDO.getPatientId() +"'";
  219. List<AccountDO> accountDOList = jdbcTemplate.query(sqlAccount,new BeanPropertyRowMapper(AccountDO.class));
  220. if (accountDOList != null && accountDOList.size() != 0){
  221. creditsDetailDO.setAccountId(accountDOList.get(0).getId());
  222. }else {
  223. AccountDO accountDO1 = new AccountDO();
  224. accountDO1.setPatientId(creditsDetailDO.getPatientId());
  225. accountDO1.setTotal(0);
  226. accountDO1.setCouponTotal(0L);
  227. accountDO1.setAccountName(creditsDetailDO.getName());
  228. if(creditsDetailDO.getIdCard().length()>=4){// 判断是否长度大于等于4
  229. String cardNumber=creditsDetailDO.getIdCard().substring(creditsDetailDO.getIdCard().length()- 4,creditsDetailDO.getIdCard().length());//截取两个数字之间的部分
  230. int random = (int)((Math.random()*9+1)*100000);
  231. accountDO1.setCardNumber(cardNumber+Integer.toString(random));
  232. }
  233. accountDO1.setHospital(creditsDetailDO.getHospital());
  234. accountDO1.setPassword("123456");
  235. accountDO1.setHospitalName(creditsDetailDO.getHospitalName());
  236. accountDO1.setStatus(1);
  237. accountDO1.setCreateTime(new Date());
  238. accountDO1.setUpdateTime(new Date());
  239. accountDao.save(accountDO1);
  240. List<AccountDO> accountDOS = jdbcTemplate.query(sqlAccount,new BeanPropertyRowMapper(AccountDO.class));
  241. creditsDetailDO.setAccountId(accountDOS.get(0).getId());
  242. }
  243. TaskDO taskDO = new TaskDO();
  244. taskDO.setTaskCode(creditsDetailDO.getFlag());
  245. taskDO.setId(creditsDetailDO.getTransactionId());
  246. /*taskDO.setPatientId(creditsDetailDO.getPatientId());*/
  247. String sql = ISqlUtils.getSql(taskDO,1,1,"*");
  248. List<TaskDO> taskDOList = jdbcTemplate.query(sql,new BeanPropertyRowMapper(TaskDO.class));
  249. creditsDetailDO.setTransactionId(taskDOList.get(0).getId());
  250. String ruleSql = "SELECT * FROM wlyy_health_bank_task_rule WHERE id= '"+taskDOList.get(0).getRuleCode()+"'";
  251. List<TaskRuleDO> taskRuleDOS = jdbcTemplate.query(ruleSql,new BeanPropertyRowMapper(TaskRuleDO.class));
  252. TaskRuleDO taskRuleDO = taskRuleDOS.get(0);
  253. creditsDetailDO.setIntegrate(taskRuleDO.getIntegrate());
  254. creditsDetailDO.setTradeDirection(taskRuleDO.getTradeDirection());
  255. String taskSql = "select * from wlyy_health_bank_task_patient_detail where task_id = '"+taskDOList.get(0).getId()+"' and patient_id = '" + creditsDetailDO.getPatientId() +"'";
  256. List<TaskPatientDetailDO> taskPatientDetailDOS = jdbcTemplate.query(taskSql,new BeanPropertyRowMapper(TaskPatientDetailDO.class));
  257. if (taskPatientDetailDOS == null || taskPatientDetailDOS.size() ==0){
  258. TaskPatientDetailDO taskPatientDetailDO = new TaskPatientDetailDO();
  259. taskPatientDetailDO.setTaskId(creditsDetailDO.getTransactionId());
  260. taskPatientDetailDO.setSaasId(creditsDetailDO.getSaasId());
  261. taskPatientDetailDO.setPatientId(creditsDetailDO.getPatientId());
  262. taskPatientDetailDO.setPatientIdcard(creditsDetailDO.getIdCard());
  263. taskPatientDetailDO.setPatientOpenid(creditsDetailDO.getOpenId());
  264. taskPatientDetailDO.setUnionId(creditsDetailDO.getUnionId());
  265. taskPatientDetailDO.setStatus(Integer.parseInt("0"));
  266. taskPatientDetailDO.setCreateTime(new Date());
  267. taskPatientDetailDO.setUpdateTime(new Date());
  268. taskPatientDetailDO.setActivityId(taskDOList.get(0).getTransactionId());
  269. taskPatientDetailDO.setTotal(Long.parseLong("0"));
  270. taskPatientDetailDao.save(taskPatientDetailDO);
  271. }else if (taskPatientDetailDOS != null && taskDOList.get(0).getPeriod() == 0){
  272. String taskSql1 = "select * from wlyy_health_bank_task_patient_detail where task_id = '"+taskDOList.get(0).getId()+
  273. "' and patient_id = '"+creditsDetailDO.getPatientId()+"' and create_time > '" + DateUtils.getDayBegin() +"' and create_time < '"+ DateUtils.getDayEnd() +"'";
  274. List<TaskPatientDetailDO> taskPatientDetailDOS1 = jdbcTemplate.query(taskSql1,new BeanPropertyRowMapper(TaskPatientDetailDO.class));
  275. if (taskPatientDetailDOS1 == null || taskPatientDetailDOS1.size() == 0){
  276. TaskPatientDetailDO taskPatientDetailDO = new TaskPatientDetailDO();
  277. taskPatientDetailDO.setTaskId(creditsDetailDO.getTransactionId());
  278. taskPatientDetailDO.setSaasId(creditsDetailDO.getSaasId());
  279. taskPatientDetailDO.setPatientId(creditsDetailDO.getPatientId());
  280. taskPatientDetailDO.setPatientIdcard(creditsDetailDO.getIdCard());
  281. taskPatientDetailDO.setPatientOpenid(creditsDetailDO.getOpenId());
  282. taskPatientDetailDO.setActivityId(taskDOList.get(0).getTransactionId());
  283. taskPatientDetailDO.setStatus(Integer.parseInt("0"));
  284. taskPatientDetailDO.setCreateTime(new Date());
  285. taskPatientDetailDO.setUpdateTime(new Date());
  286. taskPatientDetailDO.setTotal(Long.parseLong("0"));
  287. taskPatientDetailDao.save(taskPatientDetailDO);
  288. }
  289. }
  290. creditsDetailDO.setCreateTime(new Date());
  291. creditsDetailDO.setUpdateTime(new Date());
  292. CreditsDetailDO creditsDetailDO1 =credittsLogDetailDao.save(creditsDetailDO);
  293. creditsDetailDO1.setFlag(creditsDetailDO.getFlag());
  294. List<CreditsDetailDO> creditsDetailDOList = new ArrayList<>();
  295. creditsDetailDOList.add(creditsDetailDO1);
  296. TaskPatientDetailDO taskPatientDetailDO = new TaskPatientDetailDO();
  297. taskPatientDetailDO.setPatientId(creditsDetailDO1.getPatientId());
  298. taskPatientDetailDO.setTaskId(creditsDetailDO1.getTransactionId());
  299. String taskSql1 = ISqlUtils.getAllSql(taskPatientDetailDO);
  300. List<TaskPatientDetailDO> taskPatientDetailDOS1 = jdbcTemplate.query(taskSql1,new BeanPropertyRowMapper(TaskPatientDetailDO.class));
  301. TaskPatientDetailDO taskPatientDetailDO1 = taskPatientDetailDOS1.get(0);
  302. if (creditsDetailDO1.getTradeDirection() == 1){
  303. taskPatientDetailDO1.setTotal(taskPatientDetailDO1.getTotal()+creditsDetailDO1.getIntegrate());
  304. }else if (creditsDetailDO.getTradeDirection() == -1){
  305. taskPatientDetailDO1.setTotal(taskPatientDetailDO1.getTotal()-creditsDetailDO1.getIntegrate());
  306. }
  307. taskPatientDetailDao.save(taskPatientDetailDO1);
  308. AccountDO accountDO = accountDao.findOne(creditsDetailDO1.getAccountId());
  309. if (creditsDetailDO1.getTradeDirection() == 1){
  310. accountDO.setTotal(accountDO.getTotal()+creditsDetailDO1.getIntegrate());
  311. }else if (creditsDetailDO.getTradeDirection() == -1){
  312. accountDO.setTotal(accountDO.getTotal()-creditsDetailDO1.getIntegrate());
  313. }
  314. AccountDO accountDO1 = accountDao.save(accountDO);
  315. List<CreditsDetailDO> creditsDetailDOS = new ArrayList<>();
  316. for (CreditsDetailDO creditsDetailDO2:creditsDetailDOList){
  317. creditsDetailDO2.setTotal(accountDO1.getTotal());
  318. creditsDetailDOS.add(creditsDetailDO2);
  319. }
  320. MixEnvelop<CreditsDetailDO, CreditsDetailDO> envelop = new MixEnvelop<>();
  321. envelop.setDetailModelList(creditsDetailDOS);
  322. return envelop;
  323. }
  324. }catch (Exception e){
  325. e.printStackTrace();
  326. MixEnvelop<CreditsDetailDO, CreditsDetailDO> envelop = new MixEnvelop<>();
  327. return envelop;
  328. }
  329. }
  330. /**
  331. * 活动排名
  332. *
  333. * @param activityId 活动id
  334. * @param ids 微信编码
  335. *
  336. * @param page 页码
  337. *
  338. * @param size 分页大小
  339. * @return
  340. */
  341. public MixEnvelop<TaskPatientDetailDO, TaskPatientDetailDO> selectByActivityRanking(String activityId, List<String> ids, Integer page, Integer size){
  342. StringBuffer buffer = new StringBuffer();
  343. buffer.append("(");
  344. if (ids == null || ids.size() == 0){
  345. buffer.append("''");
  346. }else {
  347. for (int i=0;i<ids.size();i++){
  348. buffer.append("'"+ids.get(i)+"'").append(",");
  349. }
  350. buffer.deleteCharAt(buffer.length()-1);
  351. }
  352. buffer.append(") ");
  353. String sql = "SELECT " +
  354. " * " +
  355. "FROM " +
  356. " ( " +
  357. " SELECT " +
  358. " SUM(ptpd.total) AS total, " +
  359. " ptpd.patient_openid AS patient_openid, " +
  360. " ptpd.task_id AS task_id, " +
  361. " ptpd.activity_id AS activity_id, " +
  362. " ptpd.create_time as create_time, " +
  363. " ptpd.patient_id AS patient_id " +
  364. " FROM " +
  365. " wlyy_health_bank_task_patient_detail ptpd " +
  366. " WHERE " +
  367. " activity_id = '" + activityId +
  368. "' GROUP BY " +
  369. " patient_id " +
  370. " ORDER BY ptpd.create_time DESC " +
  371. " )btpd1 " +
  372. " WHERE patient_id IN "+buffer+
  373. " ORDER BY btpd1.total DESC "+" LIMIT " + (page-1)*size+","+size;
  374. List<TaskPatientDetailDO> taskPatientDetailDOS = jdbcTemplate.query(sql,new BeanPropertyRowMapper(TaskPatientDetailDO.class));
  375. for (TaskPatientDetailDO taskPatientDetailDO : taskPatientDetailDOS){
  376. String accountSql = "select * from wlyy_health_bank_account where patient_id = '"+taskPatientDetailDO.getPatientId()+"'";
  377. List<AccountDO> accountDOS = jdbcTemplate.query(accountSql,new BeanPropertyRowMapper(AccountDO.class));
  378. taskPatientDetailDO.setAccountDO(accountDOS.get(0));
  379. }
  380. String sqlCount = "SELECT " +
  381. " count(1) AS total " +
  382. "FROM " +
  383. " ( " +
  384. " SELECT " +
  385. " SUM(ptpd.total) AS total, " +
  386. " ptpd.patient_openid AS patient_openid, " +
  387. " ptpd.task_id AS task_id, " +
  388. " ptpd.activity_id AS activity_id, " +
  389. " ptpd.create_time as create_time, " +
  390. " ptpd.patient_id AS patient_id " +
  391. " FROM " +
  392. " wlyy_health_bank_task_patient_detail ptpd " +
  393. " WHERE " +
  394. " activity_id = '" + activityId+
  395. "' GROUP BY " +
  396. " patient_openid " +
  397. " ORDER BY ptpd.create_time DESC " +
  398. " )btpd1 " +
  399. "WHERE patient_openid IN "+buffer+
  400. " ORDER BY btpd1.total DESC ";
  401. List<Map<String,Object>> rstotal = jdbcTemplate.queryForList(sqlCount);
  402. Long count = 0L;
  403. if(rstotal!=null&&rstotal.size()>0){
  404. count = (Long) rstotal.get(0).get("total");
  405. }
  406. return MixEnvelop.getSuccessListWithPage(HealthBankMapping.api_success, taskPatientDetailDOS,page,size,count);
  407. }
  408. /**
  409. * 根据活动查询积分
  410. *
  411. * @param activityId 活动id
  412. *
  413. * @param patientId 居民id
  414. *
  415. * @param page 页码
  416. *
  417. * @param size 分页大小
  418. * @return
  419. */
  420. public MixEnvelop<CreditsDetailDO, CreditsDetailDO> selectByActivity(String activityId, String patientId, Integer page, Integer size){
  421. String sql="SELECT * " +
  422. "FROM " +
  423. " wlyy_health_bank_credits_detail " +
  424. "WHERE" +
  425. " transaction_id IN ( " +
  426. " SELECT " +
  427. " bt.id " +
  428. " FROM " +
  429. " wlyy_health_bank_task bt " +
  430. " WHERE " +
  431. " transaction_id = '"+activityId +"' " +
  432. " ) " +
  433. " and patient_id = '" +patientId+
  434. "' LIMIT "+(page-1)*size +","+size;
  435. List<CreditsDetailDO> creditsDetailDOS = jdbcTemplate.query(sql,new BeanPropertyRowMapper(CreditsDetailDO.class));
  436. for (CreditsDetailDO creditsDetailDO : creditsDetailDOS){
  437. TaskDO taskDO = taskDao.findOne(creditsDetailDO.getTransactionId());
  438. creditsDetailDO.setTaskDO(taskDO);
  439. }
  440. String sqlcount = "SELECT count(1) AS" +
  441. " total FROM " +
  442. " wlyy_health_bank_credits_detail " +
  443. "WHERE" +
  444. " transaction_id IN ( " +
  445. " SELECT " +
  446. " bt.id " +
  447. " FROM " +
  448. " wlyy_health_bank_task bt " +
  449. " WHERE " +
  450. " transaction_id = '"+activityId +"' " +
  451. " ) " +
  452. " and patient_id = '" +patientId+
  453. "'";
  454. List<Map<String,Object>> rstotal = jdbcTemplate.queryForList(sqlcount);
  455. Long count = 0L;
  456. if(rstotal!=null&&rstotal.size()>0){
  457. count = (Long) rstotal.get(0).get("total");
  458. }
  459. return MixEnvelop.getSuccessListWithPage(HealthBankMapping.api_success,creditsDetailDOS,page,size,count);
  460. }
  461. /**
  462. * 根据活动查找全部排行
  463. *
  464. * @param activityId 活动id
  465. * @param page 页码
  466. * @param size 分页大小
  467. * @return
  468. */
  469. public MixEnvelop<TaskPatientDetailDO, TaskPatientDetailDO> selectByActivityRanking1(String activityId, String patientId, Integer page, Integer size){
  470. String sql = "SELECT " +
  471. " * " +
  472. "FROM " +
  473. " ( " +
  474. " SELECT " +
  475. " SUM(ptpd.total) AS total, " +
  476. " ptpd.patient_openid AS patient_openid, " +
  477. " ptpd.task_id AS task_id, " +
  478. " ptpd.activity_id AS activity_id, " +
  479. " ptpd.create_time as create_time, " +
  480. " ptpd.patient_id AS patient_id " +
  481. " FROM " +
  482. " wlyy_health_bank_task_patient_detail ptpd " +
  483. " WHERE " +
  484. " activity_id = '" + activityId +
  485. "' GROUP BY " +
  486. " patient_openid " +
  487. " ORDER BY ptpd.create_time DESC " +
  488. " )btpd1 " +
  489. " ORDER BY btpd1.total DESC "+" LIMIT " + (page-1)*size+","+size;
  490. List<TaskPatientDetailDO> taskPatientDetailDOS = jdbcTemplate.query(sql,new BeanPropertyRowMapper(TaskPatientDetailDO.class));
  491. logger.info(taskPatientDetailDOS.toString()+""+taskPatientDetailDOS.size());
  492. for (int i = 0;taskPatientDetailDOS != null&&taskPatientDetailDOS.size()!=0 && i<taskPatientDetailDOS.size();i++){
  493. TaskPatientDetailDO taskPatientDetailDO = taskPatientDetailDOS.get(i);
  494. String accountSql = "select * from wlyy_health_bank_account where patient_id = '"+taskPatientDetailDO.getPatientId()+"'";
  495. List<AccountDO> accountDOS = jdbcTemplate.query(accountSql,new BeanPropertyRowMapper(AccountDO.class));
  496. if (taskPatientDetailDOS.get(i).getPatientId().equalsIgnoreCase(patientId)){
  497. /*String taskSql = "select count(1)+1 as total from (" +
  498. "select * from (SELECT " +
  499. " SUM(ptpd.total) AS total, " +
  500. " ptpd.patient_openid AS patient_openid, " +
  501. " ptpd.task_id AS task_id, " +
  502. " ptpd.activity_id AS activity_id, " +
  503. " ptpd.create_time as create_time, " +
  504. " ptpd.patient_id AS patient_id " +
  505. " FROM " +
  506. " wlyy_health_bank_task_patient_detail ptpd " +
  507. " WHERE " +
  508. " activity_id = '" + activityId + "')ptpd1 where" +
  509. " ptpd1.patient_id = '"+patientId+"' AND ptpd1.total > "+taskPatientDetailDOS.get(i).getTotal()+") ptpd2";
  510. List<Map<String,Object>> rstotal1 = jdbcTemplate.queryForList(taskSql);
  511. Long count = 0L;
  512. if(rstotal1!=null&&rstotal1.size()>0){
  513. count = (Long) rstotal1.get(0).get("total");
  514. }
  515. accountDOS.get(0).setActivityRanking(count);*/
  516. taskPatientDetailDO.setIsFlag(1);
  517. }else {
  518. taskPatientDetailDO.setIsFlag(0);
  519. }
  520. taskPatientDetailDO.setAccountDO(accountDOS.get(0));
  521. }
  522. String sqlCount = "SELECT " +
  523. " count(1) AS total " +
  524. "FROM " +
  525. " ( " +
  526. " SELECT " +
  527. " SUM(ptpd.total) AS total, " +
  528. " ptpd.patient_openid AS patient_openid, " +
  529. " ptpd.task_id AS task_id, " +
  530. " ptpd.activity_id AS activity_id, " +
  531. " ptpd.create_time as create_time, " +
  532. " ptpd.patient_id AS patient_id " +
  533. " FROM " +
  534. " wlyy_health_bank_task_patient_detail ptpd " +
  535. " WHERE " +
  536. " activity_id = '" + activityId+
  537. "' GROUP BY " +
  538. " patient_openid " +
  539. " ORDER BY ptpd.create_time DESC " +
  540. " )btpd1 " +
  541. " ORDER BY btpd1.total DESC ";
  542. List<Map<String,Object>> rstotal = jdbcTemplate.queryForList(sqlCount);
  543. Long count = 0L;
  544. if(rstotal!=null&&rstotal.size()>0){
  545. count = (Long) rstotal.get(0).get("total");
  546. }
  547. return MixEnvelop.getSuccessListWithPage(HealthBankMapping.api_success, taskPatientDetailDOS,page,size,count);
  548. }
  549. /**
  550. * 医生主动加分
  551. *
  552. * @param array 居民信息集合
  553. *
  554. * @param ruleId 规则id
  555. * @return
  556. */
  557. public MixEnvelop<Boolean, Boolean> doctorAddIntegrate(JSONArray array, String ruleId, String description) throws Exception {
  558. MixEnvelop<Boolean, Boolean> envelop = new MixEnvelop<>();
  559. for (int i=0;i<array.size();i++){
  560. TaskRuleDO taskRuleDO = taskRuleDao.findOne(ruleId);
  561. JSONObject object = array.getJSONObject(i);
  562. String patientId = object.getString("code");
  563. String idCard = object.getString("idcard");
  564. String unionId = object.getString("unionid");
  565. String openId = object.getString("openid");
  566. String hospital = object.getString("hospital");
  567. String hospitalName = object.getString("hospitalName");
  568. String taskSql = "select * from wlyy_health_bank_task bt where type = 'RULE_TASK' AND transaction_id = '"+ruleId +"'";
  569. List<TaskDO> taskDOList = jdbcTemplate.query(taskSql,new BeanPropertyRowMapper(TaskDO.class));
  570. TaskDO taskDO1 = new TaskDO();
  571. if (taskDOList.isEmpty() && taskDOList.size() == 0){
  572. TaskDO taskDO = new TaskDO();
  573. taskDO.setTransactionId(taskRuleDO.getId());
  574. taskDO.setTaskCode("RULE");
  575. taskDO.setStatus(1);
  576. taskDO.setRuleCode(taskRuleDO.getId());
  577. taskDO.setType("RULE_TASK");
  578. taskDO.setCreateTime(new Date());
  579. taskDO.setUpdateTime(new Date());
  580. taskDO.setPeriod(taskRuleDO.getPeriod());
  581. taskDO.setSaasId("dev");
  582. taskDO.setTitle(taskRuleDO.getDescription());
  583. taskDO.setContent(taskRuleDO.getDescription());
  584. taskDO1 = taskDao.save(taskDO);
  585. }else {
  586. taskDO1 = taskDOList.get(0);
  587. }
  588. if (taskRuleDO.getPeriod() == 1){
  589. String sql = "select * from wlyy_health_bank_task_patient_detail where patient_id = '"+patientId+"'AND task_id ='"+taskDO1.getId()+"'";
  590. List<TaskPatientDetailDO> taskPatientDetailDOS = jdbcTemplate.query(sql,new BeanPropertyRowMapper(TaskPatientDetailDO.class));
  591. if (taskPatientDetailDOS != null && taskPatientDetailDOS.size() != 0){
  592. throw new Exception("已奖励过");
  593. }
  594. }else if (taskRuleDO.getPeriod() == 0){
  595. SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
  596. Date date = new Date();
  597. String date1 = dateFormat.format(date);
  598. String begin = DateUtils.getMinMonthDate(date1);
  599. String end = DateUtils.getMaxMonthDate(date1);
  600. 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+"'";
  601. List<TaskPatientDetailDO> taskPatientDetailDOS = jdbcTemplate.query(sql,new BeanPropertyRowMapper(TaskPatientDetailDO.class));
  602. if (taskPatientDetailDOS != null && taskPatientDetailDOS.size() != 0){
  603. throw new Exception("已奖励过");
  604. }
  605. }
  606. String sql1 = "select * from wlyy_health_bank_task_patient_detail where task_id = '"+taskDO1.getId()+"' " +
  607. "AND patient_idcard = '"+idCard+"' AND patient_openid = '"+openId+"' AND union_id = '"+unionId+"'";
  608. List<TaskPatientDetailDO> taskPatientDetailDOS = jdbcTemplate.query(sql1,new BeanPropertyRowMapper(TaskPatientDetailDO.class));
  609. TaskPatientDetailDO taskPatientDetailDO1 = new TaskPatientDetailDO();
  610. if (taskPatientDetailDOS.isEmpty() && taskPatientDetailDOS.size() == 0){
  611. TaskPatientDetailDO taskPatientDetailDO = new TaskPatientDetailDO();
  612. taskPatientDetailDO.setStatus(1);
  613. taskPatientDetailDO.setTotal(Long.parseLong("0"));
  614. taskPatientDetailDO.setPatientId(patientId);
  615. taskPatientDetailDO.setPatientOpenid(openId);
  616. taskPatientDetailDO.setPatientIdcard(idCard);
  617. taskPatientDetailDO.setUnionId(unionId);
  618. taskPatientDetailDO.setSaasId("dev");
  619. taskPatientDetailDO.setCreateTime(new Date());
  620. taskPatientDetailDO.setUpdateTime(new Date());
  621. taskPatientDetailDO.setTaskId(taskDO1.getId());
  622. taskPatientDetailDO1 = taskPatientDetailDao.save(taskPatientDetailDO);
  623. }else {
  624. taskPatientDetailDO1 = taskPatientDetailDOS.get(0);
  625. }
  626. String sql = "select * from wlyy_health_bank_account where patient_id = '"+patientId+"'";
  627. List<AccountDO> accountDOS = jdbcTemplate.query(sql,new BeanPropertyRowMapper(AccountDO.class));
  628. if (taskRuleDO.getTradeDirection() == -1 && taskRuleDO.getIntegrate() == 0){
  629. String integrateSql = "select * from wlyy_health_bank_credits_detail where patient_id = '"+patientId+"' " +
  630. "AND transaction_id IN (SELECT id FROM wlyy_health_bank_task WHERE task_code IN ('BP_BIND','GLU_BIND','BP_MEASURE','GLU_MEASURE','RULE'))";
  631. List<CreditsDetailDO> creditsDetailDOS = jdbcTemplate.query(integrateSql,new BeanPropertyRowMapper(CreditsDetailDO.class));
  632. AccountDO accountDO = accountDOS.get(0);
  633. for (CreditsDetailDO creditsDetailDO:creditsDetailDOS){
  634. if (creditsDetailDO.getTradeDirection()==1&&creditsDetailDO.getStatus()!=0){
  635. /* int total = accountDO.getTotal() - creditsDetailDO.getIntegrate();*/
  636. creditsDetailDO.setStatus(0);
  637. creditsDetailDO.setDescription(description);
  638. credittsLogDetailDao.save(creditsDetailDO);
  639. }
  640. }
  641. accountDO.setTotal(0);
  642. accountDao.save(accountDO);
  643. CreditsDetailDO creditsDetailDO1 = new CreditsDetailDO();
  644. creditsDetailDO1.setStatus(1);
  645. creditsDetailDO1.setSaasId("dev");
  646. creditsDetailDO1.setAccountId(accountDOS.get(0).getId());
  647. creditsDetailDO1.setHospital(hospital);
  648. creditsDetailDO1.setHospitalName(hospitalName);
  649. creditsDetailDO1.setPatientId(patientId);
  650. creditsDetailDO1.setIntegrate(taskRuleDO.getIntegrate());
  651. creditsDetailDO1.setTradeDirection(taskRuleDO.getTradeDirection());
  652. creditsDetailDO1.setDescription(description);
  653. creditsDetailDO1.setCreateTime(new Date());
  654. creditsDetailDO1.setUpdateTime(new Date());
  655. creditsDetailDO1.setTransactionId(taskDO1.getId());
  656. creditsDetailDO1.setTradeType("ACTIVITY_TASK");
  657. credittsLogDetailDao.save(creditsDetailDO1);
  658. }else{
  659. CreditsDetailDO creditsDetailDO = new CreditsDetailDO();
  660. creditsDetailDO.setStatus(1);
  661. creditsDetailDO.setSaasId("dev");
  662. creditsDetailDO.setAccountId(accountDOS.get(0).getId());
  663. creditsDetailDO.setHospital(hospital);
  664. creditsDetailDO.setHospitalName(hospitalName);
  665. creditsDetailDO.setPatientId(patientId);
  666. creditsDetailDO.setIntegrate(taskRuleDO.getIntegrate());
  667. creditsDetailDO.setTradeDirection(taskRuleDO.getTradeDirection());
  668. creditsDetailDO.setDescription(description);
  669. creditsDetailDO.setCreateTime(new Date());
  670. creditsDetailDO.setUpdateTime(new Date());
  671. creditsDetailDO.setTransactionId(taskDO1.getId());
  672. creditsDetailDO.setTradeType("ACTIVITY_TASK");
  673. creditsDetailDO = credittsLogDetailDao.save(creditsDetailDO);
  674. if (taskRuleDO.getTradeDirection() == -1){
  675. if (taskRuleDO.getIntegrate() == 0){
  676. String integrateSql = "select * from wlyy_health_bank_credits_detail where patient_id = '"+patientId+"' " +
  677. "AND transaction_id IN (SELECT id FROM wlyy_health_bank_task WHERE task_code IN ('BP_BIND','GLU_BIND','BP_MEASURE','GLU_MEASURE','RULE'))";
  678. List<CreditsDetailDO> creditsDetailDOS = jdbcTemplate.query(integrateSql,new BeanPropertyRowMapper(CreditsDetailDO.class));
  679. AccountDO accountDO = accountDOS.get(0);
  680. for (CreditsDetailDO creditsDetailDO1:creditsDetailDOS){
  681. if (creditsDetailDO1.getTradeDirection()==1&&creditsDetailDO1.getStatus()!=0){
  682. int total = accountDO.getTotal() - creditsDetailDO1.getIntegrate();
  683. creditsDetailDO1.setStatus(0);
  684. creditsDetailDO1.setDescription(description);
  685. credittsLogDetailDao.save(creditsDetailDO1);
  686. }
  687. }
  688. accountDO.setTotal(0);
  689. accountDao.save(accountDO);
  690. }else if (taskRuleDO.getIntegrate()!=0){
  691. AccountDO accountDO = accountDOS.get(0);
  692. int total = accountDO.getTotal() - creditsDetailDO.getIntegrate();
  693. accountDO.setTotal(total);
  694. accountDao.save(accountDO);
  695. }
  696. }else if (taskRuleDO.getTradeDirection() == 1){
  697. AccountDO accountDO = accountDOS.get(0);
  698. accountDO.setTotal(accountDO.getTotal() + taskRuleDO.getIntegrate());
  699. accountDao.save(accountDO);
  700. }
  701. }
  702. }
  703. envelop.setObj(true);
  704. return envelop;
  705. }
  706. /**
  707. * 固定数据
  708. *
  709. * @param
  710. * @return
  711. *//*
  712. public List<TaskDO> getTasks(String patientId){
  713. List<TaskDO> taskDOList = new ArrayList<>();
  714. TaskDO taskDO = new TaskDO();
  715. *//*taskDO.setPatientId(patientId);*//*
  716. taskDO.setTaskCode("BIND");
  717. taskDO.setPeriod(1);
  718. taskDO.setTaskTitle("首次绑定");
  719. taskDO.setTaskContent("(绑定社区登记发放的设备,可获得各类型设备的首绑奖励。)");
  720. taskDO.setTradeType("activity");
  721. taskDO.setTransactionId("402885e96324a409016324c0a45a0006");
  722. taskDO.setCreateTime(new Date());
  723. taskDO.setUpdateTime(new Date());
  724. taskDOList.add(taskDO);
  725. TaskDO taskDO1 = new TaskDO();
  726. *//*taskDO1.setPatientId(patientId);*//*
  727. taskDO1.setTaskCode("MEASURE");
  728. taskDO1.setPeriod(0);
  729. taskDO1.setTaskTitle("每日测量");
  730. taskDO1.setTaskContent("(使用社区发放的已关联您身份信息的设备,绑定后每天完成测量)");
  731. taskDO1.setTradeType("activity");
  732. taskDO1.setTransactionId("402885e96324a409016324c0a45a0006");
  733. taskDO1.setCreateTime(new Date());
  734. taskDO1.setUpdateTime(new Date());
  735. taskDOList.add(taskDO1);
  736. return taskDOList;
  737. }
  738. */
  739. public MixEnvelop<CreditsDetailDO, CreditsDetailDO> stepAddIntegrate(CreditsDetailDO creditsDetailDO){
  740. try {
  741. synchronized (creditsDetailDO.getPatientId()){
  742. String sqlAccount = "select * from wlyy_health_bank_account ba where ba.patient_id = '"+creditsDetailDO.getPatientId() +"'";
  743. List<AccountDO> accountDOList = jdbcTemplate.query(sqlAccount,new BeanPropertyRowMapper(AccountDO.class));
  744. if (accountDOList != null && accountDOList.size() != 0){
  745. creditsDetailDO.setAccountId(accountDOList.get(0).getId());
  746. }else {
  747. AccountDO accountDO1 = new AccountDO();
  748. accountDO1.setPatientId(creditsDetailDO.getPatientId());
  749. accountDO1.setSaasId("dev");
  750. accountDO1.setTotal(0);
  751. accountDO1.setCouponTotal(0L);
  752. accountDO1.setAccountName(creditsDetailDO.getName());
  753. if(creditsDetailDO.getIdCard().length()>=4){// 判断是否长度大于等于4
  754. String cardNumber=creditsDetailDO.getIdCard().substring(creditsDetailDO.getIdCard().length()- 4,creditsDetailDO.getIdCard().length());//截取两个数字之间的部分
  755. int random = (int)((Math.random()*9+1)*100000);
  756. accountDO1.setCardNumber(cardNumber+Integer.toString(random));
  757. }
  758. accountDO1.setHospital(creditsDetailDO.getHospital());
  759. accountDO1.setPassword("123456");
  760. accountDO1.setHospitalName(creditsDetailDO.getHospitalName());
  761. accountDO1.setStatus(1);
  762. accountDO1.setCreateTime(new Date());
  763. accountDO1.setUpdateTime(new Date());
  764. accountDao.save(accountDO1);
  765. List<AccountDO> accountDOS = jdbcTemplate.query(sqlAccount,new BeanPropertyRowMapper(AccountDO.class));
  766. creditsDetailDO.setAccountId(accountDOS.get(0).getId());
  767. }
  768. String sql1 = "select * from wlyy_health_bank_task_patient_detail where "+
  769. " patient_idcard = '"+creditsDetailDO.getIdCard()+"' AND union_id = '"+creditsDetailDO.getUnionId()+"' AND task_id = '"+creditsDetailDO.getTransactionId()+"'";
  770. List<TaskPatientDetailDO> taskPatientDetailDOList = jdbcTemplate.query(sql1,new BeanPropertyRowMapper(TaskPatientDetailDO.class));
  771. if(taskPatientDetailDOList == null || taskPatientDetailDOList.size()==0){
  772. throw new Exception("该居民参与活动查不到!");
  773. }
  774. TaskPatientDetailDO taskPatientDetailDO = taskPatientDetailDOList.get(0);
  775. String sql = "select * from wlyy_health_bank_credits_detail where patient_id = '"+creditsDetailDO.getPatientId()+"' AND " +
  776. "transaction_id = '"+creditsDetailDO.getTransactionId()+"' AND create_time > '"+DateUtils.getDayBegin() +"' AND" +
  777. " create_time < '"+DateUtils.getDayEnd()+"'AND ISNULL(description)";
  778. List<CreditsDetailDO> creditsDetailDOS = jdbcTemplate.query(sql,new BeanPropertyRowMapper<>(CreditsDetailDO.class));
  779. ActivityRuleDO activityRuleDO = activityRuleDao.selectByActivityId(taskPatientDetailDO.getActivityId());
  780. if (activityRuleDO.getValue1() != null){
  781. JSONObject object = JSONObject.parseObject(activityRuleDO.getValue1());
  782. //获取步数
  783. JSONArray array = object.getJSONArray("healthStep");
  784. int j = array.size();
  785. int z= 0;
  786. for (int i = 0;i<=array.size();i++){
  787. JSONObject jsonObject = array.getJSONObject(i);
  788. Long step = jsonObject.getLong("step");
  789. int integrate = jsonObject.getInteger("integrate");
  790. if (creditsDetailDOS != null && creditsDetailDOS.size() != 0){
  791. CreditsDetailDO creditsDetailDO1 = creditsDetailDOS.get(0);
  792. if (creditsDetailDO.getStepNumber()>0 && creditsDetailDO.getStepNumber()<=step){
  793. int integrate1=creditsDetailDO1.getIntegrate();
  794. creditsDetailDO1.setIntegrate(creditsDetailDO.getIntegrate());
  795. creditsDetailDO1.setTradeDirection(1);
  796. CreditsDetailDO creditsDetailDO2 = credittsLogDetailDao.save(creditsDetailDO1);
  797. AccountDO accountDO = accountDao.findOne(creditsDetailDO2.getAccountId());
  798. accountDO.setTotal(accountDO.getTotal()-integrate1+creditsDetailDO2.getIntegrate());
  799. AccountDO accountDO1 = accountDao.save(accountDO);
  800. creditsDetailDO2.setTotal(accountDO1.getTotal());
  801. taskPatientDetailDO.setTotal(taskPatientDetailDO.getTotal()-integrate1+creditsDetailDO2.getIntegrate());
  802. taskPatientDetailDao.save(taskPatientDetailDO);
  803. creditsDetailDOS.clear();
  804. creditsDetailDOS.add(creditsDetailDO2);
  805. break;
  806. }
  807. }else {
  808. CreditsDetailDO creditsDetailDO1 = new CreditsDetailDO();
  809. if (creditsDetailDO.getStepNumber()>0 && creditsDetailDO.getStepNumber()<=step){
  810. creditsDetailDO1.setIntegrate(creditsDetailDO.getIntegrate());
  811. creditsDetailDO1.setTradeDirection(1);
  812. creditsDetailDO1.setSaasId("dev");
  813. creditsDetailDO1.setTradeType("ACTIVITY_TASK");
  814. creditsDetailDO1.setPatientId(creditsDetailDO.getPatientId());
  815. creditsDetailDO1.setHospital(creditsDetailDO.getHospital());
  816. creditsDetailDO1.setHospitalName(creditsDetailDO.getHospitalName());
  817. creditsDetailDO1.setAccountId(creditsDetailDO.getAccountId());
  818. creditsDetailDO1.setStatus(1);
  819. creditsDetailDO1.setCreateTime(new Date());
  820. creditsDetailDO1.setUpdateTime(new Date());
  821. creditsDetailDO1.setTransactionId(taskPatientDetailDO.getTaskId());
  822. CreditsDetailDO creditsDetailDO2 = credittsLogDetailDao.save(creditsDetailDO1);
  823. AccountDO accountDO = accountDao.findOne(creditsDetailDO2.getAccountId());
  824. accountDO.setTotal(accountDO.getTotal()+creditsDetailDO2.getIntegrate());
  825. taskPatientDetailDO.setTotal(taskPatientDetailDO.getTotal()+creditsDetailDO2.getIntegrate());
  826. taskPatientDetailDao.save(taskPatientDetailDO);
  827. creditsDetailDO2.setTotal(accountDO.getTotal());
  828. creditsDetailDOS.add(creditsDetailDO2);
  829. break;
  830. }
  831. }
  832. }
  833. }
  834. /* for (int i = 0;i<array.size();i++){
  835. JSONObject jsonObject = array.getJSONObject(i);
  836. if (creditsDetailDOS != null && creditsDetailDOS.size() != 0){
  837. CreditsDetailDO creditsDetailDO1 = creditsDetailDOS.get(0);
  838. TaskRuleDO taskRuleDO = taskRuleDao.findOne(taskDO.getRuleCode());
  839. if (creditsDetailDO.getStepNumber() < step1 && creditsDetailDO.getStepNumber() >0){
  840. creditsDetailDO1.setIntegrate(0);
  841. creditsDetailDO1.setTradeDirection(1);
  842. CreditsDetailDO creditsDetailDO2 = credittsLogDetailDao.save(creditsDetailDO1);
  843. AccountDO accountDO = accountDao.findOne(creditsDetailDO2.getAccountId());
  844. accountDO.setTotal(accountDO.getTotal()+creditsDetailDO2.getIntegrate());
  845. AccountDO accountDO1 = accountDao.save(accountDO);
  846. creditsDetailDO2.setTotal(accountDO1.getTotal());
  847. taskPatientDetailDO.setTotal(taskPatientDetailDO.getTotal()+creditsDetailDO2.getIntegrate());
  848. taskPatientDetailDao.save(taskPatientDetailDO);
  849. creditsDetailDOS.clear();
  850. creditsDetailDOS.add(creditsDetailDO2);
  851. }else if (creditsDetailDO.getStepNumber() >= step1 && creditsDetailDO.getStepNumber() < step2){
  852. creditsDetailDO1.setIntegrate(creditsDetailDO1.getIntegrate()+1);
  853. creditsDetailDO1.setTradeDirection(1);
  854. CreditsDetailDO creditsDetailDO2 = credittsLogDetailDao.save(creditsDetailDO1);
  855. AccountDO accountDO = accountDao.findOne(creditsDetailDO2.getAccountId());
  856. accountDO.setTotal(accountDO.getTotal()+(creditsDetailDO2.getIntegrate()));
  857. AccountDO accountDO1 = accountDao.save(accountDO);
  858. creditsDetailDO2.setTotal(accountDO1.getTotal());
  859. taskPatientDetailDO.setTotal(taskPatientDetailDO.getTotal()+(creditsDetailDO2.getIntegrate()));
  860. taskPatientDetailDao.save(taskPatientDetailDO);
  861. creditsDetailDOS.clear();
  862. creditsDetailDOS.add(creditsDetailDO2);
  863. }else if (creditsDetailDO.getStepNumber() >= step2){
  864. if (creditsDetailDO1.getIntegrate() == 1){
  865. creditsDetailDO1.setIntegrate(2);
  866. creditsDetailDO1.setTradeDirection(1);
  867. CreditsDetailDO creditsDetailDO2 = credittsLogDetailDao.save(creditsDetailDO1);
  868. AccountDO accountDO = accountDao.findOne(creditsDetailDO2.getAccountId());
  869. accountDO.setTotal(accountDO.getTotal()+(creditsDetailDO2.getIntegrate()-1));
  870. AccountDO accountDO1 = accountDao.save(accountDO);
  871. creditsDetailDO2.setTotal(accountDO1.getTotal());
  872. taskPatientDetailDO.setTotal(taskPatientDetailDO.getTotal()+(creditsDetailDO2.getIntegrate()-1));
  873. taskPatientDetailDao.save(taskPatientDetailDO);
  874. creditsDetailDOS.clear();
  875. creditsDetailDOS.add(creditsDetailDO2);
  876. }*//*else if(creditsDetailDO1.getIntegrate() == 3){
  877. creditsDetailDO1.setIntegrate(creditsDetailDO1.getIntegrate()+5);
  878. creditsDetailDO1.setTradeDirection(1);
  879. CreditsDetailDO creditsDetailDO2 = credittsLogDetailDao.save(creditsDetailDO1);
  880. AccountDO accountDO = accountDao.findOne(creditsDetailDO2.getAccountId());
  881. accountDO.setTotal(accountDO.getTotal()+(creditsDetailDO2.getIntegrate()-3));
  882. AccountDO accountDO1 = accountDao.save(accountDO);
  883. creditsDetailDO2.setTotal(accountDO1.getTotal());
  884. taskPatientDetailDO.setTotal(taskPatientDetailDO.getTotal()+(creditsDetailDO2.getIntegrate()-3));
  885. taskPatientDetailDao.save(taskPatientDetailDO);
  886. creditsDetailDOS.clear();
  887. creditsDetailDOS.add(creditsDetailDO2);
  888. }*//*
  889. }
  890. }else{
  891. CreditsDetailDO creditsDetailDO1 = new CreditsDetailDO();
  892. if (creditsDetailDO.getStepNumber() < step1 && creditsDetailDO.getStepNumber() >0){
  893. creditsDetailDO1.setIntegrate(0);
  894. creditsDetailDO1.setTradeDirection(1);
  895. }else if (creditsDetailDO.getStepNumber() >= step1 && creditsDetailDO.getStepNumber() < step2){
  896. creditsDetailDO1.setIntegrate(1);
  897. creditsDetailDO1.setTradeDirection(1);
  898. }else if (creditsDetailDO.getStepNumber() >=step2){
  899. creditsDetailDO1.setIntegrate(2);
  900. creditsDetailDO1.setTradeDirection(1);
  901. }
  902. creditsDetailDO1.setSaasId("dev");
  903. creditsDetailDO1.setTradeType("ACTIVITY_TASK");
  904. creditsDetailDO1.setPatientId(creditsDetailDO.getPatientId());
  905. creditsDetailDO1.setHospital(creditsDetailDO.getHospital());
  906. creditsDetailDO1.setHospitalName(creditsDetailDO.getHospitalName());
  907. creditsDetailDO1.setAccountId(creditsDetailDO.getAccountId());
  908. creditsDetailDO1.setStatus(1);
  909. creditsDetailDO1.setCreateTime(new Date());
  910. creditsDetailDO1.setUpdateTime(new Date());
  911. creditsDetailDO1.setTransactionId(taskPatientDetailDO.getTaskId());
  912. CreditsDetailDO creditsDetailDO2 = credittsLogDetailDao.save(creditsDetailDO1);
  913. AccountDO accountDO = accountDao.findOne(creditsDetailDO2.getAccountId());
  914. accountDO.setTotal(accountDO.getTotal()+creditsDetailDO2.getIntegrate());
  915. taskPatientDetailDO.setTotal(taskPatientDetailDO.getTotal()+creditsDetailDO2.getIntegrate());
  916. taskPatientDetailDao.save(taskPatientDetailDO);
  917. creditsDetailDO2.setTotal(accountDO.getTotal());
  918. creditsDetailDOS.add(creditsDetailDO2);
  919. }*/
  920. /* String step = redisTemplate.opsForValue().get(STEP);
  921. if (StringUtils.isEmpty(step)){
  922. throw new Exception("获取步数失败!");
  923. }
  924. JSONObject object = JSONObject.parseObject(step);
  925. int step1 = object.getInteger("step1");
  926. int step2 = object.getInteger("step2");
  927. */
  928. try{
  929. activeRecordService.insert("dev",creditsDetailDOS.get(0).getTransactionId(),null,null,null,creditsDetailDOS.get(0).getPatientId());
  930. }catch (Exception e){
  931. logger.error("插入活跃出错:"+e.getMessage());
  932. }
  933. MixEnvelop<CreditsDetailDO, CreditsDetailDO> envelop = new MixEnvelop<>();
  934. envelop.setDetailModelList(creditsDetailDOS);
  935. return envelop;
  936. }
  937. }catch (Exception e){
  938. e.printStackTrace();
  939. MixEnvelop<CreditsDetailDO, CreditsDetailDO> envelop = new MixEnvelop<>();
  940. return envelop;
  941. }
  942. }
  943. public MixEnvelop doctorDescreaseIntegrate(String ruleId,String patientId,String doctorId,String hospital,String hospitalName,Integer integrate,String reason) throws Exception {
  944. MixEnvelop envelop = new MixEnvelop<>();
  945. envelop.setObj(true);
  946. TaskDO taskDO = taskDao.selectByTaskRuleId(ruleId);
  947. String sql = "select * from wlyy_health_bank_account where status = 1 and patient_id = '"+patientId+"'";
  948. List<AccountDO> accountDOs = jdbcTemplate.query(sql,new BeanPropertyRowMapper(AccountDO.class));
  949. AccountDO accountDO = accountDOs.get(0);
  950. if (taskDO == null){
  951. TaskRuleDO taskRuleDO = taskRuleDao.findByCode(ruleId);
  952. TaskDO taskDO1 = new TaskDO();
  953. taskDO1.setTitle(taskRuleDO.getName());
  954. taskDO1.setContent(taskRuleDO.getDescription());
  955. taskDO1.setSaasId("dev");
  956. taskDO1.setTaskCode("RULE");
  957. taskDO1.setType("RULE_TASK");
  958. taskDO1.setTransactionId(ruleId);
  959. taskDO1.setCreateTime(new Date());
  960. taskDO1.setUpdateTime(new Date());
  961. taskDO1.setRuleCode(taskRuleDO.getId());
  962. taskDO1.setStatus(1);
  963. taskDO = taskDao.save(taskDO1);
  964. }
  965. TaskPatientDetailDO patientDetailDO = taskPatientDetailDao.selectByTaskIdAndPatientId(taskDO.getId(),patientId);
  966. if (patientDetailDO == null){
  967. TaskPatientDetailDO taskPatientDetailDO = new TaskPatientDetailDO();
  968. taskPatientDetailDO.setSaasId("dev");
  969. taskPatientDetailDO.setTaskId(taskDO.getId());
  970. taskPatientDetailDO.setPatientId(patientId);
  971. taskPatientDetailDO.setDoctorId(doctorId);
  972. taskPatientDetailDO.setPatientIdcard(accountDO.getIdCard());
  973. taskPatientDetailDO.setCreateTime(new Date());
  974. taskPatientDetailDO.setUpdateTime(new Date());
  975. taskPatientDetailDO.setStatus(1);
  976. taskPatientDetailDao.save(taskPatientDetailDO);
  977. }
  978. if (accountDO.getTotal().compareTo(integrate)>=0){
  979. CreditsDetailDO creditsDetailDO = new CreditsDetailDO();
  980. creditsDetailDO.setAccountId(accountDO.getId());
  981. creditsDetailDO.setIdCard(accountDO.getIdCard());
  982. creditsDetailDO.setSaasId("dev");
  983. creditsDetailDO.setTradeType("ACTIVITY_TASK");
  984. creditsDetailDO.setTradeDirection(-1);
  985. creditsDetailDO.setIntegrate(integrate);
  986. creditsDetailDO.setDescription(reason);
  987. creditsDetailDO.setStatus(1);
  988. creditsDetailDO.setHospital(hospital);
  989. creditsDetailDO.setHospitalName(hospitalName);
  990. creditsDetailDO.setCreateTime(new Date());
  991. creditsDetailDO.setUpdateTime(new Date());
  992. creditsDetailDO.setTransactionId(taskDO.getId());
  993. creditsDetailDO.setPatientId(patientId);
  994. creditsDetailDO.setHospitalName(hospitalName);
  995. credittsLogDetailDao.save(creditsDetailDO);
  996. accountDO.setTotal(accountDO.getTotal()-integrate);
  997. accountDao.save(accountDO);
  998. }else {
  999. envelop.setMessage("积分不足!");
  1000. throw new Exception("积分不足!");
  1001. }
  1002. return envelop;
  1003. }
  1004. /*public JSONObject getStepNumber(){
  1005. String step = redisTemplate.·().get(STEP);
  1006. logger.info("redis数据:"+step);
  1007. String sql = ""
  1008. String step1 = systemDictDao.
  1009. logger.info("数据库的数据:"+step1);
  1010. if (step != null && step.equalsIgnoreCase(step1)){
  1011. JSONObject jsonObject = JSONObject.parseObject(step);
  1012. logger.info("数据不一致:"+jsonObject.toString());
  1013. return jsonObject;
  1014. }else {
  1015. JSONObject jsonObject = JSONObject.parseObject(step);
  1016. redisTemplate.opsForValue().set(STEP,step1);
  1017. logger.info("数据一致:"+jsonObject.toString());
  1018. return jsonObject;
  1019. }
  1020. }*/
  1021. public MixEnvelop<CreditsDetailDO, CreditsDetailDO> weekReward(CreditsDetailDO creditsDetailDO) {
  1022. try {
  1023. synchronized (creditsDetailDO.getPatientId()) {
  1024. String sqlAccount = "select * from wlyy_health_bank_account ba where ba.patient_id = '" + creditsDetailDO.getPatientId() + "'";
  1025. List<AccountDO> accountDOList = jdbcTemplate.query(sqlAccount, new BeanPropertyRowMapper(AccountDO.class));
  1026. if (accountDOList != null && accountDOList.size() != 0) {
  1027. creditsDetailDO.setAccountId(accountDOList.get(0).getId());
  1028. }
  1029. String creditsSql = "select * from wlyy_health_bank_credits_detail where patient_id = '" + creditsDetailDO.getPatientId() + "' and " +
  1030. "DATE_FORMAT(create_time,'%Y-%m-%d') IN " + creditsDetailDO.getWeekTimes()+" and description = '周奖励'";
  1031. List<CreditsDetailDO> creditsDetailDOS1 = jdbcTemplate.query(creditsSql, new BeanPropertyRowMapper(CreditsDetailDO.class));
  1032. List<CreditsDetailDO> creditsDetailDOList = new ArrayList<>();
  1033. MixEnvelop<CreditsDetailDO, CreditsDetailDO> envelop = new MixEnvelop<>();
  1034. if (getWeekOfDate(new Date()).equalsIgnoreCase("星期五")||getWeekOfDate(new Date()).equalsIgnoreCase("星期六")||getWeekOfDate(new Date()).equalsIgnoreCase("星期日")){
  1035. String sql = "select sum(integrate) as total from wlyy_health_bank_credits_detail where patient_id = '" + creditsDetailDO.getPatientId() + "' and description = '周奖励'";
  1036. List<Map<String,Object>> rstotal = jdbcTemplate.queryForList(sql);
  1037. Long count = 0L;
  1038. if(rstotal!=null&&rstotal.size()>0){
  1039. Object object = rstotal.get(0).get("total");
  1040. if (object != null){
  1041. count = Long.parseLong(object.toString());
  1042. }
  1043. }
  1044. if (count > 12){
  1045. envelop.setMessage("奖励积分已达到12分。不能再奖励了!");
  1046. return envelop;
  1047. }else{
  1048. if (creditsDetailDOS1 == null || creditsDetailDOS1.size() == 0) {
  1049. creditsDetailDO.setTradeType("ACTIVITY_TASK");
  1050. creditsDetailDO.setSaasId("dev");
  1051. creditsDetailDO.setStatus(1);
  1052. creditsDetailDO.setTransactionId(creditsDetailDO.getTransactionId());
  1053. creditsDetailDO.setCreateTime(new Date());
  1054. creditsDetailDO.setUpdateTime(new Date());
  1055. creditsDetailDO.setTradeDirection(1);
  1056. CreditsDetailDO creditsDetailDO1 = credittsLogDetailDao.save(creditsDetailDO);
  1057. creditsDetailDOList.add(creditsDetailDO1);
  1058. TaskPatientDetailDO taskPatientDetailDO = new TaskPatientDetailDO();
  1059. taskPatientDetailDO.setPatientId(creditsDetailDO1.getPatientId());
  1060. taskPatientDetailDO.setTaskId(creditsDetailDO1.getTransactionId());
  1061. String taskSql1 = ISqlUtils.getAllSql(taskPatientDetailDO);
  1062. List<TaskPatientDetailDO> taskPatientDetailDOS1 = jdbcTemplate.query(taskSql1, new BeanPropertyRowMapper(TaskPatientDetailDO.class));
  1063. TaskPatientDetailDO taskPatientDetailDO1 = taskPatientDetailDOS1.get(0);
  1064. if (creditsDetailDO1.getTradeDirection() == 1) {
  1065. taskPatientDetailDO1.setTotal(taskPatientDetailDO1.getTotal() + creditsDetailDO1.getIntegrate());
  1066. } else if (creditsDetailDO.getTradeDirection() == -1) {
  1067. taskPatientDetailDO1.setTotal(taskPatientDetailDO1.getTotal() - creditsDetailDO1.getIntegrate());
  1068. }
  1069. taskPatientDetailDao.save(taskPatientDetailDO1);
  1070. AccountDO accountDO = accountDao.findOne(creditsDetailDO1.getAccountId());
  1071. if (creditsDetailDO1.getTradeDirection() == 1) {
  1072. accountDO.setTotal(accountDO.getTotal() + creditsDetailDO1.getIntegrate());
  1073. } else if (creditsDetailDO.getTradeDirection() == -1) {
  1074. accountDO.setTotal(accountDO.getTotal() - creditsDetailDO1.getIntegrate());
  1075. }
  1076. AccountDO accountDO1 = accountDao.save(accountDO);
  1077. List<CreditsDetailDO> creditsDetailDOS = new ArrayList<>();
  1078. for (CreditsDetailDO creditsDetailDO2 : creditsDetailDOList) {
  1079. creditsDetailDO2.setTotal(accountDO1.getTotal());
  1080. creditsDetailDO2.setFlag("1");
  1081. creditsDetailDOS.add(creditsDetailDO2);
  1082. }
  1083. envelop.setDetailModelList(creditsDetailDOS);
  1084. }else {
  1085. for (CreditsDetailDO creditsDetailDO1 : creditsDetailDOS1){
  1086. creditsDetailDO1.setFlag("2");
  1087. }
  1088. envelop.setDetailModelList(creditsDetailDOS1);
  1089. }
  1090. return envelop;
  1091. }
  1092. }else{
  1093. CreditsDetailDO creditsDetailDO1 = new CreditsDetailDO();
  1094. creditsDetailDO1.setFlag("0");
  1095. creditsDetailDOList.add(creditsDetailDO1);
  1096. envelop.setDetailModelList(creditsDetailDOList);
  1097. return envelop;
  1098. }
  1099. }
  1100. } catch (Exception e) {
  1101. e.printStackTrace();
  1102. MixEnvelop<CreditsDetailDO, CreditsDetailDO> envelop = new MixEnvelop<>();
  1103. return envelop;
  1104. }
  1105. }
  1106. public String getWeekOfDate(Date dt) {
  1107. String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
  1108. Calendar cal = Calendar.getInstance();
  1109. cal.setTime(dt);
  1110. int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
  1111. if (w < 0)
  1112. w = 0;
  1113. return weekDays[w];
  1114. }
  1115. /**
  1116. * 健康问答添加积分
  1117. *
  1118. * @param creditsDetailDO
  1119. * @return
  1120. * @throws Exception
  1121. */
  1122. public MixEnvelop<CreditsDetailDO,CreditsDetailDO> patientGetInteger(CreditsDetailDO creditsDetailDO) throws Exception {
  1123. MixEnvelop<CreditsDetailDO,CreditsDetailDO> envelop = new MixEnvelop<>();
  1124. TaskDO taskDO = taskDao.selectById(creditsDetailDO.getTransactionId());
  1125. ActivityRuleDO ruleDO = activityRuleDao.selectByActivityId(taskDO.getTransactionId());
  1126. JSONObject object = JSONObject.parseObject(ruleDO.getValue1());
  1127. Integer type = object.getInteger("type");
  1128. AccountDO accountDO = accountDao.selectByPatientId(creditsDetailDO.getPatientId());
  1129. creditsDetailDO.setUpdateTime(new Date());
  1130. creditsDetailDO.setCreateTime(new Date());
  1131. creditsDetailDO.setStatus(1);
  1132. if (type == 2){
  1133. creditsDetailDO.setCoupon(creditsDetailDO.getCoupon());
  1134. creditsDetailDO.setIntegrate(null);
  1135. }else if (type==1){
  1136. creditsDetailDO.setIntegrate(creditsDetailDO.getCoupon());
  1137. creditsDetailDO.setCoupon(null);
  1138. }
  1139. creditsDetailDO.setSaasId("dev");
  1140. creditsDetailDO.setTradeType("ACTIVITY_TASK");
  1141. creditsDetailDO.setTradeDirection(1);
  1142. creditsDetailDO.setAccountId(accountDO.getId());
  1143. CreditsDetailDO creditsDetailDO1 = credittsLogDetailDao.save(creditsDetailDO);
  1144. if (creditsDetailDO1 == null){
  1145. throw new Exception("积分添加失败!");
  1146. }
  1147. TaskPatientDetailDO patientDetailDO = taskPatientDetailDao.selectByTaskIdAndPatientId(creditsDetailDO.getTransactionId(),creditsDetailDO.getPatientId());
  1148. patientDetailDO.setUpdateTime(new Date());
  1149. patientDetailDO.setCreateTime(new Date());
  1150. if (type == 2){
  1151. patientDetailDO.setCouponTotal(patientDetailDO.getCouponTotal()+creditsDetailDO.getCoupon());
  1152. }else if (type==1||type==3){
  1153. patientDetailDO.setTotal(patientDetailDO.getTotal()+creditsDetailDO.getIntegrate());
  1154. }
  1155. patientDetailDO.setStatus(1);
  1156. TaskPatientDetailDO taskPatientDetailDO = taskPatientDetailDao.save(patientDetailDO);
  1157. if (taskPatientDetailDO == null){
  1158. throw new Exception("修改活动状态和积分失败!");
  1159. }
  1160. if (type == 2){
  1161. accountDO.setCouponTotal(accountDO.getCouponTotal()+creditsDetailDO.getCoupon());
  1162. }else if (type==1||type==3){
  1163. accountDO.setTotal(accountDO.getTotal()+creditsDetailDO.getIntegrate());
  1164. }
  1165. accountDO.setUpdateTime(new Date());
  1166. AccountDO accountDO1 = accountDao.save(accountDO);
  1167. if (accountDO1 == null){
  1168. throw new Exception("修改银行账户积分失败!");
  1169. }
  1170. envelop.setObj(creditsDetailDO);
  1171. return envelop;
  1172. }
  1173. /**
  1174. * 健康问答--获取当日积分
  1175. *
  1176. * @param patient
  1177. * @param transactionId
  1178. * @return
  1179. */
  1180. public MixEnvelop<CreditsDetailDO,CreditsDetailDO> selectByDate(String patient,String transactionId){
  1181. MixEnvelop<CreditsDetailDO,CreditsDetailDO> envelop = new MixEnvelop<>();
  1182. String sql ="select * from wlyy_health_bank_credits_detail where patient_id = '"+patient+"' and " +
  1183. "transaction_id = '"+transactionId+"' and create_time >='"+ DateUtils.getDayBegin()+"' and " +
  1184. "create_time <= '"+DateUtils.getDayEnd()+"'";
  1185. List<CreditsDetailDO> creditsDetailDOS = jdbcTemplate.query(sql,new BeanPropertyRowMapper<>(CreditsDetailDO.class));
  1186. CreditsDetailDO creditsDetailDO = new CreditsDetailDO();
  1187. if (creditsDetailDOS != null && creditsDetailDOS.size()!=0){
  1188. creditsDetailDO = creditsDetailDOS.get(0);
  1189. }
  1190. TaskDO taskDO = taskDao.selectById(transactionId);
  1191. ActivityRuleDO ruleDO = activityRuleDao.selectByActivityId(taskDO.getTransactionId());
  1192. //活动总积分排名
  1193. String rankingSql1 = "SELECT count(1)+1 AS total FROM (SELECT " +
  1194. " SUM(total) AS sum , " +
  1195. " patient_id, " +
  1196. " activity_id " +
  1197. "FROM " +
  1198. " wlyy_health_bank_task_patient_detail btpd WHERE btpd.patient_id !='"+patient+"' " +
  1199. "GROUP BY btpd.patient_id )btpd1 " +
  1200. "WHERE " +
  1201. " btpd1.activity_id = '"+taskDO.getTransactionId()+"' AND btpd1.sum >= (SELECT SUM(s.total) as total " +
  1202. "FROM wlyy_health_bank_task_patient_detail s WHERE s.patient_id ='"+patient+"' AND s.activity_id='"+taskDO.getTransactionId()+"')" ;
  1203. List<Map<String,Object>> rstotal1 = jdbcTemplate.queryForList(rankingSql1);
  1204. Long count = 0L;
  1205. if(rstotal1!=null&&rstotal1.size()>0){
  1206. count = (Long) rstotal1.get(0).get("total");
  1207. }
  1208. //总活动劵的排名
  1209. String rankingSql2 = "SELECT count(1)+1 AS total FROM (SELECT " +
  1210. " SUM(coupon_total) AS sum , " +
  1211. " patient_id, " +
  1212. " activity_id " +
  1213. "FROM " +
  1214. " wlyy_health_bank_task_patient_detail btpd WHERE btpd.patient_id !='"+patient+"' "+
  1215. "GROUP BY btpd.patient_id )btpd1 " +
  1216. "WHERE " +
  1217. " btpd1.activity_id = '"+taskDO.getTransactionId()+"' AND btpd1.sum >= (SELECT SUM(s.coupon_total) as couponTotal " +
  1218. "FROM wlyy_health_bank_task_patient_detail s WHERE s.patient_id ='"+patient+"' AND s.activity_id='"+taskDO.getTransactionId()+"')" ;
  1219. List<Map<String,Object>> rstotal2 = jdbcTemplate.queryForList(rankingSql2);
  1220. Long count2 = 0L;
  1221. if(rstotal2!=null&&rstotal2.size()>0){
  1222. count2 = (Long) rstotal2.get(0).get("total");
  1223. }
  1224. JSONObject object = JSONObject.parseObject(ruleDO.getValue1());
  1225. Integer type = object.getInteger("type");
  1226. if (type == 2){
  1227. creditsDetailDO.setActivityRanking(count2);
  1228. creditsDetailDO.setMark(1);
  1229. }else if (type==1||type==3){
  1230. creditsDetailDO.setActivityRanking(count);
  1231. creditsDetailDO.setMark(2);
  1232. }
  1233. envelop.setObj(creditsDetailDO);
  1234. return envelop;
  1235. }
  1236. /**
  1237. * 分享获取积分
  1238. *
  1239. * @param creditsDetailDO
  1240. * @return
  1241. */
  1242. public MixEnvelop<CreditsDetailDO, CreditsDetailDO> shareIntegrate(CreditsDetailDO creditsDetailDO) {
  1243. try {
  1244. synchronized (creditsDetailDO.getPatientId()) {
  1245. String sqlAccount = "select * from wlyy_health_bank_account ba where ba.patient_id = '" + creditsDetailDO.getPatientId() + "'";
  1246. List<AccountDO> accountDOList = jdbcTemplate.query(sqlAccount, new BeanPropertyRowMapper(AccountDO.class));
  1247. if (accountDOList != null && accountDOList.size() != 0) {
  1248. creditsDetailDO.setAccountId(accountDOList.get(0).getId());
  1249. }
  1250. String creditsSql = "select * from wlyy_health_bank_credits_detail where patient_id = '" + creditsDetailDO.getPatientId() + "' and description = '分享'";
  1251. List<CreditsDetailDO> creditsDetailDOS1 = jdbcTemplate.query(creditsSql, new BeanPropertyRowMapper(CreditsDetailDO.class));
  1252. List<CreditsDetailDO> creditsDetailDOList = new ArrayList<>();
  1253. MixEnvelop<CreditsDetailDO, CreditsDetailDO> envelop = new MixEnvelop<>();
  1254. if (creditsDetailDOS1 == null || creditsDetailDOS1.size() == 0) {
  1255. creditsDetailDO.setTradeType("ACTIVITY_TASK");
  1256. creditsDetailDO.setDescription("分享");
  1257. creditsDetailDO.setSaasId("dev");
  1258. creditsDetailDO.setStatus(1);
  1259. creditsDetailDO.setTransactionId(creditsDetailDO.getTransactionId());
  1260. creditsDetailDO.setCreateTime(new Date());
  1261. creditsDetailDO.setUpdateTime(new Date());
  1262. creditsDetailDO.setTradeDirection(1);
  1263. CreditsDetailDO creditsDetailDO1 = credittsLogDetailDao.save(creditsDetailDO);
  1264. creditsDetailDO1.setFlag("1");
  1265. creditsDetailDOList.add(creditsDetailDO1);
  1266. TaskPatientDetailDO taskPatientDetailDO = new TaskPatientDetailDO();
  1267. taskPatientDetailDO.setPatientId(creditsDetailDO1.getPatientId());
  1268. taskPatientDetailDO.setTaskId(creditsDetailDO1.getTransactionId());
  1269. String taskSql1 = ISqlUtils.getAllSql(taskPatientDetailDO);
  1270. List<TaskPatientDetailDO> taskPatientDetailDOS1 = jdbcTemplate.query(taskSql1, new BeanPropertyRowMapper(TaskPatientDetailDO.class));
  1271. TaskPatientDetailDO taskPatientDetailDO1 = taskPatientDetailDOS1.get(0);
  1272. if (creditsDetailDO1.getTradeDirection() == 1) {
  1273. taskPatientDetailDO1.setTotal(taskPatientDetailDO1.getTotal() + creditsDetailDO1.getIntegrate());
  1274. } else if (creditsDetailDO.getTradeDirection() == -1) {
  1275. taskPatientDetailDO1.setTotal(taskPatientDetailDO1.getTotal() - creditsDetailDO1.getIntegrate());
  1276. }
  1277. taskPatientDetailDao.save(taskPatientDetailDO1);
  1278. AccountDO accountDO = accountDao.findOne(creditsDetailDO1.getAccountId());
  1279. if (creditsDetailDO1.getTradeDirection() == 1) {
  1280. accountDO.setTotal(accountDO.getTotal() + creditsDetailDO1.getIntegrate());
  1281. } else if (creditsDetailDO.getTradeDirection() == -1) {
  1282. accountDO.setTotal(accountDO.getTotal() - creditsDetailDO1.getIntegrate());
  1283. }
  1284. accountDao.save(accountDO);
  1285. envelop.setDetailModelList(creditsDetailDOList);
  1286. }else {
  1287. for (CreditsDetailDO creditsDetailDO1:creditsDetailDOS1){
  1288. creditsDetailDO1.setFlag("2");
  1289. creditsDetailDOList.add(creditsDetailDO1);
  1290. }
  1291. envelop.setDetailModelList(creditsDetailDOList);
  1292. }
  1293. return envelop;
  1294. }
  1295. } catch (Exception e) {
  1296. e.printStackTrace();
  1297. MixEnvelop<CreditsDetailDO, CreditsDetailDO> envelop = new MixEnvelop<>();
  1298. return envelop;
  1299. }
  1300. }
  1301. /**
  1302. * 商城积分支付
  1303. * @param patient
  1304. * @param total
  1305. * @param hospital
  1306. * @param hospitalName
  1307. * @param array
  1308. * @return
  1309. * @throws Exception
  1310. */
  1311. public MixEnvelop<CreditsDetailDO,CreditsDetailDO> payIntegrate(String patient,Double total,String hospital,String hospitalName,JSONArray array) throws Exception {
  1312. MixEnvelop<CreditsDetailDO,CreditsDetailDO> envelop = new MixEnvelop<>();
  1313. AccountDO accountDO = accountDao.selectByPatientId(patient);
  1314. Integer total1 = total.intValue();
  1315. if (accountDO == null){
  1316. envelop.setMessage("银行账户不存在!");
  1317. throw new Exception("银行账户不存在!");
  1318. }else {
  1319. if (total1>accountDO.getTotal()){
  1320. envelop.setMessage("积分余额不足!");
  1321. throw new Exception("积分余额不足!");
  1322. }else{
  1323. for (int i=0;i<array.size();i++){
  1324. JSONObject object = array.getJSONObject(i);
  1325. CreditsDetailDO creditsDetailDO = new CreditsDetailDO();
  1326. creditsDetailDO.setPatientId(patient);
  1327. creditsDetailDO.setAccountId(accountDO.getId());
  1328. creditsDetailDO.setHospital(hospital);
  1329. creditsDetailDO.setTradeType("SHOPPING_PAY");
  1330. creditsDetailDO.setTradeDirection(-1);
  1331. creditsDetailDO.setTransactionId(object.getString("orderId"));
  1332. creditsDetailDO.setIntegrate(object.getDouble("integrate").intValue());
  1333. creditsDetailDO.setCreateTime(new Date());
  1334. creditsDetailDO.setUpdateTime(new Date());
  1335. creditsDetailDO.setSaasId("dev");
  1336. creditsDetailDO.setId(getCode());
  1337. creditsDetailDO.setCoupon(0);
  1338. creditsDetailDO.setStatus(1);
  1339. credittsLogDetailDao.save(creditsDetailDO);
  1340. }
  1341. accountDO.setTotal(accountDO.getTotal()-total1);
  1342. accountDao.save(accountDO);
  1343. }
  1344. }
  1345. return envelop;
  1346. }
  1347. }