|
@@ -1,18 +1,29 @@
|
|
|
package cn.hnthyy.thmz.service.impl.thmz;
|
|
|
|
|
|
+import cn.hnthyy.thmz.Utils.HttpUtil;
|
|
|
+import cn.hnthyy.thmz.entity.thmz.Config;
|
|
|
import cn.hnthyy.thmz.entity.thmz.Discount;
|
|
|
+import cn.hnthyy.thmz.mapper.thmz.ConfigMapper;
|
|
|
import cn.hnthyy.thmz.mapper.thmz.DiscountMapper;
|
|
|
import cn.hnthyy.thmz.service.thmz.DiscountService;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
+import java.net.InetAddress;
|
|
|
+import java.net.URLEncoder;
|
|
|
+import java.net.UnknownHostException;
|
|
|
import java.util.*;
|
|
|
-
|
|
|
+@Slf4j
|
|
|
@Service
|
|
|
public class DiscountServiceImpl implements DiscountService {
|
|
|
@SuppressWarnings("all")
|
|
|
@Autowired
|
|
|
private DiscountMapper discountMapper;
|
|
|
+ @SuppressWarnings("all")
|
|
|
+ @Autowired
|
|
|
+ private ConfigMapper configMapper;
|
|
|
/**
|
|
|
* 缓存
|
|
|
*/
|
|
@@ -23,6 +34,7 @@ public class DiscountServiceImpl implements DiscountService {
|
|
|
discount.setCreateTime(new Date());
|
|
|
int num = discountMapper.insert(discount);
|
|
|
getCacheMap();
|
|
|
+ refreshGroupDiscount();
|
|
|
return num;
|
|
|
}
|
|
|
@Override
|
|
@@ -51,6 +63,7 @@ public class DiscountServiceImpl implements DiscountService {
|
|
|
discount.setUpdateTime(new Date());
|
|
|
int num = discountMapper.update(discount);
|
|
|
getCacheMap();
|
|
|
+ refreshGroupDiscount();
|
|
|
return num;
|
|
|
}
|
|
|
|
|
@@ -78,6 +91,7 @@ public class DiscountServiceImpl implements DiscountService {
|
|
|
public int removeById(Long id) {
|
|
|
int num = discountMapper.deleteById(id);
|
|
|
getCacheMap();
|
|
|
+ refreshGroupDiscount();
|
|
|
return num;
|
|
|
}
|
|
|
|
|
@@ -92,4 +106,42 @@ public class DiscountServiceImpl implements DiscountService {
|
|
|
}
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
+ * 刷新服务集群缓存
|
|
|
+ */
|
|
|
+ private void refreshGroupDiscount(){
|
|
|
+ String localhost=null;
|
|
|
+ try {
|
|
|
+ localhost=InetAddress.getLocalHost().getHostAddress();
|
|
|
+ } catch (UnknownHostException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ if(localhost==null || StringUtils.isBlank(localhost)){
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ Config config= configMapper.selectConfigByKey("thmz_group");
|
|
|
+ if(config==null || config.getConfigValue()==null || StringUtils.isBlank(config.getConfigValue())){
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ String [] arr=config.getConfigValue().split(",");
|
|
|
+ if(arr==null || arr.length==0){
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ for (int i=0;i<arr.length;i++){
|
|
|
+ String ip=arr[i];
|
|
|
+ if(ip==null || StringUtils.isBlank(ip)){
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ if(localhost.equals(ip)){
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ HttpUtil.sendHttpGet(ip+"/thmz/refreshDiscount", "utf-8", 3000);
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ log.error("刷新服务集群缓存失败!");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|