ActivityController.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. package com.yihu.jw.controller;/**
  2. * Created by nature of king on 2018/4/27.
  3. */
  4. import com.alibaba.fastjson.JSONArray;
  5. import com.alibaba.fastjson.JSONObject;
  6. import com.yihu.jw.entity.health.bank.ActivityDO;
  7. import com.yihu.jw.restmodel.web.MixEnvelop;
  8. import com.yihu.jw.restmodel.web.ObjEnvelop;
  9. import com.yihu.jw.restmodel.web.PageEnvelop;
  10. import com.yihu.jw.restmodel.web.endpoint.EnvelopRestEndpoint;
  11. import com.yihu.jw.rm.health.bank.HealthBankMapping;
  12. import com.yihu.jw.service.ActivityService;
  13. import com.yihu.jw.util.date.DateUtil;
  14. import io.swagger.annotations.Api;
  15. import io.swagger.annotations.ApiOperation;
  16. import io.swagger.annotations.ApiParam;
  17. import org.apache.commons.lang3.StringUtils;
  18. import org.springframework.beans.factory.annotation.Autowired;
  19. import org.springframework.cloud.sleuth.Tracer;
  20. import org.springframework.web.bind.annotation.*;
  21. import java.text.SimpleDateFormat;
  22. import java.util.ArrayList;
  23. import java.util.Date;
  24. import java.util.List;
  25. /**
  26. * @author wangzhinan
  27. * @create 2018-04-27 14:14
  28. * @desc 健康活动
  29. **/
  30. @RestController
  31. @RequestMapping(HealthBankMapping.api_health_bank_common)
  32. @Api(tags = "健康活动相关操作",description = "健康活动相关操作")
  33. public class ActivityController extends EnvelopRestEndpoint {
  34. @Autowired
  35. private ActivityService service;
  36. @Autowired
  37. private Tracer tracer;
  38. /**
  39. * publish activity
  40. *
  41. * @param activity 活动对象
  42. * @return
  43. */
  44. @PostMapping(value = HealthBankMapping.healthBank.createActivity)
  45. @ApiOperation(value = "发布活动")
  46. public MixEnvelop<Boolean, Boolean> publishActivity(@ApiParam(name = "activity",value = "健康活动JSON")
  47. @RequestParam(value = "activity",required = true)String activity,
  48. @ApiParam(name = "value1",value = "积分JSON")
  49. @RequestParam(value = "value1",required = false)String value1,
  50. @ApiParam(name = "value2",value = "时间规则")
  51. @RequestParam(value = "value2",required = false)String value2,
  52. @ApiParam(name = "value3",value = "兑奖规则")
  53. @RequestParam(value = "value3",required = false)String value3){
  54. try {
  55. ActivityDO activityDO = toEntity(activity,ActivityDO.class);
  56. return service.insert(activityDO,value1,value2,value3);
  57. }catch (Exception e){
  58. e.printStackTrace();
  59. tracer.getCurrentSpan().logEvent(e.getMessage());
  60. return MixEnvelop.getError(e.getMessage());
  61. }
  62. }
  63. /**
  64. * find health activity
  65. *
  66. * @param activity 活动对象
  67. * @param page 页码
  68. * @param size 分页大小
  69. * @return
  70. */
  71. @PostMapping(value = HealthBankMapping.healthBank.findActivity)
  72. @ApiOperation(value = "查看健康活动")
  73. public MixEnvelop<ActivityDO, ActivityDO> getActivity(@ApiParam(name = "activity",value = "健康活动JSON")
  74. @RequestParam(value = "activity",required = false)String activity,
  75. @ApiParam(name = "page", value = "第几页,从1开始")
  76. @RequestParam(value = "page", defaultValue = "1",required = false)Integer page,
  77. @ApiParam(name = "size",defaultValue = "10",value = ",每页分页大小")
  78. @RequestParam(value = "size", required = false)Integer size){
  79. try{
  80. ActivityDO activityDO = toEntity(activity,ActivityDO.class);
  81. return service.findByCondition(activityDO,page,size);
  82. }catch (Exception e){
  83. e.printStackTrace();
  84. tracer.getCurrentSpan().logEvent(e.getMessage());
  85. return MixEnvelop.getError(e.getMessage());
  86. }
  87. }
  88. /**
  89. * out activity
  90. *
  91. * @param activity 活动对象
  92. * @return
  93. */
  94. @PostMapping(value = HealthBankMapping.healthBank.updateActivity)
  95. @ApiOperation(value = "编辑活动")
  96. public MixEnvelop<Boolean, Boolean> outActivity(@ApiParam(name = "activity", value = "健康活动JSON")
  97. @RequestParam(value = "activity", required = true) String activity,
  98. @ApiParam(name = "value1", value = "活动币JSON")
  99. @RequestParam(value = "value1", required = false) String value1,
  100. @ApiParam(name = "value2", value = "活动时间")
  101. @RequestParam(value = "value2", required = false) String value2,
  102. @ApiParam(name = "value3", value = "兑奖设置")
  103. @RequestParam(value = "value3", required = false) String value3) {
  104. try {
  105. ActivityDO activityDO = toEntity(activity, ActivityDO.class);
  106. return service.update(activityDO,value1,value2,value3);
  107. } catch (Exception e) {
  108. e.printStackTrace();
  109. tracer.getCurrentSpan().logEvent(e.getMessage());
  110. return MixEnvelop.getError(e.getMessage());
  111. }
  112. }
  113. /**
  114. * 查看参与的活动
  115. *
  116. * @param activity 活动对象
  117. * @param page 页码
  118. * @param size 分页大小
  119. * @return
  120. */
  121. @PostMapping(value = HealthBankMapping.healthBank.selectByPatient)
  122. @ApiOperation(value = "参与的活动")
  123. public MixEnvelop<ActivityDO, ActivityDO> selectByPatient(@ApiParam(name = "activity",value = "健康活动JSON")
  124. @RequestParam(value = "activity",required = false)String activity,
  125. @ApiParam(name = "page", value = "第几页,从1开始")
  126. @RequestParam(value = "page", defaultValue = "1",required = false)Integer page,
  127. @ApiParam(name = "size",defaultValue = "10",value = ",每页分页大小")
  128. @RequestParam(value = "size", required = false)Integer size){
  129. try{
  130. ActivityDO activityDO = toEntity(activity,ActivityDO.class);
  131. return service.selectByPatient(activityDO,page,size);
  132. }catch (Exception e){
  133. e.printStackTrace();
  134. tracer.getCurrentSpan().logEvent(e.getMessage());
  135. return MixEnvelop.getError(e.getMessage());
  136. }
  137. }
  138. /**
  139. * 批量删除数据
  140. *
  141. * @param ids id集合[""]
  142. * @return
  143. */
  144. @PostMapping(value = HealthBankMapping.healthBank.batchActivity)
  145. @ApiOperation(value = "批量删除活动")
  146. public MixEnvelop<Boolean, Boolean> batchDelete(@ApiParam(name="ids",value = "id集合")
  147. @RequestParam(value = "ids",required = false)String ids){
  148. try{
  149. MixEnvelop<Boolean, Boolean> envelop = new MixEnvelop<>();
  150. JSONArray array = JSONArray.parseArray(ids);
  151. List<String> activityIds = new ArrayList<>();
  152. for (int i = 0;i<array.size();i++){
  153. activityIds.add(array.getString(i));
  154. }
  155. service.batchDelete(activityIds);
  156. envelop.setObj(true);
  157. return envelop;
  158. }catch (Exception e){
  159. e.printStackTrace();
  160. tracer.getCurrentSpan().logEvent(e.getMessage());
  161. return MixEnvelop.getError(e.getMessage());
  162. }
  163. }
  164. /**
  165. * 删除数据
  166. *
  167. * @param id 活动id
  168. * @return
  169. */
  170. @PostMapping(value = HealthBankMapping.healthBank.daleteActivity)
  171. @ApiOperation(value = "删除活动")
  172. public MixEnvelop<Boolean, Boolean> daleteActivity(@ApiParam(name="id",value = "id集合")
  173. @RequestParam(value = "id",required = false)String id){
  174. try{
  175. MixEnvelop<Boolean, Boolean> envelop = new MixEnvelop<>();
  176. service.daleteActivity(id);
  177. envelop.setObj(true);
  178. return envelop;
  179. }catch (Exception e){
  180. e.printStackTrace();
  181. tracer.getCurrentSpan().logEvent(e.getMessage());
  182. return MixEnvelop.getError(e.getMessage());
  183. }
  184. }
  185. /**
  186. * 编辑活动:上下线、推荐
  187. *
  188. * @param id
  189. * @param type
  190. * @return
  191. */
  192. @PostMapping(value = HealthBankMapping.healthBank.editActivity)
  193. @ApiOperation(value = "编辑活动:上下线、推荐")
  194. public ObjEnvelop<ActivityDO> editActivity(
  195. @ApiParam(name = "id", value = "活动id")
  196. @RequestParam(value = "id", required = true) String id,
  197. @ApiParam(name = "type", value = "操作类型:上线:0,下线:2,推荐:3,取消推荐:4")
  198. @RequestParam(value = "type", required = true) String type) {
  199. try {
  200. ActivityDO activityDO=service.editActivity(id, type);
  201. return ObjEnvelop.getSuccess("操作成功!",activityDO);
  202. } catch (Exception e) {
  203. e.printStackTrace();
  204. tracer.getCurrentSpan().logEvent(e.getMessage());
  205. return ObjEnvelop.getError(e.getMessage());
  206. }
  207. }
  208. /**
  209. * 编辑活动:上下线、推荐
  210. *
  211. * @param id
  212. * @return
  213. */
  214. @PostMapping(value = HealthBankMapping.healthBank.findActivityById)
  215. @ApiOperation(value = "根据id获取活动详情+活动规则")
  216. public ObjEnvelop<JSONObject> findActivityById(
  217. @ApiParam(name = "id", value = "活动id")
  218. @RequestParam(value = "id", required = true) String id) {
  219. try {
  220. JSONObject activityDO=service.findActivityById(id);
  221. return ObjEnvelop.getSuccess("获取成功!",activityDO);
  222. } catch (Exception e) {
  223. e.printStackTrace();
  224. tracer.getCurrentSpan().logEvent(e.getMessage());
  225. return ObjEnvelop.getError("获取失败!"+e.getMessage());
  226. }
  227. }
  228. @PostMapping(value = HealthBankMapping.healthBank.pageActivity)
  229. @ApiOperation(value = "分页查询活动")
  230. public PageEnvelop<ActivityDO> pageActivity(@ApiParam(name = "status", value = "活动状态:1上线,2下线")
  231. @RequestParam(value = "status", required = false) String status,
  232. @ApiParam(name = "crowdType", value = "人群类型:1:签约居民、2平台用户")
  233. @RequestParam(value = "crowdType", required = false) String crowdType,
  234. @ApiParam(name = "releaseTime", value = "下线开始时间")
  235. @RequestParam(value = "releaseTime", required = false) String releaseTime,
  236. @ApiParam(name = "activityOfflineTime", value = "下线结束时间")
  237. @RequestParam(value = "activityOfflineTime", required = false) String activityOfflineTime,
  238. @ApiParam(name = "filter", value = "活动名、发布机构、面向范围名称")
  239. @RequestParam(value = "filter", required = false) String filter,
  240. @ApiParam(name = "page", value = "第几页,从1开始")
  241. @RequestParam(value = "page", defaultValue = "1", required = false) Integer page,
  242. @ApiParam(name = "size", defaultValue = "10", value = ",每页分页大小")
  243. @RequestParam(value = "size", required = false) Integer size) {
  244. try{
  245. StringBuffer stringBuffer=new StringBuffer();
  246. stringBuffer.append("delFlag=1;");
  247. if (StringUtils.isNotEmpty(filter)) {
  248. stringBuffer.append("title?").append(filter).append(" g1;").append("organizer?").append(filter).append(" g1;").append("areaName?").append(filter).append(" g1;");
  249. }
  250. if (null != status && "2".equals(status) ) {
  251. stringBuffer.append("status=2;");
  252. } else if (null != status && "1".equals(status) ) {
  253. stringBuffer.append("status<2;");
  254. }
  255. if(null!=crowdType&&StringUtils.isNotBlank(crowdType)){
  256. stringBuffer.append("crowdType="+crowdType+";");
  257. }
  258. if(StringUtils.isNotBlank(releaseTime)){
  259. stringBuffer.append("activityOfflineTime>="+releaseTime+" 00:00:00;");
  260. }
  261. if(StringUtils.isNotBlank(activityOfflineTime)){
  262. stringBuffer.append("activityOfflineTime<="+ activityOfflineTime+" 59:59:59;");
  263. }
  264. String sorts="-createTime";
  265. List<ActivityDO> activityDOList = service.search("", stringBuffer.toString(), sorts, page, size);
  266. int count = (int) service.getCount(stringBuffer.toString());
  267. return success(activityDOList, count, page, size);
  268. }catch (Exception e){
  269. e.printStackTrace();
  270. tracer.getCurrentSpan().logEvent(e.getMessage());
  271. return PageEnvelop.getError("获取失败"+e.getMessage(),-1);
  272. }
  273. }
  274. }