Browse Source

个人机构地址初始化

huangzhiguo 2 năm trước cách đây
mục cha
commit
97d2748a97

+ 4 - 4
src/main/java/com/caimei365/tools/controller/BaseApi.java

@@ -61,11 +61,11 @@ public class BaseApi {
     /**
      * 临时接口
      */
-    /*@GetMapping("/record/update")
-    public void svipCoupon() throws IOException {
+    @GetMapping("/record/update")
+    public void svipCoupon(Integer startId, Integer endId) throws IOException {
 
-        cmBehaviorRecordService.insertTodayData();
-    }*/
+        cmBehaviorRecordService.updateClubAddress(startId, endId);
+    }
 
     /**
      * 数据ip地址

+ 23 - 0
src/main/java/com/caimei365/tools/mapper/CmBehaviorRecordMapper.java

@@ -135,5 +135,28 @@ public interface CmBehaviorRecordMapper {
      * @param shopId 供应商id
      */
     void insertShopId(@Param("recordId") Integer recordId, @Param("shopId") Integer shopId);
+
+    /**
+     * 个人机构注册ip
+     * @return
+     */
+    List<CmBehaviorRecordPo> selRegisterIP(Integer startId, Integer endId);
+
+    /**
+     * 省份
+     * @return
+     */
+    Integer selProvince(@Param("regionPro") String regionPro);
+
+    /**
+     * 市
+     * @return
+     */
+    AddressPo selCity(@Param("regionCity") String regionCity);
+
+    /**
+     * 更新机构地址数据
+     */
+    void upClubAddress(@Param("clubId") String clubId, @Param("provinceId") Integer provinceId, @Param("cityId") Integer cityId);
 }
 

+ 21 - 0
src/main/java/com/caimei365/tools/model/po/AddressPo.java

@@ -0,0 +1,21 @@
+package com.caimei365.tools.model.po;
+
+import lombok.Data;
+
+/**
+ * Description
+ *
+ * @author : Charles
+ * @date : 2023/4/6
+ */
+@Data
+public class AddressPo {
+    /**
+     * 省份Id
+     */
+    private Integer provinceId;
+    /**
+     * 市Id
+     */
+    private Integer cityId;
+}

+ 12 - 0
src/main/java/com/caimei365/tools/model/po/CmBehaviorRecordPo.java

@@ -159,4 +159,16 @@ public class CmBehaviorRecordPo {
      * 标签
      */
     private String label;
+    /**
+     * 用户注册IP
+     */
+    private String registerIp;
+    /**
+     * 机构省份Id
+     */
+    private Integer provinceId;
+    /**
+     * 机构市Id
+     */
+    private Integer cityId;
 }

+ 2 - 0
src/main/java/com/caimei365/tools/service/CmBehaviorRecordService.java

@@ -23,4 +23,6 @@ public interface CmBehaviorRecordService {
     void insertBehaviorUser();
 
     void insertTodayData();
+
+    void updateClubAddress(Integer startId, Integer endId);
 }

+ 96 - 2
src/main/java/com/caimei365/tools/service/impl/CmBehaviorRecordServiceImpl.java

@@ -2,6 +2,7 @@ package com.caimei365.tools.service.impl;
 
 import com.alibaba.fastjson.JSONObject;
 import com.caimei365.tools.mapper.CmBehaviorRecordMapper;
+import com.caimei365.tools.model.po.AddressPo;
 import com.caimei365.tools.model.po.CmBehaviorRecordPo;
 import com.caimei365.tools.model.po.CmUserPo;
 import com.caimei365.tools.model.po.IpSavePo;
@@ -368,7 +369,7 @@ public class CmBehaviorRecordServiceImpl implements CmBehaviorRecordService {
             int num = 0;
             for (CmBehaviorRecordPo recordPo : selDataList) {
                 if (recordPo.getIP().equals(data.getIP()) && recordPo.getUserId().equals(data.getUserId())) {
-                    if (num < 5) {
+                    if (num < 10) {
                         if (!item.contains(recordPo.getPageLabel())) {
                             item.add(recordPo.getPageLabel());
                             num++;
@@ -413,7 +414,7 @@ public class CmBehaviorRecordServiceImpl implements CmBehaviorRecordService {
             for (CmBehaviorRecordPo recordPo : selDataList) {
                 if (recordPo.getIP().equals(data.getIP()) && recordPo.getUserId().equals(data.getUserId())) {
                     // 标签信息
-                    if (num < 5) {
+                    if (num < 10) {
                         if (!item.contains(recordPo.getPageLabel())) {
                             item.add(recordPo.getPageLabel());
                             num++;
@@ -431,6 +432,53 @@ public class CmBehaviorRecordServiceImpl implements CmBehaviorRecordService {
         }
     }
 
+    @Override
+    public void updateClubAddress(Integer startId, Integer endId) {
+        List<CmBehaviorRecordPo> recordPoList = recordMapper.selRegisterIP(startId, endId);
+        if (recordPoList.size() > 0) {
+            recordPoList.forEach(r -> {
+                if (StringUtils.isNotBlank(r.getRegisterIp()) && (null == r.getProvinceId() && null == r.getCityId())) {
+                    log.info("个人机构地址初始化=====》"+r.getClubId());
+                    try {
+                        Map<String, String> map = recordAddress(r.getRegisterIp());
+                        if (!map.isEmpty()) {
+                            // 省份
+                            String regionPro = map.get("regionPro");
+                            Integer provinceId = recordMapper.selProvince(regionPro);
+                            // 市
+                            String regionCity = map.get("regionCity");
+                            AddressPo addressPo = recordMapper.selCity(regionCity);
+
+                            Integer cityId = null;
+                            if (null != provinceId) {
+                                if (null != addressPo) {
+                                    if (null != addressPo.getProvinceId()) {
+                                        if (addressPo.getProvinceId().equals(provinceId)) {
+                                            // 省份id与地址表的省份地址对应 取地址表地址 机构给予 省、市地址
+                                            recordMapper.upClubAddress(r.getClubId(), provinceId, addressPo.getCityId());
+                                        } else {
+                                            // 省份id与地址表的省份地址不对应 取地址表地址
+                                            provinceId = addressPo.getProvinceId();
+                                            recordMapper.upClubAddress(r.getClubId(), provinceId, addressPo.getCityId());
+                                        }
+                                    } else {
+                                        // 地址表没有省份id 机构给予省份地址Id
+                                        recordMapper.upClubAddress(r.getClubId(), provinceId, cityId);
+                                    }
+                                } else {
+                                    // 省份不为空 市为空,机构给予省份地址Id
+                                    recordMapper.upClubAddress(r.getClubId(), provinceId, cityId);
+                                }
+                            }
+                        }
+                    } catch (IOException e) {
+                        e.printStackTrace();
+                    }
+                }
+            });
+        }
+    }
+
 
     // 获取IP对应地址 ---- 太平洋
     public static String recordIp(String ip) throws IOException {
@@ -470,6 +518,52 @@ public class CmBehaviorRecordServiceImpl implements CmBehaviorRecordService {
         }
         return region;
     }
+    // 获取IP对应地址 ---- 太平洋
+    public static Map<String, String> recordAddress(String ip) throws IOException {
+        URL url = null;
+        HttpURLConnection connection = null;
+        String encoding = "gbk";
+        String text = "";
+        String line = "";
+        String regionPro = "";
+        String regionCity = "";
+        Map<String,String> map = new HashMap<>();
+        String urlStr = "http://whois.pconline.com.cn/ipJson.jsp?ip=" + ip + "&json=true";
+        try {
+            url = new URL(urlStr);
+            connection = (HttpURLConnection) url.openConnection();    //新建链接实例
+            connection.setConnectTimeout(20000);    //设置链接超时时间,单位毫秒
+            connection.setReadTimeout(20000);        //设置读取数据超时时间,单位毫秒
+            connection.setDoOutput(true);        //是否打开输出流true|false
+            connection.setDoInput(true);        //是否打开输入流true|false
+            connection.setRequestMethod("GET");        // 提交方式get|post
+            connection.setUseCaches(false);        // 是否加入缓存true|false
+            connection.connect();    //打开链接端口
+
+            BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), encoding));    // 往对端写完数据对端服务器返回数据。以BufferedReader流来读取
+
+            while ((line = reader.readLine()) != null) {
+                text += line + "\n";
+            }
+            reader.close();
+
+            JSONObject jsonObject = JSONObject.parseObject(text);
+            // 省份
+            regionPro = jsonObject.get("pro").toString();
+            map.put("regionPro", regionPro);
+            // 市区
+            regionCity = jsonObject.get("city").toString();
+            map.put("regionCity", regionCity);
+
+        } catch (MalformedURLException e) {
+            e.printStackTrace();
+        } finally {
+            if (connection != null) {
+                connection.disconnect();    //关闭连接
+            }
+        }
+        return map;
+    }
 
     private String source(String link) {
         if (link.contains("www.baidu.com")) {

+ 24 - 0
src/main/resources/mapper/CmBehaviorRecordMapper.xml

@@ -415,5 +415,29 @@
     <insert id="insertShopId">
         insert into cm_behavior_shopId (recordID, shopID) values (#{recordId}, #{shopId})
     </insert>
+    <select id="selRegisterIP" resultType="com.caimei365.tools.model.po.CmBehaviorRecordPo">
+        SELECT u.userID as userId, u.registerIP as registerIp, c.clubID as clubId, c.provinceID as provinceId , c.cityID as cityId
+        FROM USER u
+        LEFT JOIN club c ON c.userID = u.userID
+        WHERE u.userIdentity = 4 and c.clubID between #{startId} and #{endId}
+    </select>
+    <select id="selProvince" resultType="java.lang.Integer">
+        SELECT provinceID as provinceId FROM province WHERE NAME LIKE concat('%', #{regionPro} ,'%') limit 1
+    </select>
+    <select id="selCity" resultType="com.caimei365.tools.model.po.AddressPo">
+        SELECT cityID as cityId, provinceID as provinceId FROM city WHERE NAME LIKE concat('%', #{regionCity} ,'%') AND validFlag = 1 limit 1
+    </select>
+    <update id="upClubAddress">
+        update club
+        <set>
+            <if test="provinceId != null and provinceId != ''">
+                provinceID = #{provinceId},
+            </if>
+            <if test="cityId != null and cityId != ''">
+                cityID = #{cityId}
+            </if>
+        </set>
+        where clubID = #{clubId}
+    </update>
 </mapper>