CreditsDetailService.java 97 KB

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