From 48fb7a00951f91bdc707e5dd2d196e5bccb752c3 Mon Sep 17 00:00:00 2001
From: Administrator <admin@example.com>
Date: 星期三, 18 六月 2025 18:41:30 +0800
Subject: [PATCH] 异常保护

---
 code_attribute/code_data_util.py |  104 ++++++++++++++++++++++++++++++++-------------------
 1 files changed, 65 insertions(+), 39 deletions(-)

diff --git a/code_attribute/code_data_util.py b/code_attribute/code_data_util.py
index 1378431..b8fbd25 100644
--- a/code_attribute/code_data_util.py
+++ b/code_attribute/code_data_util.py
@@ -4,24 +4,23 @@
 
 # 鑲$エ浠g爜鐩稿叧鐨勫弬鏁�
 import decimal
-import time
 
 from code_attribute import gpcode_manager
-from db import mysql_data, redis_manager
-from db.redis_manager import RedisUtils
+from db import redis_manager_delegate as redis_manager
+from db.mysql_data_delegate import Mysqldb
+from db.redis_manager_delegate import RedisUtils
 from utils import tool
 
+__db = 0
 _redisManager = redis_manager.RedisManager(0)
 
 
 # 浠g爜瀵瑰簲鐨勪环鏍兼槸鍚︽纭�
 def is_same_code_with_price(code, price):
-    if code == '600066':
-        print('杩涘叆璋冭瘯')
     # 鏄ㄦ棩鏀剁洏浠�
-    price_close = gpcode_manager.get_price_pre(code)
-    max_price = tool.to_price(decimal.Decimal(str(price_close)) * decimal.Decimal("1.1"))
-    min_price = tool.to_price(decimal.Decimal(str(price_close)) * decimal.Decimal("0.9"))
+    price_close = gpcode_manager.CodePrePriceManager.get_price_pre_cache(code)
+    max_price = tool.to_price(decimal.Decimal(str(price_close)) * decimal.Decimal(tool.get_limit_up_rate(code)))
+    min_price = tool.to_price(decimal.Decimal(str(price_close)) * decimal.Decimal(tool.get_limit_down_rate(code)))
     if min_price <= decimal.Decimal(str(price)) <= max_price:
         return True
     return False
@@ -29,45 +28,72 @@
 
 # 鑷敱娴侀�氳偂鏈伐鍏风被
 class ZYLTGBUtil:
+    __db = 0
+    __mysql = Mysqldb()
+
     @classmethod
     def save(cls, code, val, unit):
-        redis = _redisManager.getRedis()
-        RedisUtils.setex(redis,"zyltgb-{}".format(code), tool.get_expire(),
-                    round(float(val) * 100000000) if int(unit) == 0 else round(
-                        float(val) * 10000))
+        RedisUtils.setex(_redisManager.getRedis(), "zyltgb-{}".format(code), tool.get_expire(),
+                         round(float(val) * 100000000) if int(unit) == 0 else round(
+                             float(val) * 10000))
+
+    @classmethod
+    def save_async(cls, code, val, price):
+        """
+        寮傛淇濆瓨鑷敱娴侀�氳偂鏈�
+        @param code:
+        @param val:
+        @param price:
+        @return:
+        """
+        RedisUtils.setex_async(cls.__db, "zyltgb-{}".format(code), tool.get_expire(),
+                               val)
+        if price > 0:
+            zylt_volume = int(int(val) / float(price))
+            cls.save_volume(code, zylt_volume)
+
+    @classmethod
+    def save_volume(cls, code, zylt_volume):
+        result = cls.__mysql.select_one(f"select * from kpl_zylt_volume where code = {code}")
+        if result:
+            cls.__mysql.execute(
+                f"update kpl_zylt_volume set zylt_volume = {zylt_volume}, update_time=now() where code = {code}")
+        else:
+            cls.__mysql.execute(
+                f"insert into kpl_zylt_volume(code, zylt_volume, create_time, update_time) values ('{code}',{zylt_volume},now(),now())")
+
+    @classmethod
+    def get_today_updated_volume_codes(cls):
+        """
+        鑾峰彇浠婃棩宸茬粡鏇存柊閲忕殑浠g爜
+        @return:
+        """
+        fresults = cls.__mysql.select_all(f"select code from kpl_zylt_volume where update_time >= '{tool.get_now_date_str()}'")
+        if fresults:
+            return [x[0] for x in fresults]
+        return []
+
+    @classmethod
+    def count_today_updated_volume_codes(cls):
+        """
+        鏌ヨ浠婃棩宸茬粡鏇存柊閲忕殑涓暟
+        @return:
+        """
+        fresults = cls.__mysql.select_one(f"select count(code) from kpl_zylt_volume where update_time >= '{tool.get_now_date_str()}'")
+        if fresults:
+            return fresults[0]
+        return 0
+
+
 
     @classmethod
     def get(cls, code):
-        redis = _redisManager.getRedis()
-        val = RedisUtils.get(redis, "zyltgb-{}".format(code))
+        val = RedisUtils.get(_redisManager.getRedis(), "zyltgb-{}".format(code))
         if val is not None:
             return int(val)
         return None
 
-    @classmethod
-    def save_list(self, datasList):
-        # 淇濆瓨鑷敱娴侀�氬競鍊�
-        mysqldb = mysql_data.Mysqldb()
-        for data in datasList:
-            # 淇濆瓨
-            _dict = {"_id": data["code"], "zyltgb": data["zyltgb"], "zyltgb_unit": data["zyltgb_unit"],
-                     "update_time": int(round(time.time() * 1000))}
-            if float(data["zyltgb"]) > 0:
-                # 淇濆瓨10澶�
-                ZYLTGBUtil.save(data["code"], data["zyltgb"], data["zyltgb_unit"])
-                result = mysqldb.select_one("select * from ths_zylt where _id='{}'".format(data["code"]))
-                if result is None:
-                    mysqldb.execute(
-                        "insert into ths_zylt(_id,zyltgb,zyltgb_unit,update_time) values ('{}','{}',{},{})".format(
-                            data["code"], data["zyltgb"], data["zyltgb_unit"], round(time.time() * 1000)))
-                else:
-                    mysqldb.execute(
-                        "update ths_zylt set zyltgb='{}',zyltgb_unit={},update_time={} where _id='{}'".format(
-                            data["zyltgb"], data["zyltgb_unit"], round(time.time() * 1000), data["code"]))
-
 
 if __name__ == "__main__":
-    redis = _redisManager.getRedis()
-    keys = RedisUtils.keys(redis, "zyltgb-*")
-    for key in keys:
-        RedisUtils.delete(redis, key)
+    #ZYLTGBUtil.save_async("000333", 256222112, 65.54)
+    print(ZYLTGBUtil.count_today_updated_volume_codes())

--
Gitblit v1.8.0