CreditsDetailService.java 107 KB

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