AdminMessageController.java 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. package com.goafanti.message.controller;
  2. import java.text.ParseException;
  3. import java.util.Date;
  4. import java.util.HashMap;
  5. import java.util.Map;
  6. import java.util.regex.Pattern;
  7. import javax.annotation.Resource;
  8. import org.apache.commons.lang3.StringUtils;
  9. import org.springframework.web.bind.annotation.RequestMapping;
  10. import org.springframework.web.bind.annotation.RequestMethod;
  11. import org.springframework.web.bind.annotation.RestController;
  12. import com.alibaba.fastjson.JSONObject;
  13. import com.goafanti.common.bo.Result;
  14. import com.goafanti.common.constant.AFTConstants;
  15. import com.goafanti.common.constant.ErrorConstants;
  16. import com.goafanti.common.controller.CertifyApiController;
  17. import com.goafanti.common.error.BusinessException;
  18. import com.goafanti.common.model.JtMessageProducer;
  19. import com.goafanti.common.model.MessageFromSystem;
  20. import com.goafanti.common.utils.DateUtils;
  21. import com.goafanti.core.shiro.token.TokenManager;
  22. import com.goafanti.message.bo.MessageListBo;
  23. import com.goafanti.message.enums.ConsumerType;
  24. import com.goafanti.message.enums.RecommendType;
  25. import com.goafanti.message.enums.SubjectType;
  26. import com.goafanti.message.service.JtMessageProducerService;
  27. import com.goafanti.message.service.MessageService;
  28. /**
  29. * 消息推送
  30. * @author Administrator
  31. *
  32. */
  33. @RestController
  34. @RequestMapping(value = "/api/admin/message")
  35. public class AdminMessageController extends CertifyApiController{
  36. @Resource
  37. private MessageService messageService;
  38. @Resource
  39. private JtMessageProducerService jtMessageProducerService;
  40. /**
  41. * 进入创建系统消息
  42. */
  43. @RequestMapping(value = "/toCreateSystemMessage", method = RequestMethod.GET)
  44. public Result toCreateSystemMessage(){
  45. Result res = new Result();
  46. JSONObject jsonObject = new JSONObject();
  47. jsonObject.put("createTime", DateUtils.formatDate(new Date(), AFTConstants.YYYYMMDDHHMMSS));
  48. res.setData(jsonObject);
  49. return res;
  50. }
  51. /**
  52. *
  53. * @param companyIds 公司集
  54. * @param userIds 用户集
  55. * @param adminIds 运营人员
  56. * @param societyTags 社会属性集
  57. * @param provinceIds 省份集
  58. * @param cityIds 城市集
  59. * @param areas 区域集
  60. * @param roles 角色集
  61. * @param industryIds 行业
  62. * @param subject 0-运营消息 , 1-客户消息 , 2-技淘推荐
  63. * @param isDraft 是否草稿
  64. * @throws ParseException
  65. */
  66. @RequestMapping(value = "/createSystemMessage", method = RequestMethod.POST)
  67. public Result createSystemMessage(String personalUserIds,String organizationUserIds, String adminIds,String companyIds,String societyTags,String provinceIds,String cityIds,String areaIds,
  68. String roles,String industryIds,String title,String body,Integer subject,Integer targetType,String createTime,Integer isDraft) throws ParseException{
  69. Result res = new Result();
  70. if(null == targetType){
  71. res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"", "必须指定人员类型"));
  72. return res;
  73. }
  74. if(null == subject){
  75. res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"", "必须指定消息类型"));
  76. return res;
  77. }
  78. if(StringUtils.isBlank(title) || StringUtils.isBlank(body)){
  79. res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"", "消息是的标题和内容不能为空"));
  80. return res;
  81. }
  82. if(null == isDraft){
  83. res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"", "必须说明是否为草稿"));
  84. return res;
  85. }
  86. checkParam(personalUserIds,organizationUserIds,adminIds,companyIds,societyTags,provinceIds,cityIds,areaIds,roles,industryIds);
  87. Map<Integer,String> targetMap = new HashMap<Integer, String>();
  88. if(targetType == ConsumerType.PERSON_CUSTOMER_RETAIL.getTypeCode()) targetMap.put(targetType, personalUserIds);
  89. if(targetType == ConsumerType.ORGANIZATION_CUSTOMER_RETAIL.getTypeCode()) targetMap.put(targetType, organizationUserIds);
  90. if(targetType == ConsumerType.USER_RETAIL.getTypeCode()) targetMap.put(targetType, adminIds);
  91. if(targetType == ConsumerType.SOCIETY_TAG.getTypeCode()) targetMap.put(targetType, societyTags);
  92. if(targetType == ConsumerType.LOCATION.getTypeCode()){
  93. targetMap.put(targetType, ConsumerType.LOCATION.getTypeName());
  94. targetMap.put(ConsumerType.LOCATION_PROVINCE.getTypeCode(), provinceIds);
  95. targetMap.put(ConsumerType.LOCATION_CITY.getTypeCode(), cityIds);
  96. targetMap.put(ConsumerType.LOCATION_AREA.getTypeCode(), areaIds);
  97. }
  98. if(targetType == ConsumerType.COMPANY.getTypeCode()) targetMap.put(targetType, companyIds);
  99. if(targetType == ConsumerType.ROLE.getTypeCode()) targetMap.put(targetType, roles);
  100. if(targetType == ConsumerType.INDUSTRY.getTypeCode()) targetMap.put(targetType, industryIds);
  101. if(StringUtils.isBlank(targetMap.get(targetType))){
  102. res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"", "必须选定目标群体"));
  103. return res;
  104. }
  105. messageService.insertSystemMessage(targetMap,targetType, title, body, subject,
  106. DateUtils.parseDate(createTime,AFTConstants.YYYYMMDDHHMMSS),isDraft);
  107. return res;
  108. }
  109. /**
  110. *
  111. * @param projectIds 项目
  112. * @param personalAchievementIds 个人成果
  113. * @param organizationAchievementIds 组织成果
  114. * @param personalDemandIds 个人需求
  115. * @param organizationDemandIds 组织需求
  116. * @param userIds 专家
  117. * @param newsIds 政策
  118. * @param sourceType 资源类型
  119. * @param userIds 用户类型
  120. * @param societyTags 社会属性
  121. * @param provinceIds 省
  122. * @param cityIds 市
  123. * @param areaIds 区
  124. * @param targetType 目标类型
  125. * @param isDraft 是否草稿
  126. * @return
  127. */
  128. @RequestMapping(value = "/createRecommendMessage", method = RequestMethod.POST)
  129. public Result createRecommendMessage(String projectIds,String personalAchievementIds,String organizationAchievementIds,
  130. String personalDemandIds,String organizationDemandIds, String expertIds, String newsIds,Integer sourceType,
  131. String personalUserIds,String organizationUserIds,String societyTags,String provinceIds,String cityIds,String areaIds,String industryIds,Integer targetType,Integer isDraft){
  132. Result res = new Result();
  133. if(null == sourceType){
  134. res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"", "必须指定推荐类型"));
  135. return res;
  136. }
  137. if(null == targetType){
  138. res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"", "必须指定目标群体"));
  139. return res;
  140. }
  141. if(null == isDraft){
  142. res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"", "必须说明是否为草稿"));
  143. return res;
  144. }
  145. checkParam(projectIds,projectIds,personalAchievementIds,organizationAchievementIds,personalDemandIds,organizationDemandIds,
  146. expertIds,newsIds,personalUserIds,organizationUserIds,provinceIds,cityIds,areaIds,industryIds);
  147. Map<Integer,String> sourceMap = new HashMap<Integer, String>();
  148. if(sourceType == RecommendType.TECH_SERVICE.getTypeCode()) sourceMap.put(sourceType, projectIds);
  149. if(sourceType == RecommendType.PERSON_ACHIEVEMENT.getTypeCode()) sourceMap.put(sourceType, personalAchievementIds);
  150. if(sourceType == RecommendType.ORGANIZATION_ACHIEVEMENT.getTypeCode()) sourceMap.put(sourceType, organizationAchievementIds);
  151. if(sourceType == RecommendType.PERSONAL_DEMAND.getTypeCode()) sourceMap.put(sourceType, personalDemandIds);
  152. if(sourceType == RecommendType.ORGANIZATION_DEMAND.getTypeCode()) sourceMap.put(sourceType, organizationDemandIds);
  153. if(sourceType == RecommendType.EXPERT.getTypeCode()) sourceMap.put(sourceType, expertIds);
  154. if(sourceType == RecommendType.NEWS.getTypeCode()) sourceMap.put(sourceType, newsIds);
  155. if(StringUtils.isBlank(sourceMap.get(sourceType))){
  156. res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"", "必须选定推荐内容"));
  157. return res;
  158. }
  159. Map<Integer,String> targetMap = new HashMap<Integer, String>();
  160. if(targetType == ConsumerType.PERSON_CUSTOMER_RETAIL.getTypeCode()) targetMap.put(targetType, personalUserIds);
  161. if(targetType == ConsumerType.ORGANIZATION_CUSTOMER_RETAIL.getTypeCode()) targetMap.put(targetType, organizationUserIds);
  162. if(targetType == ConsumerType.SOCIETY_TAG.getTypeCode()) targetMap.put(targetType, societyTags);
  163. if(targetType == ConsumerType.LOCATION.getTypeCode()){
  164. targetMap.put(targetType, ConsumerType.LOCATION.getTypeName());
  165. targetMap.put(ConsumerType.LOCATION_PROVINCE.getTypeCode(), provinceIds);
  166. targetMap.put(ConsumerType.LOCATION_CITY.getTypeCode(), cityIds);
  167. targetMap.put(ConsumerType.LOCATION_AREA.getTypeCode(), areaIds);
  168. }
  169. if(targetType == ConsumerType.INDUSTRY.getTypeCode()) targetMap.put(targetType, industryIds);
  170. if(StringUtils.isBlank(targetMap.get(targetType))){
  171. res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"", "必须指定目标群体"));
  172. return res;
  173. }
  174. messageService.insertRecommendMessage(sourceMap,sourceType,targetMap,targetType,SubjectType.TUI_JIAN.getTypeCode(),isDraft);
  175. return res;
  176. }
  177. /**
  178. * 查看消息列表
  179. * @param isDraft 是否未草稿
  180. * @param subject 消息类型
  181. * @return
  182. */
  183. @RequestMapping(value = "/listSystemMessage", method = RequestMethod.GET)
  184. public Result listSystemMessage(Integer isDraft,Integer subject,Integer page,Integer pageNo,Integer pageSize){
  185. Result res = new Result();
  186. if(null == isDraft){
  187. res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"", "消息发送状态不能为空"));
  188. return res;
  189. }
  190. /*if(null == subject){
  191. res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR, "消息类型不能为空"));
  192. return res;
  193. }*/
  194. res.setData(messageService.listSystemMessage(isDraft, subject,pageNo,pageSize));
  195. return res;
  196. }
  197. /**
  198. * 更新消息
  199. * @param messageId 消息ID
  200. * @param title 消息标题
  201. * @param body 消息内容
  202. * @return
  203. */
  204. @RequestMapping(value = "/updateSystemMessage", method = RequestMethod.POST)
  205. public Result updateSystemMessage(String messageId,String title,String body,Integer isDraft){
  206. Result res = new Result();
  207. if(StringUtils.isBlank(messageId)){
  208. res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"", "消息编号不能为空"));
  209. return res;
  210. }
  211. if(null == isDraft){
  212. res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"", "消息发送状态不能为空"));
  213. return res;
  214. }
  215. if(StringUtils.isBlank(title) || StringUtils.isBlank(body)){
  216. res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"", "消息标题和消息内容不能为空"));
  217. return res;
  218. }
  219. messageService.updateSystemMessage(messageId, title, body, isDraft);
  220. return res;
  221. }
  222. /**
  223. * 删除消息
  224. * @param messageId 消息ID
  225. * @return
  226. */
  227. @RequestMapping(value = "/deleteSystemMessage", method = RequestMethod.GET)
  228. public Result deleteSystemMessage(String messageId){
  229. Result res = new Result();
  230. if(StringUtils.isBlank(messageId)){
  231. res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"", "消息编号为空"));
  232. return res;
  233. }
  234. messageService.deleteSystemMessage(messageId);
  235. return res;
  236. }
  237. /**
  238. * 读取消息详情
  239. * @param messageId 消息ID
  240. * @return
  241. */
  242. @RequestMapping(value = "/getSystemMessageDetail", method = RequestMethod.GET)
  243. public Result getSystemMessageDetail(String messageId){
  244. Result res = new Result();
  245. if(StringUtils.isBlank(messageId)){
  246. res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"", "消息编号为空"));
  247. return res;
  248. }
  249. // 读取消息详情
  250. MessageFromSystem mfs = messageService.selectSystemMessageDetail(messageId);
  251. MessageListBo bo = new MessageListBo();
  252. bo.setCreateTime(mfs.getCreateTime() == null ? ""
  253. : DateUtils.formatDate(mfs.getCreateTime(), AFTConstants.YYYYMMDDHHMMSS));
  254. bo.setResourceType(String.valueOf(mfs.getResourceType()));
  255. bo.setSubject(String.valueOf(mfs.getSubject()));
  256. bo.setMessageId(mfs.getId());
  257. bo.setTitle(mfs.getTitle());
  258. bo.setBody(mfs.getBody());
  259. bo.setResourceId(mfs.getResourceId());
  260. bo.setConsumerType(""+mfs.getConsumerType());
  261. res.setData(bo);
  262. return res;
  263. }
  264. /**
  265. * 校验
  266. * @param ids
  267. */
  268. public void checkParam(String... ids){
  269. String regex = "^[0-9a-z-,]*$";
  270. for(String s:ids){
  271. if(StringUtils.isNotBlank(s) && !Pattern.matches(regex, s)){
  272. throw new BusinessException(buildError(ErrorConstants.PARAM_ERROR, "参数不合法", s));
  273. }
  274. }
  275. }
  276. //消息编号检查
  277. private Result checkMessageId(String messageId){
  278. Result res = new Result();
  279. if(StringUtils.isBlank(messageId)){
  280. res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"", "消息编号不能为空"));
  281. }
  282. return res;
  283. }
  284. //消息数据检查
  285. private Result checkMessage(JtMessageProducer messageProducer){
  286. Result res = new Result();
  287. if(StringUtils.isBlank(messageProducer.getTitle())){
  288. res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"", "消息标题不能为空"));
  289. }
  290. if(null == messageProducer.getSubject()){
  291. res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"", "消息类别不能为空"));
  292. }
  293. if(null == messageProducer.getIsDraft()){
  294. res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"", "消息是否为草稿没指定"));
  295. }
  296. if(null == messageProducer.getIsSend()){
  297. res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"", "消息是否要发送没指定"));
  298. }
  299. if(null == messageProducer.getDeleteSign()){
  300. res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"", "消息是否为删除没指定"));
  301. }
  302. return res;
  303. }
  304. /**
  305. * 发送消息接口
  306. * @param messageId
  307. * @return
  308. */
  309. @RequestMapping(value = "/sendMessage", method = RequestMethod.GET)
  310. public Result sendMessage(String messageId){
  311. Result res = checkMessageId(messageId);
  312. if (res.getError().isEmpty()) {
  313. //发送消息
  314. res.setData(jtMessageProducerService.addSendMessage(messageId));
  315. }
  316. return res;
  317. }
  318. /**
  319. * 获得单个消息数据接口
  320. * @param messageId
  321. * @return
  322. */
  323. @RequestMapping(value="/getMessageById", method = RequestMethod.GET)
  324. public Result getMessageById(String messageId){
  325. Result res = checkMessageId(messageId);
  326. if (res.getError().isEmpty()) {
  327. //查看消息
  328. res.setData(jtMessageProducerService.getMessageById(messageId));
  329. }
  330. return res;
  331. }
  332. /**
  333. * 修改消息接口
  334. * @param messageProducer
  335. * @return
  336. */
  337. @RequestMapping(value="/updMessageById", method = RequestMethod.POST)
  338. public Result updMessageById(JtMessageProducer messageProducer, String messageSendTime){
  339. Result res = checkMessageId(messageProducer.getId());
  340. try {
  341. messageProducer.setSendTime(DateUtils.parseDate(messageSendTime,AFTConstants.YYYYMMDDHHMMSS));
  342. } catch (Exception e) {
  343. messageProducer.setSendTime(null);
  344. }
  345. if (res.getError().isEmpty()) {
  346. //查看消息
  347. res.setData(jtMessageProducerService.updateMessageById(messageProducer));
  348. }
  349. return res;
  350. }
  351. /**
  352. * 获得消息列表
  353. * @param messageProducer
  354. * @param sendStartTime
  355. * @param sendEndTime
  356. * @param pageNo
  357. * @param pageSize
  358. * @return
  359. */
  360. @RequestMapping(value="/getMessageList", method = RequestMethod.GET)
  361. public Result getMessageList(JtMessageProducer messageProducer,String sendStartTime, String sendEndTime, Integer pageNo,Integer pageSize ){
  362. Result res = new Result();
  363. if(StringUtils.isBlank(TokenManager.getUserId())){
  364. res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"", "登录数据失效"));
  365. }
  366. if (res.getError().isEmpty()) {
  367. //查看消息列表
  368. res.setData(jtMessageProducerService.getMessageList(messageProducer, sendStartTime, sendEndTime, pageNo, pageSize ));
  369. }
  370. return res;
  371. }
  372. /**
  373. * 新增消息接口
  374. * @param messageProducer
  375. * @return
  376. */
  377. @RequestMapping(value="/createMyMessage", method = RequestMethod.POST)
  378. public Result createMyMessage(JtMessageProducer messageProducer, String messageCreateTime){
  379. try {
  380. messageProducer.setCreateTime(DateUtils.parseDate(messageCreateTime,AFTConstants.YYYYMMDDHHMMSS));
  381. } catch (Exception e) {
  382. messageProducer.setCreateTime(null);
  383. }
  384. Result res = checkMessage(messageProducer);
  385. if(StringUtils.isBlank(TokenManager.getUserId()) && StringUtils.isBlank(TokenManager.getAdminId())){
  386. res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"", "登录数据失效"));
  387. }
  388. if (res.getError().isEmpty()) {
  389. //新增消息
  390. int err = jtMessageProducerService.addMessage(messageProducer);
  391. if(err == -1){
  392. res.getError().add(buildError(ErrorConstants.PARAM_EMPTY_ERROR,"", "没有找到您的用户信息,不能创建消息"));
  393. }
  394. res.setData(err);
  395. }
  396. return res;
  397. }
  398. }