def query_share_code(self): """查询股票代码""" if self.menu_qt.share_code_input.text( ) == '' or not self.menu_qt.share_code_input.text().isdigit(): # QMessageBox.showinfo("提示", "请输入有效的股票代码") return False else: self.fill_share_info() share_info = DbUtil.query_share_info_by_code( share_code=self.menu_qt.share_code_input.text()) if share_info: self.menu_qt.share_name_show.setText(share_info.share_name) buy_record = DbUtil.query_business_record( share_id=share_info.id) if isinstance(buy_record, list) and len(buy_record) > 1: self.__sum_share_info(buy_record) else: if buy_record: self.menu_qt.can_sale_num_show.setText( buy_record.buy_num) self.menu_qt.buv_avg_price_show.setText( round(buy_record.buy_avg_price, 3)) else: self.menu_qt.win_show.setText("0") self.menu_qt.buv_avg_price_show.setText("0.000") self.menu_qt.can_sale_num_show.setText("0") self.menu_qt.have_num_show.setText("0")
def query_share_code(cls): """查询股票代码""" if cls.share_code.get() == '' or not cls.share_code.get().isdigit(): messagebox.showinfo("提示", "请输入有效的股票代码") else: share_info = DbUtil.query_share_info_by_code(share_code=cls.share_code.get()) if share_info: cls.share_name.set(share_info.share_name) buy_record = DbUtil.query_business_record(share_id=share_info.id) if isinstance(buy_record, list): cls.__sum_share_info(buy_record) else: if buy_record: cls.can_sale_num.set(buy_record.buy_num) cls.buy_avg_price.set(round(buy_record.buy_avg_price, 3)) else: messagebox.showerror("错误", "股票代码无效!") return False
def _save(cls): """保存记录到数据库""" file_path = cls.file_path.get() error_code_list = [] if os.path.isfile(file_path): with open(file_path, mode="r") as f: data_list = f.readlines() if len(data_list) > 0: for data in data_list: data_ = cls.__handle_origin_data(data) if data_ == WindowCons.FALSE_NUMBER: messagebox.showerror("错误", "数据文件有问题,请检查!") return if data_[0].isdigit() and len(data_[1].strip()) > 0 and len(data_[2].strip()): DbUtil.import_share_info(cls.__handle_share_code(data_[0]), data_[1], data_[2]) else: error_code_list.append(data_[0]) messagebox.showinfo("提示", "数据导入成功")
def package_share_info(self): """组装股票信息list""" str_list = [] share_info_list = DbUtil.query_share_info_like_code( share_code=self.menu_qt.share_code_input.text()) if len(share_info_list) > 0: for share_info in share_info_list: str = share_info.share_code + WindowCons.SPACE_SYMBOL + share_info.share_name str_list.append(str) return QStringListModel(str_list) else: return False
def _compute_avg_and_save(cls, buy_num=0, sale_num=0): """计算买入平均价""" share_info = DbUtil.query_share_info_by_code(cls.share_code.get()) record = DbUtil.query_last_business_record(share_info.id) if record: curr_num = record.curr_num + buy_num - sale_num avg_price = ((record.buy_avg_price * record.curr_num) + (cls.buy_num.get() * cls.buy_price.get()) - ( cls.sale_num.get() * cls.sale_price.get())) / curr_num else: curr_num = cls.buy_num.get() avg_price = cls.buy_price.get() if not askokcancel("提示", "请确认数据无误后选择保存!"): return False if buy_num > 0: date_time = datetime.datetime.strptime(cls.buy_date.get(), "%Y%m%d") buiness_record = BusinessRecord(share_id=share_info.id, buy_time=date_time, buy_num=cls.buy_num.get(), buy_avg_price=avg_price, curr_num=curr_num, buy_price=cls.buy_price.get(), delate=WindowCons.FALSE_NUMBER) DbUtil.add_record(buiness_record) if sale_num > 0: date_time = datetime.datetime.strptime(cls.sale_date.get(), "%Y%m%d") buiness_record = BusinessRecord(share_id=share_info.id, buy_time=date_time, sale_num=cls.sale_num.get(), buy_avg_price=avg_price, curr_num=curr_num, sale_price=cls.sale_price.get(), final_profit=cls.final_win.get(), delate=WindowCons.FALSE_NUMBER) DbUtil.add_record(buiness_record) return True
def _compute_avg_and_save(self, buy_num=0, sale_num=0): """计算买入平均价""" share_info = DbUtil.query_share_info_by_code( self.menu_qt.share_code_input.text()) record = DbUtil.query_last_business_record(share_info.id) buy_price = float(self.menu_qt.buy_price_input.text()) sale_price = float(self.menu_qt.sale_price_input.text()) if record: curr_num = record.curr_num + buy_num - sale_num if curr_num != 0: avg_price = ((record.buy_avg_price * record.curr_num) + (buy_num * buy_price) - (sale_num * sale_price)) / curr_num else: avg_price = 0.000 else: curr_num = buy_num avg_price = buy_price if not self.compute_win(record.final_profit): return False save_confirm = self.message_box.ask_ok("请确认数据无误后点击确定") if save_confirm == MessageBox.CANCEL_NO: return False if buy_num > 0: try: date_time = datetime.datetime.strptime( self.menu_qt.buy_date_input.text(), "%Y/%m/%d").date() except: date_time = datetime.datetime.strptime( self.menu_qt.buy_date_input.text(), "%Y-%m-%d").date() buiness_record = BusinessRecord(share_id=share_info.id, buy_time=date_time, buy_num=buy_num, buy_avg_price=avg_price, curr_num=curr_num, buy_price=buy_price, final_profit=float( self.menu_qt.win_show.text()), delate=WindowCons.FALSE_NUMBER) DbUtil.add_record(buiness_record) if sale_num > 0: try: date_time = datetime.datetime.strptime( self.menu_qt.sale_date_input.text(), "%Y/%m/%d").date() except: date_time = datetime.datetime.strptime( self.menu_qt.sale_date_input.text(), "%Y-%m-%d").date() buiness_record = BusinessRecord(share_id=share_info.id, buy_time=date_time, sale_num=sale_num, buy_avg_price=avg_price, curr_num=curr_num, sale_price=sale_price, final_profit=float( self.menu_qt.win_show.text()), delate=WindowCons.FALSE_NUMBER) DbUtil.add_record(buiness_record) return True
def login(): """登录""" username = var_usr_name.get() password = var_usr_pwd.get() if username is None or username.strip( ) == '' or password is None or password.strip() == '': messagebox.showerror("ERROR", "username or password need to fill") else: user = DbUtil.query_user(username, password) if user: messagebox.showinfo("Welcome", "Login successful!") WindowStatus.login_window = False WindowCons.LOGIN_SUCCESS = True # 登录成功 WindowCons.LOGIN_USER_ID = user.id cls.window_signin.destroy() else: messagebox.showerror("ERROR", "Username or Password is invalid!") WindowStatus.login_window = True