From ba52d7ac92a36f413eacaa686f8535e859664ec6 Mon Sep 17 00:00:00 2001
From: Administrator <admin@example.com>
Date: 星期一, 28 八月 2023 09:45:11 +0800
Subject: [PATCH] bug修改

---
 third_data/code_plate_key_manager.py |  458 +++++++++++++++++++++++++++++++--------------------------
 1 files changed, 249 insertions(+), 209 deletions(-)

diff --git a/third_data/code_plate_key_manager.py b/third_data/code_plate_key_manager.py
index 1306311..74497b6 100644
--- a/third_data/code_plate_key_manager.py
+++ b/third_data/code_plate_key_manager.py
@@ -6,29 +6,106 @@
 import json
 
 import constant
+from db.redis_manager_delegate import RedisUtils
 from third_data import kpl_block_util
 from utils import global_util, tool
 from log_module import log
-from db import redis_manager
+from db import redis_manager_delegate as redis_manager
 
-from log_module.log import logger_kpl_limit_up, logger_kpl_block_can_buy
+from log_module.log import logger_kpl_limit_up, logger_kpl_block_can_buy, logger_kpl_debug
 from third_data.kpl_util import KPLPlatManager
 from trade import trade_manager
+
+
+# 浠g爜绮鹃�夋澘鍧楃鐞�
+class KPLCodeJXBlockManager:
+    __redisManager = redis_manager.RedisManager(3)
+    __code_blocks = {}
+    # 澶囩敤
+    __code_by_blocks = {}
+
+    def __get_redis(self):
+        return self.__redisManager.getRedis()
+
+    def save_jx_blocks(self, code, blocks, by=False):
+        if blocks is None:
+            return
+        if len(blocks) > 2:
+            blocks = blocks[:2]
+
+        # 淇濆瓨鍓�2鏉℃暟鎹�
+        if by:
+            RedisUtils.setex(self.__get_redis(), f"kpl_jx_blocks_by-{code}", tool.get_expire(), json.dumps(blocks))
+            self.__code_by_blocks[code] = blocks
+        else:
+            RedisUtils.setex(self.__get_redis(), f"kpl_jx_blocks-{code}", tool.get_expire(), json.dumps(blocks))
+            self.__code_blocks[code] = blocks
+
+    # 鑾峰彇绮鹃�夋澘鍧�
+    def get_jx_blocks(self, code, by=False):
+        if by:
+            if code in self.__code_by_blocks:
+                return self.__code_by_blocks[code]
+            val = RedisUtils.get(self.__get_redis(), f"kpl_jx_blocks_by-{code}")
+            if val is None:
+                return None
+            else:
+                val = json.loads(val)
+                self.__code_by_blocks[code] = val
+            return self.__code_by_blocks[code]
+        else:
+            if code in self.__code_blocks:
+                return self.__code_blocks[code]
+            val = RedisUtils.get(self.__get_redis(), f"kpl_jx_blocks-{code}")
+            if val is None:
+                return None
+            else:
+                val = json.loads(val)
+                self.__code_blocks[code] = val
+            return self.__code_blocks[code]
+
+    def get_jx_blocks_cache(self, code, by=False):
+        if by:
+            return self.__code_by_blocks.get(code)
+        else:
+            return self.__code_blocks.get(code)
 
 
 # 寮�鐩樺暒绂佹浜ゆ槗鏉垮潡绠$悊
 class KPLPlateForbiddenManager:
     __redisManager = redis_manager.RedisManager(3)
+    __kpl_forbidden_plates_cache = set()
 
-    def __get_redis(self):
-        return self.__redisManager.getRedis()
+    __instance = None
+
+    def __new__(cls, *args, **kwargs):
+        if not cls.__instance:
+            cls.__instance = super(KPLPlateForbiddenManager, cls).__new__(cls, *args, **kwargs)
+            cls.__load_datas()
+        return cls.__instance
+
+    @classmethod
+    def __load_datas(cls):
+        __redis = cls.__get_redis()
+        try:
+            __kpl_forbidden_plates_cache = RedisUtils.smembers(__redis, "kpl_forbidden_plates")
+        finally:
+            RedisUtils.realse(__redis)
+
+    @classmethod
+    def __get_redis(cls):
+        return cls.__redisManager.getRedis()
 
     def save_plate(self, plate):
-        self.__get_redis().sadd("kpl_forbidden_plates", plate)
-        self.__get_redis().expire("kpl_forbidden_plates", tool.get_expire())
+        self.__kpl_forbidden_plates_cache.add(plate)
+        RedisUtils.sadd(self.__get_redis(), "kpl_forbidden_plates", plate)
+        RedisUtils.expire(self.__get_redis(), "kpl_forbidden_plates", tool.get_expire())
 
     def list_all(self):
-        return self.__get_redis().smembers("kpl_forbidden_plates")
+        return RedisUtils.smembers(self.__get_redis(), "kpl_forbidden_plates")
+
+    def list_all_cache(self):
+        return self.__kpl_forbidden_plates_cache
 
 
 class LimitUpCodesPlateKeyManager:
@@ -62,16 +139,18 @@
 
     # 浠婃棩娑ㄥ仠鍘熷洜鍙樺寲
     def set_today_limit_up_reason_change(self, code, from_reason, to_reason):
-        self.__get_redis().sadd(f"kpl_limit_up_reason_his-{code}", from_reason)
-        self.__get_redis().expire(f"kpl_limit_up_reason_his-{code}", tool.get_expire())
+        RedisUtils.sadd(self.__get_redis(), f"kpl_limit_up_reason_his-{code}", from_reason)
+        RedisUtils.expire(self.__get_redis(), f"kpl_limit_up_reason_his-{code}", tool.get_expire())
         self.__set_total_keys(code)
 
+    # 璁剧疆浠g爜鐨勪粖鏃ユ定鍋滃師鍥�
     def __set_total_keys(self, code):
         keys = set()
-        keys_his = self.__get_redis().smembers(f"kpl_limit_up_reason_his-{code}")
-        keys |= keys_his
+        # keys_his = self.__get_redis().smembers(f"kpl_limit_up_reason_his-{code}")
+        # keys |= keys_his
         if code in self.today_limit_up_reason_dict:
-            keys.add(self.today_limit_up_reason_dict.get(code))
+            if self.today_limit_up_reason_dict.get(code) not in constant.KPL_INVALID_BLOCKS:
+                keys.add(self.today_limit_up_reason_dict.get(code))
         self.total_code_keys_dict[code] = keys
         for k in keys:
             if k not in self.total_key_codes_dict:
@@ -79,6 +158,8 @@
             self.total_key_codes_dict[k].add(code)
 
         logger_kpl_limit_up.info("{}鏉垮潡鍏抽敭璇�:{}", code, keys)
+
+    # 鏍规嵁浼犲叆鐨勫叧閿瘝涓庢定鍋滀唬鐮佷俊鎭尮閰嶈韩浣�
 
     def get_codes_by_key_without_mine(self, key, code):
         # 鍙瘮杈冧粖鏃ユ定鍋滃師鍥�
@@ -184,7 +265,7 @@
             # 鍚庢帓鎵嶈兘鎸傚崟
             return 0, "韬綅涓嶄负鍚庢帓"
 
-        forbidden_plates = cls.__KPLPlateForbiddenManager.list_all()
+        forbidden_plates = cls.__KPLPlateForbiddenManager.list_all_cache()
         if key in forbidden_plates:
             return 0, "涓嶄拱璇ユ澘鍧�"
 
@@ -203,7 +284,7 @@
     def is_in_top(cls, keys):
         reasons = cls.get_can_buy_key_set()
         log.logger_kpl_debug.debug("甯傚満娴佸叆鍓�5:{}", reasons)
-        forbidden_plates = cls.__KPLPlateForbiddenManager.list_all()
+        forbidden_plates = cls.__KPLPlateForbiddenManager.list_all_cache()
         reasons = reasons - forbidden_plates
         temp_set = keys & reasons
         log.logger_kpl_debug.debug("甯傚満娴佸叆鍓�5鍖归厤缁撴灉:{}", temp_set)
@@ -226,15 +307,16 @@
 
     def set_history_limit_up_reason(self, code, reasons):
         self.__history_limit_up_reason_dict[code] = set(reasons)
-        self.__get_redis().setex(f"kpl_his_limit_up_reason-{code}", tool.get_expire(), json.dumps(list(reasons)))
+        RedisUtils.setex(self.__get_redis(), f"kpl_his_limit_up_reason-{code}", tool.get_expire(),
+                         json.dumps(list(reasons)))
+        logger_kpl_debug.debug(f"璁剧疆鍘嗗彶娑ㄥ仠鍘熷洜锛歿code}-{reasons}")
 
-        # 濡傛灉杩斿洖鍊间笉涓篘one琛ㄧず宸茬粡鍔犺浇杩囧巻鍙插師鍥犱簡
-
+    # 濡傛灉杩斿洖鍊间笉涓篘one琛ㄧず宸茬粡鍔犺浇杩囧巻鍙插師鍥犱簡
     def get_history_limit_up_reason(self, code):
         reasons = self.__history_limit_up_reason_dict.get(code)
         if reasons is None:
             # 浠庡唴瀛樹腑鍔犺浇
-            val = self.__get_redis().get(f"kpl_his_limit_up_reason-{code}")
+            val = RedisUtils.get(self.__get_redis(), f"kpl_his_limit_up_reason-{code}")
             if val is not None:
                 val = set(json.loads(val))
                 self.__history_limit_up_reason_dict[code] = val
@@ -245,15 +327,19 @@
         else:
             return reasons
 
+    def get_history_limit_up_reason_cache(self, code):
+        reasons = self.__history_limit_up_reason_dict.get(code)
+        return reasons
+
     def set_blocks(self, code, blocks):
         self.__blocks_dict[code] = set(blocks)
-        self.__get_redis().setex(f"kpl_blocks-{code}", tool.get_expire(), json.dumps(list(blocks)))
+        RedisUtils.setex(self.__get_redis(), f"kpl_blocks-{code}", tool.get_expire(), json.dumps(list(blocks)))
 
     def get_blocks(self, code):
         reasons = self.__blocks_dict.get(code)
         if reasons is None:
             # 浠庡唴瀛樹腑鍔犺浇
-            val = self.__get_redis().get(f"kpl_blocks-{code}")
+            val = RedisUtils.get(self.__get_redis(), f"kpl_blocks-{code}")
             if val is not None:
                 val = set(json.loads(val))
                 self.__blocks_dict[code] = val
@@ -278,19 +364,20 @@
 class TargetCodePlateKeyManager:
     __redisManager = redis_manager.RedisManager(1)
     __CodesPlateKeysManager = CodesHisReasonAndBlocksManager()
+    __KPLCodeJXBlockManager = KPLCodeJXBlockManager()
 
     def __get_redis(self):
         return self.__redisManager.getRedis()
 
-    # 杩斿洖key闆嗗悎(鎺掗櫎鏃犳晥鏉垮潡),浠婃棩娑ㄥ仠鍘熷洜,浠婃棩鍘嗗彶娑ㄥ仠鍘熷洜,鍘嗗彶娑ㄥ仠鍘熷洜,浜岀骇,鏉垮潡
+    # 杩斿洖key闆嗗悎(鎺掗櫎鏃犳晥鏉垮潡),浠婃棩娑ㄥ仠鍘熷洜,浠婃棩鍘嗗彶娑ㄥ仠鍘熷洜,鍘嗗彶娑ㄥ仠鍘熷洜,浜岀骇,绮鹃�夋澘鍧�
     def get_plate_keys(self, code):
         keys = set()
         k1 = set()
         if code in LimitUpCodesPlateKeyManager.today_total_limit_up_reason_dict:
             k1 = {LimitUpCodesPlateKeyManager.today_total_limit_up_reason_dict[code]}
-        # 鍔犺浇浠婃棩鍘嗗彶鍘熷洜
-        k11 = self.__get_redis().smembers(f"kpl_limit_up_reason_his-{code}")
-        k2 = self.__CodesPlateKeysManager.get_history_limit_up_reason(code)
+        # 鍔犺浇浠婃棩鍘嗗彶鍘熷洜,鏆傛椂涓嶉渶瑕佸巻鍙插師鍥犱簡
+        k11 = set()  # RedisUtils.smembers(self.__get_redis(), f"kpl_limit_up_reason_his-{code}")
+        k2 = self.__CodesPlateKeysManager.get_history_limit_up_reason_cache(code)
         if k2 is None:
             k2 = set()
         k3 = set()
@@ -298,9 +385,13 @@
         if industry:
             k3 = {industry}
 
-        k4 = self.__CodesPlateKeysManager.get_blocks(code)
-        if k4 is None:
-            k4 = set()
+        k4 = set()
+        jingxuan_blocks = self.__KPLCodeJXBlockManager.get_jx_blocks_cache(code)
+        if not jingxuan_blocks:
+            jingxuan_blocks = self.__KPLCodeJXBlockManager.get_jx_blocks_cache(code, by=True)
+        if jingxuan_blocks:
+            jingxuan_blocks = jingxuan_blocks[:2]
+            k4 |= set([x[1] for x in jingxuan_blocks])
         for k in [k1, k11, k2, k3, k4]:
             keys |= k
 
@@ -325,13 +416,27 @@
     __TargetCodePlateKeyManager = TargetCodePlateKeyManager()
     __LimitUpCodesPlateKeyManager = LimitUpCodesPlateKeyManager()
     __CodesHisReasonAndBlocksManager = CodesHisReasonAndBlocksManager()
+    __CodesTradeStateManager = trade_manager.CodesTradeStateManager()
+    __can_buy_compute_result_dict = {}
 
     # 鑾峰彇鍙互涔扮殑鏉垮潡
     # current_limit_up_datas: 浠婃棩瀹炴椂娑ㄥ仠
     # latest_2_day_limit_up_datas锛氭渶杩�2澶╃殑瀹炴椂娑ㄥ仠锛堜笉鍚粖鏃ワ級
     # limit_up_record_datas锛氫粖鏃ュ巻鍙叉定鍋�
+    # yesterday_current_limit_up_codes 锛� 鏄ㄦ棩娑ㄥ仠浠g爜
+    # before_blocks_dict锛氬巻鍙叉定鍋滃師鍥�
     @classmethod
-    def get_can_buy_block(cls, code, current_limit_up_datas, latest_2_day_limit_up_datas, limit_up_record_datas):
+    def get_can_buy_block(cls, code, current_limit_up_datas, limit_up_record_datas, yesterday_current_limit_up_codes,
+                          before_blocks_dict):
+        # 鍔犺浇娑ㄥ仠浠g爜鐨勭洰鏍囨澘鍧�
+        def load_code_block():
+            for d in limit_up_record_datas:
+                if d[2] in constant.KPL_INVALID_BLOCKS and d[3] in before_blocks_dict:
+                    code_limit_up_reason_dict[d[3]] = list(before_blocks_dict.get(d[3]))[0]
+                else:
+                    code_limit_up_reason_dict[d[3]] = d[2]
+            return code_limit_up_reason_dict
+
         now_time = int(tool.get_now_time_str().replace(":", ""))
         times = [100000, 103000, 110000, 133000, 150000]
         time_index = 0
@@ -339,179 +444,117 @@
             if now_time < times[i]:
                 time_index = i
                 break
-        # 鑾峰彇鏉垮潡
+        # 鑾峰彇鐩爣浠g爜鏉垮潡
         keys, k1, k11, k2, k3, k4 = cls.__TargetCodePlateKeyManager.get_plate_keys(code)
-        log.logger_kpl_debug.info("{}鍏抽敭璇嶏細鎵�鏈�-{},浠婃棩-{},浠婃棩鍘嗗彶-{},鍘嗗彶-{},浜岀骇琛屼笟-{},浠g爜鏉垮潡-{}", code, keys, k1, k11, k2, k3, k4)
+        log.logger_kpl_debug.info("{}鍏抽敭璇嶏細浠婃棩-{},浠婃棩鍘嗗彶-{},鍘嗗彶-{},浜岀骇琛屼笟-{},浠g爜鏉垮潡-{}", code, k1, k11, k2, k3, k4)
+        keys = set()
+        if k1:
+            for k in k1:
+                if k not in constant.KPL_INVALID_BLOCKS:
+                    keys.add(k)
+        # 濮嬬粓鑾峰彇绮鹃�夋澘鍧�
+        if True:
+            # 鑾峰彇
+            if k4:
+                keys |= k4
+                keys = keys - constant.KPL_INVALID_BLOCKS
+
+        log.logger_kpl_debug.info("{}鏈�缁堝叧閿瘝锛歿}", code, keys)
+
         # 娑ㄥ仠鍒楄〃涓尮閰嶅叧閿瘝锛岃繑鍥烇紙鏉垮潡:浠g爜闆嗗悎锛夛紝浠g爜闆嗗悎涓凡缁忔帓闄よ嚜韬�
-        match_limit_up_result = cls.__LimitUpCodesPlateKeyManager.match_limit_up_reason_keys(code, keys)
-        log.logger_kpl_debug.info("{}鍏抽敭璇嶈韩浣嶅尮閰嶇粨鏋滐細{}", code, match_limit_up_result)
-        if not match_limit_up_result:
-            return cls.BLOCK_TYPE_NONE, None, "鏈湪娑ㄥ仠鍒楄〃涓湭鍖归厤鍒版定鍋滃師鍥�"
-        # 鑾峰彇鏉垮潡褰掔被
-        for block in match_limit_up_result:
-            # 鑾峰彇寮哄娍鏉垮潡
-            strong_result = kpl_block_util.is_strong_block(block, current_limit_up_datas, latest_2_day_limit_up_datas)
-            # 鑾峰彇鐚涙媺鏉垮潡
-            soon_limit_up_result = kpl_block_util.is_soon_limit_up(code, block, limit_up_record_datas)
+        if not keys:
+            return None, "灏氭湭鎵惧埌娑ㄥ仠鍘熷洜"
+        code_limit_up_reason_dict = {}
+        load_code_block()
+        msg_list = []
 
-            # 鑾峰彇韬綅
-            rank = kpl_block_util.get_code_rank(code, block, limit_up_record_datas)
-            # 涓绘澘韬綅
-            sh_sz_rank = kpl_block_util.get_sh_sz_code_rank(code, block, limit_up_record_datas)
-            # 鏄惁鍚庢帓
-            is_back_row = kpl_block_util.is_back_row(code, block, current_limit_up_datas)
+        can_buy_blocks = []
+        for block in keys:
+            log.logger_kpl_debug.info(f"鍒ゆ柇鏉垮潡鏄惁鍙拱锛歿block}")
+            # is_top_8_record, top_8_record = kpl_block_util.is_record_top_block(code, block, limit_up_record_datas,
+            #                                                                    yesterday_current_limit_up_codes, 50)
+            # is_top_4_current, top_4_current = kpl_block_util.is_current_top_block(code, block, current_limit_up_datas,
+            #                                                                       yesterday_current_limit_up_codes, 50)
+            # is_top_4 = is_top_8_record and is_top_4_current
+            # msg_list.append(f"\n瀹炴椂top10(娑ㄥ仠鏁伴噺锛歿len(current_limit_up_datas)})")
+            # msg_list.append(f"鍘嗗彶top20(娑ㄥ仠鏁伴噺锛歿len(top_8_record)})")
 
-            # 鏄惁婊¤冻甯傚満娴佸叆鍓嶅嚑
-            is_in_top_input = RealTimeKplMarketData.is_in_top(set([block]))[0]
-
-            log.logger_kpl_debug.info("{}-{}  鏉垮潡鍒ゆ柇缁撴灉锛氬己鍔挎澘鍧�-{}  鐚涙媺鏉垮潡-{} 韬綅-{} 涓绘澘韬綅-{} 鏄惁鍚庢帓-{} 鏄惁鍦ㄦ祦鍏ュ墠鎺�-{}", code, block,
-                                      strong_result, soon_limit_up_result, rank, sh_sz_rank, is_back_row,
-                                      is_in_top_input)
-
-            if time_index == 0:
-                # 09:30:00 - 10:00:00
-                if strong_result[0]:
-                    # 寮哄娍鏉垮潡
-                    # 涔颁富鏉块緳1,2,3,4  涔板悗鎺�
-                    if is_back_row and sh_sz_rank <= 3:
-                        return cls.BLOCK_TYPE_STRONG, block, f"{block} 寮哄娍鏉垮潡:涔颁富鏉块緳1,2,3,4  涔板悗鎺�"
-
-                if soon_limit_up_result[0]:
-                    # 鐚涙媺鏉垮潡
-                    # 鍙拱榫�2 涔板悗鎺�
-                    if is_back_row and rank == 1:
-                        return cls.BLOCK_TYPE_SOON_LIMIT_UP, block, f"{block} 鐚涙媺鏉垮潡:鍙拱榫�2,涔板悗鎺�"
-                # 鍏朵粬鏉垮潡
-                if is_in_top_input and sh_sz_rank <= 1 and is_back_row:
-                    # 鐪嬬簿閫�/琛屼笟娴佸叆   涔伴緳涓绘澘1,2  涔板悗鎺�
-                    return cls.BLOCK_TYPE_COMMON, block, f"{block} 鍏朵粬鏉垮潡:鐪嬬簿閫�/琛屼笟娴佸叆   涔伴緳涓绘澘1,2  涔板悗鎺�"
-
-            elif time_index == 1:
-                # 10:00:00 - 10:30:00
-                if strong_result[0]:
-                    # 寮哄娍鏉垮潡
-                    # 涔颁富鏉块緳1,2,3  涔板悗鎺�
-                    if is_back_row and sh_sz_rank <= 2:
-                        return cls.BLOCK_TYPE_STRONG, block, f"{block} 寮哄娍鏉垮潡:涔颁富鏉块緳1,2,3  涔板悗鎺�"
-
-                if soon_limit_up_result[0]:
-                    # 鐚涙媺鏉垮潡
-                    # 鍙拱榫�2 涔板悗鎺�
-                    if is_back_row and rank == 1:
-                        return cls.BLOCK_TYPE_SOON_LIMIT_UP, block, f"{block} 鐚涙媺鏉垮潡:鍙拱榫�2,涔板悗鎺�"
-                # 鍏朵粬鏉垮潡
-                if is_in_top_input and sh_sz_rank <= 1 and is_back_row:
-                    # 鐪嬬簿閫�/琛屼笟娴佸叆   涔伴緳涓绘澘1,2  涔板悗鎺�
-                    return cls.BLOCK_TYPE_COMMON, block, f"{block} 鍏朵粬鏉垮潡:鐪嬬簿閫�/琛屼笟娴佸叆   涔伴緳涓绘澘1,2  涔板悗鎺�"
-            elif time_index == 2:
-                # 10:30:00 - 11:00:00
-                if strong_result[0]:
-                    # 寮哄娍鏉垮潡
-                    # 涔颁富鏉块緳1,2  涔板悗鎺�
-                    if is_back_row and sh_sz_rank <= 1:
-                        return cls.BLOCK_TYPE_STRONG, block, f"{block} 寮哄娍鏉垮潡:涔颁富鏉块緳1,2  涔板悗鎺�"
-
-                if soon_limit_up_result[0]:
-                    # 鐚涙媺鏉垮潡
-                    # 鍙拱榫�2 涔板悗鎺�
-                    if is_back_row and rank == 1:
-                        return cls.BLOCK_TYPE_SOON_LIMIT_UP, block, f"{block} 鐚涙媺鏉垮潡:鍙拱榫�2,涔板悗鎺�"
-                # 鍏朵粬鏉垮潡
-                if is_in_top_input and sh_sz_rank <= 1 and is_back_row:
-                    # 鐪嬬簿閫�/琛屼笟娴佸叆   涔伴緳涓绘澘1,2  涔板悗鎺�
-                    return cls.BLOCK_TYPE_COMMON, block, f"{block} 鍏朵粬鏉垮潡:鐪嬬簿閫�/琛屼笟娴佸叆   涔伴緳涓绘澘1,2  涔板悗鎺�"
-            elif time_index == 3:
-                # 11:00:00 - 13:30:00
-                if soon_limit_up_result[0]:
-                    # 鐚涙媺鏉垮潡
-                    # 鍙拱榫�2 涔板悗鎺�
-                    if is_back_row and rank == 1:
-                        return cls.BLOCK_TYPE_SOON_LIMIT_UP, block, f"{block} 鐚涙媺鏉垮潡:鍙拱榫�2,涔板悗鎺�"
-                # 鍏朵粬鏉垮潡
-                if is_in_top_input and sh_sz_rank <= 1 and is_back_row:
-                    # 鐪嬬簿閫�/琛屼笟娴佸叆   涔伴緳涓绘澘1,2  涔板悗鎺�
-                    return cls.BLOCK_TYPE_COMMON, block, f"{block} 鍏朵粬鏉垮潡锛氱湅绮鹃��/琛屼笟娴佸叆,涔伴緳涓绘澘1,2 ,涔板悗鎺�"
-            elif time_index == 4:
-                # 13:30:00 - 15:00:00
-                if soon_limit_up_result[0]:
-                    # 鐚涙媺鏉垮潡
-                    # 鍙拱榫�2 涔板悗鎺�
-                    if is_back_row and rank == 1:
-                        return cls.BLOCK_TYPE_SOON_LIMIT_UP, block, f"{block} 鐚涙媺鏉垮潡锛氬彧涔伴緳2,涔板悗鎺�"
-                # 鍏朵粬鏉垮潡
-                if is_in_top_input:
-                    # 绮鹃��/琛屼笟娴佸叆绗﹀悎
-                    if sh_sz_rank <= 1 and is_back_row:
-                        # 鐪嬬簿閫�/琛屼笟娴佸叆   涔伴緳涓绘澘1,2  涔板悗鎺�
-                        return cls.BLOCK_TYPE_COMMON, block, f"{block} 鍏朵粬鏉垮潡锛氱簿閫�/琛屼笟娴佸叆绗﹀悎,涔伴緳涓绘澘1,2,涔板悗鎺�"
+            # 鑾峰彇涓绘澘瀹炴椂韬綅,鍓旈櫎楂樹綅鏉�
+            current_shsz_rank = kpl_block_util.get_code_current_rank(code, block, current_limit_up_datas,
+                                                                     code_limit_up_reason_dict,
+                                                                     yesterday_current_limit_up_codes, shsz=True)
+            record_shsz_rank = kpl_block_util.get_code_record_rank(code, block, limit_up_record_datas,
+                                                                   code_limit_up_reason_dict,
+                                                                   yesterday_current_limit_up_codes, shsz=True)
+            # 鑾峰彇涓绘澘鍘嗗彶韬綅
+            if True:
+                pen_limit_up_codes = kpl_block_util.get_shsz_open_limit_up_codes(code, block, limit_up_record_datas,
+                                                                                 code_limit_up_reason_dict)
+                if pen_limit_up_codes:
+                    # 涓绘澘寮�1
+                    if current_shsz_rank < len(pen_limit_up_codes) + 1 and record_shsz_rank < len(
+                            pen_limit_up_codes) + 1:
+                        # 灞炰簬榫�1,榫�2
+                        can_buy_blocks.append((block,
+                                               f"{block}锛歵op10娑ㄥ仠鏉垮潡锛屼富鏉垮紑1({pen_limit_up_codes}),灞炰簬涓绘澘鍓嶉緳{len(pen_limit_up_codes) + 1}(瀹炴椂韬綅-{current_shsz_rank})"))
+                        continue
+                    else:
+                        msg_list.append(
+                            f"鏉垮潡-{block}: top4娑ㄥ仠鏉垮潡锛屼富鏉垮紑1锛坽pen_limit_up_codes}锛�,涓嶄负涓绘澘鍓嶉緳{len(pen_limit_up_codes) + 1}锛堝疄鏃惰韩浣�-{current_shsz_rank},鍘嗗彶韬綅-{record_shsz_rank}锛�")
+                        continue
                 else:
-                    if sh_sz_rank == 0 and not is_back_row:
-                        return cls.BLOCK_TYPE_START_UP, block, f"{block} 鍏朵粬鏉垮潡: 涔颁富鏉块緳1,涔颁富鏉跨嫭鑻�"
+                    if current_shsz_rank == 0 and record_shsz_rank < 2:
+                        can_buy_blocks.append((block, f"{block}锛歵op4娑ㄥ仠鏉垮潡锛岄潪涓绘澘寮�1锛屽睘浜庨緳1"))
+                        continue
+                    else:
+                        msg_list.append(
+                            f"鏉垮潡-{block}: top4娑ㄥ仠鏉垮潡锛岄潪涓绘澘寮�1,涓嶄负涓绘澘榫�1锛堝疄鏃惰韩浣�-{current_shsz_rank},鍘嗗彶韬綅-{record_shsz_rank}锛�")
+                        continue
+            else:
+                pass
+                # # 鏄惁婊¤冻琛屼笟绮鹃�夋祦鍏ヨ姹�
+                # is_in_top_input = RealTimeKplMarketData.is_in_top(set([block]))[0]
+                # if not is_in_top_input:
+                #     msg_list.append(
+                #         f"鏉垮潡-{block}: 闈瀟op4娑ㄥ仠鏉垮潡锛屼笉婊¤冻绮鹃��/琛屼笟娴佸叆瑕佹眰")
+                #     continue
+                # else:
+                #     # 鏄惁涓轰富鏉块緳1锛堝疄鏃堕緳1锛屽巻鍙查緳2浠ュ唴锛�
+                #     if current_shsz_rank == 0 and record_shsz_rank < 2:
+                #         can_buy_blocks.append((block, f"{block}锛氫笉鏄痶op4娑ㄥ仠鏉垮潡锛屾弧瓒崇簿閫�/琛屼笟娴佸叆瑕佹眰锛屾弧瓒充富鏉块緳1"))
+                #         continue
+                #     else:
+                #         msg_list.append(
+                #             f"鏉垮潡-{block}: 涓嶆槸top4娑ㄥ仠鏉垮潡锛屾弧瓒崇簿閫�/琛屼笟娴佸叆瑕佹眰,涓嶄负涓绘澘榫�1锛堝疄鏃惰韩浣�-{current_shsz_rank},鍘嗗彶韬綅-{record_shsz_rank}锛�")
+                #         continue
+        if len(can_buy_blocks) == len(keys):
+            blocks = [x[0] for x in can_buy_blocks]
+            blocks_msg = "\n".join([x[1] for x in can_buy_blocks])
+            return blocks, blocks_msg
 
-        return cls.BLOCK_TYPE_NONE, None, f"鏉垮潡({match_limit_up_result.keys()})涓嶇鍚堜拱鍏ユ潯浠�"
+        return None, "\n".join(msg_list)
 
     # 鏄惁鍙互涓嬪崟
     # 杩斿洖锛氭槸鍚﹀彲浠ヤ笅鍗�,娑堟伅,鏉垮潡绫诲瀷
     @classmethod
-    def can_buy(cls, code, current_limit_up_datas, latest_2_day_limit_up_datas, limit_up_record_datas):
-        if constant.TEST:
-            return True, "", cls.BLOCK_TYPE_NONE
-        block_type, block, block_msg = cls.get_can_buy_block(code, current_limit_up_datas, latest_2_day_limit_up_datas,
-                                                             limit_up_record_datas)
-        if block_type == cls.BLOCK_TYPE_NONE:
-            return False, block_msg, block_type
+    def can_buy(cls, code):
+        # if constant.TEST:
+        #     return True, cls.BLOCK_TYPE_NONE
+        return cls.__can_buy_compute_result_dict.get(code)
 
-        # ---------------------------------鍒ゆ柇鐩爣浠g爜鐨勬澘鍧�-------------------start------------
-        # 鍒ゆ柇鍖归厤鍑虹殑娑ㄥ仠鍘熷洜锛屽垽鏂槸鍚︽湁宸茬粡涓嬪崟鐨勭エ
-        # reason_need_buy_dict = {}
-        # for k in match_limit_up_result:
-        #     codes = match_limit_up_result[k]
-        #     final_codes_keys = [keys]
-        #     for code_ in codes:
-        #         temp_key_set = set()
-        #         temp_key_set |= cls.__CodesHisReasonAndBlocksManager.get_total_keys(code_)
-        #         temp = cls.__LimitUpCodesPlateKeyManager.total_code_keys_dict.get(code_)
-        #         if temp:
-        #             temp_key_set |= temp
-        #         # 浜岀骇
-        #         industry = global_util.code_industry_map.get(code_)
-        #         if industry:
-        #             temp_key_set.add(industry)
-        #
-        #         final_codes_keys.append(temp_key_set)
-        #     # 姹傚叡鍚岀殑鍏抽敭璇�
-        #     intersection = set(final_codes_keys[0])
-        #     for s in final_codes_keys:
-        #         intersection &= s
-        #     log.logger_kpl_debug.info("{}鐨勬澘鍧楁眰浜ら泦锛歿}-{}", code, k, intersection)
-        #
-        #     # 姹傚叕鍏辩殑鏉垮潡鏄惁鍦ㄦ祦鍏ュ墠5涓�
-        #     is_in, valid_keys = RealTimeKplMarketData.is_in_top(intersection)
-        #     if is_in:
-        #         reason_need_buy_dict[k] = (is_in, valid_keys)
-        # ---------------------------------鍒ゆ柇鐩爣浠g爜鐨勬澘鍧�-------------------end------------
+    @classmethod
+    def __compute_can_buy_blocks(cls, code, current_limit_up_datas, limit_up_record_datas,
+                                 yesterday_current_limit_up_codes, before_blocks_dict):
 
-        # 鑾峰彇鏉垮潡鍙互涓嬪崟鐨勪釜鏁�
-        # can_buy_codes_count_dict = {}
-        #
-        # for key__ in match_limit_up_result:
-        #     can_buy_count, msg = RealTimeKplMarketData.get_can_buy_codes_count(code, key__)
-        #     can_buy_codes_count_dict[key__] = can_buy_count
-
-        # has_available_key = False
-        # for key in can_buy_codes_count_dict:
-        #     if can_buy_codes_count_dict[key] > 0:
-        #         has_available_key = True
-        #         break
-        # if not has_available_key:
-        #     return False, f"鍖归厤鍒扮殑銆恵','.join(match_limit_up_result.keys())}銆戞病鍦ㄧ簿閫�/琛屼笟鍙互涔板叆鐨勬澘鍧椾腑"
-
-        # ---------------------------------鍔犺浇宸茬粡涓嬪崟/鎴愪氦鐨勪唬鐮佷俊鎭�------------start-------------
-        # match_reasons = match_limit_up_result.keys()
-        # 鍒ゆ柇鍖归厤鍒扮殑鍘熷洜鏄惁宸茬粡鏈変笅鍗�/涔板叆鎴愬姛鐨勪唬鐮�
-        codes_delegate = set(trade_manager.get_codes_by_trade_states(
+        blocks, block_msg = cls.get_can_buy_block(code, current_limit_up_datas,
+                                                  limit_up_record_datas, yesterday_current_limit_up_codes,
+                                                  before_blocks_dict)
+        if not blocks:
+            return False, block_msg
+        log.logger_kpl_debug.info(f"{code}:鑾峰彇濮旀墭/涔板叆浠g爜")
+        codes_delegate = set(cls.__CodesTradeStateManager.get_codes_by_trade_states_cache(
             {trade_manager.TRADE_STATE_BUY_DELEGATED, trade_manager.TRADE_STATE_BUY_PLACE_ORDER}))
-        codes_success = set(trade_manager.get_codes_by_trade_states(
+        codes_success = set(cls.__CodesTradeStateManager.get_codes_by_trade_states_cache(
             {trade_manager.TRADE_STATE_BUY_SUCCESS}))
 
         codes = codes_delegate | codes_success
@@ -520,11 +563,13 @@
         trade_codes_blocks_dict = {}
         # 宸茬粡鎴愪氦鐨勬澘鍧�
         trade_success_blocks_count = {}
+        log.logger_kpl_debug.info(f"{code}:鑾峰彇浠g爜鏉垮潡")
         for c in codes:
             keys_, k1_, k11_, k2_, k3_, k4_ = cls.__TargetCodePlateKeyManager.get_plate_keys(c)
             # 瀹炴椂娑ㄥ仠鍘熷洜
-            trade_codes_blocks_dict[c] = k1_
+            trade_codes_blocks_dict[c] = k1_ | k4_
         # 缁熻鏉垮潡涓殑浠g爜
+        log.logger_kpl_debug.info(f"{code}:缁熻鏉垮潡涓殑浠g爜")
         trade_block_codes_dict = {}
         for c in trade_codes_blocks_dict:
             for b in trade_codes_blocks_dict[c]:
@@ -537,34 +582,29 @@
                 trade_block_codes_dict[b].add(c)
 
         # ---------------------------------鍔犺浇宸茬粡涓嬪崟/鎴愪氦鐨勪唬鐮佷俊鎭�------------end-------------
-
+        log.logger_kpl_debug.info(f"{code}:寮�濮嬭绠楁槸鍚﹀彲浠ヤ拱")
         msg_list = []
-        for key in [block]:
+        for key in blocks:
             # 鏉垮潡涓凡缁忔湁鎴愪氦鐨勫氨涓嶄笅鍗曚簡
             if key in trade_success_blocks_count:
                 success_codes_count = len(trade_success_blocks_count[key])
-                if success_codes_count >= 2:
+                if success_codes_count >= 1:
                     msg_list.append(f"銆恵key}銆戜腑宸茬粡鏈墈success_codes_count}涓垚浜や唬鐮�")
                     log.logger_kpl_debug.debug(f"{code}锛氭澘鍧楋紙{key}锛夊凡缁忔湁鎴愪氦銆恵trade_success_blocks_count[key]}銆�")
                     continue
-                # 10:30浠ュ悗涔�1涓�
-                if int(tool.get_now_time_str().replace(":", "")) > int("103000") and success_codes_count >= 1:
-                    msg_list.append(f"銆恵key}銆戜腑宸茬粡鏈墈success_codes_count}涓垚浜や唬鐮�")
-                    log.logger_kpl_debug.debug(f"{code}锛氭澘鍧楋紙{key}锛夊凡缁忔湁鎴愪氦銆恵trade_success_blocks_count[key]}銆�")
-                    continue
-            return True, block_msg, block_type
-            # 鏉垮潡鍙互涓嬪崟鏁伴噺
-            # if trade_block_codes_dict.get(key) is None or len(trade_block_codes_dict.get(key)) < \
-            #         can_buy_codes_count_dict[key]:
-            #     order_count = len(trade_block_codes_dict.get(key)) if key in trade_block_codes_dict else 0
-            #     logger_kpl_block_can_buy.info(
-            #         f"code={code}锛氥�恵key}銆戝彲浠ヤ笅鍗曪紝鐜版湁鏁伴噺锛歿order_count} 鏈�澶ф暟閲忥細{can_buy_codes_count_dict[key]}")
-            #     return True, f"鍙互涓嬪崟锛屾澘鍧�:銆恵key}銆�,鏉垮潡涓凡缁忎笅鍗曠殑鏁伴噺锛歿order_count}"
-            # else:
-            #     order_count = len(trade_block_codes_dict.get(key))
-            #     msg_list.append(f"銆恵key}銆戜腑涓嬪崟浠g爜鏁伴噺{order_count}/鍏佽涓嬪崟鏁伴噺{can_buy_codes_count_dict[key]}")
+            return True, block_msg
 
-        return False, ",".join(msg_list), block_type
+        return False, ",".join(msg_list)
+
+    @classmethod
+    def update_can_buy_blocks(cls, code, current_limit_up_datas, limit_up_record_datas,
+                              yesterday_current_limit_up_codes,
+                              before_blocks_dict):
+        can_buy, msg = cls.__compute_can_buy_blocks(code, current_limit_up_datas, limit_up_record_datas,
+                                                    yesterday_current_limit_up_codes,
+                                                    before_blocks_dict)
+        # 淇濆瓨鏉垮潡璁$畻缁撴灉
+        cls.__can_buy_compute_result_dict[code] = (can_buy, msg)
 
 
 if __name__ == "__main__":

--
Gitblit v1.8.0