Commit 89b489343a925e655db1f0efd898013873facc21

Authored by 周伟鹏
1 parent e3dc5c9c

站点分页接口

src/main/java/com/cnlive/shenhe/controller/SitesController.java 0 → 100644
  1 +package com.cnlive.shenhe.controller;
  2 +
  3 +import com.cnlive.shenhe.serviceImpl.SitesServiceImpl;
  4 +import com.cnlive.shenhe.utils.CommonUtils;
  5 +import org.springframework.beans.factory.annotation.Autowired;
  6 +import org.springframework.stereotype.Controller;
  7 +import org.springframework.web.bind.annotation.GetMapping;
  8 +import org.springframework.web.bind.annotation.RequestMapping;
  9 +import org.springframework.web.bind.annotation.ResponseBody;
  10 +
  11 +/**
  12 + * @Auther: 周伟鹏
  13 + * @Date: 2018/11/30 17:28
  14 + * @Description:
  15 + */
  16 +@Controller
  17 +@RequestMapping("/users")
  18 +public class SitesController {
  19 +
  20 + @Autowired
  21 + SitesServiceImpl sitesService;
  22 +
  23 +
  24 + @GetMapping(value = "/page")
  25 + @ResponseBody
  26 + public Object getTest(Integer page,Integer pageSize,String orderByClause,String key,Integer spid){
  27 + if(CommonUtils.isNull(page))page=1;
  28 + if(CommonUtils.isNull(pageSize))pageSize=10;
  29 + return sitesService.getPage(page,pageSize,orderByClause,key,spid);
  30 + }
  31 +
  32 +}
... ...
src/main/java/com/cnlive/shenhe/job/UsersJob.java renamed to src/main/java/com/cnlive/shenhe/job/UsersSiteJob.java
... ... @@ -3,9 +3,14 @@ package com.cnlive.shenhe.job;
3 3 import com.alibaba.fastjson.JSON;
4 4 import com.alibaba.fastjson.JSONArray;
5 5 import com.alibaba.fastjson.JSONObject;
  6 +import com.cnlive.shenhe.entity.ShySites;
6 7 import com.cnlive.shenhe.entity.ShyUsers;
  8 +import com.cnlive.shenhe.mapper.ShySitesMapper;
7 9 import com.cnlive.shenhe.mapper.ShyUsersMapper;
8   -import com.cnlive.shenhe.utils.*;
  10 +import com.cnlive.shenhe.utils.CommonConst;
  11 +import com.cnlive.shenhe.utils.CommonUtils;
  12 +import com.cnlive.shenhe.utils.HttpUtil;
  13 +import com.cnlive.shenhe.utils.OpenUtil;
9 14 import org.slf4j.Logger;
10 15 import org.slf4j.LoggerFactory;
11 16 import org.springframework.beans.factory.annotation.Autowired;
... ... @@ -14,18 +19,19 @@ import org.springframework.scheduling.annotation.Scheduled;
14 19 import org.springframework.stereotype.Component;
15 20 import tk.mybatis.mapper.entity.Example;
16 21  
17   -import java.io.UnsupportedEncodingException;
18   -import java.net.URLEncoder;
19   -import java.util.*;
  22 +import java.util.Date;
  23 +import java.util.HashMap;
  24 +import java.util.List;
  25 +import java.util.Map;
20 26  
21 27 /**
22 28 * @Auther: 周伟鹏
23 29 * @Date: 2018/11/30 11:31
24   - * @Description:
  30 + * @Description:同步用户和站点信息
25 31 */
26 32 @Component
27   -public class UsersJob {
28   - private static Logger logger = LoggerFactory.getLogger(UsersJob.class);
  33 +public class UsersSiteJob {
  34 + private static Logger logger = LoggerFactory.getLogger(UsersSiteJob.class);
29 35  
30 36 @Value("${sync_users_api}")
31 37 private String syncUserApi;
... ... @@ -36,6 +42,9 @@ public class UsersJob {
36 42 @Autowired
37 43 ShyUsersMapper shyUsersMapper;
38 44  
  45 + @Autowired
  46 + ShySitesMapper shySitesMapper;
  47 +
39 48  
40 49 @Scheduled(fixedRate = 30 * 60 * 1000)
41 50 public void syncUsers() {
... ... @@ -59,6 +68,7 @@ public class UsersJob {
59 68 if ("0".equals(errorCode)) {
60 69 JSONArray data = result.getJSONArray("data");
61 70 for (int i = 0; i < data.size(); i++) {
  71 + //用户信息入库
62 72 JSONObject user = data.getJSONObject(i);
63 73 Integer id = user.getInteger("sid");
64 74 String email = user.getString("email");
... ... @@ -92,6 +102,16 @@ public class UsersJob {
92 102 } else {
93 103 shyUsersMapper.updateByPrimaryKeySelective(shyUsers);
94 104 }
  105 + //站点信息入库
  106 + ShySites shySites = new ShySites();
  107 + shySites.setSpid(spid);
  108 + List<ShySites> shySitesList = shySitesMapper.select(shySites);
  109 + if(shySitesList.isEmpty()){
  110 + shySites.setCompany(company);
  111 + shySites.setName(company_brief);
  112 + shySitesMapper.insertSelective(shySites);
  113 + }
  114 +
95 115 }
96 116 } else {
97 117 logger.error("请求用户云失败,errorCode:{},errorMessage{}", errorCode, result.getString("errorMessage"));
... ... @@ -103,74 +123,4 @@ public class UsersJob {
103 123  
104 124 }
105 125  
106   -
107   - /**
108   - * 生成URL
109   - *
110   - * @param url
111   - * @param params
112   - * @param app_key
113   - * @return
114   - */
115   - public static String buildURL(String url, Map<String, String> params, String app_key) {
116   - Map<String, String> map = order(params);
117   - if (map.containsKey("sign")) {
118   - map.remove("sign");
119   - }
120   - String str = mapJoin(map, true, false);
121   - String sign = SHA1.SHA1Digest(str + "&key=" + app_key).toUpperCase();
122   - map.put("sign", sign);
123   - return url + "?" + mapJoin(map, false, true);
124   - }
125   -
126   - /**
127   - * Map key 排序
128   - *
129   - * @param map
130   - * @return
131   - */
132   - private static Map<String, String> order(Map<String, String> map) {
133   - HashMap<String, String> tempMap = new LinkedHashMap<String, String>();
134   - List<Map.Entry<String, String>> infoIds = new ArrayList<Map.Entry<String, String>>(map.entrySet());
135   -
136   - Collections.sort(infoIds, new Comparator<Map.Entry<String, String>>() {
137   - public int compare(Map.Entry<String, String> o1, Map.Entry<String, String> o2) {
138   - return (o1.getKey()).toString().compareTo(o2.getKey());
139   - }
140   - });
141   -
142   - for (int i = 0; i < infoIds.size(); i++) {
143   - Map.Entry<String, String> item = infoIds.get(i);
144   - tempMap.put(item.getKey(), item.getValue());
145   - }
146   - return tempMap;
147   - }
148   -
149   - /**
150   - * url 参数串连
151   - *
152   - * @param map
153   - * @param keyLower
154   - * @param valueUrlencode
155   - * @return
156   - */
157   - private static String mapJoin(Map<String, String> map, boolean keyLower, boolean valueUrlencode) {
158   - StringBuilder stringBuilder = new StringBuilder();
159   - for (String key : map.keySet()) {
160   - if (map.get(key) != null && !"".equals(map.get(key))) {
161   - try {
162   - stringBuilder.append(key)
163   - .append("=")
164   - .append(valueUrlencode ? URLEncoder.encode(map.get(key), "utf-8").replace("+", "%20") : map.get(key))
165   - .append("&");
166   - } catch (UnsupportedEncodingException e) {
167   - e.printStackTrace();
168   - }
169   - }
170   - }
171   - if (stringBuilder.length() > 0) {
172   - stringBuilder.deleteCharAt(stringBuilder.length() - 1);
173   - }
174   - return stringBuilder.toString();
175   - }
176 126 }
... ...
src/main/java/com/cnlive/shenhe/serviceImpl/SitesServiceImpl.java 0 → 100644
  1 +package com.cnlive.shenhe.serviceImpl;
  2 +
  3 +import com.cnlive.shenhe.entity.ShySites;
  4 +import com.cnlive.shenhe.mapper.ShySitesMapper;
  5 +import com.cnlive.shenhe.utils.CommonUtils;
  6 +import com.github.pagehelper.PageHelper;
  7 +import com.github.pagehelper.PageInfo;
  8 +import org.springframework.beans.factory.annotation.Autowired;
  9 +import org.springframework.stereotype.Service;
  10 +import tk.mybatis.mapper.entity.Example;
  11 +
  12 +import java.util.List;
  13 +
  14 +/**
  15 + * @Auther: 周伟鹏
  16 + * @Date: 2018/11/29 14:23
  17 + * @Description:
  18 + */
  19 +@Service
  20 +public class SitesServiceImpl {
  21 + @Autowired
  22 + ShySitesMapper shySitesMapper;
  23 +
  24 + public PageInfo<ShySites> getPage(Integer page, Integer pageSize, String orderByClause,String key, Integer spId){
  25 + Example example = new Example(ShySites.class);
  26 + Example.Criteria criteria = example.createCriteria();
  27 + if (CommonUtils.isNotEmpty(orderByClause)) {
  28 + example.setOrderByClause(orderByClause);
  29 + }
  30 + if (CommonUtils.isNotEmpty(key)){
  31 + criteria.andEqualTo("name", "%" + key + "%");
  32 + criteria.andLike("company", "%" + key + "%");
  33 + }
  34 + if (CommonUtils.isNotNull(spId))
  35 + criteria.andEqualTo("spid", spId);
  36 + PageHelper.startPage(page, pageSize, true);
  37 + List<ShySites> shySitesList = shySitesMapper.selectByExample(example);
  38 + PageInfo<ShySites> pageInfo = new PageInfo<ShySites>(shySitesList);
  39 + return pageInfo;
  40 + }
  41 +
  42 +
  43 +}
... ...