MessageServiceImpl.java 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. package com.goafanti.message.service.impl;
  2. import java.util.ArrayList;
  3. import java.util.Arrays;
  4. import java.util.Date;
  5. import java.util.HashMap;
  6. import java.util.List;
  7. import java.util.Map;
  8. import java.util.UUID;
  9. import org.apache.commons.lang3.StringUtils;
  10. import org.apache.shiro.SecurityUtils;
  11. import org.springframework.beans.factory.annotation.Autowired;
  12. import org.springframework.stereotype.Service;
  13. import org.springframework.transaction.annotation.Transactional;
  14. import com.goafanti.app.bo.IndexBo;
  15. import com.goafanti.common.dao.JpushEasemobAccountMapper;
  16. import com.goafanti.common.dao.MessageFromSystemMapper;
  17. import com.goafanti.common.dao.MessageProducerMapper;
  18. import com.goafanti.common.model.Admin;
  19. import com.goafanti.common.model.JpushEasemobAccount;
  20. import com.goafanti.common.model.MessageConsumer;
  21. import com.goafanti.common.model.MessageFromSystem;
  22. import com.goafanti.common.model.MessageProducer;
  23. import com.goafanti.common.model.User;
  24. import com.goafanti.core.mybatis.BaseMybatisDao;
  25. import com.goafanti.core.mybatis.page.Pagination;
  26. import com.goafanti.core.shiro.token.TokenManager;
  27. import com.goafanti.message.JGMessageHelper;
  28. import com.goafanti.message.bo.MessageBo;
  29. import com.goafanti.message.bo.MessageListBo;
  30. import com.goafanti.message.enums.ConsumerType;
  31. import com.goafanti.message.enums.RecommendType;
  32. import com.goafanti.message.enums.SubjectType;
  33. import com.goafanti.message.service.MessageService;
  34. @Service
  35. public class MessageServiceImpl extends BaseMybatisDao<MessageFromSystemMapper> implements MessageService{
  36. @Autowired
  37. private MessageFromSystemMapper messageFromSystemMapper;
  38. @Autowired
  39. private MessageProducerMapper messageProducerMapper;
  40. @Autowired
  41. private JpushEasemobAccountMapper jpushEasemobAccountMapper;
  42. @Autowired
  43. private JGMessageHelper jgMessageHelper;
  44. @Override
  45. public String selectSendUser(Map<Integer, String> selectMap,Integer type) {
  46. if(selectMap.containsKey(ConsumerType.PERSON_CUSTOMER_RETAIL.getTypeCode())){ //客户散户
  47. return selectMap.get(ConsumerType.PERSON_CUSTOMER_RETAIL.getTypeCode());
  48. }else if(selectMap.containsKey(ConsumerType.ORGANIZATION_CUSTOMER_RETAIL.getTypeCode())){
  49. return selectMap.get(ConsumerType.ORGANIZATION_CUSTOMER_RETAIL.getTypeCode());
  50. }else if(selectMap.containsKey(ConsumerType.USER_RETAIL.getTypeCode())){
  51. return selectMap.get(ConsumerType.USER_RETAIL.getTypeCode()); //用户散户
  52. }else if(selectMap.containsKey(ConsumerType.SOCIETY_TAG.getTypeCode())){ // 社会标签
  53. String sql = "select group_concat(id) from user where source=0 and status != 2 and society_tag in("+ selectMap.get(ConsumerType.SOCIETY_TAG.getTypeCode()) +")";
  54. return messageFromSystemMapper.selectSendTarget(sql);
  55. }else if(selectMap.containsKey(ConsumerType.LOCATION.getTypeCode())){ //用户地区
  56. String sql = "";
  57. String result = "";
  58. if(selectMap.containsKey(ConsumerType.LOCATION_PROVINCE.getTypeCode())){ //省
  59. sql = "select group_concat(a.id) from user a left join user_identity b on a.id = b.uid where a.source=0 and b.province in (" + selectMap.get(ConsumerType.LOCATION_PROVINCE.getTypeCode()) + ")";
  60. String tmp=messageFromSystemMapper.selectSendTarget(sql);
  61. result +="," + tmp==null?"":tmp;
  62. }
  63. if(selectMap.containsKey(ConsumerType.LOCATION_CITY.getTypeCode())){ //市
  64. sql = "select group_concat(a.id) from user a left join user_identity b on a.id = b.uid where a.source=0 and b.city in (" + selectMap.get(ConsumerType.LOCATION_CITY.getTypeCode()) + ") and a.status != 2";
  65. String tmp=messageFromSystemMapper.selectSendTarget(sql);
  66. result +="," + tmp==null?"":tmp;
  67. }
  68. if(selectMap.containsKey(ConsumerType.LOCATION_AREA.getTypeCode())){ //区
  69. sql = "select group_concat(a.id) from user a left join user_identity b on a.id = b.uid where a.source=0 and b.area in (" + selectMap.get(ConsumerType.LOCATION_AREA.getTypeCode()) + ") and a.status != 2";
  70. String tmp=messageFromSystemMapper.selectSendTarget(sql);
  71. result +="," + tmp==null?"":tmp;
  72. }
  73. if(!result.equals("")&&result!=null){
  74. String data[] = result.substring(1).split(",");
  75. List<String> list = new ArrayList<String>();
  76. boolean flag;
  77. for(int i=0; i< data.length;i++){
  78. flag = false;
  79. for(int j=0; j<list.size(); j++){
  80. if(data[i].equals(list.get(j))){
  81. flag = true;
  82. break;
  83. }
  84. }
  85. if(!flag) list.add(data[i]);
  86. }
  87. String[] tmp = (String[])list.toArray(new String[list.size()]);
  88. return Arrays.toString(tmp);
  89. }
  90. }else if(selectMap.containsKey(ConsumerType.INDUSTRY.getTypeCode())){ //行业
  91. String sql = "select group_concat(a.id) from user a left join user_identity b on a.id = b.uid where a.source=0 and b.industry in (" + selectMap.get(ConsumerType.INDUSTRY.getTypeCode()) + ") and a.status != 2";
  92. if(messageFromSystemMapper.selectSendTarget(sql)==null){
  93. return "";
  94. }
  95. return messageFromSystemMapper.selectSendTarget(sql);
  96. }
  97. return "";
  98. }
  99. @Override
  100. public String selectSendAdmin(Map<Integer, String> selectMap,Integer type) {
  101. String sql = "";
  102. if(selectMap.containsKey(ConsumerType.COMPANY.getTypeCode())){
  103. sql = "select group_concat(id) from admin where department_id in ("+ changeSql(selectMap.get(ConsumerType.COMPANY.getTypeCode())) + ")";
  104. }else if(selectMap.containsKey(ConsumerType.ROLE.getTypeCode())){
  105. sql = "select group_concat(id) from admin a left join user_role b on a.id = b.uid where b.rid in (" + changeSql(selectMap.get(ConsumerType.ROLE.getTypeCode())) + ")";
  106. }
  107. if(!sql.equals("")) return messageFromSystemMapper.selectSendTarget(sql);
  108. return "";
  109. }
  110. @Override
  111. public List<MessageFromSystem> selectRecommend(Map<Integer, String> selectMap,Integer type){
  112. String sql = "";
  113. if(selectMap.containsKey(RecommendType.TECH_SERVICE.getTypeCode())){ //科技服务
  114. sql = "select bname as title,introduce as body,id as resourceId from business_project where id in (" + changeSql(selectMap.get(RecommendType.TECH_SERVICE.getTypeCode())) + ")";
  115. }else if(selectMap.containsKey(RecommendType.PERSON_ACHIEVEMENT.getTypeCode())){ //个人成果
  116. sql = "select name as title,summary as body,id as resourceId from achievement where id in (" + changeSql(selectMap.get(RecommendType.PERSON_ACHIEVEMENT.getTypeCode())) +")";
  117. }else if(selectMap.containsKey(RecommendType.ORGANIZATION_ACHIEVEMENT.getTypeCode())){ //组织成果
  118. sql = "select name as title,summary as body,id as resourceId from achievement where id in (" + changeSql(selectMap.get(RecommendType.ORGANIZATION_ACHIEVEMENT.getTypeCode())) +")";
  119. }else if(selectMap.containsKey(RecommendType.PERSONAL_DEMAND.getTypeCode())){ //个人需求
  120. sql = "select name as title,problem_des as body,id as resourceId from demand where id in (" + changeSql(selectMap.get(RecommendType.PERSONAL_DEMAND.getTypeCode())) +")";
  121. }else if(selectMap.containsKey(RecommendType.ORGANIZATION_DEMAND.getTypeCode())){ //组织需求
  122. sql = "select name as title,problem_des as body,id as resourceId from demand where id in (" + changeSql(selectMap.get(RecommendType.ORGANIZATION_DEMAND.getTypeCode())) +")";
  123. }else if(selectMap.containsKey(RecommendType.EXPERT.getTypeCode())){ //专家
  124. sql = "select nickname as title ,introduction as body,id as resourceId from user where id in("+ changeSql(selectMap.get(RecommendType.EXPERT.getTypeCode())) +")";
  125. }else if(selectMap.containsKey(RecommendType.NEWS.getTypeCode())){ //政策
  126. sql = "select title,summary as body,id as resourceId from news where id in("+ changeSql(selectMap.get(RecommendType.NEWS.getTypeCode())) +")";
  127. }
  128. if(!sql.equals("")) return messageFromSystemMapper.selectRecommendTarget(sql);
  129. return null;
  130. }
  131. @Override
  132. @Transactional
  133. public int insertSystemMessage(Map<Integer, String> targetMap, Integer targetType,String title, String body,
  134. Integer subject,Date createTime,Integer isDraft) {
  135. MessageFromSystem mfs = new MessageFromSystem();
  136. mfs.setId(UUID.randomUUID().toString());
  137. mfs.setTitle(title);
  138. mfs.setBody(body);
  139. mfs.setCreateTime(createTime);
  140. mfs.setSubject(subject);
  141. mfs.setConsumerType(getTargetType(targetType));
  142. mfs.setIsDraft(isDraft);
  143. mfs.setDeleteSign(Boolean.FALSE);
  144. if(SecurityUtils.getSubject().getPrincipal() instanceof Admin){
  145. Admin admin = (Admin)SecurityUtils.getSubject().getPrincipal();
  146. mfs.setCreater(admin.getName());
  147. mfs.setProducerId(admin.getId());
  148. }
  149. messageFromSystemMapper.insert(mfs);
  150. List<MessageProducer> producerTarget = new ArrayList<MessageProducer>();
  151. List<MessageBo> sendTarget = new ArrayList<MessageBo>();
  152. String[] cids;
  153. if(subject == SubjectType.KE_HU.getTypeCode()){
  154. String tmp = selectSendUser(targetMap,targetType);
  155. if(tmp !=null){
  156. cids = tmp.split(",");
  157. MessageProducer mp;
  158. MessageBo bo;
  159. String registrationId = "";
  160. for(String cid : cids){
  161. mp = new MessageProducer();
  162. mp.setId(UUID.randomUUID().toString());
  163. mp.setMessageId(mfs.getId());
  164. mp.setConsumerId(cid);
  165. producerTarget.add(mp);
  166. registrationId = messageFromSystemMapper.checkJPushAcc(cid);
  167. if(StringUtils.isNotBlank(registrationId)){
  168. bo = new MessageBo();
  169. bo.setMessageId(mfs.getId());
  170. bo.setMessageTitle(mfs.getTitle());
  171. bo.setMessageBody(mfs.getBody());
  172. bo.setRegistrationId(registrationId);
  173. sendTarget.add(bo);
  174. }
  175. }
  176. }
  177. if(producerTarget.size()>0) insertBatchProducer(producerTarget);
  178. //推送消息
  179. }else if(subject == SubjectType.YUN_YING.getTypeCode()){
  180. String tmp = selectSendUser(targetMap,targetType);
  181. if(tmp !=null){
  182. cids = tmp.split(",");
  183. MessageProducer mp;
  184. MessageBo bo;
  185. String registrationId = "";
  186. for(String cid : cids){
  187. mp = new MessageProducer();
  188. mp.setId(UUID.randomUUID().toString());
  189. mp.setMessageId(mfs.getId());
  190. mp.setConsumerId(cid);
  191. producerTarget.add(mp);
  192. registrationId = messageFromSystemMapper.checkJPushAcc(cid);
  193. if(StringUtils.isNotBlank(registrationId)){
  194. bo = new MessageBo();
  195. bo.setMessageId(mfs.getId());
  196. bo.setMessageTitle(mfs.getTitle());
  197. bo.setMessageBody(mfs.getBody());
  198. bo.setRegistrationId(registrationId);
  199. sendTarget.add(bo);
  200. }
  201. }
  202. }
  203. if(producerTarget.size()>0) insertBatchProducer(producerTarget);
  204. //推送消息
  205. if(sendTarget.size()>0) jgMessageHelper.sendSystemMessage(sendTarget);
  206. }
  207. return 1;
  208. }
  209. @Override
  210. @Transactional
  211. public int insertRecommendMessage(Map<Integer, String> sourceMap, Integer sourceType, Map<Integer, String> targetMap,
  212. Integer targetType,Integer subject,Integer isDraft) {
  213. List<MessageFromSystem> mfsList = selectRecommend(sourceMap, sourceType);
  214. List<MessageProducer> producerTarget = new ArrayList<MessageProducer>();
  215. List<MessageFromSystem> messageTarget = new ArrayList<MessageFromSystem>();
  216. List<MessageBo> sendTarget = new ArrayList<MessageBo>();
  217. if(mfsList != null && mfsList.size()>0){
  218. MessageFromSystem message = null;
  219. for(MessageFromSystem mfs : mfsList){
  220. message = new MessageFromSystem();
  221. String messageId = "";
  222. Date createTime = new Date();
  223. message.setCreateTime(createTime);
  224. message.setSubject(subject);
  225. message.setConsumerType(getTargetType(targetType)); //适配客户类型
  226. message.setResourceType(getSourceType(sourceType)); //适配成果需求
  227. message.setIsDraft(isDraft);
  228. message.setDeleteSign(Boolean.FALSE);
  229. if(SecurityUtils.getSubject().getPrincipal() instanceof Admin){
  230. Admin admin = (Admin)SecurityUtils.getSubject().getPrincipal();
  231. message.setCreater(admin.getName());
  232. message.setProducerId(admin.getId());
  233. }
  234. messageId = UUID.randomUUID().toString();
  235. message.setId(messageId);
  236. message.setTitle(mfs.getTitle());
  237. message.setBody(mfs.getBody());
  238. message.setResourceId(mfs.getResourceId());
  239. messageTarget.add(message);
  240. //插入消息推送表
  241. if(subject == SubjectType.KE_HU.getTypeCode()||subject == SubjectType.TUI_JIAN.getTypeCode()){ //只推客户
  242. MessageProducer mp = null;
  243. MessageBo bo = null;
  244. String tmp = selectSendUser(targetMap,targetType);
  245. String[] cids = null;
  246. if(tmp != null){
  247. cids = tmp.split(",");
  248. String registrationId = "";
  249. for(String cid : cids){
  250. mp = new MessageProducer();
  251. mp.setId(UUID.randomUUID().toString());
  252. mp.setConsumerId(cid);
  253. mp.setCreateTime(createTime);
  254. mp.setMessageId(messageId);
  255. producerTarget.add(mp);
  256. registrationId = messageFromSystemMapper.checkJPushAcc(cid);
  257. if(StringUtils.isNotBlank(registrationId)){
  258. bo = new MessageBo();
  259. bo.setMessageId(message.getId());
  260. bo.setMessageTitle(message.getTitle());
  261. bo.setMessageBody(message.getBody());
  262. bo.setResourceId(message.getResourceId());
  263. bo.setResourceType(String.valueOf(message.getResourceType()));
  264. bo.setRegistrationId(registrationId);
  265. sendTarget.add(bo);
  266. }
  267. }
  268. }
  269. }
  270. }
  271. //批量插入
  272. if(messageTarget.size()>0) messageFromSystemMapper.insertBatchMessage(messageTarget);
  273. if(producerTarget.size()>0) messageFromSystemMapper.insertBatchProducer(producerTarget);
  274. //推送消息
  275. if(sendTarget.size()>0) jgMessageHelper.sendSystemMessage(sendTarget);
  276. return 1;
  277. }
  278. return 0;
  279. }
  280. @Override
  281. public List<MessageBo> selectSyncMessage(String uid) {
  282. return messageFromSystemMapper.selectSyncMessage(uid);
  283. }
  284. @Override
  285. public int updateMessageProducer(List<MessageProducer> producerList){
  286. for(MessageProducer mp : producerList){
  287. messageProducerMapper.updateByPrimaryKeySelective(mp);
  288. }
  289. return 1;
  290. }
  291. @Override
  292. public int insertBatchProducer(List<MessageProducer> target){
  293. return messageFromSystemMapper.insertBatchProducer(target);
  294. }
  295. @Override
  296. public int insertBatchConsumer(List<MessageConsumer> target) {
  297. return messageFromSystemMapper.insertBatchConsumer(target);
  298. }
  299. @Override
  300. public int updateJpushEasemobAccount(String uuid,String registrationId,String easemobName, String easemobPass) {
  301. return messageFromSystemMapper.updateJpushEasemobAccount(uuid, registrationId,easemobName,easemobPass,TokenManager.getUserId(),new Date());
  302. }
  303. @Override
  304. public int updateMessageConsumer(String messageId) {
  305. return messageFromSystemMapper.updateMessageConsumer(messageId, TokenManager.getUserId() , new Date());
  306. }
  307. @SuppressWarnings("unchecked")
  308. @Override
  309. public Pagination<MessageListBo> listSystemMessage(Integer isDraft,Integer subject,Integer pageNo,Integer pageSize) {
  310. Map<String,Object> params = new HashMap<String,Object>();
  311. params.put("isDraft", isDraft);
  312. params.put("subject", subject);
  313. return (Pagination<MessageListBo>)findPage("listSystemMessageByPage", "listSystemMessageCount", params, pageNo, pageSize);
  314. }
  315. @Override
  316. public int updateSystemMessage(String messageId, String title, String body, Integer isDraft) {
  317. MessageFromSystem mfs = new MessageFromSystem();
  318. mfs.setId(messageId);
  319. mfs.setTitle(title);
  320. mfs.setBody(body);
  321. mfs.setIsDraft(isDraft);
  322. return messageFromSystemMapper.updateByPrimaryKeySelective(mfs);
  323. }
  324. @Override
  325. public int deleteSystemMessage(String messageId) {
  326. MessageFromSystem mfs = new MessageFromSystem();
  327. mfs.setDeleteSign(Boolean.TRUE);
  328. mfs.setId(messageId);
  329. return messageFromSystemMapper.updateByPrimaryKeySelective(mfs);
  330. }
  331. @Override
  332. public IndexBo selectMessageWithGroup() {
  333. if(TokenManager.getToken() instanceof Admin){
  334. return messageFromSystemMapper.selectMessageWithGroup(TokenManager.getAdminId());
  335. }else if(TokenManager.getToken() instanceof User){
  336. return messageFromSystemMapper.selectMessageWithGroup(TokenManager.getUserId());
  337. }
  338. return null;
  339. }
  340. @SuppressWarnings("unchecked")
  341. @Override
  342. public Pagination<MessageListBo> listPersonalMessage(Integer subject, Integer resourceType,Integer pageNo,Integer pageSize) {
  343. if(subject == SubjectType.KE_HU.getTypeCode() || subject == SubjectType.TUI_JIAN.getTypeCode()){
  344. Map<String,Object> params = new HashMap<String, Object>();
  345. if(null != subject) params.put("subject", subject);
  346. if(null != resourceType) params.put("resourceType", resourceType);
  347. params.put("uid", TokenManager.getUserId());
  348. return (Pagination<MessageListBo>)findPage("listPersonalMessageByPage", "listPersonalMessageCount", params, pageNo, pageSize);
  349. }
  350. return null;
  351. }
  352. @Override
  353. public MessageFromSystem selectSystemMessageDetail(String messageId) {
  354. return messageFromSystemMapper.selectByPrimaryKey(messageId);
  355. }
  356. /**
  357. * 为个人和组织做转换
  358. * @param sourceType
  359. * @return
  360. */
  361. private Integer getSourceType(Integer sourceType){
  362. if(RecommendType.PERSON_ACHIEVEMENT.getTypeCode() == sourceType
  363. || RecommendType.ORGANIZATION_ACHIEVEMENT.getTypeCode() == sourceType){
  364. sourceType = RecommendType.ACHIEVEMENT.getTypeCode();
  365. }else if(RecommendType.PERSONAL_DEMAND.getTypeCode() == sourceType
  366. || RecommendType.ORGANIZATION_DEMAND.getTypeCode() == sourceType){
  367. sourceType = RecommendType.DEMAND.getTypeCode();
  368. }
  369. return sourceType;
  370. }
  371. /**
  372. * 为个人和组织客户做转换
  373. * @param targetType
  374. * @return
  375. */
  376. private Integer getTargetType(Integer targetType){
  377. if(ConsumerType.PERSON_CUSTOMER_RETAIL.getTypeCode() == targetType
  378. || ConsumerType.ORGANIZATION_CUSTOMER_RETAIL.getTypeCode() == targetType){
  379. targetType = ConsumerType.CUSTOMER_RETAIL.getTypeCode();
  380. }
  381. return targetType;
  382. }
  383. @Override
  384. public JpushEasemobAccount selectSynAccByUid(String uid) {
  385. return jpushEasemobAccountMapper.selectSynAccByUid(uid);
  386. }
  387. private String changeSql(String source){
  388. String result = "";
  389. if(StringUtils.isNotBlank(source)){
  390. source = "'" + source + "'";
  391. result = source.replace(",", "','");
  392. }
  393. return result;
  394. }
  395. @Override
  396. public int addJpushEasemobAccount(String uuid,String uid,String easemobName,String easemobPass) {
  397. JpushEasemobAccount jea = new JpushEasemobAccount();
  398. jea.setUid(uid);
  399. jea.setUuid(uuid);
  400. jea.setEasemobName(easemobName);
  401. jea.setEasemobPass(easemobPass);
  402. return jpushEasemobAccountMapper.insert(jea);
  403. }
  404. }