ActivityService.java 73 KB

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