AdminMessageController.java 17 KB

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