MessageServiceImpl.java 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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. for(String cid : cids){
  160. mp = new MessageProducer();
  161. mp.setId(UUID.randomUUID().toString());
  162. mp.setMessageId(mfs.getId());
  163. mp.setConsumerId(cid);
  164. producerTarget.add(mp);
  165. bo = new MessageBo();
  166. bo.setMessageId(mfs.getId());
  167. bo.setMessageTitle(mfs.getTitle());
  168. bo.setMessageBody(mfs.getBody());
  169. bo.setResourceId(mfs.getResourceId());
  170. bo.setResourceType(String.valueOf(mfs.getResourceType()));
  171. bo.setRegistrationId(cid);
  172. sendTarget.add(bo);
  173. }
  174. }
  175. if(producerTarget.size()>0) insertBatchProducer(producerTarget);
  176. //推送消息
  177. }else if(subject == SubjectType.YUN_YING.getTypeCode()){
  178. String tmp = selectSendUser(targetMap,targetType);
  179. if(tmp !=null){
  180. cids = tmp.split(",");
  181. MessageProducer mp;
  182. MessageBo bo;
  183. for(String cid : cids){
  184. mp = new MessageProducer();
  185. mp.setId(UUID.randomUUID().toString());
  186. mp.setMessageId(mfs.getId());
  187. mp.setConsumerId(cid);
  188. producerTarget.add(mp);
  189. bo = new MessageBo();
  190. bo.setMessageId(mfs.getId());
  191. bo.setMessageTitle(mfs.getTitle());
  192. bo.setMessageBody(mfs.getBody());
  193. bo.setResourceId(mfs.getResourceId());
  194. bo.setResourceType(String.valueOf(mfs.getResourceType()));
  195. bo.setRegistrationId(cid);
  196. sendTarget.add(bo);
  197. }
  198. }
  199. if(producerTarget.size()>0) insertBatchProducer(producerTarget);
  200. //推送消息
  201. if(sendTarget.size()>0) jgMessageHelper.sendSystemMessage(sendTarget);
  202. }
  203. return 1;
  204. }
  205. @Override
  206. @Transactional
  207. public int insertRecommendMessage(Map<Integer, String> sourceMap, Integer sourceType, Map<Integer, String> targetMap,
  208. Integer targetType,Integer subject,Integer isDraft) {
  209. List<MessageFromSystem> mfsList = selectRecommend(sourceMap, sourceType);
  210. List<MessageProducer> producerTarget = new ArrayList<MessageProducer>();
  211. List<MessageFromSystem> messageTarget = new ArrayList<MessageFromSystem>();
  212. List<MessageBo> sendTarget = new ArrayList<MessageBo>();
  213. if(mfsList != null && mfsList.size()>0){
  214. MessageFromSystem message = null;
  215. for(MessageFromSystem mfs : mfsList){
  216. message = new MessageFromSystem();
  217. String messageId = "";
  218. Date createTime = new Date();
  219. message.setCreateTime(createTime);
  220. message.setSubject(subject);
  221. message.setConsumerType(getTargetType(targetType)); //适配客户类型
  222. message.setResourceType(getSourceType(sourceType)); //适配成果需求
  223. message.setIsDraft(isDraft);
  224. message.setDeleteSign(Boolean.FALSE);
  225. if(SecurityUtils.getSubject().getPrincipal() instanceof Admin){
  226. Admin admin = (Admin)SecurityUtils.getSubject().getPrincipal();
  227. message.setCreater(admin.getName());
  228. message.setProducerId(admin.getId());
  229. }
  230. messageId = UUID.randomUUID().toString();
  231. message.setId(messageId);
  232. message.setTitle(mfs.getTitle());
  233. message.setBody(mfs.getBody());
  234. message.setResourceId(mfs.getResourceId());
  235. messageTarget.add(message);
  236. //插入消息推送表
  237. if(subject == SubjectType.KE_HU.getTypeCode()||subject == SubjectType.TUI_JIAN.getTypeCode()){ //只推客户
  238. MessageProducer mp = null;
  239. MessageBo bo = null;
  240. String tmp = selectSendUser(targetMap,targetType);
  241. String[] cids = null;
  242. if(tmp != null){
  243. cids = tmp.split(",");
  244. System.out.println("cids=="+cids);
  245. for(String cid : cids){
  246. mp = new MessageProducer();
  247. mp.setId(UUID.randomUUID().toString());
  248. mp.setConsumerId(cid);
  249. mp.setCreateTime(createTime);
  250. mp.setMessageId(messageId);
  251. producerTarget.add(mp);
  252. bo = new MessageBo();
  253. bo.setMessageId(messageId);
  254. bo.setMessageTitle(message.getTitle());
  255. bo.setMessageBody(message.getBody());
  256. bo.setResourceId(message.getResourceId());
  257. bo.setResourceType(String.valueOf(message.getResourceType()));
  258. bo.setRegistrationId(cid);
  259. sendTarget.add(bo);
  260. }
  261. }
  262. }
  263. }
  264. //批量插入
  265. if(messageTarget.size()>0) messageFromSystemMapper.insertBatchMessage(messageTarget);
  266. if(producerTarget.size()>0) messageFromSystemMapper.insertBatchProducer(producerTarget);
  267. //推送消息
  268. if(sendTarget.size()>0) jgMessageHelper.sendSystemMessage(sendTarget);
  269. return 1;
  270. }
  271. return 0;
  272. }
  273. @Override
  274. public List<MessageBo> selectSyncMessage(String uid) {
  275. return messageFromSystemMapper.selectSyncMessage(uid);
  276. }
  277. @Override
  278. public int updateMessageProducer(List<MessageProducer> producerList){
  279. for(MessageProducer mp : producerList){
  280. messageProducerMapper.updateByPrimaryKeySelective(mp);
  281. }
  282. return 1;
  283. }
  284. @Override
  285. public int insertBatchProducer(List<MessageProducer> target){
  286. return messageFromSystemMapper.insertBatchProducer(target);
  287. }
  288. @Override
  289. public int insertBatchConsumer(List<MessageConsumer> target) {
  290. return messageFromSystemMapper.insertBatchConsumer(target);
  291. }
  292. @Override
  293. public int updateJpushEasemobAccount(String uuid,String registrationId,String easemobName, String easemobPass) {
  294. return messageFromSystemMapper.updateJpushEasemobAccount(uuid, registrationId,easemobName,easemobPass,TokenManager.getUserId(),new Date());
  295. }
  296. @Override
  297. public int updateMessageConsumer(String messageId) {
  298. return messageFromSystemMapper.updateMessageConsumer(messageId, TokenManager.getUserId() , new Date());
  299. }
  300. @SuppressWarnings("unchecked")
  301. @Override
  302. public Pagination<MessageListBo> listSystemMessage(Integer isDraft,Integer subject,Integer pageNo,Integer pageSize) {
  303. Map<String,Object> params = new HashMap<String,Object>();
  304. params.put("isDraft", isDraft);
  305. params.put("subject", subject);
  306. return (Pagination<MessageListBo>)findPage("listSystemMessageByPage", "listSystemMessageCount", params, pageNo, pageSize);
  307. }
  308. @Override
  309. public int updateSystemMessage(String messageId, String title, String body, Integer isDraft) {
  310. MessageFromSystem mfs = new MessageFromSystem();
  311. mfs.setId(messageId);
  312. mfs.setTitle(title);
  313. mfs.setBody(body);
  314. mfs.setIsDraft(isDraft);
  315. return messageFromSystemMapper.updateByPrimaryKeySelective(mfs);
  316. }
  317. @Override
  318. public int deleteSystemMessage(String messageId) {
  319. MessageFromSystem mfs = new MessageFromSystem();
  320. mfs.setDeleteSign(Boolean.TRUE);
  321. mfs.setId(messageId);
  322. return messageFromSystemMapper.updateByPrimaryKeySelective(mfs);
  323. }
  324. @Override
  325. public IndexBo selectMessageWithGroup() {
  326. if(TokenManager.getToken() instanceof Admin){
  327. return messageFromSystemMapper.selectMessageWithGroup(TokenManager.getAdminId());
  328. }else if(TokenManager.getToken() instanceof User){
  329. return messageFromSystemMapper.selectMessageWithGroup(TokenManager.getUserId());
  330. }
  331. return null;
  332. }
  333. @SuppressWarnings("unchecked")
  334. @Override
  335. public Pagination<MessageListBo> listPersonalMessage(Integer subject, Integer resourceType,Integer pageNo,Integer pageSize) {
  336. if(subject == SubjectType.KE_HU.getTypeCode() || subject == SubjectType.TUI_JIAN.getTypeCode()){
  337. Map<String,Object> params = new HashMap<String, Object>();
  338. if(null != subject) params.put("subject", subject);
  339. if(null != resourceType) params.put("resourceType", resourceType);
  340. params.put("uid", TokenManager.getUserId());
  341. return (Pagination<MessageListBo>)findPage("listPersonalMessageByPage", "listPersonalMessageCount", params, pageNo, pageSize);
  342. }
  343. return null;
  344. }
  345. @Override
  346. public MessageFromSystem selectSystemMessageDetail(String messageId) {
  347. return messageFromSystemMapper.selectByPrimaryKey(messageId);
  348. }
  349. /**
  350. * 为个人和组织做转换
  351. * @param sourceType
  352. * @return
  353. */
  354. private Integer getSourceType(Integer sourceType){
  355. if(RecommendType.PERSON_ACHIEVEMENT.getTypeCode() == sourceType
  356. || RecommendType.ORGANIZATION_ACHIEVEMENT.getTypeCode() == sourceType){
  357. sourceType = RecommendType.ACHIEVEMENT.getTypeCode();
  358. }else if(RecommendType.PERSONAL_DEMAND.getTypeCode() == sourceType
  359. || RecommendType.ORGANIZATION_DEMAND.getTypeCode() == sourceType){
  360. sourceType = RecommendType.DEMAND.getTypeCode();
  361. }
  362. return sourceType;
  363. }
  364. /**
  365. * 为个人和组织客户做转换
  366. * @param targetType
  367. * @return
  368. */
  369. private Integer getTargetType(Integer targetType){
  370. if(ConsumerType.PERSON_CUSTOMER_RETAIL.getTypeCode() == targetType
  371. || ConsumerType.ORGANIZATION_CUSTOMER_RETAIL.getTypeCode() == targetType){
  372. targetType = ConsumerType.CUSTOMER_RETAIL.getTypeCode();
  373. }
  374. return targetType;
  375. }
  376. @Override
  377. public JpushEasemobAccount selectSynAccByUid(String uid) {
  378. return jpushEasemobAccountMapper.selectSynAccByUid(uid);
  379. }
  380. private String changeSql(String source){
  381. String result = "";
  382. if(StringUtils.isNotBlank(source)){
  383. source = "'" + source + "'";
  384. result = source.replace(",", "','");
  385. }
  386. return result;
  387. }
  388. @Override
  389. public int addJpushEasemobAccount(String uuid,String uid,String easemobName,String easemobPass) {
  390. JpushEasemobAccount jea = new JpushEasemobAccount();
  391. jea.setUid(uid);
  392. jea.setUuid(uuid);
  393. jea.setEasemobName(easemobName);
  394. jea.setEasemobPass(easemobPass);
  395. return jpushEasemobAccountMapper.insert(jea);
  396. }
  397. }