From ab11008d660832876dce7c6d05a2b5be0da1dd20 Mon Sep 17 00:00:00 2001
From: Administrator <admin@example.com>
Date: 星期日, 23 七月 2023 23:51:16 +0800
Subject: [PATCH] 选票板块即将更改

---
 third_data/code_plate_key_manager.py |  212 ++++++++++++++++++++--------------------------------
 1 files changed, 83 insertions(+), 129 deletions(-)

diff --git a/third_data/code_plate_key_manager.py b/third_data/code_plate_key_manager.py
index 3f09e63..67639df 100644
--- a/third_data/code_plate_key_manager.py
+++ b/third_data/code_plate_key_manager.py
@@ -66,12 +66,14 @@
         self.__get_redis().expire(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 +81,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):
         # 鍙瘮杈冧粖鏃ユ定鍋滃師鍥�
@@ -331,7 +335,17 @@
     # latest_2_day_limit_up_datas锛氭渶杩�2澶╃殑瀹炴椂娑ㄥ仠锛堜笉鍚粖鏃ワ級
     # limit_up_record_datas锛氫粖鏃ュ巻鍙叉定鍋�
     @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,
+                          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,143 +353,83 @@
             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, k1, k11, k2, k3, k4)
         keys = set()
-        if k3:
-            # 鍖归厤浜岀骇琛屼笟
-            keys |= k3
         if k1:
-            # 鏈変粖鏃ユ定鍋滃師鍥�
-            keys |= k1
-            pass
-        elif k2:
-            # 浠婃棩鏃犳定鍋滀絾鏈夊巻鍙叉定鍋�
-            keys |= k2
-        else:
-            if k4:
-                keys |= k4
+            for k in k1:
+                if k not in constant.KPL_INVALID_BLOCKS:
+                    keys.add(k)
+        if not keys:
+            for k in k2:
+                if k not in constant.KPL_INVALID_BLOCKS:
+                    keys.add(k)
+        if not keys:
+            if k3:
+                keys |= k3
         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)
-
-            # 鑾峰彇韬綅
-            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)
-
-            # 鏄惁婊¤冻甯傚満娴佸叆鍓嶅嚑
-            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 <= 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 == 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,涔板悗鎺�"
+        if not keys:
+            return cls.BLOCK_TYPE_NONE, None, "灏氭湭鎵惧埌娑ㄥ仠鍘熷洜"
+        code_limit_up_reason_dict = {}
+        load_code_block()
+        msg_list = []
+        for block in keys:
+            is_top_4 = kpl_block_util.is_top_block(code, block, limit_up_record_datas, 4)
+            # 鑾峰彇涓绘澘瀹炴椂韬綅
+            current_shsz_rank = kpl_block_util.get_code_current_rank(code, block, current_limit_up_datas,
+                                                                     code_limit_up_reason_dict, shsz=True)
+            record_shsz_rank = kpl_block_util.get_code_record_rank(code, block, limit_up_record_datas,
+                                                                   code_limit_up_reason_dict, shsz=True)
+            # 鑾峰彇涓绘澘鍘嗗彶韬綅
+            if is_top_4:
+                is_open_limit_up, msg = kpl_block_util.is_shsz_open_limit_up(code, block, limit_up_record_datas,
+                                                                             code_limit_up_reason_dict)
+                if is_open_limit_up:
+                    # 涓绘澘寮�1
+                    if current_shsz_rank < 2 and record_shsz_rank < 2:
+                        # 灞炰簬榫�1,榫�2
+                        return block, f"top4娑ㄥ仠鏉垮潡锛屼富鏉垮紑1,灞炰簬榫�1/榫�2(瀹炴椂韬綅-{current_shsz_rank})"
+                    else:
+                        msg_list.append(
+                            f"鏉垮潡-{block}: top4娑ㄥ仠鏉垮潡锛屼富鏉垮紑1锛坽msg}锛�,涓嶄负涓绘澘榫�1榫�2锛堝疄鏃惰韩浣�-{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,涔颁富鏉跨嫭鑻�"
-
-        return cls.BLOCK_TYPE_NONE, None, f"鏉垮潡({match_limit_up_result.keys()})涓嶇鍚堜拱鍏ユ潯浠�"
+                    if current_shsz_rank == 0 and record_shsz_rank < 2:
+                        return block, "top4娑ㄥ仠鏉垮潡锛岄潪涓绘澘寮�1锛屽睘浜庨緳1"
+                    else:
+                        msg_list.append(
+                            f"鏉垮潡-{block}: top4娑ㄥ仠鏉垮潡锛岄潪涓绘澘寮�1,涓嶄负涓绘澘榫�1锛堝疄鏃惰韩浣�-{current_shsz_rank},鍘嗗彶韬綅-{record_shsz_rank}锛�")
+                        continue
+            else:
+                # 鏄惁婊¤冻琛屼笟绮鹃�夋祦鍏ヨ姹�
+                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:
+                        return block, "涓嶆槸top4娑ㄥ仠鏉垮潡锛屾弧瓒崇簿閫�/琛屼笟娴佸叆瑕佹眰锛屾弧瓒充富鏉块緳1"
+                    else:
+                        msg_list.append(
+                            f"鏉垮潡-{block}: 涓嶆槸top4娑ㄥ仠鏉垮潡锛屾弧瓒崇簿閫�/琛屼笟娴佸叆瑕佹眰,涓嶄负涓绘澘榫�1锛堝疄鏃惰韩浣�-{current_shsz_rank},鍘嗗彶韬綅-{record_shsz_rank}锛�")
+                        continue
+        return None, "\r\n".join(msg_list)
 
     # 鏄惁鍙互涓嬪崟
     # 杩斿洖锛氭槸鍚﹀彲浠ヤ笅鍗�,娑堟伅,鏉垮潡绫诲瀷
     @classmethod
-    def can_buy(cls, code, current_limit_up_datas, latest_2_day_limit_up_datas, limit_up_record_datas):
+    def can_buy(cls, code, current_limit_up_datas, limit_up_record_datas,before_blocks_dict):
         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
+        block, block_msg = cls.get_can_buy_block(code, current_limit_up_datas,
+                                                 limit_up_record_datas, before_blocks_dict)
+        if block is None:
+            return False, block_msg
 
         # ---------------------------------鍒ゆ柇鐩爣浠g爜鐨勬澘鍧�-------------------start------------
         # 鍒ゆ柇鍖归厤鍑虹殑娑ㄥ仠鍘熷洜锛屽垽鏂槸鍚︽湁宸茬粡涓嬪崟鐨勭エ
@@ -568,7 +522,7 @@
                     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
+            return True, block_msg
             # 鏉垮潡鍙互涓嬪崟鏁伴噺
             # if trade_block_codes_dict.get(key) is None or len(trade_block_codes_dict.get(key)) < \
             #         can_buy_codes_count_dict[key]:
@@ -580,7 +534,7 @@
             #     order_count = len(trade_block_codes_dict.get(key))
             #     msg_list.append(f"銆恵key}銆戜腑涓嬪崟浠g爜鏁伴噺{order_count}/鍏佽涓嬪崟鏁伴噺{can_buy_codes_count_dict[key]}")
 
-        return False, ",".join(msg_list), block_type
+        return False, ",".join(msg_list)
 
 
 if __name__ == "__main__":

--
Gitblit v1.8.0