ActivityService.java 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049
  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){
  60. if (value2 != null && value2 !=""){
  61. JSONObject object = JSONObject.parseObject(value2);
  62. activityDO.setActivityOfflineTime(object.getDate("activityOfflineTime"));
  63. activityDO.setRegistrationTime(object.getDate("registrationTime"));
  64. activityDO.setDeadLine(object.getDate("deadLine"));
  65. }
  66. activityDO.setCreateTime(new Date());
  67. activityDO.setUpdateTime(new Date());
  68. activityDO.setDelFlag(1);
  69. activityDO= activityDao.save(activityDO);
  70. if (value3 !=null && value3 !=""){
  71. JSONObject object = JSONObject.parseObject(value3);
  72. String exchangeType = object.getString("exchangeType");
  73. if (exchangeType.equals("1")){
  74. String array = (null!=object.get("goodDetail")?String.valueOf(object.get("goodDetail")):"");
  75. if (StringUtils.isNotBlank(array)){
  76. JSONArray array1= JSON.parseArray(array);
  77. for (int i =0;i<array1.size();i++){
  78. JSONObject jsonObject = array1.getJSONObject(i);
  79. TaskGoodsDO taskGoodsDO = new TaskGoodsDO();
  80. taskGoodsDO.setCreateTime(new Date());
  81. taskGoodsDO.setUpdateTime(new Date());
  82. taskGoodsDO.setActivityId(activityDO.getId());
  83. taskGoodsDO.setCoupon(jsonObject.getInteger("couponNum"));
  84. taskGoodsDO.setImg(jsonObject.getString("picUrl"));
  85. taskGoodsDO.setName(jsonObject.getString("name"));
  86. taskGoodsDO.setSaasId("dev");
  87. taskGoodsDO.setStatus(1);
  88. taskGoodsDao.save(taskGoodsDO);
  89. }
  90. }
  91. }
  92. }
  93. TaskDO taskDO = new TaskDO();
  94. taskDO.setTitle(activityDO.getTitle());
  95. taskDO.setStatus(1);
  96. taskDO.setTransactionId(activityDO.getId());
  97. taskDO.setType("ACTIVITY_TASK");
  98. taskDO.setSaasId("dev");
  99. taskDO.setContent(activityDO.getDescription());
  100. taskDO.setPeriod(1);
  101. taskDO.setTaskCode("ACTIVITY_QUESTION");
  102. taskDO.setStartTime(activityDO.getRegistrationTime());
  103. taskDO.setEndTime(activityDO.getActivityOfflineTime());
  104. taskDO.setCreateTime(new Date());
  105. taskDO.setUpdateTime(new Date());
  106. taskDao.save(taskDO);
  107. ActivityRuleDO activityRuleDO = new ActivityRuleDO();
  108. activityRuleDO.setCreateTime(new Date());
  109. activityRuleDO.setUpdateTime(new Date());
  110. activityRuleDO.setActivityId(activityDO.getId());
  111. activityRuleDO.setSaasId("dev");
  112. activityRuleDO.setValue1(value1);
  113. activityRuleDO.setValue2(value2);
  114. activityRuleDO.setValue3(value3);
  115. activityRuleDO.setDelFlag(1);
  116. /* activityRuleDO.setDel(1);*/
  117. activityRuleDao.save(activityRuleDO);
  118. MixEnvelop<Boolean, Boolean> envelop = new MixEnvelop<>();
  119. envelop.setObj(true);
  120. return envelop;
  121. }
  122. /**
  123. * find by condition
  124. *
  125. * @param activityDO 活动参数对象
  126. * @param page 页码
  127. * @param size 每页大小
  128. * @return
  129. * @throws ParseException
  130. */
  131. public MixEnvelop<ActivityDO, ActivityDO> findByCondition(ActivityDO activityDO, Integer page, Integer size) throws ParseException {
  132. String sql = new ISqlUtils().getSql(activityDO,page,size,"*");
  133. List<ActivityDO> activityDOS = jdbcTemplate.query(sql,new BeanPropertyRowMapper(ActivityDO.class));
  134. for (ActivityDO activityDO1:activityDOS){
  135. String taskSql = "SELECT" +
  136. " COUNT(1) AS total1 " +
  137. "FROM " +
  138. " ( " +
  139. " SELECT DISTINCT " +
  140. " (btpd.patient_id) " +
  141. " FROM " +
  142. " wlyy_health_bank_task_patient_detail btpd " +
  143. " WHERE " +
  144. " activity_id = '" +activityDO1.getId()+
  145. "' ) btpd1";
  146. List<Map<String,Object>> rstotal = jdbcTemplate.queryForList(taskSql);
  147. Long count = 0L;
  148. if(rstotal!=null&&rstotal.size()>0){
  149. count = (Long) rstotal.get(0).get("total1");
  150. }
  151. activityDO1.setTotal(count);
  152. String taskSql1 = "select * from wlyy_health_bank_task_patient_detail btpd where activity_id = '"+activityDO1.getId()
  153. +"' and (patient_idcard = '"+activityDO.getPatientIdcard()+"' OR union_id = '"+activityDO.getUnionId()+"')";
  154. List<TaskPatientDetailDO> taskPatientDetailDOS = jdbcTemplate.query(taskSql1,new BeanPropertyRowMapper(TaskPatientDetailDO.class));
  155. activityDO1.setTaskPatientDetailDOS(taskPatientDetailDOS);
  156. String tasksql = "select * from wlyy_health_bank_task where transaction_id = '"+activityDO1.getId()+"'";
  157. List<TaskDO> taskDOList = jdbcTemplate.query(tasksql,new BeanPropertyRowMapper(TaskDO.class));
  158. List<TaskGoodsDO> taskGoodsDOS = taskGoodsDao.selectByActivityId(activityDO1.getId());
  159. for (TaskGoodsDO taskGoodsDO:taskGoodsDOS){
  160. ExchangeGoodsDO exchangeGoodsDO = exchangeGoodsDao.selectByActivityGoodsIdAndPatientId(taskGoodsDO.getId(),activityDO.getPatientId());
  161. taskGoodsDO.setExchangeGoodsDO(exchangeGoodsDO);
  162. }
  163. activityDO1.setTaskGoodsDOS(taskGoodsDOS);
  164. ActivityRuleDO activityRuleDO = activityRuleDao.selectByActivityId(activityDO1.getId());
  165. activityDO1.setActivityRuleDO(activityRuleDO);
  166. activityDO1.setTaskDOS(taskDOList);
  167. }
  168. String sqlcount = new ISqlUtils().getSql(activityDO,0,0,"count");
  169. List<Map<String,Object>> rstotal = jdbcTemplate.queryForList(sqlcount);
  170. Long count = 0L;
  171. if(rstotal!=null&&rstotal.size()>0){
  172. count = (Long) rstotal.get(0).get("total");
  173. }
  174. return MixEnvelop.getSuccessListWithPage(HealthBankMapping.api_success,activityDOS,page,size,count);
  175. }
  176. /**
  177. * update activityDO
  178. *
  179. * @param activityDO 活动参数对象
  180. * @return
  181. */
  182. @org.springframework.transaction.annotation.Transactional(rollbackFor = Exception.class)
  183. public MixEnvelop<Boolean, Boolean> update(ActivityDO activityDO, String value1, String value2, String value3) throws Exception {
  184. if (StringUtils.isBlank(activityDO.getId())) {
  185. throw new Exception("活动id不能为空!");
  186. }
  187. if (StringUtils.isNotBlank(value2)){
  188. JSONObject object = JSONObject.parseObject(value2);
  189. activityDO.setActivityOfflineTime(object.getDate("activityOfflineTime"));
  190. activityDO.setRegistrationTime(object.getDate("registrationTime"));
  191. activityDO.setDeadLine(object.getDate("deadLine"));
  192. }
  193. activityDO.setUpdateTime(new Date());
  194. activityDO.setDelFlag(1);
  195. //删除活动任务与奖品
  196. taskGoodsDao.deleteByActivityId(activityDO.getId());
  197. if (value3 !=null && value3 !=""){
  198. JSONObject object = JSONObject.parseObject(value3);
  199. String exchangeType = object.getString("exchangeType");
  200. if (exchangeType.equals("1")){
  201. String array = (null!=object.get("goodDetail")?String.valueOf(object.get("goodDetail")):"");
  202. if (StringUtils.isNotBlank(array)){
  203. JSONArray array1= JSON.parseArray(array);
  204. for (int i =0;i<array1.size();i++){
  205. JSONObject jsonObject = array1.getJSONObject(i);
  206. TaskGoodsDO taskGoodsDO = new TaskGoodsDO();
  207. taskGoodsDO.setActivityId(activityDO.getId());
  208. taskGoodsDO.setCoupon(jsonObject.getInteger("couponNum"));
  209. taskGoodsDO.setImg(jsonObject.getString("picUrl"));
  210. taskGoodsDO.setName(jsonObject.getString("name"));
  211. taskGoodsDO.setCreateTime(new Date());
  212. taskGoodsDO.setUpdateTime(new Date());
  213. taskGoodsDO.setSaasId("dev");
  214. taskGoodsDO.setStatus(1);
  215. taskGoodsDao.save(taskGoodsDO);
  216. }
  217. }
  218. }
  219. }
  220. ActivityRuleDO activityRuleDO = activityRuleDao.selectByActivityId(activityDO.getId());
  221. activityRuleDO.setUpdateTime(new Date());
  222. activityRuleDO.setSaasId("dev");
  223. activityRuleDO.setValue1(value1);
  224. activityRuleDO.setValue2(value2);
  225. activityRuleDO.setValue3(value3);
  226. activityRuleDO.setDelFlag(1);
  227. activityRuleDao.save(activityRuleDO);
  228. System.out.println("----------------------------update--"+activityDO.getId()+activityDO.getAreaName());
  229. // activityDO.setCreateTime(new Date());
  230. activityDao.save(activityDO);
  231. MixEnvelop<Boolean, Boolean> envelop = new MixEnvelop<>();
  232. envelop.setObj(true);
  233. return envelop;
  234. }
  235. /**
  236. * 获取参与的活动
  237. *
  238. * @param activityDO 活动对象
  239. * @param page 页码
  240. * @param size 分页大小
  241. * @return
  242. */
  243. public MixEnvelop<ActivityDO, ActivityDO> selectByPatient(ActivityDO activityDO, Integer page, Integer size){
  244. String condition = "";
  245. if (activityDO.getUnionId()!=null&&activityDO.getUnionId() !=""){
  246. condition="AND union_id = '"+ activityDO.getUnionId()+"'";
  247. }
  248. String sql ="SELECT * " +
  249. " FROM wlyy_health_bank_activity " +
  250. "WHERE " +
  251. "id IN ( " +
  252. "SELECT bt.transaction_id " +
  253. "FROM wlyy_health_bank_task bt " +
  254. "WHERE id IN (" +
  255. " SELECT task_id FROM " +
  256. "wlyy_health_bank_task_patient_detail" +
  257. " WHERE " +
  258. " patient_idcard = '"+activityDO.getPatientIdcard()+"' "+ condition+")" +
  259. " )" +
  260. " LIMIT "+(page-1)*size +","+size;
  261. List<ActivityDO> activityDOS = jdbcTemplate.query(sql,new BeanPropertyRowMapper(ActivityDO.class));
  262. for (ActivityDO activityDO1:activityDOS){
  263. String activitySql ="SELECT btpd1.sum AS total FROM (SELECT " +
  264. " SUM(total) AS sum , " +
  265. " patient_id, " +
  266. " patient_openid, " +
  267. " patient_idcard, " +
  268. " activity_id," +
  269. " union_id " +
  270. " FROM " +
  271. " wlyy_health_bank_task_patient_detail " +
  272. "GROUP BY patient_openid,patient_idcard,union_id)btpd1 " +
  273. "WHERE " +
  274. " btpd1.activity_id = '"+activityDO1.getId() +"' AND patient_openid = '"+activityDO.getOpenId()+ "' AND patient_idcard = '"+activityDO.getPatientIdcard()+"' "+condition;
  275. List<Map<String,Object>> rstotal = jdbcTemplate.queryForList(activitySql);
  276. Long count = 0L;
  277. if(rstotal!=null&&rstotal.size()>0){
  278. Object object = rstotal.get(0).get("total");
  279. count = Long.parseLong(object.toString());
  280. }
  281. activityDO1.setSum(count);
  282. String rankingSql = "SELECT count(1)+1 AS total FROM (SELECT " +
  283. " SUM(total) AS sum , " +
  284. " patient_id, " +
  285. " patient_openid, " +
  286. " patient_idcard, " +
  287. " activity_id " +
  288. "FROM " +
  289. " wlyy_health_bank_task_patient_detail " +
  290. "GROUP BY patient_openid,patient_idcard,union_id)btpd1 " +
  291. "WHERE " +
  292. " btpd1.activity_id = '"+activityDO1.getId()+"' AND btpd1.sum >" +activityDO1.getSum() ;
  293. List<Map<String,Object>> rstotal1 = jdbcTemplate.queryForList(rankingSql);
  294. Integer count1 = 0;
  295. if(rstotal1!=null&&rstotal1.size()>0){
  296. Object object = rstotal1.get(0).get("total");
  297. count1 = Integer.parseInt(object.toString());
  298. }
  299. activityDO1.setActivityRanking(count1);
  300. String taskSql = "SELECT" +
  301. " COUNT(1) AS total1 " +
  302. "FROM " +
  303. " ( " +
  304. " SELECT * " +
  305. " FROM " +
  306. " wlyy_health_bank_task_patient_detail btpd " +
  307. " WHERE " +
  308. " activity_id = '" +activityDO1.getId()+
  309. "' GROUP BY patient_openid,patient_idcard,union_id) btpd1";
  310. List<Map<String,Object>> rstotal2 = jdbcTemplate.queryForList(taskSql);
  311. Long count2 = 0L;
  312. if(rstotal2!=null&&rstotal2.size()>0){
  313. count2 = (Long) rstotal2.get(0).get("total1");
  314. }
  315. activityDO1.setTotal(count2);
  316. }
  317. String sqlcount = "SELECT count(1) AS total" +
  318. " FROM wlyy_health_bank_activity " +
  319. "WHERE " +
  320. "id IN ( " +
  321. "SELECT bt.transaction_id " +
  322. "FROM wlyy_health_bank_task bt " +
  323. "WHERE id IN (" +
  324. " SELECT task_id FROM " +
  325. "wlyy_health_bank_task_patient_detail" +
  326. " WHERE " +
  327. " patient_openid = '"+activityDO.getOpenId()+ "' AND patient_idcard = '"+activityDO.getPatientIdcard()+"'"+condition+ ")" +
  328. " )";
  329. List<Map<String,Object>> rstotal = jdbcTemplate.queryForList(sqlcount);
  330. Long count = 0L;
  331. if(rstotal!=null&&rstotal.size()>0){
  332. count = (Long) rstotal.get(0).get("total");
  333. }
  334. return MixEnvelop.getSuccessListWithPage(HealthBankMapping.api_success,activityDOS,page,size,count);
  335. }
  336. /**
  337. * 批量删除活动
  338. *
  339. * @param ids 活动id集合
  340. * @return
  341. */
  342. public MixEnvelop<Boolean, Boolean> batchDelete(List<String> ids){
  343. MixEnvelop<Boolean, Boolean> envelop = new MixEnvelop<>();
  344. for (int i =0;i<ids.size();i++){
  345. List<TaskDO> taskDOList = taskDao.selectByActivityId(ids.get(i));
  346. for (TaskDO taskDO:taskDOList){
  347. taskDO.setStatus(0);
  348. taskDO.setCreateTime(new Date());
  349. taskDO.setUpdateTime(new Date());
  350. taskDao.save(taskDO);
  351. }
  352. List<TaskPatientDetailDO> taskPatientDetailDOS = taskPatientDetailDao.selectByActivityId(ids.get(i));
  353. for(TaskPatientDetailDO taskPatientDetailDO:taskPatientDetailDOS){
  354. taskPatientDetailDO.setStatus(-1);
  355. taskPatientDetailDO.setCreateTime(new Date());
  356. taskPatientDetailDO.setUpdateTime(new Date());
  357. taskPatientDetailDao.save(taskPatientDetailDO);
  358. }
  359. ActivityRuleDO ruleDO = activityRuleDao.selectByActivityId(ids.get(i));
  360. if(null!=ruleDO){
  361. ruleDO.setDelFlag(0);
  362. activityRuleDao.save(ruleDO);
  363. }
  364. ActivityDO activityDO = activityDao.findOne(ids.get(i));
  365. activityDO.setStatus(-1);
  366. activityDO.setCreateTime(new Date());
  367. activityDO.setUpdateTime(new Date());
  368. activityDO.setDelFlag(0);
  369. activityDao.save(activityDO);
  370. }
  371. return envelop;
  372. }
  373. /**
  374. * 删除活动
  375. *
  376. * @param id 活动id
  377. * @return
  378. */
  379. public MixEnvelop<Boolean, Boolean> daleteActivity(String id){
  380. MixEnvelop<Boolean, Boolean> envelop = new MixEnvelop<>();
  381. List<TaskDO> taskDOList = taskDao.selectByActivityId(id);
  382. for (TaskDO taskDO:taskDOList){
  383. taskDO.setStatus(0);
  384. taskDO.setCreateTime(new Date());
  385. taskDO.setUpdateTime(new Date());
  386. taskDao.save(taskDO);
  387. }
  388. List<TaskPatientDetailDO> taskPatientDetailDOS = taskPatientDetailDao.selectByActivityId(id);
  389. for(TaskPatientDetailDO taskPatientDetailDO:taskPatientDetailDOS){
  390. taskPatientDetailDO.setStatus(-1);
  391. taskPatientDetailDO.setCreateTime(new Date());
  392. taskPatientDetailDO.setUpdateTime(new Date());
  393. taskPatientDetailDao.save(taskPatientDetailDO);
  394. }
  395. ActivityRuleDO ruleDO = activityRuleDao.selectByActivityId(id);
  396. if(null!=ruleDO){
  397. ruleDO.setDelFlag(0);
  398. activityRuleDao.save(ruleDO);
  399. }
  400. ActivityDO activityDO = activityDao.findOne(id);
  401. activityDO.setStatus(-1);
  402. activityDO.setCreateTime(new Date());
  403. activityDO.setUpdateTime(new Date());
  404. activityDO.setDelFlag(0);
  405. activityDao.save(activityDO);
  406. return envelop;
  407. }
  408. /**
  409. * 编辑活动:上、下线,推荐
  410. * @param id
  411. * @param type 操作类型:上线:0,下线:2,推荐:3,取消推荐:4
  412. * @return
  413. * @throws Exception
  414. */
  415. public ActivityDO editActivity(String id, String type) throws Exception {
  416. ActivityDO activityDO = activityDao.findOne(id);
  417. ActivityRuleDO activityRuleDO= activityRuleDao.selectByActivityId(id);
  418. if("3".equals(type)){
  419. //推荐
  420. activityDO.setRecommended(1);
  421. }else if("4".equals(type)){
  422. //取消推荐
  423. activityDO.setRecommended(0);
  424. }
  425. else if("2".equals(type)){
  426. //下线
  427. activityDO.setStatus(2);
  428. }else if("0".equals(type)){
  429. //判断该活动是不是竞走,只能存在一条有效竞走
  430. if("竞走".equals(activityDO.getType())){
  431. List<ActivityDO> lst= activityDao.findByIdAndType(activityDO.getType());
  432. if(null!=lst&&lst.size()>0){
  433. throw new Exception("已存在一条上线的竞走活动!");
  434. }
  435. }
  436. //上线
  437. if(null!=activityRuleDO){
  438. String ruleJson=activityRuleDO.getValue2();
  439. JSONObject obj=JSONObject.parseObject(ruleJson);
  440. // SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
  441. SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm");
  442. //报名截止时间
  443. Date registrationTime=(null!=obj.get("registrationTime")? sdf2.parse(obj.get("registrationTime").toString()):null);
  444. //参与截止时间
  445. Date deadLine=(null!=obj.get("deadLine")? sdf2.parse(obj.get("deadLine").toString()):null);
  446. //活动下线时间
  447. Date activityOfflineTime=(null!=obj.get("activityOfflineTime")? sdf2.parse(obj.get("activityOfflineTime").toString()):null);
  448. //获取当前时间
  449. Date now = DateUtil.strToDate(sdf2.format(new Date()),"yyyy-MM-dd HH:mm");
  450. if(null!=activityOfflineTime&&activityOfflineTime.compareTo(now)<=0){
  451. //活动下线
  452. activityDO.setStatus(2);
  453. }else if((null!=activityOfflineTime&&activityOfflineTime.compareTo(now)>0) && (null!=deadLine&&deadLine.compareTo(now)<=0)){
  454. //活动结束
  455. activityDO.setStatus(-1);
  456. }else if((null!=deadLine&&deadLine.compareTo(now)>0)&&(null!=registrationTime&&registrationTime.compareTo(now)<=0)){
  457. //活动未结束
  458. activityDO.setStatus(0);
  459. }else if (null!=registrationTime&&registrationTime.compareTo(now)>1){
  460. //报名未截止
  461. activityDO.setStatus(1);
  462. }else{
  463. //没有时间限制,上线
  464. activityDO.setStatus(0);
  465. }
  466. }else {
  467. throw new Exception("活动规则缺失!");
  468. }
  469. }
  470. // activityDO.setReleaseTime(new Date());
  471. activityDO= activityDao.save(activityDO);
  472. return activityDO;
  473. }
  474. /**
  475. * 根据活动id获取活动详情+规则
  476. * @param id
  477. * @return
  478. * @throws Exception
  479. */
  480. public JSONObject findActivityById(String id)throws Exception{
  481. JSONObject obj=new JSONObject();
  482. ActivityDO activityDO = activityDao.findOne(id);
  483. obj.put("jsonData",activityDO);
  484. ActivityRuleDO activityRuleDO=new ActivityRuleDO();
  485. if(null!=activityDO){
  486. activityRuleDO= activityRuleDao.selectByActivityId(id);
  487. activityDO.setActivityRuleDO(activityRuleDO);
  488. }
  489. obj.put("value1", null == activityRuleDO ? null : activityRuleDO.getValue1());
  490. obj.put("value2", null == activityRuleDO ? null : activityRuleDO.getValue2());
  491. obj.put("value3", null == activityRuleDO ? null : activityRuleDO.getValue3());
  492. return obj;
  493. }
  494. /**
  495. * 活动排名
  496. *
  497. * @param activityId
  498. * @param patient
  499. * @param page
  500. * @param size
  501. * @return
  502. */
  503. public MixEnvelop<JSONObject,JSONObject> selectActivityRanking(String activityId,String patient,Integer page,Integer size,String street,String town){
  504. MixEnvelop<JSONObject,JSONObject> envelop = new MixEnvelop<>();
  505. ActivityDO activityDO = activityDao.selectById(activityId);
  506. JSONObject jsonObject = new JSONObject();
  507. TaskPatientDetailDO taskPatientDetailDO1 = taskPatientDetailDao.selectByActivityIdAndPatientId(activityId,patient);
  508. //积分排行
  509. 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 " +
  510. "FROM wlyy_health_bank_task_patient_detail btpd " +
  511. "WHERE btpd.activity_id = '"+activityId +
  512. "' GROUP BY btpd.patient_id " +
  513. " ) btpd " +
  514. "ORDER BY " +
  515. " btpd.total DESC,btpd.create_time ASC LIMIT " + (page-1)*size+","+size;
  516. List<TaskPatientDetailDO> patientDetailDOS = jdbcTemplate.query(sql,new BeanPropertyRowMapper<>(TaskPatientDetailDO.class));
  517. long count=0L;
  518. int j =0;
  519. for (TaskPatientDetailDO taskPatientDetailDO:patientDetailDOS){
  520. String timeSeparated = DateUtils.getDatePoor(new Date(),taskPatientDetailDO.getUpdateTime());
  521. String goodsSql = "select * from wlyy_health_bank_exchange_goods where patient_id ='"+taskPatientDetailDO.getPatientId()+"' " +
  522. "and activity_goods_id in(select id from wlyy_health_bank_activity_goods where activity_id ='"+activityId+"')";
  523. List<ExchangeGoodsDO> exchangeGoodsDOS = jdbcTemplate.query(goodsSql,new BeanPropertyRowMapper<>(ExchangeGoodsDO.class));
  524. for (ExchangeGoodsDO exchangeGoodsDO :exchangeGoodsDOS){
  525. TaskGoodsDO taskGoodsDO = taskGoodsDao.selectById(exchangeGoodsDO.getActivityGoodsId());
  526. exchangeGoodsDO.setTaskGoodsDO(taskGoodsDO);
  527. }
  528. taskPatientDetailDO.setExchangeGoodsDO(exchangeGoodsDOS);
  529. taskPatientDetailDO.setTimeSeparated(timeSeparated);
  530. j++;
  531. if (taskPatientDetailDO.getPatientId().equals(patient)){
  532. count = j;
  533. }
  534. }
  535. //活动劵排行
  536. 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 " +
  537. "FROM wlyy_health_bank_task_patient_detail btpd " +
  538. "WHERE btpd.activity_id = '"+activityId +
  539. "' GROUP BY btpd.patient_id " +
  540. " ) btpd " +
  541. "ORDER BY " +
  542. " btpd.couponTotal DESC,btpd.create_time ASC LIMIT " + (page-1)*size+","+size;
  543. List<TaskPatientDetailDO> patientDetailDOS1 = jdbcTemplate.query(sqlCoupon,new BeanPropertyRowMapper<>(TaskPatientDetailDO.class));
  544. long count2 = 0l;
  545. int i=0;
  546. for (TaskPatientDetailDO taskPatientDetailDO:patientDetailDOS1){
  547. String timeSeparated = DateUtils.getDatePoor(new Date(),taskPatientDetailDO.getUpdateTime());
  548. String goodsSql = "select * from wlyy_health_bank_exchange_goods where patient_id ='"+taskPatientDetailDO.getPatientId()+"' " +
  549. "and activity_goods_id in(select id from wlyy_health_bank_activity_goods where activity_id ='"+activityId+"')";
  550. List<ExchangeGoodsDO> exchangeGoodsDOS = jdbcTemplate.query(goodsSql,new BeanPropertyRowMapper<>(ExchangeGoodsDO.class));
  551. for (ExchangeGoodsDO exchangeGoodsDO :exchangeGoodsDOS){
  552. TaskGoodsDO taskGoodsDO = taskGoodsDao.selectById(exchangeGoodsDO.getActivityGoodsId());
  553. exchangeGoodsDO.setTaskGoodsDO(taskGoodsDO);
  554. }
  555. i++;
  556. if (taskPatientDetailDO.getPatientId().equals(patient)){
  557. count2=i;
  558. }
  559. taskPatientDetailDO.setExchangeGoodsDO(exchangeGoodsDOS);
  560. taskPatientDetailDO.setTimeSeparated(timeSeparated);
  561. }
  562. //活动总积分排名
  563. /*String rankingSql1 = "SELECT count(1)+1 AS total FROM (SELECT " +
  564. " SUM(total) AS sum , " +
  565. " patient_id, " +
  566. " activity_id, " +
  567. " create_time "+
  568. " FROM " +
  569. " wlyy_health_bank_task_patient_detail htpd " +
  570. " where htpd.activity_id ='"+activityId+"' AND htpd.patient_id !='"+patient+
  571. "' GROUP BY patient_id )btpd1 " +
  572. "WHERE " +
  573. " btpd1.sum >= (SELECT SUM(s.total) as total " +
  574. "FROM wlyy_health_bank_task_patient_detail s WHERE s.patient_id ='"+patient+"' AND s.activity_id='"+activityId+"') " +
  575. " AND btpd1.create_time >=(SELECT s.create_time " +
  576. "FROM wlyy_health_bank_task_patient_detail s WHERE s.patient_id ='"+patient+"' AND s.activity_id='"+activityId+"')" ;
  577. List<Map<String,Object>> rstotal1 = jdbcTemplate.queryForList(rankingSql1);
  578. Long count = 0L;
  579. if(rstotal1!=null&&rstotal1.size()>0){
  580. count = (Long) rstotal1.get(0).get("total");
  581. }*/
  582. Long countTotal = 0L;
  583. if ((count-2)>=0){
  584. countTotal = (count-2);
  585. }
  586. //排名前一个积分
  587. String rankingSql9 = "SELECT btpd1.sum as total FROM (SELECT " +
  588. " SUM(total) AS sum , " +
  589. " patient_id, " +
  590. " activity_id " +
  591. " FROM " +
  592. " wlyy_health_bank_task_patient_detail btpd where btpd.activity_id ='"+activityId+"' AND btpd.patient_id !='"+patient+
  593. "' GROUP BY patient_id )btpd1 " +
  594. "WHERE " +
  595. " btpd1.activity_id = '"+activityDO.getId()+"' AND btpd1.sum >= (SELECT SUM(s.total) as total " +
  596. "FROM wlyy_health_bank_task_patient_detail s WHERE s.patient_id ='"+patient+"' AND s.activity_id='"+activityId+"')" +
  597. " limit "+countTotal+",1" ;
  598. List<Map<String,Object>> rstotal9 = jdbcTemplate.queryForList(rankingSql9);
  599. Integer total = 0;
  600. if(rstotal9!=null&&rstotal9.size()>0){
  601. total = Integer.parseInt(rstotal9.get(0).get("total").toString()) ;
  602. }
  603. String totalSql = " SELECT SUM(s.total) as total " +
  604. " FROM wlyy_health_bank_task_patient_detail s WHERE s.patient_id ='"+patient+"' AND s.activity_id='"+activityId+"'";
  605. List<Map<String,Object>> rstotal10 = jdbcTemplate.queryForList(totalSql);
  606. Integer total1 = 0;
  607. if(rstotal10!=null&&rstotal10.size()>0){
  608. total1 = Integer.parseInt( rstotal10.get(0).get("total").toString());
  609. }
  610. //总活动劵的排名
  611. /*String rankingSql2 = "SELECT count(1)+1 AS total FROM (SELECT " +
  612. " SUM(coupon_total) AS sum , " +
  613. " patient_id, " +
  614. " activity_id," +
  615. " create_time " +
  616. "FROM " +
  617. " wlyy_health_bank_task_patient_detail htpd WHERE htpd.activity_id ='"+activityId+"' AND htpd.patient_id !='"+patient+
  618. "' GROUP BY patient_id )btpd1 " +
  619. "WHERE " +
  620. " btpd1.sum >= (SELECT SUM(s.coupon_total) as couponTotal " +
  621. "FROM wlyy_health_bank_task_patient_detail s WHERE s.patient_id ='"+patient+"' AND s.activity_id='"+activityId+"')"+
  622. " AND btpd1.create_time >=(SELECT s.create_time " +
  623. "FROM wlyy_health_bank_task_patient_detail s WHERE s.patient_id ='"+patient+"' AND s.activity_id='"+activityId+"')";
  624. List<Map<String,Object>> rstotal2 = jdbcTemplate.queryForList(rankingSql2);
  625. Long count2 = 0L;
  626. if(rstotal2!=null&&rstotal2.size()>0){
  627. count2 = (Long) rstotal2.get(0).get("total");
  628. }*/
  629. Long countTotal1 = 0L;
  630. if ((count2-2)>=0){
  631. countTotal1=(count2-2);
  632. }
  633. //活动劵前一个活动劵
  634. String rankingSql10 = "SELECT btpd1.sum as total FROM (SELECT " +
  635. " SUM(coupon_total) AS sum , " +
  636. " patient_id, " +
  637. " activity_id "+
  638. "FROM " +
  639. " wlyy_health_bank_task_patient_detail btpd WHERE btpd.activity_id ='"+activityId+"' AND btpd.patient_id !='"+patient+
  640. "' GROUP BY patient_id )btpd1 " +
  641. "WHERE " +
  642. " btpd1.sum >= (SELECT SUM(s.total) as total " +
  643. "FROM wlyy_health_bank_task_patient_detail s WHERE s.patient_id ='"+patient+"' AND s.activity_id='"+activityId+"')" +
  644. " limit "+countTotal1+",1" ;
  645. List<Map<String,Object>> totalSql10 = jdbcTemplate.queryForList(rankingSql10);
  646. Integer total2 = 0;
  647. if(totalSql10!=null&&totalSql10.size()>0){
  648. total2 = Integer.parseInt(totalSql10.get(0).get("total").toString());
  649. }
  650. String totalSql1 = " SELECT SUM(s.coupon_total) as total " +
  651. " FROM wlyy_health_bank_task_patient_detail s WHERE s.patient_id ='"+patient+"' AND s.activity_id='"+activityId+"'";
  652. List<Map<String,Object>> totalSql11 = jdbcTemplate.queryForList(totalSql1);
  653. Integer total3 = 0;
  654. if(totalSql11!=null&&totalSql11.size()>0){
  655. total3 = Integer.parseInt(totalSql11.get(0).get("total").toString());
  656. }
  657. //活动劵
  658. //街道
  659. 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 " +
  660. "FROM wlyy_health_bank_task_patient_detail btpd " +
  661. "WHERE btpd.activity_id = '"+activityId +
  662. "' GROUP BY btpd.patient_id " +
  663. " ) btpd "+
  664. "ORDER BY " +
  665. " 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 ";
  666. List<TaskPatientDetailDO> patientDetailDOS2 = jdbcTemplate.query(rankingSql3,new BeanPropertyRowMapper<>(TaskPatientDetailDO.class));
  667. long count3=0L;
  668. for (TaskPatientDetailDO taskPatientDetailDO:patientDetailDOS2){
  669. String timeSeparated = DateUtils.getDatePoor(new Date(),taskPatientDetailDO.getUpdateTime());
  670. String goodsSql = "select * from wlyy_health_bank_exchange_goods where patient_id ='"+taskPatientDetailDO.getPatientId()+"' " +
  671. "and activity_goods_id in(select id from wlyy_health_bank_activity_goods where activity_id ='"+activityId+"')";
  672. List<ExchangeGoodsDO> exchangeGoodsDOS = jdbcTemplate.query(goodsSql,new BeanPropertyRowMapper<>(ExchangeGoodsDO.class));
  673. for (ExchangeGoodsDO exchangeGoodsDO :exchangeGoodsDOS){
  674. TaskGoodsDO taskGoodsDO = taskGoodsDao.selectById(exchangeGoodsDO.getActivityGoodsId());
  675. exchangeGoodsDO.setTaskGoodsDO(taskGoodsDO);
  676. }
  677. taskPatientDetailDO.setExchangeGoodsDO(exchangeGoodsDOS);
  678. taskPatientDetailDO.setTimeSeparated(timeSeparated);
  679. if (!taskPatientDetailDO.getPatientId().equals(patient)){
  680. count3+=1;
  681. }
  682. }
  683. //区排名
  684. 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 " +
  685. "FROM wlyy_health_bank_task_patient_detail btpd " +
  686. "WHERE btpd.activity_id = '"+activityId +
  687. "' GROUP BY btpd.patient_id " +
  688. " ) btpd "+
  689. "ORDER BY " +
  690. " 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 ";
  691. List<TaskPatientDetailDO> patientDetailDOS3 = jdbcTemplate.query(rankingSql4,new BeanPropertyRowMapper<>(TaskPatientDetailDO.class));
  692. long count4=0L;
  693. for (TaskPatientDetailDO taskPatientDetailDO:patientDetailDOS3){
  694. String timeSeparated = DateUtils.getDatePoor(new Date(),taskPatientDetailDO.getUpdateTime());
  695. String goodsSql = "select * from wlyy_health_bank_exchange_goods where patient_id ='"+taskPatientDetailDO.getPatientId()+"' " +
  696. "and activity_goods_id in(select id from wlyy_health_bank_activity_goods where activity_id ='"+activityId+"')";
  697. List<ExchangeGoodsDO> exchangeGoodsDOS = jdbcTemplate.query(goodsSql,new BeanPropertyRowMapper<>(ExchangeGoodsDO.class));
  698. for (ExchangeGoodsDO exchangeGoodsDO :exchangeGoodsDOS){
  699. TaskGoodsDO taskGoodsDO = taskGoodsDao.selectById(exchangeGoodsDO.getActivityGoodsId());
  700. exchangeGoodsDO.setTaskGoodsDO(taskGoodsDO);
  701. }
  702. taskPatientDetailDO.setExchangeGoodsDO(exchangeGoodsDOS);
  703. taskPatientDetailDO.setTimeSeparated(timeSeparated);
  704. if (!taskPatientDetailDO.getPatientId().equals(patient)){
  705. count4+=1;
  706. }
  707. }
  708. //活动劵排名
  709. //街道
  710. 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 " +
  711. "FROM wlyy_health_bank_task_patient_detail btpd " +
  712. "WHERE btpd.activity_id = '"+activityId +
  713. "' GROUP BY btpd.patient_id " +
  714. " ) btpd " +
  715. "ORDER BY " +
  716. " 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 ";
  717. List<TaskPatientDetailDO> patientDetailDOS4 = jdbcTemplate.query(rankingSql5,new BeanPropertyRowMapper<>(TaskPatientDetailDO.class));
  718. long count5 = 0l;
  719. for (TaskPatientDetailDO taskPatientDetailDO:patientDetailDOS4){
  720. String timeSeparated = DateUtils.getDatePoor(new Date(),taskPatientDetailDO.getUpdateTime());
  721. String goodsSql = "select * from wlyy_health_bank_exchange_goods where patient_id ='"+taskPatientDetailDO.getPatientId()+"' " +
  722. "and activity_goods_id in(select id from wlyy_health_bank_activity_goods where activity_id ='"+activityId+"')";
  723. List<ExchangeGoodsDO> exchangeGoodsDOS = jdbcTemplate.query(goodsSql,new BeanPropertyRowMapper<>(ExchangeGoodsDO.class));
  724. for (ExchangeGoodsDO exchangeGoodsDO :exchangeGoodsDOS){
  725. TaskGoodsDO taskGoodsDO = taskGoodsDao.selectById(exchangeGoodsDO.getActivityGoodsId());
  726. exchangeGoodsDO.setTaskGoodsDO(taskGoodsDO);
  727. }
  728. if (taskPatientDetailDO.getPatientId().equals(taskPatientDetailDO.getPatientId())){
  729. count5+=1;
  730. }
  731. taskPatientDetailDO.setExchangeGoodsDO(exchangeGoodsDOS);
  732. taskPatientDetailDO.setTimeSeparated(timeSeparated);
  733. }
  734. //区排名
  735. 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 " +
  736. "FROM wlyy_health_bank_task_patient_detail btpd " +
  737. "WHERE btpd.activity_id = '"+activityId +
  738. "' GROUP BY btpd.patient_id " +
  739. " ) btpd " +
  740. "ORDER BY " +
  741. " 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 ";
  742. List<TaskPatientDetailDO> patientDetailDOS5 = jdbcTemplate.query(rankingSql6,new BeanPropertyRowMapper<>(TaskPatientDetailDO.class));
  743. long count6 = 0l;
  744. for (TaskPatientDetailDO taskPatientDetailDO:patientDetailDOS5){
  745. String timeSeparated = DateUtils.getDatePoor(new Date(),taskPatientDetailDO.getUpdateTime());
  746. String goodsSql = "select * from wlyy_health_bank_exchange_goods where patient_id ='"+taskPatientDetailDO.getPatientId()+"' " +
  747. "and activity_goods_id in(select id from wlyy_health_bank_activity_goods where activity_id ='"+activityId+"')";
  748. List<ExchangeGoodsDO> exchangeGoodsDOS = jdbcTemplate.query(goodsSql,new BeanPropertyRowMapper<>(ExchangeGoodsDO.class));
  749. for (ExchangeGoodsDO exchangeGoodsDO :exchangeGoodsDOS){
  750. TaskGoodsDO taskGoodsDO = taskGoodsDao.selectById(exchangeGoodsDO.getActivityGoodsId());
  751. exchangeGoodsDO.setTaskGoodsDO(taskGoodsDO);
  752. }
  753. if (taskPatientDetailDO.getPatientId().equals(taskPatientDetailDO.getPatientId())){
  754. count6+=1;
  755. }
  756. taskPatientDetailDO.setExchangeGoodsDO(exchangeGoodsDOS);
  757. taskPatientDetailDO.setTimeSeparated(timeSeparated);
  758. }
  759. //街道排名
  760. /* String rankingSql3 = "SELECT count(1)+1 AS total FROM (SELECT " +
  761. " SUM(btpd.coupon_total) AS sum , " +
  762. " btpd.patient_id, " +
  763. " btpd.activity_id," +
  764. " create_time " +
  765. "FROM " +
  766. " wlyy_health_bank_task_patient_detail btpd WHERE btpd.activity_id ='"+activityId+"' AND btpd.patient_id !='"+patient+
  767. "' GROUP BY patient_id )btpd1 " +
  768. "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 " +
  769. "FROM wlyy_health_bank_task_patient_detail s WHERE s.patient_id ='"+patient+"' AND s.activity_id='"+activityId+"')"+
  770. " AND btpd1.create_time >=(SELECT s.create_time " +
  771. "FROM wlyy_health_bank_task_patient_detail s WHERE s.patient_id ='"+patient+"' AND s.activity_id='"+activityId+"')" ;
  772. List<Map<String,Object>> rstotal3 = jdbcTemplate.queryForList(rankingSql3);
  773. Long count3 = 0L;
  774. if(rstotal3!=null&&rstotal3.size()>0){
  775. count3 = (Long) rstotal3.get(0).get("total");
  776. }*/
  777. //区排名
  778. /* String rankingSql4 = "SELECT count(1)+1 AS total FROM (SELECT " +
  779. " SUM(btpd.coupon_total) AS sum , " +
  780. " btpd.patient_id, " +
  781. " btpd.activity_id," +
  782. " create_time " +
  783. "FROM " +
  784. " wlyy_health_bank_task_patient_detail btpd WHERE btpd.activity_id ='"+activityId+"' AND btpd.patient_id !='"+patient+
  785. "' GROUP BY patient_id )btpd1 " +
  786. "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 " +
  787. "FROM wlyy_health_bank_task_patient_detail s WHERE s.patient_id ='"+patient+"' AND s.activity_id='"+activityId+"')"+
  788. " AND btpd1.create_time >=(SELECT s.create_time " +
  789. "FROM wlyy_health_bank_task_patient_detail s WHERE s.patient_id ='"+patient+"' AND s.activity_id='"+activityId+"')" ;
  790. List<Map<String,Object>> rstotal4 = jdbcTemplate.queryForList(rankingSql4);
  791. Long count4 = 0L;
  792. if(rstotal4!=null&&rstotal4.size()>0){
  793. count4= (Long) rstotal4.get(0).get("total");
  794. }*/
  795. //积分
  796. //街道排名
  797. /*String rankingSql5 = "SELECT count(1)+1 AS total FROM (SELECT " +
  798. " SUM(btpd.total) AS sum , " +
  799. " btpd.patient_id, " +
  800. " btpd.activity_id," +
  801. " create_time " +
  802. "FROM " +
  803. " wlyy_health_bank_task_patient_detail btpd WHERE btpd.activity_id ='"+activityId+"' AND btpd.patient_id !='"+patient+
  804. "' GROUP BY patient_id )btpd1 " +
  805. "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 " +
  806. "FROM wlyy_health_bank_task_patient_detail s WHERE s.patient_id ='"+patient+"' AND s.activity_id='"+activityId+"')"+
  807. " AND btpd1.create_time >=(SELECT s.create_time " +
  808. "FROM wlyy_health_bank_task_patient_detail s WHERE s.patient_id ='"+patient+"' AND s.activity_id='"+activityId+"')" ;
  809. List<Map<String,Object>> rstotal5 = jdbcTemplate.queryForList(rankingSql5);
  810. Long count5 = 0L;
  811. if(rstotal5!=null&&rstotal5.size()>0){
  812. count5 = (Long) rstotal5.get(0).get("total");
  813. }
  814. //区排名
  815. String rankingSql6 = "SELECT count(1)+1 AS total FROM (SELECT " +
  816. " SUM(btpd.total) AS sum , " +
  817. " btpd.patient_id, " +
  818. " btpd.activity_id," +
  819. " create_time " +
  820. "FROM " +
  821. " wlyy_health_bank_task_patient_detail btpd WHERE btpd.activity_id ='"+activityId+"' AND btpd.patient_id !='"+patient+
  822. "' GROUP BY patient_id )btpd1 " +
  823. "WHERE " +
  824. " 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 " +
  825. "FROM wlyy_health_bank_task_patient_detail s WHERE s.patient_id ='"+patient+"' AND s.activity_id='"+activityId+"')"
  826. + " AND btpd1.create_time >=(SELECT s.create_time " +
  827. "FROM wlyy_health_bank_task_patient_detail s WHERE s.patient_id ='"+patient+"' AND s.activity_id='"+activityId+"')" ;
  828. List<Map<String,Object>> rstotal6 = jdbcTemplate.queryForList(rankingSql6);
  829. Long count6 = 0L;
  830. if(rstotal6!=null&&rstotal6.size()>0){
  831. count6= (Long) rstotal6.get(0).get("total");
  832. }
  833. */
  834. //团队排名
  835. String rankingSql7 = "SELECT count(1)+1 AS total FROM (SELECT " +
  836. " SUM(btpd.total) AS sum , " +
  837. " btpd.patient_id, " +
  838. " btpd.activity_id," +
  839. " create_time " +
  840. "FROM " +
  841. " wlyy_health_bank_task_patient_detail btpd WHERE btpd.activity_id ='"+activityId+"' AND btpd.patient_id !='"+patient+
  842. "' GROUP BY patient_id )btpd1 " +
  843. "WHERE btpd1.patient_id IN(SELECT sf.patient FROM wlyy.wlyy_sign_family sf WHERE sf.status=1 " +
  844. "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 " +
  845. "FROM wlyy_health_bank_task_patient_detail s WHERE s.patient_id ='"+patient+"' AND s.activity_id='"+activityId+"')"+
  846. " AND btpd1.create_time >=(SELECT s.create_time " +
  847. "FROM wlyy_health_bank_task_patient_detail s WHERE s.patient_id ='"+patient+"' AND s.activity_id='"+activityId+"')" ;
  848. List<Map<String,Object>> rstotal7 = jdbcTemplate.queryForList(rankingSql7);
  849. Long count7 = 0L;
  850. if(rstotal7!=null&&rstotal7.size()>0){
  851. count7 = (Long) rstotal7.get(0).get("total");
  852. }
  853. //区排名
  854. String rankingSql8 = "SELECT count(1)+1 AS total FROM (SELECT " +
  855. " SUM(btpd.total) AS sum , " +
  856. " btpd.patient_id, " +
  857. " btpd.activity_id," +
  858. " create_time " +
  859. "FROM " +
  860. " wlyy_health_bank_task_patient_detail btpd WHERE btpd.activity_id ='"+activityId+"' AND btpd.patient_id !='"+patient+
  861. "' GROUP BY patient_id )btpd1 " +
  862. "WHERE btpd1.patient_id IN(SELECT sf.patient FROM wlyy.wlyy_sign_family sf WHERE sf.status=1 " +
  863. "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 " +
  864. "FROM wlyy_health_bank_task_patient_detail s WHERE s.patient_id ='"+patient+"' AND s.activity_id='"+activityId+"')" +
  865. " AND btpd1.create_time >=(SELECT s.create_time " +
  866. "FROM wlyy_health_bank_task_patient_detail s WHERE s.patient_id ='"+patient+"' AND s.activity_id='"+activityId+"')" ;
  867. List<Map<String,Object>> rstotal8 = jdbcTemplate.queryForList(rankingSql8);
  868. Long count8 = 0L;
  869. if(rstotal8!=null&&rstotal8.size()>0){
  870. count8 = (Long) rstotal8.get(0).get("total");
  871. }
  872. /* //活动劵
  873. String rankingSql4 = "SELECT count(1)+1 AS total FROM (SELECT " +
  874. " SUM(coupon_total) AS sum , " +
  875. " patient_id, " +
  876. " activity_id " +
  877. "FROM " +
  878. " wlyy_health_bank_task_patient_detail " +
  879. "GROUP BY patient_id )btpd1 " +
  880. "WHERE " +
  881. " btpd1.activity_id = '"+activityDO.getId()+"'"+buffer+" AND btpd1.sum >= (SELECT SUM(s.coupon_total) as couponTotal " +
  882. "FROM wlyy_health_bank_task_patient_detail s WHERE s.patient_id ='"+patient+"' AND s.activity_id='"+activityId+"')" ;
  883. List<Map<String,Object>> rstotal4 = jdbcTemplate.queryForList(rankingSql4);
  884. Long count4 = 0L;
  885. if(rstotal4!=null&&rstotal4.size()>0){
  886. count4= (Long) rstotal4.get(0).get("total");
  887. }
  888. //积分
  889. String rankingSql5 = "SELECT count(1)+1 AS total FROM (SELECT " +
  890. " SUM(total) AS sum , " +
  891. " patient_id, " +
  892. " activity_id " +
  893. "FROM " +
  894. " wlyy_health_bank_task_patient_detail " +
  895. "GROUP BY patient_id )btpd1 " +
  896. "WHERE " +
  897. " btpd1.activity_id = '"+activityDO.getId()+"'"+buffer+" AND btpd1.sum >= (SELECT SUM(s.total) as total " +
  898. "FROM wlyy_health_bank_task_patient_detail s WHERE s.patient_id ='"+patient+"' AND s.activity_id='"+activityId+"')" ;
  899. List<Map<String,Object>> rstotal5 = jdbcTemplate.queryForList(rankingSql5);
  900. Long count5 = 0L;
  901. if(rstotal5!=null&&rstotal5.size()>0){
  902. count5 = (Long) rstotal5.get(0).get("total");
  903. }
  904. String rankingSql6 = "SELECT count(1)+1 AS total FROM (SELECT " +
  905. " SUM(total) AS sum , " +
  906. " patient_id, " +
  907. " activity_id " +
  908. "FROM " +
  909. " wlyy_health_bank_task_patient_detail " +
  910. "GROUP BY patient_id )btpd1 " +
  911. "WHERE " +
  912. " btpd1.activity_id = '"+activityDO.getId()+"'"+buffer+" AND btpd1.sum >= (SELECT SUM(s.total) as total " +
  913. "FROM wlyy_health_bank_task_patient_detail s WHERE s.patient_id ='"+patient+"' AND s.activity_id='"+activityId+"')" ;
  914. List<Map<String,Object>> rstotal6 = jdbcTemplate.queryForList(rankingSql6);
  915. Long count6 = 0L;
  916. if(rstotal6!=null&&rstotal6.size()>0){
  917. count6= (Long) rstotal6.get(0).get("total");
  918. }*/
  919. ActivityRuleDO activityRuleDO = activityRuleDao.selectByActivityId(activityId);
  920. JSONObject object = JSONObject.parseObject(activityRuleDO.getValue1());
  921. Integer type = object.getInteger("type");
  922. if (type == 2){
  923. jsonObject.put("activityTownCouponRanking",count6);//活动劵全区排名
  924. jsonObject.put("activityStreetCouponRanking",count5);//活动劵街道排名
  925. jsonObject.put("activityCouponRanking",count2);//活动劵的排名
  926. jsonObject.put("couponTotal",taskPatientDetailDO1.getCouponTotal());//本人活动劵总数
  927. jsonObject.put("couponRanking",patientDetailDOS1);//活动劵人数排名
  928. jsonObject.put("signCouponRanking",count7);//活动劵团队排名
  929. if (count2 ==1){
  930. jsonObject.put("differTotal",0);
  931. }else {
  932. jsonObject.put("differTotal",total2-total3);//相距前一个差多少活动劵
  933. }
  934. }else if (type ==1){
  935. jsonObject.put("IntegrateRanking",patientDetailDOS);
  936. jsonObject.put("activityStreetIntegrateRanking",count3);//积分街道排名
  937. jsonObject.put("total",taskPatientDetailDO1.getTotal());//本人总积分
  938. jsonObject.put("activityTownIntegrateRanking",count4);//积分全区排名
  939. jsonObject.put("activityRanking",count);//积分排名
  940. jsonObject.put("signRanking",count8);//积分团队排名
  941. if (count==1){
  942. jsonObject.put("differTotal",0);//相距前一个差多少积分
  943. }else {
  944. jsonObject.put("differTotal",total-total1);//相距前一个差多少积分
  945. }
  946. }
  947. jsonObject.put("type",type);//积分还是活动劵
  948. envelop.setObj(jsonObject);
  949. return envelop;
  950. }
  951. public JSONObject findActivityByType(String type){
  952. JSONObject object=new JSONObject();
  953. List<String> types = Arrays.asList(type.split(","));
  954. for(String str:types){
  955. List<ActivityDO> lst= activityDao.findByIdAndType(str);
  956. if(null!=lst&&lst.size()>0){
  957. object.put(str,true);
  958. }else {
  959. object.put(str,false);
  960. }
  961. }
  962. return object;
  963. }
  964. public JSONObject ListActivityDO(String type, String status,String crowdType,String releaseTime,String activityOfflineTime, String filter,Integer page,Integer size)throws ParseException {
  965. JSONObject obj=new JSONObject();
  966. StringBuffer stringBuffer=new StringBuffer();
  967. if (StringUtils.isNotEmpty(type)) {
  968. stringBuffer.append(" and type='").append(type).append("'");
  969. }
  970. if (StringUtils.isNotEmpty(filter)) {
  971. stringBuffer.append(" and (title like '%").append(filter).append("%' or ").append("organizer like '%").append(filter).append("%' or ").append("area_name like '%").append(filter).append("%')");
  972. }
  973. if (null != status && "2".equals(status) ) {
  974. stringBuffer.append(" and status in (-1,2) ");
  975. } else if (null != status && "1".equals(status) ) {
  976. stringBuffer.append(" and status in (0,1) ");
  977. }
  978. if(null!=crowdType&&StringUtils.isNotBlank(crowdType)){
  979. stringBuffer.append(" and crowd_type="+crowdType);
  980. }
  981. if(StringUtils.isNotBlank(releaseTime)){
  982. stringBuffer.append(" and activity_offline_time>='"+releaseTime+" 00:00:00' ");
  983. }
  984. if(StringUtils.isNotBlank(activityOfflineTime)){
  985. stringBuffer.append(" and activity_offline_time<='"+ activityOfflineTime+" 59:59:59' ");
  986. }
  987. stringBuffer.append(" order by create_time desc");
  988. 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;
  989. List<ActivityDO> activityDOList = jdbcTemplate.query(sql,new BeanPropertyRowMapper(ActivityDO.class));
  990. String sqlcount = "SELECT COUNT(1) AS total FROM wlyy_health_bank.wlyy_health_bank_activity WHERE 1=1 AND del_flag=1 "+stringBuffer.toString();
  991. List<Map<String,Object>> rstotal = jdbcTemplate.queryForList(sqlcount);
  992. Long count = 0L;
  993. if(rstotal!=null&&rstotal.size()>0){
  994. count = (Long) rstotal.get(0).get("total");
  995. }
  996. obj .put("activityDOList",activityDOList);
  997. obj .put("count",count);
  998. return obj;
  999. }
  1000. }