ActivityService.java 73 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434
  1. package com.yihu.jw.service;/**
  2. * Created by nature of king on 2018/4/27.
  3. */
  4. import com.alibaba.fastjson.JSON;
  5. import com.alibaba.fastjson.JSONArray;
  6. import com.alibaba.fastjson.JSONObject;
  7. import com.fasterxml.jackson.databind.ObjectMapper;
  8. import com.google.common.base.Joiner;
  9. import com.netflix.discovery.util.StringUtil;
  10. import com.yihu.jw.dao.*;
  11. import com.yihu.jw.entity.health.bank.*;
  12. import com.yihu.jw.restmodel.web.MixEnvelop;
  13. import com.yihu.jw.rm.health.bank.HealthBankMapping;
  14. import com.yihu.jw.util.DateUtils;
  15. import com.yihu.jw.util.ISqlUtils;
  16. import com.yihu.jw.util.date.DateUtil;
  17. import com.yihu.mysql.query.BaseJpaService;
  18. import org.apache.commons.lang3.StringUtils;
  19. import org.springframework.beans.factory.annotation.Autowired;
  20. import org.springframework.jdbc.core.BeanPropertyRowMapper;
  21. import org.springframework.jdbc.core.JdbcTemplate;
  22. import org.springframework.stereotype.Service;
  23. import javax.transaction.RollbackException;
  24. import javax.transaction.Transactional;
  25. import java.text.ParseException;
  26. import java.text.SimpleDateFormat;
  27. import java.util.*;
  28. /**
  29. * @author wangzhinan
  30. * @create 2018-04-27 14:38
  31. * @desc health activity Service
  32. **/
  33. @Service
  34. @Transactional
  35. public class ActivityService extends BaseJpaService<ActivityDO,ActivityDao> {
  36. @Autowired
  37. private ActivityDao activityDao;
  38. @Autowired
  39. private TaskPatientDetailDao taskPatientDetailDao;
  40. @Autowired
  41. private TaskDao taskDao;
  42. @Autowired
  43. private JdbcTemplate jdbcTemplate;
  44. @Autowired
  45. private ActivityRuleDao activityRuleDao;
  46. @Autowired
  47. private TaskGoodsDao taskGoodsDao;
  48. @Autowired
  49. private AccountDao accountDao;
  50. @Autowired
  51. private ExchangeGoodsDao exchangeGoodsDao;
  52. /**
  53. * insert activityDO
  54. *
  55. * @param activityDO 活动参数对象
  56. * @return
  57. */
  58. @org.springframework.transaction.annotation.Transactional(rollbackFor = Exception.class)
  59. public MixEnvelop<Boolean, Boolean> insert(ActivityDO activityDO,String value1,String value2,String value3,String registrationNumber){
  60. if (value2 != null && value2 !=""){
  61. JSONObject object = JSONObject.parseObject(value2);
  62. String activityOfflineTime = object.getString("activityOfflineTime");
  63. String[] str = activityOfflineTime.split(",");
  64. if (str.length == 2){
  65. activityDO.setActivityOfflineTime(DateUtil.strToDate(str[1]));
  66. }else {
  67. activityDO.setActivityOfflineTime(DateUtil.strToDate(str[0]));
  68. }
  69. String registrationTime = object.getString("registrationTime");
  70. String[] str1 = registrationTime.split(",");
  71. if(str1.length==2){
  72. activityDO.setRegistrationTime(DateUtil.strToDate(str1[1]));
  73. }else {
  74. activityDO.setRegistrationTime(DateUtil.strToDate(str1[0]));
  75. }
  76. String deadLine = object.getString("deadLine");
  77. String[] str2 = deadLine.split(",");
  78. if (str2.length==2){
  79. activityDO.setDeadLine(DateUtil.strToDate(str2[1]));
  80. }else {
  81. activityDO.setDeadLine(DateUtil.strToDate(str2[0]));
  82. }
  83. }
  84. activityDO.setCreateTime(new Date());
  85. activityDO.setUpdateTime(new Date());
  86. activityDO.setDelFlag(1);
  87. activityDO.setRegistrationNumber(registrationNumber);
  88. activityDO= activityDao.save(activityDO);
  89. if (value3 !=null && value3 !=""){
  90. JSONObject object = JSONObject.parseObject(value3);
  91. String exchangeType = object.getString("exchangeType");
  92. if (exchangeType.equals("1")){
  93. String array = (null!=object.get("goodDetail")?String.valueOf(object.get("goodDetail")):"");
  94. if (StringUtils.isNotBlank(array)){
  95. JSONArray array1= JSON.parseArray(array);
  96. for (int i =0;i<array1.size();i++){
  97. JSONObject jsonObject = array1.getJSONObject(i);
  98. TaskGoodsDO taskGoodsDO = new TaskGoodsDO();
  99. taskGoodsDO.setCreateTime(new Date());
  100. taskGoodsDO.setUpdateTime(new Date());
  101. taskGoodsDO.setActivityId(activityDO.getId());
  102. taskGoodsDO.setCoupon(jsonObject.getInteger("couponNum"));
  103. taskGoodsDO.setImg(jsonObject.getString("picUrl"));
  104. taskGoodsDO.setName(jsonObject.getString("name"));
  105. taskGoodsDO.setSaasId("dev");
  106. taskGoodsDO.setStatus(1);
  107. taskGoodsDao.save(taskGoodsDO);
  108. }
  109. }
  110. }
  111. }
  112. TaskDO taskDO = new TaskDO();
  113. taskDO.setTitle(activityDO.getTitle());
  114. taskDO.setStatus(1);
  115. taskDO.setTransactionId(activityDO.getId());
  116. taskDO.setType("ACTIVITY_TASK");
  117. taskDO.setSaasId("dev");
  118. taskDO.setContent(activityDO.getDescription());
  119. taskDO.setPeriod(1);
  120. taskDO.setTaskCode("ACTIVITY_QUESTION");
  121. taskDO.setStartTime(activityDO.getRegistrationTime());
  122. taskDO.setEndTime(activityDO.getActivityOfflineTime());
  123. taskDO.setCreateTime(new Date());
  124. taskDO.setUpdateTime(new Date());
  125. taskDao.save(taskDO);
  126. ActivityRuleDO activityRuleDO = new ActivityRuleDO();
  127. activityRuleDO.setCreateTime(new Date());
  128. activityRuleDO.setUpdateTime(new Date());
  129. activityRuleDO.setActivityId(activityDO.getId());
  130. activityRuleDO.setSaasId("dev");
  131. activityRuleDO.setValue1(value1);
  132. activityRuleDO.setValue2(value2);
  133. activityRuleDO.setValue3(value3);
  134. activityRuleDO.setDelFlag(1);
  135. /* activityRuleDO.setDel(1);*/
  136. activityRuleDao.save(activityRuleDO);
  137. MixEnvelop<Boolean, Boolean> envelop = new MixEnvelop<>();
  138. envelop.setObj(true);
  139. return envelop;
  140. }
  141. public MixEnvelop<ActivityDO,ActivityDO> createActivity(String planCode,String patient,String name,String idcard,String hospital,String hospitalName,String templateName){
  142. MixEnvelop<ActivityDO, ActivityDO> envelop = new MixEnvelop<>();
  143. AccountDO accountDO = accountDao.selectByPatientId(patient);
  144. if (accountDO==null){
  145. accountDO = new AccountDO();
  146. accountDO.setIdCard(idcard);
  147. accountDO.setStatus(1);
  148. accountDO.setTotal(0);
  149. accountDO.setPatientId(patient);
  150. accountDO.setCouponTotal(0L);
  151. accountDO.setPassword("123456");
  152. if(idcard.length()>=4){// 判断是否长度大于等于4
  153. String cardNumber=idcard.substring(idcard.length()- 4,idcard.length());//截取两个数字之间的部分
  154. int random = (int)((Math.random()*9+1)*100000);
  155. accountDO.setCardNumber(cardNumber+Integer.toString(random));
  156. }
  157. accountDO.setAccountName(name);
  158. accountDO.setHospitalName(hospitalName);
  159. accountDO.setHospital(hospital);
  160. accountDO.setSaasId("dev");
  161. accountDO.setCreateTime(new Date());
  162. accountDO.setUpdateTime(new Date());
  163. accountDO = accountDao.save(accountDO);
  164. }
  165. ActivityDO activityDO = activityDao.selectByTemplateIdAndType(planCode,"健康计划");
  166. if (activityDO==null){
  167. activityDO = new ActivityDO();
  168. activityDO.setStatus(1);
  169. activityDO.setSaasId("dev");
  170. activityDO.setCreateTime(new Date());
  171. activityDO.setUpdateTime(new Date());
  172. activityDO.setOrganizer("SYSTEM");
  173. activityDO.setTitle(templateName);
  174. activityDO.setType("健康计划");
  175. activityDO.setDescription(templateName);
  176. activityDO.setGlobelTemplate(planCode);
  177. activityDO.setIntroduction(templateName);
  178. activityDO.setDelFlag(1);
  179. activityDO = activityDao.save(activityDO);
  180. TaskDO taskDO = new TaskDO();
  181. taskDO.setSaasId("dev");
  182. taskDO.setCreateTime(new Date());
  183. taskDO.setUpdateTime(new Date());
  184. taskDO.setTitle(templateName);
  185. taskDO.setType("ACTIVITY_TASK");
  186. taskDO.setTaskCode("ACTIVITY_PLAN");
  187. taskDO.setTransactionId(activityDO.getId());
  188. taskDO.setContent(templateName);
  189. taskDO.setStatus(1);
  190. taskDO = taskDao.save(taskDO);
  191. TaskPatientDetailDO taskPatientDetailDO = taskPatientDetailDao.selectByTaskIdAndPatientId(taskDO.getId(),patient);
  192. if (taskPatientDetailDO==null){
  193. taskPatientDetailDO = new TaskPatientDetailDO();
  194. taskPatientDetailDO.setSaasId("dev");
  195. taskPatientDetailDO.setTaskId(taskDO.getId());
  196. taskPatientDetailDO.setActivityId(activityDO.getId());
  197. taskPatientDetailDO.setPatientIdcard(idcard);
  198. taskPatientDetailDO.setPatientId(patient);
  199. taskPatientDetailDO.setHospital(hospital);
  200. taskPatientDetailDO.setHospitalName(hospitalName);
  201. taskPatientDetailDO.setCreateTime(new Date());
  202. taskPatientDetailDO.setUpdateTime(new Date());
  203. taskPatientDetailDO.setTotal(0L);
  204. taskPatientDetailDO.setCouponTotal(0L);
  205. taskPatientDetailDO.setStatus(1);
  206. taskPatientDetailDao.save(taskPatientDetailDO);
  207. }
  208. }else {
  209. List<TaskDO> taskDOS = taskDao.selectByActivityId(activityDO.getId());
  210. if (taskDOS!=null&&taskDOS.size()!=0){
  211. TaskDO taskDO = taskDOS.get(0);
  212. TaskPatientDetailDO taskPatientDetailDO = taskPatientDetailDao.selectByTaskIdAndPatientId(taskDO.getId(),patient);
  213. if (taskPatientDetailDO==null){
  214. taskPatientDetailDO = new TaskPatientDetailDO();
  215. taskPatientDetailDO.setSaasId("dev");
  216. taskPatientDetailDO.setTaskId(taskDO.getId());
  217. taskPatientDetailDO.setActivityId(activityDO.getId());
  218. taskPatientDetailDO.setPatientIdcard(idcard);
  219. taskPatientDetailDO.setPatientId(patient);
  220. taskPatientDetailDO.setHospital(hospital);
  221. taskPatientDetailDO.setHospitalName(hospitalName);
  222. taskPatientDetailDO.setCreateTime(new Date());
  223. taskPatientDetailDO.setUpdateTime(new Date());
  224. taskPatientDetailDO.setTotal(0L);
  225. taskPatientDetailDO.setCouponTotal(0L);
  226. taskPatientDetailDO.setStatus(1);
  227. taskPatientDetailDao.save(taskPatientDetailDO);
  228. }
  229. }
  230. }
  231. envelop.setObj(activityDO);
  232. return envelop;
  233. }
  234. /**
  235. * find by condition
  236. *
  237. * @param activityDO 活动参数对象
  238. * @param page 页码
  239. * @param size 每页大小
  240. * @return
  241. * @throws ParseException
  242. */
  243. public MixEnvelop<ActivityDO, ActivityDO> findByCondition(ActivityDO activityDO, Integer page, Integer size) throws ParseException {
  244. String sql = new ISqlUtils().getSql(activityDO,page,size,"sort");
  245. List<ActivityDO> activityDOS = jdbcTemplate.query(sql,new BeanPropertyRowMapper(ActivityDO.class));
  246. for (ActivityDO activityDO1:activityDOS){
  247. String taskSql = "SELECT" +
  248. " COUNT(1) AS total1 " +
  249. "FROM " +
  250. " ( " +
  251. " SELECT DISTINCT " +
  252. " (btpd.patient_id) " +
  253. " FROM " +
  254. " wlyy_health_bank_task_patient_detail btpd " +
  255. " WHERE " +
  256. " activity_id = '" +activityDO1.getId()+
  257. "' ) btpd1";
  258. List<Map<String,Object>> rstotal = jdbcTemplate.queryForList(taskSql);
  259. Long count = 0L;
  260. if(rstotal!=null&&rstotal.size()>0){
  261. count = (Long) rstotal.get(0).get("total1");
  262. }
  263. activityDO1.setTotal(count);
  264. String taskSql1 = "select * from wlyy_health_bank_task_patient_detail btpd where activity_id = '"+activityDO1.getId()
  265. +"' and (patient_idcard = '"+activityDO.getPatientIdcard()+"' OR union_id = '"+activityDO.getUnionId()+"')";
  266. List<TaskPatientDetailDO> taskPatientDetailDOS = jdbcTemplate.query(taskSql1,new BeanPropertyRowMapper(TaskPatientDetailDO.class));
  267. activityDO1.setTaskPatientDetailDOS(taskPatientDetailDOS);
  268. String tasksql = "select * from wlyy_health_bank_task where transaction_id = '"+activityDO1.getId()+"'";
  269. List<TaskDO> taskDOList = jdbcTemplate.query(tasksql,new BeanPropertyRowMapper(TaskDO.class));
  270. List<TaskGoodsDO> taskGoodsDOS = taskGoodsDao.selectByActivityId(activityDO1.getId());
  271. for (TaskGoodsDO taskGoodsDO:taskGoodsDOS){
  272. ExchangeGoodsDO exchangeGoodsDO = exchangeGoodsDao.selectByActivityGoodsIdAndPatientId(taskGoodsDO.getId(),activityDO.getPatientId());
  273. taskGoodsDO.setExchangeGoodsDO(exchangeGoodsDO);
  274. }
  275. activityDO1.setTaskGoodsDOS(taskGoodsDOS);
  276. ActivityRuleDO activityRuleDO = activityRuleDao.selectByActivityId(activityDO1.getId());
  277. activityDO1.setActivityRuleDO(activityRuleDO);
  278. activityDO1.setTaskDOS(taskDOList);
  279. }
  280. String sqlcount = new ISqlUtils().getSql(activityDO,0,0,"count");
  281. List<Map<String,Object>> rstotal = jdbcTemplate.queryForList(sqlcount);
  282. Long count = 0L;
  283. if(rstotal!=null&&rstotal.size()>0){
  284. count = (Long) rstotal.get(0).get("total");
  285. }
  286. return MixEnvelop.getSuccessListWithPage(HealthBankMapping.api_success,activityDOS,page,size,count);
  287. }
  288. /**
  289. * update activityDO
  290. *
  291. * @param activityDO 活动参数对象
  292. * @return
  293. */
  294. @org.springframework.transaction.annotation.Transactional(rollbackFor = Exception.class)
  295. public MixEnvelop<Boolean, Boolean> update(ActivityDO activityDO, String value1, String value2, String value3,String registrationNumber) throws Exception {
  296. if (StringUtils.isBlank(activityDO.getId())) {
  297. throw new Exception("活动id不能为空!");
  298. }
  299. if (StringUtils.isNotBlank(value2)){
  300. JSONObject object = JSONObject.parseObject(value2);
  301. String activityOfflineTime = object.getString("activityOfflineTime");
  302. String[] str = activityOfflineTime.split(",");
  303. if (str.length == 2){
  304. activityDO.setActivityOfflineTime(DateUtil.strToDate(str[1]));
  305. }else {
  306. activityDO.setActivityOfflineTime(DateUtil.strToDate(str[0]));
  307. }
  308. String registrationTime = object.getString("registrationTime");
  309. String[] str1 = registrationTime.split(",");
  310. if(str1.length==2){
  311. activityDO.setRegistrationTime(DateUtil.strToDate(str1[1]));
  312. }else {
  313. activityDO.setRegistrationTime(DateUtil.strToDate(str1[0]));
  314. }
  315. String deadLine = object.getString("deadLine");
  316. String[] str2 = deadLine.split(",");
  317. if (str2.length==2){
  318. activityDO.setDeadLine(DateUtil.strToDate(str2[1]));
  319. }else {
  320. activityDO.setDeadLine(DateUtil.strToDate(str2[0]));
  321. }
  322. }
  323. activityDO.setRegistrationNumber(registrationNumber);
  324. activityDO.setUpdateTime(new Date());
  325. activityDO.setDelFlag(1);
  326. //删除活动任务与奖品
  327. taskGoodsDao.deleteByActivityId(activityDO.getId());
  328. if (value3 !=null && value3 !=""){
  329. JSONObject object = JSONObject.parseObject(value3);
  330. String exchangeType = object.getString("exchangeType");
  331. if (exchangeType.equals("1")){
  332. String array = (null!=object.get("goodDetail")?String.valueOf(object.get("goodDetail")):"");
  333. if (StringUtils.isNotBlank(array)){
  334. JSONArray array1= JSON.parseArray(array);
  335. for (int i =0;i<array1.size();i++){
  336. JSONObject jsonObject = array1.getJSONObject(i);
  337. TaskGoodsDO taskGoodsDO = new TaskGoodsDO();
  338. taskGoodsDO.setActivityId(activityDO.getId());
  339. taskGoodsDO.setCoupon(jsonObject.getInteger("couponNum"));
  340. taskGoodsDO.setImg(jsonObject.getString("picUrl"));
  341. taskGoodsDO.setName(jsonObject.getString("name"));
  342. taskGoodsDO.setCreateTime(new Date());
  343. taskGoodsDO.setUpdateTime(new Date());
  344. taskGoodsDO.setSaasId("dev");
  345. taskGoodsDO.setStatus(1);
  346. taskGoodsDao.save(taskGoodsDO);
  347. }
  348. }
  349. }
  350. }
  351. ActivityRuleDO activityRuleDO = activityRuleDao.selectByActivityId(activityDO.getId());
  352. activityRuleDO.setUpdateTime(new Date());
  353. activityRuleDO.setSaasId("dev");
  354. activityRuleDO.setValue1(value1);
  355. activityRuleDO.setValue2(value2);
  356. activityRuleDO.setValue3(value3);
  357. activityRuleDO.setDelFlag(1);
  358. activityRuleDao.save(activityRuleDO);
  359. System.out.println("----------------------------update--"+activityDO.getId()+activityDO.getAreaName());
  360. // activityDO.setCreateTime(new Date());
  361. activityDao.save(activityDO);
  362. MixEnvelop<Boolean, Boolean> envelop = new MixEnvelop<>();
  363. envelop.setObj(true);
  364. return envelop;
  365. }
  366. /**
  367. * 获取参与的活动
  368. *
  369. * @param activityDO 活动对象
  370. * @param page 页码
  371. * @param size 分页大小
  372. * @return
  373. */
  374. public MixEnvelop<ActivityDO, ActivityDO> selectByPatient(ActivityDO activityDO, Integer page, Integer size){
  375. String condition = "";
  376. if (activityDO.getUnionId()!=null&&activityDO.getUnionId() !=""){
  377. condition="AND union_id = '"+ activityDO.getUnionId()+"'";
  378. }
  379. String sql ="SELECT * " +
  380. " FROM wlyy_health_bank_activity " +
  381. "WHERE " +
  382. "id IN ( " +
  383. "SELECT bt.transaction_id " +
  384. "FROM wlyy_health_bank_task bt " +
  385. "WHERE id IN (" +
  386. " SELECT task_id FROM " +
  387. "wlyy_health_bank_task_patient_detail" +
  388. " WHERE " +
  389. " patient_idcard = '"+activityDO.getPatientIdcard()+"' "+ condition+")" +
  390. " ) AND del_flag =1 " +
  391. " LIMIT "+(page-1)*size +","+size;
  392. List<ActivityDO> activityDOS = jdbcTemplate.query(sql,new BeanPropertyRowMapper(ActivityDO.class));
  393. for (ActivityDO activityDO1:activityDOS){
  394. String activitySql ="SELECT btpd1.sum AS total FROM (SELECT " +
  395. " SUM(total) AS sum , " +
  396. " patient_id, " +
  397. " patient_openid, " +
  398. " patient_idcard, " +
  399. " activity_id," +
  400. " union_id " +
  401. " FROM " +
  402. " wlyy_health_bank_task_patient_detail " +
  403. "GROUP BY patient_openid,patient_idcard,union_id)btpd1 " +
  404. "WHERE " +
  405. " btpd1.activity_id = '"+activityDO1.getId() +"' AND patient_openid = '"+activityDO.getOpenId()+ "' AND patient_idcard = '"+activityDO.getPatientIdcard()+"' "+condition;
  406. List<Map<String,Object>> rstotal = jdbcTemplate.queryForList(activitySql);
  407. Long count = 0L;
  408. if(rstotal!=null&&rstotal.size()>0){
  409. Object object = rstotal.get(0).get("total");
  410. count = Long.parseLong(object.toString());
  411. }
  412. activityDO1.setSum(count);
  413. String rankingSql = "SELECT count(1)+1 AS total FROM (SELECT " +
  414. " SUM(total) AS sum , " +
  415. " patient_id, " +
  416. " patient_openid, " +
  417. " patient_idcard, " +
  418. " activity_id " +
  419. "FROM " +
  420. " wlyy_health_bank_task_patient_detail " +
  421. "GROUP BY patient_openid,patient_idcard,union_id)btpd1 " +
  422. "WHERE " +
  423. " btpd1.activity_id = '"+activityDO1.getId()+"' AND btpd1.sum >" +activityDO1.getSum() ;
  424. List<Map<String,Object>> rstotal1 = jdbcTemplate.queryForList(rankingSql);
  425. Integer count1 = 0;
  426. if(rstotal1!=null&&rstotal1.size()>0){
  427. Object object = rstotal1.get(0).get("total");
  428. count1 = Integer.parseInt(object.toString());
  429. }
  430. activityDO1.setActivityRanking(count1);
  431. String taskSql = "SELECT" +
  432. " COUNT(1) AS total1 " +
  433. "FROM " +
  434. " ( " +
  435. " SELECT * " +
  436. " FROM " +
  437. " wlyy_health_bank_task_patient_detail btpd " +
  438. " WHERE " +
  439. " activity_id = '" +activityDO1.getId()+
  440. "' GROUP BY patient_openid,patient_idcard,union_id) btpd1";
  441. List<Map<String,Object>> rstotal2 = jdbcTemplate.queryForList(taskSql);
  442. Long count2 = 0L;
  443. if(rstotal2!=null&&rstotal2.size()>0){
  444. count2 = (Long) rstotal2.get(0).get("total1");
  445. }
  446. activityDO1.setTotal(count2);
  447. ActivityRuleDO activityRuleDO = activityRuleDao.selectByActivityId(activityDO1.getId());
  448. activityDO1.setActivityRuleDO(activityRuleDO);
  449. }
  450. String sqlcount = "SELECT count(1) AS total" +
  451. " FROM wlyy_health_bank_activity " +
  452. "WHERE " +
  453. "id IN ( " +
  454. "SELECT bt.transaction_id " +
  455. "FROM wlyy_health_bank_task bt " +
  456. "WHERE id IN (" +
  457. " SELECT task_id FROM " +
  458. "wlyy_health_bank_task_patient_detail" +
  459. " WHERE " +
  460. " patient_openid = '"+activityDO.getOpenId()+ "' AND patient_idcard = '"+activityDO.getPatientIdcard()+"'"+condition+ ")" +
  461. " )";
  462. List<Map<String,Object>> rstotal = jdbcTemplate.queryForList(sqlcount);
  463. Long count = 0L;
  464. if(rstotal!=null&&rstotal.size()>0){
  465. count = (Long) rstotal.get(0).get("total");
  466. }
  467. return MixEnvelop.getSuccessListWithPage(HealthBankMapping.api_success,activityDOS,page,size,count);
  468. }
  469. /**
  470. * 批量删除活动
  471. *
  472. * @param ids 活动id集合
  473. * @return
  474. */
  475. public MixEnvelop<Boolean, Boolean> batchDelete(List<String> ids){
  476. MixEnvelop<Boolean, Boolean> envelop = new MixEnvelop<>();
  477. for (int i =0;i<ids.size();i++){
  478. List<TaskDO> taskDOList = taskDao.selectByActivityId(ids.get(i));
  479. for (TaskDO taskDO:taskDOList){
  480. taskDO.setStatus(0);
  481. taskDO.setCreateTime(new Date());
  482. taskDO.setUpdateTime(new Date());
  483. taskDao.save(taskDO);
  484. }
  485. List<TaskPatientDetailDO> taskPatientDetailDOS = taskPatientDetailDao.selectByActivityId(ids.get(i));
  486. for(TaskPatientDetailDO taskPatientDetailDO:taskPatientDetailDOS){
  487. taskPatientDetailDO.setStatus(-1);
  488. taskPatientDetailDO.setCreateTime(new Date());
  489. taskPatientDetailDO.setUpdateTime(new Date());
  490. taskPatientDetailDao.save(taskPatientDetailDO);
  491. }
  492. ActivityRuleDO ruleDO = activityRuleDao.selectByActivityId(ids.get(i));
  493. if(null!=ruleDO){
  494. ruleDO.setDelFlag(0);
  495. activityRuleDao.save(ruleDO);
  496. }
  497. ActivityDO activityDO = activityDao.findOne(ids.get(i));
  498. activityDO.setStatus(-1);
  499. activityDO.setCreateTime(new Date());
  500. activityDO.setUpdateTime(new Date());
  501. activityDO.setDelFlag(0);
  502. activityDao.save(activityDO);
  503. }
  504. return envelop;
  505. }
  506. /**
  507. * 删除活动
  508. *
  509. * @param id 活动id
  510. * @return
  511. */
  512. public MixEnvelop<Boolean, Boolean> daleteActivity(String id){
  513. MixEnvelop<Boolean, Boolean> envelop = new MixEnvelop<>();
  514. List<TaskDO> taskDOList = taskDao.selectByActivityId(id);
  515. for (TaskDO taskDO:taskDOList){
  516. taskDO.setStatus(0);
  517. taskDO.setCreateTime(new Date());
  518. taskDO.setUpdateTime(new Date());
  519. taskDao.save(taskDO);
  520. }
  521. List<TaskPatientDetailDO> taskPatientDetailDOS = taskPatientDetailDao.selectByActivityId(id);
  522. for(TaskPatientDetailDO taskPatientDetailDO:taskPatientDetailDOS){
  523. taskPatientDetailDO.setStatus(-1);
  524. taskPatientDetailDO.setCreateTime(new Date());
  525. taskPatientDetailDO.setUpdateTime(new Date());
  526. taskPatientDetailDao.save(taskPatientDetailDO);
  527. }
  528. ActivityRuleDO ruleDO = activityRuleDao.selectByActivityId(id);
  529. if(null!=ruleDO){
  530. ruleDO.setDelFlag(0);
  531. activityRuleDao.save(ruleDO);
  532. }
  533. ActivityDO activityDO = activityDao.findOne(id);
  534. activityDO.setStatus(-1);
  535. activityDO.setCreateTime(new Date());
  536. activityDO.setUpdateTime(new Date());
  537. activityDO.setDelFlag(0);
  538. activityDao.save(activityDO);
  539. return envelop;
  540. }
  541. /**
  542. * 编辑活动:上、下线,推荐
  543. * @param id
  544. * @param type 操作类型:上线:0,下线:2,推荐:3,取消推荐:4
  545. * @return
  546. * @throws Exception
  547. */
  548. public ActivityDO editActivity(String id, String type) throws Exception {
  549. ActivityDO activityDO = activityDao.findOne(id);
  550. ActivityRuleDO activityRuleDO= activityRuleDao.selectByActivityId(id);
  551. if("3".equals(type)){
  552. //推荐
  553. List<ActivityDO> activityDOS = activityDao.findByIdAndRecommended(1);
  554. if (activityDOS!=null&& activityDOS.size()>=3){
  555. throw new Exception("只允许推荐三个!");
  556. }else {
  557. activityDO.setRecommended(1);
  558. }
  559. }else if("4".equals(type)){
  560. //取消推荐
  561. activityDO.setRecommended(0);
  562. }
  563. else if("2".equals(type)){
  564. //下线
  565. activityDO.setStatus(2);
  566. }else if("0".equals(type)){
  567. //判断该活动是不是竞走,只能存在一条有效竞走
  568. /*if("竞走".equals(activityDO.getType())){
  569. List<ActivityDO> lst= activityDao.findByIdAndType(activityDO.getType());
  570. if(null!=lst&&lst.size()>0){
  571. throw new Exception("已存在一条上线的竞走活动!");
  572. }
  573. }*/
  574. //上线
  575. if(null!=activityRuleDO){
  576. String ruleJson=activityRuleDO.getValue2();
  577. JSONObject obj=JSONObject.parseObject(ruleJson);
  578. // SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
  579. SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm");
  580. //报名截止时间
  581. String registrationTime1 = obj.getString("registrationTime");
  582. String[] str1 = registrationTime1.split(",");
  583. String s = null;
  584. if (str1.length==2) {
  585. s=str1[1];
  586. }else {
  587. s=str1[0];
  588. }
  589. Date registrationTime=(""!=s&&null!=s? sdf2.parse(s):null);
  590. //参与截止时间
  591. String deadLine2 = obj.getString("deadLine");
  592. String[] str2 = deadLine2.split(",");
  593. String s1= null;
  594. if (str2.length==2){
  595. s1=str2[1];
  596. }else {
  597. s1=str2[0];
  598. }
  599. Date deadLine=(null!=s1&&""!=s1? sdf2.parse(s1):null);
  600. //活动下线时间
  601. String activityOfflineTime1 = obj.getString("activityOfflineTime");
  602. String[] str = activityOfflineTime1.split(",");
  603. String s2= null;
  604. if (str.length==2){
  605. s2=str[1];
  606. }else {
  607. s2=str[0];
  608. }
  609. Date activityOfflineTime=(""!=s2&&null!=s2 ? sdf2.parse(s2):null);
  610. //获取当前时间
  611. Date now = DateUtil.strToDate(sdf2.format(new Date()),"yyyy-MM-dd HH:mm");
  612. if(null!=activityOfflineTime&&activityOfflineTime.compareTo(now)<=0){
  613. //活动下线
  614. activityDO.setStatus(2);
  615. }else if((null!=activityOfflineTime&&activityOfflineTime.compareTo(now)>0) && (null!=deadLine&&deadLine.compareTo(now)<=0)){
  616. //活动结束
  617. activityDO.setStatus(-1);
  618. }else if((null!=deadLine&&deadLine.compareTo(now)>0)&&(null!=registrationTime&&registrationTime.compareTo(now)<=0)){
  619. //活动未结束
  620. activityDO.setStatus(0);
  621. }else if (null!=registrationTime&&registrationTime.compareTo(now)>1){
  622. //报名未截止
  623. activityDO.setStatus(1);
  624. }else{
  625. //没有时间限制,上线
  626. activityDO.setStatus(0);
  627. }
  628. }else {
  629. throw new Exception("活动规则缺失!");
  630. }
  631. }
  632. // activityDO.setReleaseTime(new Date());
  633. activityDO= activityDao.save(activityDO);
  634. return activityDO;
  635. }
  636. /**
  637. * 根据活动id获取活动详情+规则
  638. * @param id
  639. * @return
  640. * @throws Exception
  641. */
  642. public JSONObject findActivityById(String id)throws Exception{
  643. JSONObject obj=new JSONObject();
  644. ActivityDO activityDO = activityDao.findOne(id);
  645. obj.put("jsonData",activityDO);
  646. ActivityRuleDO activityRuleDO=new ActivityRuleDO();
  647. if(null!=activityDO){
  648. activityRuleDO= activityRuleDao.selectByActivityId(id);
  649. activityDO.setActivityRuleDO(activityRuleDO);
  650. }
  651. obj.put("value1", null == activityRuleDO ? null : activityRuleDO.getValue1());
  652. obj.put("value2", null == activityRuleDO ? null : activityRuleDO.getValue2());
  653. obj.put("value3", null == activityRuleDO ? null : activityRuleDO.getValue3());
  654. return obj;
  655. }
  656. /**
  657. * 活动排名
  658. *
  659. * @param activityId
  660. * @param patient
  661. * @param page
  662. * @param size
  663. * @return
  664. */
  665. public MixEnvelop<JSONObject,JSONObject> selectActivityRanking(String activityId,String patient,Integer page,Integer size,String street,String town,String hospital){
  666. MixEnvelop<JSONObject,JSONObject> envelop = new MixEnvelop<>();
  667. ActivityDO activityDO = activityDao.selectById(activityId);
  668. JSONObject jsonObject = new JSONObject();
  669. TaskPatientDetailDO taskPatientDetailDO1 = taskPatientDetailDao.selectByActivityIdAndPatientId(activityId,patient);
  670. //积分排行
  671. String sql ="SELECT btpd.patient_id AS patientId,btpd.total,btpd.create_time AS updateTime FROM ( SELECT sum(btpd.total) AS total, btpd.patient_id,btpd.create_time " +
  672. "FROM wlyy_health_bank_task_patient_detail btpd " +
  673. "WHERE btpd.activity_id = '"+activityId +
  674. "' GROUP BY btpd.patient_id " +
  675. " ) btpd " +
  676. "ORDER BY " +
  677. " btpd.total DESC,btpd.create_time ASC LIMIT " + (page-1)*size+","+size;
  678. List<TaskPatientDetailDO> patientDetailDOS = jdbcTemplate.query(sql,new BeanPropertyRowMapper<>(TaskPatientDetailDO.class));
  679. long count=1L;
  680. int j =0;
  681. for (TaskPatientDetailDO taskPatientDetailDO:patientDetailDOS){
  682. String timeSeparated = DateUtils.getDatePoor(new Date(),taskPatientDetailDO.getUpdateTime());
  683. String goodsSql = "select * from wlyy_health_bank_exchange_goods where patient_id ='"+taskPatientDetailDO.getPatientId()+"' " +
  684. "and activity_goods_id in(select id from wlyy_health_bank_activity_goods where activity_id ='"+activityId+"')";
  685. List<ExchangeGoodsDO> exchangeGoodsDOS = jdbcTemplate.query(goodsSql,new BeanPropertyRowMapper<>(ExchangeGoodsDO.class));
  686. for (ExchangeGoodsDO exchangeGoodsDO :exchangeGoodsDOS){
  687. TaskGoodsDO taskGoodsDO = taskGoodsDao.selectById(exchangeGoodsDO.getActivityGoodsId());
  688. exchangeGoodsDO.setTaskGoodsDO(taskGoodsDO);
  689. }
  690. taskPatientDetailDO.setExchangeGoodsDO(exchangeGoodsDOS);
  691. taskPatientDetailDO.setTimeSeparated(timeSeparated);
  692. j++;
  693. if (taskPatientDetailDO.getPatientId().equals(patient)){
  694. count = j;
  695. }
  696. }
  697. //活动劵排行
  698. String sqlCoupon ="SELECT btpd.patient_id AS patientId,btpd.couponTotal,btpd.create_time AS updateTime FROM ( SELECT sum(btpd.coupon_total) AS couponTotal, btpd.patient_id,btpd.create_time " +
  699. "FROM wlyy_health_bank_task_patient_detail btpd " +
  700. "WHERE btpd.activity_id = '"+activityId +
  701. "' GROUP BY btpd.patient_id " +
  702. " ) btpd " +
  703. "ORDER BY " +
  704. " btpd.couponTotal DESC,btpd.create_time ASC LIMIT " + (page-1)*size+","+size;
  705. List<TaskPatientDetailDO> patientDetailDOS1 = jdbcTemplate.query(sqlCoupon,new BeanPropertyRowMapper<>(TaskPatientDetailDO.class));
  706. long count2 = 1l;
  707. int i=0;
  708. for (TaskPatientDetailDO taskPatientDetailDO:patientDetailDOS1){
  709. String timeSeparated = DateUtils.getDatePoor(new Date(),taskPatientDetailDO.getUpdateTime());
  710. String goodsSql = "select * from wlyy_health_bank_exchange_goods where patient_id ='"+taskPatientDetailDO.getPatientId()+"' " +
  711. "and activity_goods_id in(select id from wlyy_health_bank_activity_goods where activity_id ='"+activityId+"')";
  712. List<ExchangeGoodsDO> exchangeGoodsDOS = jdbcTemplate.query(goodsSql,new BeanPropertyRowMapper<>(ExchangeGoodsDO.class));
  713. for (ExchangeGoodsDO exchangeGoodsDO :exchangeGoodsDOS){
  714. TaskGoodsDO taskGoodsDO = taskGoodsDao.selectById(exchangeGoodsDO.getActivityGoodsId());
  715. exchangeGoodsDO.setTaskGoodsDO(taskGoodsDO);
  716. }
  717. i++;
  718. if (taskPatientDetailDO.getPatientId().equals(patient)){
  719. count2=i;
  720. }
  721. taskPatientDetailDO.setExchangeGoodsDO(exchangeGoodsDOS);
  722. taskPatientDetailDO.setTimeSeparated(timeSeparated);
  723. }
  724. //活动总积分排名
  725. /*String rankingSql1 = "SELECT count(1)+1 AS total FROM (SELECT " +
  726. " SUM(total) AS sum , " +
  727. " patient_id, " +
  728. " activity_id, " +
  729. " create_time "+
  730. " FROM " +
  731. " wlyy_health_bank_task_patient_detail htpd " +
  732. " where htpd.activity_id ='"+activityId+"' AND htpd.patient_id !='"+patient+
  733. "' GROUP BY patient_id )btpd1 " +
  734. "WHERE " +
  735. " btpd1.sum >= (SELECT SUM(s.total) as total " +
  736. "FROM wlyy_health_bank_task_patient_detail s WHERE s.patient_id ='"+patient+"' AND s.activity_id='"+activityId+"') " +
  737. " AND btpd1.create_time >=(SELECT s.create_time " +
  738. "FROM wlyy_health_bank_task_patient_detail s WHERE s.patient_id ='"+patient+"' AND s.activity_id='"+activityId+"')" ;
  739. List<Map<String,Object>> rstotal1 = jdbcTemplate.queryForList(rankingSql1);
  740. Long count = 0L;
  741. if(rstotal1!=null&&rstotal1.size()>0){
  742. count = (Long) rstotal1.get(0).get("total");
  743. }*/
  744. Long countTotal = 0L;
  745. if ((count-2)>=0){
  746. countTotal = (count-2);
  747. }
  748. //排名前一个积分
  749. String rankingSql9 = "SELECT btpd1.sum as total FROM (SELECT " +
  750. " SUM(total) AS sum , " +
  751. " patient_id, " +
  752. " activity_id " +
  753. " FROM " +
  754. " wlyy_health_bank_task_patient_detail btpd where btpd.activity_id ='"+activityId+"' AND btpd.patient_id !='"+patient+
  755. "' GROUP BY patient_id )btpd1 " +
  756. "WHERE " +
  757. " btpd1.activity_id = '"+activityDO.getId()+"' AND btpd1.sum >= (SELECT SUM(s.total) as total " +
  758. "FROM wlyy_health_bank_task_patient_detail s WHERE s.patient_id ='"+patient+"' AND s.activity_id='"+activityId+"')" +
  759. " ORDER BY btpd1.sum desc "+
  760. " limit "+countTotal+",1" ;
  761. List<Map<String,Object>> rstotal9 = jdbcTemplate.queryForList(rankingSql9);
  762. Integer total = 0;
  763. if(rstotal9!=null&&rstotal9.size()>0){
  764. total = Integer.parseInt(rstotal9.get(0).get("total").toString()) ;
  765. }
  766. String totalSql = " SELECT SUM(s.total) as total " +
  767. " FROM wlyy_health_bank_task_patient_detail s WHERE s.patient_id ='"+patient+"' AND s.activity_id='"+activityId+"'";
  768. List<Map<String,Object>> rstotal10 = jdbcTemplate.queryForList(totalSql);
  769. Integer total1 = 0;
  770. if(rstotal10!=null&&rstotal10.size()>0){
  771. total1 = Integer.parseInt( rstotal10.get(0).get("total").toString());
  772. }
  773. //总活动劵的排名
  774. /*String rankingSql2 = "SELECT count(1)+1 AS total FROM (SELECT " +
  775. " SUM(coupon_total) AS sum , " +
  776. " patient_id, " +
  777. " activity_id," +
  778. " create_time " +
  779. "FROM " +
  780. " wlyy_health_bank_task_patient_detail htpd WHERE htpd.activity_id ='"+activityId+"' AND htpd.patient_id !='"+patient+
  781. "' GROUP BY patient_id )btpd1 " +
  782. "WHERE " +
  783. " btpd1.sum >= (SELECT SUM(s.coupon_total) as couponTotal " +
  784. "FROM wlyy_health_bank_task_patient_detail s WHERE s.patient_id ='"+patient+"' AND s.activity_id='"+activityId+"')"+
  785. " AND btpd1.create_time >=(SELECT s.create_time " +
  786. "FROM wlyy_health_bank_task_patient_detail s WHERE s.patient_id ='"+patient+"' AND s.activity_id='"+activityId+"')";
  787. List<Map<String,Object>> rstotal2 = jdbcTemplate.queryForList(rankingSql2);
  788. Long count2 = 0L;
  789. if(rstotal2!=null&&rstotal2.size()>0){
  790. count2 = (Long) rstotal2.get(0).get("total");
  791. }*/
  792. Long countTotal1 = 0L;
  793. if ((count2-2)>=0){
  794. countTotal1=(count2-2);
  795. }
  796. //活动劵前一个活动劵
  797. String rankingSql10 = "SELECT btpd1.sum as total FROM (SELECT " +
  798. " SUM(coupon_total) AS sum , " +
  799. " patient_id, " +
  800. " activity_id "+
  801. "FROM " +
  802. " wlyy_health_bank_task_patient_detail btpd WHERE btpd.activity_id ='"+activityId+"' AND btpd.patient_id !='"+patient+
  803. "' GROUP BY patient_id )btpd1 " +
  804. "WHERE " +
  805. " btpd1.sum >= (SELECT SUM(s.total) as total " +
  806. "FROM wlyy_health_bank_task_patient_detail s WHERE s.patient_id ='"+patient+"' AND s.activity_id='"+activityId+"')" +
  807. " ORDER BY btpd1.sum desc "+
  808. " limit "+countTotal1+",1" ;
  809. List<Map<String,Object>> totalSql10 = jdbcTemplate.queryForList(rankingSql10);
  810. Integer total2 = 0;
  811. if(totalSql10!=null&&totalSql10.size()>0){
  812. total2 = Integer.parseInt(totalSql10.get(0).get("total").toString());
  813. }
  814. String totalSql1 = " SELECT SUM(s.coupon_total) as total " +
  815. " FROM wlyy_health_bank_task_patient_detail s WHERE s.patient_id ='"+patient+"' AND s.activity_id='"+activityId+"'";
  816. List<Map<String,Object>> totalSql11 = jdbcTemplate.queryForList(totalSql1);
  817. Integer total3 = 0;
  818. if(totalSql11!=null&&totalSql11.size()>0){
  819. total3 = Integer.parseInt(totalSql11.get(0).get("total").toString());
  820. }
  821. //活动劵
  822. //街道
  823. String rankingSql3 ="SELECT btpd1.patientId, btpd1.total, btpd1.create_time AS updateTime FROM (SELECT btpd.patient_id AS patientId,btpd.total,btpd.create_time AS create_time FROM ( SELECT sum(btpd.total) AS total, btpd.patient_id,btpd.create_time " +
  824. "FROM wlyy_health_bank_task_patient_detail btpd " +
  825. "WHERE btpd.activity_id = '"+activityId +
  826. "' GROUP BY btpd.patient_id " +
  827. " ) btpd "+
  828. "ORDER BY " +
  829. " btpd.total DESC)btpd1 WHERE btpd1.patientId IN(SELECT p.code FROM wlyy.wlyy_patient p WHERE p.street = '"+street+"')"+" ORDER BY btpd1.create_time ";
  830. List<TaskPatientDetailDO> patientDetailDOS2 = jdbcTemplate.query(rankingSql3,new BeanPropertyRowMapper<>(TaskPatientDetailDO.class));
  831. long count3=1L;
  832. for (TaskPatientDetailDO taskPatientDetailDO:patientDetailDOS2){
  833. String timeSeparated = DateUtils.getDatePoor(new Date(),taskPatientDetailDO.getUpdateTime());
  834. String goodsSql = "select * from wlyy_health_bank_exchange_goods where patient_id ='"+taskPatientDetailDO.getPatientId()+"' " +
  835. "and activity_goods_id in(select id from wlyy_health_bank_activity_goods where activity_id ='"+activityId+"')";
  836. List<ExchangeGoodsDO> exchangeGoodsDOS = jdbcTemplate.query(goodsSql,new BeanPropertyRowMapper<>(ExchangeGoodsDO.class));
  837. for (ExchangeGoodsDO exchangeGoodsDO :exchangeGoodsDOS){
  838. TaskGoodsDO taskGoodsDO = taskGoodsDao.selectById(exchangeGoodsDO.getActivityGoodsId());
  839. exchangeGoodsDO.setTaskGoodsDO(taskGoodsDO);
  840. }
  841. taskPatientDetailDO.setExchangeGoodsDO(exchangeGoodsDOS);
  842. taskPatientDetailDO.setTimeSeparated(timeSeparated);
  843. if (!taskPatientDetailDO.getPatientId().equals(patient)){
  844. count3+=1;
  845. }
  846. }
  847. //积分社区排行
  848. String sql1= "select s.rank,IFNULL(s.sum,0) as sum,s.patient_id from (SELECT @rownum :=@rownum + 1 AS rank, r.sum, r.patient_id FROM " +
  849. "(SELECT@rowNum := 0) b," +
  850. " (SELECT sum(bcd.integrate) AS sum, hbtd.patient_id, hbtd.create_time FROM " +
  851. "wlyy_health_bank_task_patient_detail hbtd " +
  852. " LEFT JOIN wlyy_health_bank_credits_detail bcd ON hbtd.patient_id=bcd.patient_id " +
  853. "AND bcd.transaction_id='"+taskPatientDetailDO1.getTaskId()+"'" +
  854. "WHERE hbtd.task_id = '"+taskPatientDetailDO1.getTaskId()+"' " +"and hbtd.hospital = '"+hospital+"'"+
  855. "GROUP BY hbtd.patient_id) r ORDER BY r.sum DESC,r.create_time DESC)s where s.patient_id ='"+patient+"'";
  856. List<Map<String,Object>> maps = jdbcTemplate.queryForList(sql1);
  857. long count10=0L;
  858. if (maps!= null && maps.size()!=0){
  859. count10 =new Double((Double) maps.get(0).get("rank")).longValue();
  860. }
  861. //区排名
  862. String rankingSql4 ="SELECT btpd1.patientId, btpd1.total, btpd1.create_time AS updateTime FROM (SELECT btpd.patient_id AS patientId,btpd.total,btpd.create_time AS create_time FROM ( SELECT sum(btpd.total) AS total, btpd.patient_id,btpd.create_time " +
  863. "FROM wlyy_health_bank_task_patient_detail btpd " +
  864. "WHERE btpd.activity_id = '"+activityId +
  865. "' GROUP BY btpd.patient_id " +
  866. " ) btpd "+
  867. "ORDER BY " +
  868. " btpd.total DESC)btpd1 WHERE btpd1.patientId IN(SELECT p.code FROM wlyy.wlyy_patient p WHERE p.town = '"+town+"')"+" ORDER BY btpd1.create_time ";
  869. List<TaskPatientDetailDO> patientDetailDOS3 = jdbcTemplate.query(rankingSql4,new BeanPropertyRowMapper<>(TaskPatientDetailDO.class));
  870. long count4=1L;
  871. for (TaskPatientDetailDO taskPatientDetailDO:patientDetailDOS3){
  872. String timeSeparated = DateUtils.getDatePoor(new Date(),taskPatientDetailDO.getUpdateTime());
  873. String goodsSql = "select * from wlyy_health_bank_exchange_goods where patient_id ='"+taskPatientDetailDO.getPatientId()+"' " +
  874. "and activity_goods_id in(select id from wlyy_health_bank_activity_goods where activity_id ='"+activityId+"')";
  875. List<ExchangeGoodsDO> exchangeGoodsDOS = jdbcTemplate.query(goodsSql,new BeanPropertyRowMapper<>(ExchangeGoodsDO.class));
  876. for (ExchangeGoodsDO exchangeGoodsDO :exchangeGoodsDOS){
  877. TaskGoodsDO taskGoodsDO = taskGoodsDao.selectById(exchangeGoodsDO.getActivityGoodsId());
  878. exchangeGoodsDO.setTaskGoodsDO(taskGoodsDO);
  879. }
  880. taskPatientDetailDO.setExchangeGoodsDO(exchangeGoodsDOS);
  881. taskPatientDetailDO.setTimeSeparated(timeSeparated);
  882. if (!taskPatientDetailDO.getPatientId().equals(patient)){
  883. count4+=1;
  884. }else {
  885. break;
  886. }
  887. }
  888. //活动劵排名
  889. //街道
  890. String rankingSql5 ="SELECT btpd1.patientId, btpd1.couponTotal, btpd1.create_time AS updateTime FROM (SELECT btpd.patient_id AS patientId,btpd.couponTotal,btpd.create_time AS create_time FROM ( SELECT sum(btpd.coupon_total) AS couponTotal, btpd.patient_id,btpd.create_time " +
  891. "FROM wlyy_health_bank_task_patient_detail btpd " +
  892. "WHERE btpd.activity_id = '"+activityId +
  893. "' GROUP BY btpd.patient_id " +
  894. " ) btpd " +
  895. "ORDER BY " +
  896. " btpd.couponTotal DESC )btpd1 WHERE btpd1.patientId IN(SELECT p.code FROM wlyy.wlyy_patient p WHERE p.street = '"+street+"') ORDER BY btpd1.create_time ";
  897. List<TaskPatientDetailDO> patientDetailDOS4 = jdbcTemplate.query(rankingSql5,new BeanPropertyRowMapper<>(TaskPatientDetailDO.class));
  898. long count5 = 1l;
  899. for (TaskPatientDetailDO taskPatientDetailDO:patientDetailDOS4){
  900. String timeSeparated = DateUtils.getDatePoor(new Date(),taskPatientDetailDO.getUpdateTime());
  901. String goodsSql = "select * from wlyy_health_bank_exchange_goods where patient_id ='"+taskPatientDetailDO.getPatientId()+"' " +
  902. "and activity_goods_id in(select id from wlyy_health_bank_activity_goods where activity_id ='"+activityId+"')";
  903. List<ExchangeGoodsDO> exchangeGoodsDOS = jdbcTemplate.query(goodsSql,new BeanPropertyRowMapper<>(ExchangeGoodsDO.class));
  904. for (ExchangeGoodsDO exchangeGoodsDO :exchangeGoodsDOS){
  905. TaskGoodsDO taskGoodsDO = taskGoodsDao.selectById(exchangeGoodsDO.getActivityGoodsId());
  906. exchangeGoodsDO.setTaskGoodsDO(taskGoodsDO);
  907. }
  908. if (taskPatientDetailDO.getPatientId().equals(taskPatientDetailDO.getPatientId())){
  909. count5+=1;
  910. }
  911. taskPatientDetailDO.setExchangeGoodsDO(exchangeGoodsDOS);
  912. taskPatientDetailDO.setTimeSeparated(timeSeparated);
  913. }
  914. //区排名
  915. String rankingSql6 ="SELECT btpd1.patientId, btpd1.couponTotal, btpd1.create_time AS updateTime FROM (SELECT btpd.patient_id AS patientId,btpd.couponTotal,btpd.create_time AS create_time FROM ( SELECT sum(btpd.coupon_total) AS couponTotal, btpd.patient_id,btpd.create_time " +
  916. "FROM wlyy_health_bank_task_patient_detail btpd " +
  917. "WHERE btpd.activity_id = '"+activityId +
  918. "' GROUP BY btpd.patient_id " +
  919. " ) btpd " +
  920. "ORDER BY " +
  921. " btpd.couponTotal DESC )btpd1 WHERE btpd1.patientId IN(SELECT p.code FROM wlyy.wlyy_patient p WHERE p.town = '"+town+"') ORDER BY btpd1.create_time ";
  922. List<TaskPatientDetailDO> patientDetailDOS5 = jdbcTemplate.query(rankingSql6,new BeanPropertyRowMapper<>(TaskPatientDetailDO.class));
  923. long count6 = 1l;
  924. for (TaskPatientDetailDO taskPatientDetailDO:patientDetailDOS5){
  925. String timeSeparated = DateUtils.getDatePoor(new Date(),taskPatientDetailDO.getUpdateTime());
  926. String goodsSql = "select * from wlyy_health_bank_exchange_goods where patient_id ='"+taskPatientDetailDO.getPatientId()+"' " +
  927. "and activity_goods_id in(select id from wlyy_health_bank_activity_goods where activity_id ='"+activityId+"')";
  928. List<ExchangeGoodsDO> exchangeGoodsDOS = jdbcTemplate.query(goodsSql,new BeanPropertyRowMapper<>(ExchangeGoodsDO.class));
  929. for (ExchangeGoodsDO exchangeGoodsDO :exchangeGoodsDOS){
  930. TaskGoodsDO taskGoodsDO = taskGoodsDao.selectById(exchangeGoodsDO.getActivityGoodsId());
  931. exchangeGoodsDO.setTaskGoodsDO(taskGoodsDO);
  932. }
  933. taskPatientDetailDO.setExchangeGoodsDO(exchangeGoodsDOS);
  934. taskPatientDetailDO.setTimeSeparated(timeSeparated);
  935. if (!taskPatientDetailDO.getPatientId().equals(taskPatientDetailDO.getPatientId())){
  936. count6+=1;
  937. }else {
  938. break;
  939. }
  940. }
  941. //社区排行
  942. String sql8= "select s.rank,IFNULL(s.sum,0) as sum,s.patient_id from (SELECT @rownum :=@rownum + 1 AS rank, r.sum, r.patient_id FROM " +
  943. "(SELECT@rowNum := 0) b," +
  944. " (SELECT sum(bcd.coupon) AS sum, hbtd.patient_id, hbtd.create_time FROM " +
  945. "wlyy_health_bank_task_patient_detail hbtd " +
  946. " LEFT JOIN wlyy_health_bank_credits_detail bcd ON hbtd.patient_id=bcd.patient_id " +
  947. "AND bcd.transaction_id='"+taskPatientDetailDO1.getTaskId()+"'" +
  948. "WHERE hbtd.task_id = '"+taskPatientDetailDO1.getTaskId()+"' " +"and hbtd.hospital = '"+hospital+"'"+
  949. "GROUP BY hbtd.patient_id) r ORDER BY r.sum DESC,r.create_time DESC)s where s.patient_id ='"+patient+"'";
  950. List<Map<String,Object>> maps1 = jdbcTemplate.queryForList(sql8);
  951. long count11=0L;
  952. if (maps1!= null && maps1.size()!=0){
  953. count11 = new Double((Double) maps1.get(0).get("rank")).longValue();
  954. }
  955. //街道排名
  956. /* String rankingSql3 = "SELECT count(1)+1 AS total FROM (SELECT " +
  957. " SUM(btpd.coupon_total) AS sum , " +
  958. " btpd.patient_id, " +
  959. " btpd.activity_id," +
  960. " create_time " +
  961. "FROM " +
  962. " wlyy_health_bank_task_patient_detail btpd WHERE btpd.activity_id ='"+activityId+"' AND btpd.patient_id !='"+patient+
  963. "' GROUP BY patient_id )btpd1 " +
  964. "WHERE btpd1.patient_id IN(SELECT p.code FROM wlyy.wlyy_patient p WHERE p.street = '"+street+"') AND btpd1.sum >= (SELECT SUM(s.coupon_total) as couponTotal " +
  965. "FROM wlyy_health_bank_task_patient_detail s WHERE s.patient_id ='"+patient+"' AND s.activity_id='"+activityId+"')"+
  966. " AND btpd1.create_time >=(SELECT s.create_time " +
  967. "FROM wlyy_health_bank_task_patient_detail s WHERE s.patient_id ='"+patient+"' AND s.activity_id='"+activityId+"')" ;
  968. List<Map<String,Object>> rstotal3 = jdbcTemplate.queryForList(rankingSql3);
  969. Long count3 = 0L;
  970. if(rstotal3!=null&&rstotal3.size()>0){
  971. count3 = (Long) rstotal3.get(0).get("total");
  972. }*/
  973. //区排名
  974. /* String rankingSql4 = "SELECT count(1)+1 AS total FROM (SELECT " +
  975. " SUM(btpd.coupon_total) AS sum , " +
  976. " btpd.patient_id, " +
  977. " btpd.activity_id," +
  978. " create_time " +
  979. "FROM " +
  980. " wlyy_health_bank_task_patient_detail btpd WHERE btpd.activity_id ='"+activityId+"' AND btpd.patient_id !='"+patient+
  981. "' GROUP BY patient_id )btpd1 " +
  982. "WHERE btpd1.patient_id IN(SELECT p.code FROM wlyy.wlyy_patient p WHERE p.town = '"+town+"') AND btpd1.sum >= (SELECT SUM(s.coupon_total) as couponTotal " +
  983. "FROM wlyy_health_bank_task_patient_detail s WHERE s.patient_id ='"+patient+"' AND s.activity_id='"+activityId+"')"+
  984. " AND btpd1.create_time >=(SELECT s.create_time " +
  985. "FROM wlyy_health_bank_task_patient_detail s WHERE s.patient_id ='"+patient+"' AND s.activity_id='"+activityId+"')" ;
  986. List<Map<String,Object>> rstotal4 = jdbcTemplate.queryForList(rankingSql4);
  987. Long count4 = 0L;
  988. if(rstotal4!=null&&rstotal4.size()>0){
  989. count4= (Long) rstotal4.get(0).get("total");
  990. }*/
  991. //积分
  992. //街道排名
  993. /*String rankingSql5 = "SELECT count(1)+1 AS total FROM (SELECT " +
  994. " SUM(btpd.total) AS sum , " +
  995. " btpd.patient_id, " +
  996. " btpd.activity_id," +
  997. " create_time " +
  998. "FROM " +
  999. " wlyy_health_bank_task_patient_detail btpd WHERE btpd.activity_id ='"+activityId+"' AND btpd.patient_id !='"+patient+
  1000. "' GROUP BY patient_id )btpd1 " +
  1001. "WHERE btpd1.patient_id IN(SELECT p.code FROM wlyy.wlyy_patient p WHERE p.street = '"+street+"') AND btpd1.sum >= (SELECT SUM(s.coupon_total) as couponTotal " +
  1002. "FROM wlyy_health_bank_task_patient_detail s WHERE s.patient_id ='"+patient+"' AND s.activity_id='"+activityId+"')"+
  1003. " AND btpd1.create_time >=(SELECT s.create_time " +
  1004. "FROM wlyy_health_bank_task_patient_detail s WHERE s.patient_id ='"+patient+"' AND s.activity_id='"+activityId+"')" ;
  1005. List<Map<String,Object>> rstotal5 = jdbcTemplate.queryForList(rankingSql5);
  1006. Long count5 = 0L;
  1007. if(rstotal5!=null&&rstotal5.size()>0){
  1008. count5 = (Long) rstotal5.get(0).get("total");
  1009. }
  1010. //区排名
  1011. String rankingSql6 = "SELECT count(1)+1 AS total FROM (SELECT " +
  1012. " SUM(btpd.total) AS sum , " +
  1013. " btpd.patient_id, " +
  1014. " btpd.activity_id," +
  1015. " create_time " +
  1016. "FROM " +
  1017. " wlyy_health_bank_task_patient_detail btpd WHERE btpd.activity_id ='"+activityId+"' AND btpd.patient_id !='"+patient+
  1018. "' GROUP BY patient_id )btpd1 " +
  1019. "WHERE " +
  1020. " btpd1.activity_id = '"+activityDO.getId()+"' and btpd1.patient_id IN(SELECT p.code FROM wlyy.wlyy_patient p WHERE p.town = '"+town+"') AND btpd1.sum >= (SELECT SUM(s.coupon_total) as couponTotal " +
  1021. "FROM wlyy_health_bank_task_patient_detail s WHERE s.patient_id ='"+patient+"' AND s.activity_id='"+activityId+"')"
  1022. + " AND btpd1.create_time >=(SELECT s.create_time " +
  1023. "FROM wlyy_health_bank_task_patient_detail s WHERE s.patient_id ='"+patient+"' AND s.activity_id='"+activityId+"')" ;
  1024. List<Map<String,Object>> rstotal6 = jdbcTemplate.queryForList(rankingSql6);
  1025. Long count6 = 0L;
  1026. if(rstotal6!=null&&rstotal6.size()>0){
  1027. count6= (Long) rstotal6.get(0).get("total");
  1028. }
  1029. */
  1030. //活动劵团队排名
  1031. String rankingSql7 = "SELECT count(1)+1 AS total FROM (SELECT " +
  1032. " SUM(btpd.coupon_total) AS sum , " +
  1033. " btpd.patient_id, " +
  1034. " btpd.activity_id," +
  1035. " create_time " +
  1036. "FROM " +
  1037. " wlyy_health_bank_task_patient_detail btpd WHERE btpd.activity_id ='"+activityId+"' AND btpd.patient_id !='"+patient+
  1038. "' GROUP BY patient_id )btpd1 " +
  1039. "WHERE btpd1.patient_id IN(SELECT sf.patient FROM wlyy.wlyy_sign_family sf WHERE sf.status=1 " +
  1040. "AND sf.admin_team_code=(SELECT sf.admin_team_code FROM wlyy.wlyy_sign_family sf WHERE sf.status=1 AND sf.patient='"+patient+"') ) AND btpd1.sum >= (SELECT SUM(s.coupon_total) as couponTotal " +
  1041. "FROM wlyy_health_bank_task_patient_detail s WHERE s.patient_id ='"+patient+"' AND s.activity_id='"+activityId+"')"+
  1042. " AND btpd1.create_time <=(SELECT s.create_time " +
  1043. "FROM wlyy_health_bank_task_patient_detail s WHERE s.patient_id ='"+patient+"' AND s.activity_id='"+activityId+"')" ;
  1044. List<Map<String,Object>> rstotal7 = jdbcTemplate.queryForList(rankingSql7);
  1045. Long count7 = 0L;
  1046. if(rstotal7!=null&&rstotal7.size()>0){
  1047. count7 = (Long) rstotal7.get(0).get("total");
  1048. }
  1049. //积分团队排名
  1050. String rankingSql8 = "SELECT count(1)+1 AS total FROM (SELECT " +
  1051. " SUM(btpd.total) AS sum , " +
  1052. " btpd.patient_id, " +
  1053. " btpd.activity_id," +
  1054. " create_time " +
  1055. "FROM " +
  1056. " wlyy_health_bank_task_patient_detail btpd WHERE btpd.activity_id ='"+activityId+"' AND btpd.patient_id !='"+patient+
  1057. "' GROUP BY patient_id )btpd1 " +
  1058. "WHERE btpd1.patient_id IN(SELECT sf.patient FROM wlyy.wlyy_sign_family sf WHERE sf.status=1 " +
  1059. "AND sf.admin_team_code=(SELECT sf.admin_team_code FROM wlyy.wlyy_sign_family sf WHERE sf.status=1 AND sf.patient='"+patient+"') ) AND btpd1.sum >= (SELECT SUM(s.total) as total " +
  1060. "FROM wlyy_health_bank_task_patient_detail s WHERE s.patient_id ='"+patient+"' AND s.activity_id='"+activityId+"')" +
  1061. " AND btpd1.create_time <=(SELECT s.create_time " +
  1062. "FROM wlyy_health_bank_task_patient_detail s WHERE s.patient_id ='"+patient+"' AND s.activity_id='"+activityId+"')" ;
  1063. List<Map<String,Object>> rstotal8 = jdbcTemplate.queryForList(rankingSql8);
  1064. Long count8 = 0L;
  1065. if(rstotal8!=null&&rstotal8.size()>0){
  1066. count8 = (Long) rstotal8.get(0).get("total");
  1067. }
  1068. /* //活动劵
  1069. String rankingSql4 = "SELECT count(1)+1 AS total FROM (SELECT " +
  1070. " SUM(coupon_total) AS sum , " +
  1071. " patient_id, " +
  1072. " activity_id " +
  1073. "FROM " +
  1074. " wlyy_health_bank_task_patient_detail " +
  1075. "GROUP BY patient_id )btpd1 " +
  1076. "WHERE " +
  1077. " btpd1.activity_id = '"+activityDO.getId()+"'"+buffer+" AND btpd1.sum >= (SELECT SUM(s.coupon_total) as couponTotal " +
  1078. "FROM wlyy_health_bank_task_patient_detail s WHERE s.patient_id ='"+patient+"' AND s.activity_id='"+activityId+"')" ;
  1079. List<Map<String,Object>> rstotal4 = jdbcTemplate.queryForList(rankingSql4);
  1080. Long count4 = 0L;
  1081. if(rstotal4!=null&&rstotal4.size()>0){
  1082. count4= (Long) rstotal4.get(0).get("total");
  1083. }
  1084. //积分
  1085. String rankingSql5 = "SELECT count(1)+1 AS total FROM (SELECT " +
  1086. " SUM(total) AS sum , " +
  1087. " patient_id, " +
  1088. " activity_id " +
  1089. "FROM " +
  1090. " wlyy_health_bank_task_patient_detail " +
  1091. "GROUP BY patient_id )btpd1 " +
  1092. "WHERE " +
  1093. " btpd1.activity_id = '"+activityDO.getId()+"'"+buffer+" AND btpd1.sum >= (SELECT SUM(s.total) as total " +
  1094. "FROM wlyy_health_bank_task_patient_detail s WHERE s.patient_id ='"+patient+"' AND s.activity_id='"+activityId+"')" ;
  1095. List<Map<String,Object>> rstotal5 = jdbcTemplate.queryForList(rankingSql5);
  1096. Long count5 = 0L;
  1097. if(rstotal5!=null&&rstotal5.size()>0){
  1098. count5 = (Long) rstotal5.get(0).get("total");
  1099. }
  1100. String rankingSql6 = "SELECT count(1)+1 AS total FROM (SELECT " +
  1101. " SUM(total) AS sum , " +
  1102. " patient_id, " +
  1103. " activity_id " +
  1104. "FROM " +
  1105. " wlyy_health_bank_task_patient_detail " +
  1106. "GROUP BY patient_id )btpd1 " +
  1107. "WHERE " +
  1108. " btpd1.activity_id = '"+activityDO.getId()+"'"+buffer+" AND btpd1.sum >= (SELECT SUM(s.total) as total " +
  1109. "FROM wlyy_health_bank_task_patient_detail s WHERE s.patient_id ='"+patient+"' AND s.activity_id='"+activityId+"')" ;
  1110. List<Map<String,Object>> rstotal6 = jdbcTemplate.queryForList(rankingSql6);
  1111. Long count6 = 0L;
  1112. if(rstotal6!=null&&rstotal6.size()>0){
  1113. count6= (Long) rstotal6.get(0).get("total");
  1114. }*/
  1115. ActivityRuleDO activityRuleDO = activityRuleDao.selectByActivityId(activityId);
  1116. JSONObject object = JSONObject.parseObject(activityRuleDO.getValue1());
  1117. Integer type = object.getInteger("type");
  1118. if (type == 2){
  1119. jsonObject.put("activityTownCouponRanking",count6);//活动劵全区排名
  1120. jsonObject.put("activityStreetCouponRanking",count5);//活动劵街道排名
  1121. jsonObject.put("activityCouponRanking",count2);//活动劵的排名
  1122. jsonObject.put("couponTotal",taskPatientDetailDO1.getCouponTotal());//本人活动劵总数
  1123. jsonObject.put("couponRanking",patientDetailDOS1);//活动劵人数排名
  1124. jsonObject.put("signCouponRanking",count7);//活动劵团队排名
  1125. jsonObject.put("activityHospitalCouponRanking",count11);//社区排名
  1126. if (count2 ==1){
  1127. jsonObject.put("differTotal",0);
  1128. }else {
  1129. jsonObject.put("differTotal",total2-total3);//相距前一个差多少活动劵
  1130. }
  1131. }else if (type ==1){
  1132. jsonObject.put("IntegrateRanking",patientDetailDOS);
  1133. jsonObject.put("activityStreetIntegrateRanking",count3);//积分街道排名
  1134. jsonObject.put("total",taskPatientDetailDO1.getTotal());//本人总积分
  1135. jsonObject.put("activityTownIntegrateRanking",count4);//积分全区排名
  1136. jsonObject.put("activityRanking",count);//积分排名
  1137. jsonObject.put("signRanking",count8);//积分团队排名
  1138. jsonObject.put("activityHospitalIntegrateRanking",count10);//社区排名
  1139. if (count==1){
  1140. jsonObject.put("differTotal",0);//相距前一个差多少积分
  1141. }else {
  1142. jsonObject.put("differTotal",total-total1);//相距前一个差多少积分
  1143. }
  1144. }
  1145. jsonObject.put("type",type);//积分还是活动劵
  1146. envelop.setObj(jsonObject);
  1147. return envelop;
  1148. }
  1149. public JSONObject findActivityByType(String type){
  1150. JSONObject object=new JSONObject();
  1151. List<String> types = Arrays.asList(type.split(","));
  1152. for(String str:types){
  1153. List<ActivityDO> lst= activityDao.findByIdAndType(str);
  1154. if(null!=lst&&lst.size()>0){
  1155. object.put(str,true);
  1156. }else {
  1157. object.put(str,false);
  1158. }
  1159. }
  1160. return object;
  1161. }
  1162. public JSONObject ListActivityDO(String type, String status,String crowdType,String releaseTime,String activityOfflineTime, String filter,Integer page,Integer size)throws ParseException {
  1163. JSONObject obj=new JSONObject();
  1164. StringBuffer stringBuffer=new StringBuffer();
  1165. if (StringUtils.isNotEmpty(type)) {
  1166. stringBuffer.append(" and type='").append(type).append("'");
  1167. }
  1168. if (StringUtils.isNotEmpty(filter)) {
  1169. stringBuffer.append(" and (title like '%").append(filter).append("%' or ").append("organizer like '%").append(filter).append("%' or ").append("area_name like '%").append(filter).append("%')");
  1170. }
  1171. if (null != status && "2".equals(status) ) {
  1172. stringBuffer.append(" and status in (-1,2) ");
  1173. } else if (null != status && "1".equals(status) ) {
  1174. stringBuffer.append(" and status in (0,1) ");
  1175. }
  1176. if(null!=crowdType&&StringUtils.isNotBlank(crowdType)){
  1177. stringBuffer.append(" and crowd_type="+crowdType);
  1178. }
  1179. if(StringUtils.isNotBlank(releaseTime)){
  1180. stringBuffer.append(" and activity_offline_time>='"+releaseTime+" 00:00:00' ");
  1181. }
  1182. if(StringUtils.isNotBlank(activityOfflineTime)){
  1183. stringBuffer.append(" and activity_offline_time<='"+ activityOfflineTime+" 59:59:59' ");
  1184. }
  1185. stringBuffer.append(" order by create_time desc");
  1186. String sql = "SELECT * FROM wlyy_health_bank.wlyy_health_bank_activity WHERE 1=1 AND del_flag=1 "+stringBuffer.toString()+" LIMIT "+(page-1)*size+","+size;
  1187. List<ActivityDO> activityDOList = jdbcTemplate.query(sql,new BeanPropertyRowMapper(ActivityDO.class));
  1188. String sqlcount = "SELECT COUNT(1) AS total FROM wlyy_health_bank.wlyy_health_bank_activity WHERE 1=1 AND del_flag=1 "+stringBuffer.toString();
  1189. List<Map<String,Object>> rstotal = jdbcTemplate.queryForList(sqlcount);
  1190. Long count = 0L;
  1191. if(rstotal!=null&&rstotal.size()>0){
  1192. count = (Long) rstotal.get(0).get("total");
  1193. }
  1194. obj .put("activityDOList",activityDOList);
  1195. obj .put("count",count);
  1196. return obj;
  1197. }
  1198. /**
  1199. * 查询排序
  1200. *
  1201. * @return
  1202. */
  1203. public List<ActivityDO> select(){
  1204. return activityDao.selectBySort();
  1205. }
  1206. /**
  1207. * 调整活动的顺序
  1208. *
  1209. * @param array
  1210. * @return
  1211. */
  1212. public MixEnvelop updateSort(JSONArray array){
  1213. MixEnvelop envelop = new MixEnvelop();
  1214. for (int i=0;i<array.size();i++){
  1215. JSONObject jsonObject = array.getJSONObject(i);
  1216. ActivityDO activityDO = activityDao.selectById(jsonObject.getString("id"));
  1217. activityDO.setSort(jsonObject.getInteger("sort"));
  1218. activityDao.save(activityDO);
  1219. }
  1220. envelop.setObj(true);
  1221. return envelop;
  1222. }
  1223. /**
  1224. * 查询参与的竟步走活动
  1225. *
  1226. * @param unionId
  1227. * @param patient
  1228. * @param page
  1229. * @param size
  1230. * @return
  1231. */
  1232. public MixEnvelop<ActivityDO,ActivityDO> selectByUnionId(String unionId,String patient,String idCard,Integer page,Integer size){
  1233. String sql ="select hba.* from wlyy_health_bank_activity hba right join wlyy_health_bank_task_patient_detail htpd on htpd.activity_id=hba.id " +
  1234. "AND htpd.union_id='"+unionId+"' AND htpd.patient_id ='"+patient+"' and htpd.patient_idcard ='"+idCard+"' " +
  1235. "where hba.del_flag=1 and hba.type='竞走' order by hba.create_time desc LIMIT " + (page-1)*size+","+size;
  1236. List<ActivityDO> activityDOList = jdbcTemplate.query(sql,new BeanPropertyRowMapper(ActivityDO.class));
  1237. for (ActivityDO activityDO:activityDOList){
  1238. ActivityRuleDO activityRuleDO = activityRuleDao.selectByActivityId(activityDO.getId());
  1239. JSONObject object = JSONObject.parseObject(activityRuleDO.getValue2());
  1240. String activityOfflineTime = object.getString("deadLine");
  1241. List<String> idList = Arrays.asList(activityOfflineTime.split(","));
  1242. if (idList !=null && idList.size()!=0){
  1243. String startTime = idList.get(0);
  1244. Date startDate = DateUtil.strToDateLong(startTime);
  1245. String endTime = idList.get(1);
  1246. Date endDate = DateUtil.strToDateLong(endTime);
  1247. String nowTime = DateUtil.dateToStrLong(DateUtil.getNowDate());
  1248. Date nowDate = DateUtil.strToDateLong(nowTime);
  1249. if(startDate.getTime()<=nowDate.getTime()&&endDate.getTime()>=nowDate.getTime()){
  1250. activityDO.setFlag(1);
  1251. }else {
  1252. activityDO.setFlag(0);
  1253. }
  1254. }
  1255. activityDO.setActivityRuleDO(activityRuleDO);
  1256. List<TaskDO> taskDOS = taskDao.selectByActivityId(activityDO.getId());
  1257. if (taskDOS!=null&&taskDOS.size()!=0){
  1258. activityDO.setTaskDOS(taskDOS);
  1259. String sqlCount1 = "select sum(integrate) as total from wlyy_health_bank_credits_detail where transaction_id='"+taskDOS.get(0).getId()+"' and patient_id = '"+patient+"'" +
  1260. "and create_time >= '"+DateUtils.getDayBegin()+"' and create_time <='"+DateUtils.getDayEnd()+"' AND description IS NULL ";
  1261. List<Map<String,Object>> rstotal1 = jdbcTemplate.queryForList(sqlCount1);
  1262. Long count1 = 0L;
  1263. if(rstotal1!=null&&rstotal1.size()>0){
  1264. Object count = rstotal1.get(0).get("total");
  1265. if (count!=null&&count!=""){
  1266. count1 =Long.parseLong(count.toString()) ;
  1267. }
  1268. }
  1269. activityDO.setNowTotal(count1);
  1270. }
  1271. }
  1272. List<ActivityDO> activityDOList1 = new ArrayList<>();
  1273. for (ActivityDO activityDO :activityDOList){
  1274. if(activityDO.getFlag()==1){
  1275. activityDOList1.add(0,activityDO);
  1276. }else {
  1277. activityDOList1.add(activityDO);
  1278. }
  1279. }
  1280. String sqlCount ="select count(1) AS total from wlyy_health_bank_activity hba right join wlyy_health_bank_task_patient_detail htpd on htpd.activity_id=hba.id " +
  1281. "where hba.del_flag=1 and hba.type='竞走' order by hba.create_time desc ";
  1282. List<Map<String,Object>> rstotal = jdbcTemplate.queryForList(sqlCount);
  1283. Long count = 0L;
  1284. if(rstotal!=null&&rstotal.size()>0){
  1285. count = (Long) rstotal.get(0).get("total");
  1286. }
  1287. return MixEnvelop.getSuccessListWithPage(HealthBankMapping.api_success,activityDOList1,page,size,count);
  1288. }
  1289. /**
  1290. * 获取当前居民参加的活动
  1291. *
  1292. * @param unionId
  1293. * @param patient
  1294. * @param idCard
  1295. * @return
  1296. */
  1297. public List<ActivityDO> selectNowByUnionId(String unionId,String patient,String idCard){
  1298. String sql ="select hba.* from wlyy_health_bank_activity hba right join wlyy_health_bank_task_patient_detail htpd on htpd.activity_id=hba.id " +
  1299. "AND htpd.union_id='"+unionId+"' AND htpd.patient_id ='"+patient+"' and htpd.patient_idcard ='"+idCard+"' " +
  1300. "where hba.del_flag=1 and hba.type='竞走' order by hba.create_time desc " ;
  1301. List<ActivityDO> activityDOList = jdbcTemplate.query(sql,new BeanPropertyRowMapper(ActivityDO.class));
  1302. List<ActivityDO> activityDOS = new ArrayList<>();
  1303. for (ActivityDO activityDO:activityDOList){
  1304. ActivityRuleDO activityRuleDO = activityRuleDao.selectByActivityId(activityDO.getId());
  1305. JSONObject object = JSONObject.parseObject(activityRuleDO.getValue2());
  1306. String activityOfflineTime = object.getString("activityOfflineTime");
  1307. List<String> idList = Arrays.asList(activityOfflineTime.split(","));
  1308. activityDO.setActivityRuleDO(activityRuleDO);
  1309. List<TaskDO> taskDOS = taskDao.selectByActivityId(activityDO.getId());
  1310. if (taskDOS!=null&&taskDOS.size()!=0){
  1311. activityDO.setTaskDOS(taskDOS);
  1312. }
  1313. if (idList !=null && idList.size()!=0){
  1314. String startTime = idList.get(0);
  1315. String endTime = idList.get(1);
  1316. String nowTime = DateUtil.dateToStrLong(DateUtil.getNowDate());
  1317. if((startTime.compareTo(nowTime)==-1&&endTime.compareTo(nowTime)==1)||startTime.compareTo(nowTime)==0||endTime.compareTo(nowTime)==0){
  1318. activityDO.setFlag(1);
  1319. activityDOS.add(activityDO);
  1320. }
  1321. }
  1322. }
  1323. return activityDOS;
  1324. }
  1325. /**
  1326. * 小程序活动排名
  1327. *
  1328. * @param taskId
  1329. * @param size
  1330. * @return
  1331. */
  1332. public JSONObject selectAppletsRanking(Integer flag,String flagName,String taskId,Integer size,String patient){
  1333. StringBuffer buffer = new StringBuffer();
  1334. if (flag == 1){
  1335. buffer.append("");
  1336. }else if (flag == 2){
  1337. buffer.append(" and hbtd.town='"+flagName+"' ");
  1338. }else if (flag==3){
  1339. buffer.append(" and hbtd.hospital = '"+flagName+"'");
  1340. }
  1341. String sql = "SELECT @rownum :=@rownum + 1 AS rank, IFNULL(r.sum,0) as sum , r.patient_id AS patient FROM " +
  1342. "(SELECT@rowNum := 0) b," +
  1343. " (SELECT " +
  1344. " sum(bcd.integrate) AS sum, hbtd.patient_id, hbtd.create_time FROM wlyy_health_bank_task_patient_detail hbtd " +
  1345. " LEFT JOIN wlyy_health_bank_credits_detail bcd ON hbtd.patient_id=bcd.patient_id AND bcd.transaction_id= '"+taskId +
  1346. "' WHERE hbtd.task_id = '"+taskId+"' " +buffer+
  1347. "GROUP BY hbtd.patient_id) r ORDER BY r.sum DESC,r.create_time DESC LIMIT 0,"+size;
  1348. List<Map<String,Object>> list = jdbcTemplate.queryForList(sql);
  1349. String sql1= "select s.rank,IFNULL(s.sum,0) as sum,s.patient_id from (SELECT @rownum :=@rownum + 1 AS rank, r.sum, r.patient_id FROM " +
  1350. "(SELECT@rowNum := 0) b," +
  1351. " (SELECT sum(bcd.integrate) AS sum, hbtd.patient_id, hbtd.create_time FROM " +
  1352. "wlyy_health_bank_task_patient_detail hbtd " +
  1353. " LEFT JOIN wlyy_health_bank_credits_detail bcd ON hbtd.patient_id=bcd.patient_id " +
  1354. "AND bcd.transaction_id='"+taskId+"'" +
  1355. "WHERE hbtd.task_id = '"+taskId+"' " +buffer+
  1356. "GROUP BY hbtd.patient_id) r ORDER BY r.sum DESC,r.create_time DESC)s where s.patient_id ='"+patient+"'";
  1357. List<Map<String,Object>> list1 = jdbcTemplate.queryForList(sql1);
  1358. JSONObject object = new JSONObject();
  1359. object.put("list",list);
  1360. if (list1!=null&&list1.size()!=0){
  1361. object.put("patient",list1.get(0));
  1362. }else {
  1363. object.put("patient",null);
  1364. }
  1365. return object;
  1366. }
  1367. }