CreditsDetailService.java 107 KB

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