ActivityController.java 13 KB

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