test_instruction.py

liuweipaul123發表於2024-06-24
import os
import time
import pandas as pd
from datetime import datetime
import datetime
import mysql.connector
from tkinter import messagebox
from tkinter.messagebox import *
import random
import requests


class test_instruction():
    def __init__(self):
        print()

    # 獲取指令登入的cookies
    def getCookies_instruction(self):
        rsp = None
        urlLogin = 'http://' + self.IP + '/login'
        print("1111",urlLogin)

        dataLogin = {
            "userName": "Nick",
            "password": "password"
        }
        try:
            rsp = requests.post(urlLogin, headers={'Content-Type': 'application/json'}, json=dataLogin)
            print(urlLogin)
            print(rsp.cookies)
            if rsp.status_code != 200:
                messagebox.showinfo("提示", "連線伺服器失敗,請檢查伺服器是否正常!")
                return 1
            else:
                return rsp.cookies

        #except requests.exceptions.InvalidURL:
        except:
            messagebox.showinfo("提示", "連線伺服器失敗,請檢查伺服器是否正常!")
            return 1


    def main_test_instruction(self,instruction_collect,openRun):
        self.IP =instruction_collect[0]
        self.headers = {'Content-Type': 'application/json'}
        loginCookies = self.getCookies_instruction()
        self.issueTime = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
        self.expireTime = datetime.datetime.now().strftime("%Y-%m-%d 19:00:00")
        self.modifyTime = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        if loginCookies != 1:
            if openRun == '單個下發指令':
                urlAdd = 'http://' + self.IP + '/api/new_instruction'
                print("urlAdd11", urlAdd)
                dataAdd = {
                    "aprrovalNo": "B123323",
                    "instructionNo": 'A' + str(random.randint(1000000, 9999999)),
                    "product": instruction_collect[1],
                    "asset": instruction_collect[2],
                    "portfolio": instruction_collect[3],
                    "symbol": instruction_collect[4],
                    "tradeSide": instruction_collect[5],
                    "targetQty": instruction_collect[6],
                    "targetAmt": instruction_collect[7],
                    "priceType": instruction_collect[8],
                    "limitPrice": instruction_collect[9],
                    "manager": instruction_collect[10],
                    "createBy": "tkinter",
                    "aprrovalStatus": "check",
                    "issueTime": self.issueTime,
                    "expireTime": self.expireTime,
                    "remark": instruction_collect[11]
                }
                rsp = requests.post(urlAdd, headers=self.headers, json=dataAdd, cookies=loginCookies)
                if json.loads(rsp.text)["error_no"] == "0":
                    print("下發指令成功!")
                    print("指令號為:" + dataAdd['instructionNo'])
                    messagebox.showinfo("提示", "下發指令成功!")
                else:
                    print("下發指令失敗!")
                    print(json.loads(rsp.text)["error_msg"])
                    messagebox.showinfo("提示", "下發指令失敗!" + json.loads(rsp.text)["error_msg"])
            elif openRun == '批次下發指令':
                urlAdd = 'http://' + self.IP + '/api/new_instruction'
                print("urlAdd11", urlAdd)
                data = pd.read_csv(os.path.dirname(os.getcwd()) + "/onequant/指令/" + "instruction" + ".csv", encoding="gbk")
                for i in range(len(data)):
                    dataBatch = {
                        "aprrovalNo": "B123323",
                        "instructionNo": 'A' + str(random.randint(1000000, 9999999)),
                        "product": str(data["產品號"][i]),
                        "asset": str(data["資產單元"][i]),
                        "portfolio": data["組合號"][i],
                        "symbol": data["程式碼"][i],
                        "tradeSide": str(data["交易方向"][i]),
                        "targetQty": str(data["目標量"][i]),
                        "targetAmt": str(data["目標金額"][i]),
                        "priceType": str(data["價格型別(0無限價,1有限價)"][i]),
                        "limitPrice": str(data["限價"][i]),
                        "manager": data["投資經理"][i],
                        "createBy": data["錄入人"][i],
                        "aprrovalStatus": data["指令狀態"][i],
                        "issueTime": self.issueTime,
                        "expireTime": self.expireTime,
                        "remark": data["備註"][i]
                    }

                    requests.post(urlAdd, headers=self.headers, json=dataBatch, cookies=loginCookies)
                messagebox.showinfo("提示", "下發" + str(len(data)) + "條指令成功!")
            elif openRun == '修改指令':
                urlModify = 'http://' + self.IP + '/api/modify_instruction'
                dataModify = {
                    "instructionNo":instruction_collect[1],
                    "targetQty": instruction_collect[2],
                    "targetAmt": instruction_collect[3],
                    "priceType": instruction_collect[4],
                    "limitPrice": instruction_collect[5],
                    "manager": instruction_collect[6],
                    "createBy": "wangsh",
                    "modifyTime": self.modifyTime,
                    "remark": instruction_collect[7]
                }
                print("dataModify", dataModify)
                rsp = requests.post(urlModify, headers=self.headers, json=dataModify,cookies=loginCookies)
                print(rsp.status_code)
                if json.loads(rsp.text)["error_no"] == "0":
                    print("修改指令成功!")
                    print("指令號為:" + dataModify['instructionNo'])
                    messagebox.showinfo("提示", "修改指令成功!")
                else:
                    print("修改指令失敗!")
                    print(json.loads(rsp.text)["error_msg"])
                    messagebox.showinfo("提示", "修改指令失敗!" + json.loads(rsp.text)["error_msg"])
            elif openRun == '凍結指令':
                urlFreeze = 'http://' + self.IP + '/api/freeze_instruction'
                dataFreeze = {
                    "instructionNo": instruction_collect[1],
                    "manager": instruction_collect[2],
                    "remark": instruction_collect[3]
                }
                rsp = requests.post(urlFreeze, headers=self.headers, json=dataFreeze,cookies=loginCookies)
                if json.loads(rsp.text)["error_no"] == "0":
                    print("指令凍結成功:" + dataFreeze["instructionNo"])
                    messagebox.showinfo("提示", "指令凍結成功!")
                else:
                    print("指令凍結失敗!")
                    print(json.loads(rsp.text)["error_msg"])
                    messagebox.showinfo("提示", "作廢凍結失敗!" + json.loads(rsp.text)["error_msg"])
            elif openRun == '作廢指令':
                urlCancel = 'http://' + self.IP + '/api/cancel_instruction'
                dataCancel = {
                    "instructionNo": instruction_collect[1],
                    "manager": instruction_collect[2],
                    "remark": instruction_collect[3]
                }
                rsp = requests.post(urlCancel, headers=self.headers, json=dataCancel,cookies=loginCookies)
                if json.loads(rsp.text)["error_no"] == "0":
                    print("指令作廢成功:" + dataCancel["instructionNo"])
                    messagebox.showinfo("提示", "指令作廢成功!")
                else:
                    print("指令作廢失敗!")
                    print(json.loads(rsp.text)["error_msg"])
                    messagebox.showinfo("提示", "作廢指令失敗!" + json.loads(rsp.text)["error_msg"])
            elif openRun == '解凍指令':
                urlUnfreeze = 'http://' + self.IP + '/api/unfreeze_instruction'
                dataUnfreeze = {
                    "instructionNo": instruction_collect[1],
                    "manager": instruction_collect[2],
                    "remark": instruction_collect[3]
                }
                rsp = requests.post(urlUnfreeze, headers=self.headers, json=dataUnfreeze,cookies=loginCookies)
                if json.loads(rsp.text)["error_no"] == "0":
                    print("指令解凍成功:" + dataUnfreeze["instructionNo"])
                    messagebox.showinfo("提示", "指令解凍成功!")
                else:
                    print("指令解凍失敗!")
                    print(json.loads(rsp.text)["error_msg"])
                    messagebox.showinfo("提示", "指令解凍失敗!" + json.loads(rsp.text)["error_msg"])