def __init__(self): self.saleOrders = DbService("saleOrders") self.saleOperations = DbService("saleOperations") self.checkOuts = DbService("checkOuts") self.products = DbService("products") self.users = DbService("users") self.conn = getConnection()
class ProductService: def __init__(self): self.products = DbService("products") self.checkOuts = DbService("checkOuts") self.purchaseOperations = DbService("purchaseOperations") self.saleOperations = DbService("saleOperations") self.purchaseOrders = DbService("purchaseOrders") self.saleOrders = DbService("saleOrders") self.conn = getConnection() # 释放连接 def dispose(self): disposeConnection(self.conn) # 新增产品 def add(self, product: Product) -> bool: try: cursor = self.conn.cursor() paras = [ product.id, product.name, product.price, product.num, product.specifications, product.notes ] rows = cursor.execute(self.products.GetOperation("add"), paras) if rows.rowcount != 0: self.conn.commit() return True return False except: return False # 删除产品 def delete(self, id: str) -> bool: try: cursor = self.conn.cursor() # 删除此商品的进货订单 rows = cursor.execute( self.purchaseOperations.GetOperation("findByProduct"), [id]).fetchall() for row in rows: cursor.execute(self.purchaseOperations.GetOperation("delete"), [row[2]]) cursor.execute(self.purchaseOrders.GetOperation("delete"), [row[2]]) # 删除此商品的出货订单 rows = cursor.execute( self.saleOperations.GetOperation("findByProduct"), [id]) for row in rows: cursor.execute(self.saleOperations.GetOperation("delete"), [row[2]]) cursor.execute(self.checkOuts.GetOperation("delete"), [row[2]]) cursor.execute(self.saleOrders.GetOperation("delete"), [row[2]]) # 删除此商品 rows = cursor.execute(self.products.GetOperation("delete"), [id]) if rows.rowcount != 0: self.conn.commit() return True return False except Exception as e: return False # 更新产品信息 def modify(self, product: Product) -> bool: try: cursor = self.conn.cursor() paras = [ product.name, product.price, product.num, product.specifications, product.notes, product.id ] rows = cursor.execute(self.products.GetOperation("modify"), paras) if rows.rowcount != 0: self.conn.commit() return True return False except: return False # 查找产品 def find(self, id: str) -> Product: try: cursor = self.conn.cursor() rows = cursor.execute(self.products.GetOperation("find"), [id]) for row in rows: return Product(row[1], row[2], float(row[3]), int(row[4]), row[5], row[6]) raise "can't find product" except: return None # 产品列表 def list(self, pageIndex: int, pageSize: int) -> list: try: cursor = self.conn.cursor() results = list() rows = cursor.execute(self.products.GetOperation("list"), [pageIndex * pageSize, pageSize]).fetchall() for row in rows: results.append( Product(row[1], row[2], float(row[3]), int(row[4]), row[5], row[6])) return results except: return [] def findWarning(self, warningNum: int) -> list: try: cursor = self.conn.cursor() results = list() rows = cursor.execute(self.products.GetOperation("findWarning"), [warningNum]).fetchall() for row in rows: results.append( Product(row[1], row[2], float(row[3]), int(row[4]), row[5], row[6])) return results except: return [] # 获取产品信息数量 def count(self) -> int: try: cursor = self.conn.cursor() for row in cursor.execute(self.products.GetOperation("count")): return row[0] return 0 except: return 0
class SaleService: def __init__(self): self.saleOrders = DbService("saleOrders") self.saleOperations = DbService("saleOperations") self.checkOuts = DbService("checkOuts") self.products = DbService("products") self.users = DbService("users") self.conn = getConnection() # 释放连接 def dispose(self): disposeConnection(self.conn) # 新增销售订单 def add(self, order: SaleOrder) -> bool: try: cursor = self.conn.cursor() for operation in order.saleOperations: # 新增操作 paras = [ operation.id, order.id, operation.product.id, operation.salePrice, operation.num ] cursor.execute(self.saleOperations.GetOperation("add"), paras) # 产品出库 product = None rows = cursor.execute(self.products.GetOperation("find"), [operation.product.id]) for row in rows: product = Product(row[1], row[2], float(row[3]), int(row[4]), row[5], row[6]) break num = product.num - int(operation.num) if num < 0: return False paras = [ product.name, product.price, num, product.specifications, product.notes, product.id ] rows = cursor.execute(self.products.GetOperation("modify"), paras) # 记录预付 for checkOut in order.checkOuts: if int(checkOut.amount) > 0: paras = [ checkOut.id, order.id, checkOut.time, checkOut.amount ] cursor.execute(self.checkOuts.GetOperation("add"), paras) # 新增订单 paras = [ order.id, order.time, order.state, order.user.id, order.selling ] rows = cursor.execute(self.saleOrders.GetOperation("add"), paras) if rows.rowcount != 0: self.conn.commit() return True return False except Exception as e: return False # 删除销售订单 def delete(self, id: str) -> bool: try: cursor = self.conn.cursor() # 订单未完成 退还库存 order = self.find(id) if not order.state: for operation in order.saleOperations: product = None rows = cursor.execute(self.products.GetOperation("find"), [operation.product.id]) for row in rows: product = Product(row[1], row[2], float(row[3]), int(row[4]) + operation.num, row[5], row[6]) paras = [ product.name, product.price, product.num, product.specifications, product.notes, product.id ] rows = cursor.execute(self.products.GetOperation("modify"), paras) # 删除此订单的进出库和结账记录 cursor.execute(self.checkOuts.GetOperation("delete"), [id]) cursor.execute(self.saleOperations.GetOperation("delete"), [id]) # 删除订单 rows = cursor.execute(self.saleOrders.GetOperation("delete"), [id]) if rows.rowcount != 0: self.conn.commit() return True return False except: return False # 修改销售订单 def modify(self, order: SaleOrder) -> bool: try: cursor = self.conn.cursor() currentOrder = self.find(order.id) if currentOrder.state: return False # 删除操作 cursor.execute(self.saleOperations.GetOperation("delete"), [order.id]) # 重新进库 for operation in currentOrder.saleOperations: product = None rows = cursor.execute(self.products.GetOperation("find"), [operation.product.id]) for row in rows: product = Product(row[1], row[2], float(row[3]), int(row[4]) + operation.num, row[5], row[6]) paras = [ product.name, product.price, product.num, product.specifications, product.notes, product.id ] rows = cursor.execute(self.products.GetOperation("modify"), paras) for operation in order.saleOperations: # 加入新操作 paras = [ str(uuid.uuid4()), order.id, operation.product.id, operation.salePrice, operation.num ] cursor.execute(self.saleOperations.GetOperation("add"), paras) # 重新出库 product = None rows = cursor.execute(self.products.GetOperation("find"), [operation.product.id]) for row in rows: product = Product(row[1], row[2], float(row[3]), int(row[4]), row[5], row[6]) break num = product.num - int(operation.num) if num < 0: return False paras = [ product.name, product.price, num, product.specifications, product.notes, product.id ] rows = cursor.execute(self.products.GetOperation("modify"), paras) # 修改订单 paras = [order.state, order.selling, order.id] rows = cursor.execute(self.saleOrders.GetOperation("modify"), paras) if rows.rowcount != 0: self.conn.commit() return True return False except: return False # 查找销售订单 def find(self, id: str) -> SaleOrder: try: cursor = self.conn.cursor() # 商品操作记录 rows = cursor.execute(self.saleOperations.GetOperation("find"), [id]).fetchall() operations = list() for row in rows: # 操作产品信息 product = None results = cursor.execute(self.products.GetOperation("find"), [row[3]]) for result in results: product = Product(result[1], result[2], float(result[3]), int(result[4]), result[5], result[6]) break operations.append( ProductOperation(row[1], product, row[4], row[5])) # 订单付款记录 rows = cursor.execute(self.checkOuts.GetOperation("find"), [id]).fetchall() checkOutList = list() for row in rows: checkOutList.append(CheckOut(row[1], row[3], row[4])) # 订单 rows = cursor.execute(self.saleOrders.GetOperation("find"), [id]) for row in rows: # 订单用户信息 user = None results = cursor.execute(self.users.GetOperation("find"), [row[4]]) for result in results: user = User(result[1], result[2], int(result[3]), result[4], result[5]) break return SaleOrder(row[1], row[2], row[3], user, row[5], operations, checkOutList) raise "can't find order" except Exception as e: return None # 销售订单列表 def list(self, pageIndex: int, pageSize: int) -> list: try: cursor = self.conn.cursor() rows = cursor.execute(self.saleOrders.GetOperation("list"), [pageIndex * pageSize, pageSize]).fetchall() results = [] for row in rows: results.append(self.find(row[1])) return results except: return [] # 按用户查找未完成订单 def findByUserWithState(self, id: str) -> list: try: cursor = self.conn.cursor() rows = cursor.execute( self.saleOrders.GetOperation("findByUserWithState"), [id]).fetchall() results = [] for row in rows: results.append(self.find(row[1])) return results except: return [] # 获取销售订单数量 def count(self) -> int: try: cursor = self.conn.cursor() for row in cursor.execute(self.saleOrders.GetOperation("count")): return row[0] return 0 except: return 0 # 完成销售订单 def complete(self, id: str) -> bool: cursor = self.conn.cursor() try: order = self.find(id) if order.state: return False rows = cursor.execute(self.saleOrders.GetOperation("modify"), [True, order.selling, id]) if rows.rowcount == 0: return False self.conn.commit() return True except: return False # 支付订单 def pay(self, id: str, amount: int) -> bool: cursor = self.conn.cursor() try: paras = [str(uuid.uuid4()), id, str(datetime.now()), amount] rows = cursor.execute(self.checkOuts.GetOperation("add"), paras) if rows.rowcount != 0: self.conn.commit() return True return False except: return False
class UserService: def __init__(self): self.users = DbService("users") self.saleOperations = DbService("saleOperations") self.checkOuts = DbService("checkOuts") self.saleOrders = DbService("saleOrders") self.conn = getConnection() # 释放连接 def dispose(self): disposeConnection(self.conn) # 新增用户 def add(self, user: User) -> bool: try: cursor = self.conn.cursor() paras = [user.id, user.name, user.tel, user.address, user.notes] rows = cursor.execute(self.users.GetOperation("add"), paras) if rows.rowcount != 0: self.conn.commit() return True return False except: return False # 删除用户 def delete(self, id: str) -> bool: try: cursor = self.conn.cursor() # 删除此用户的出货订单、订单操作、账单 rows = cursor.execute(self.saleOrders.GetOperation("findByUser"), [id]).fetchall() for row in rows: cursor.execute(self.saleOperations.GetOperation("delete"), [row[1]]) cursor.execute(self.checkOuts.GetOperation("delete"), [row[1]]) cursor.execute(self.saleOrders.GetOperation("delete"), [row[1]]) rows = cursor.execute(self.users.GetOperation("delete"), [id]) if rows.rowcount != 0: self.conn.commit() return True return False except Exception as e: return False # 修改用户信息 def modify(self, user: User) -> bool: try: cursor = self.conn.cursor() paras = [user.name, user.tel, user.address, user.notes, user.id] rows = cursor.execute(self.users.GetOperation("modify"), paras) if rows.rowcount != 0: self.conn.commit() return True return False except: return False # 查找用户信息 def find(self, id: str) -> User: try: cursor = self.conn.cursor() rows = cursor.execute(self.users.GetOperation("find"), [id]) for row in rows: return User(row[1], row[2], int(row[3]), row[4], row[5]) raise "can't find user" except: return None # 用户信息列表 def list(self, pageIndex: int, pageSize: int) -> list: try: cursor = self.conn.cursor() results = list() rows = cursor.execute(self.users.GetOperation("list"), [pageIndex * pageSize, pageSize]).fetchall() for row in rows: results.append( User(row[1], row[2], int(row[3]), row[4], row[5])) return results except: return [] # 获取用户信息数量 def count(self) -> int: try: cursor = self.conn.cursor() for row in cursor.execute(self.users.GetOperation("count")): return row[0] return 0 except: return 0
def __init__(self): self.purchaseOrders = DbService("purchaseOrders") self.purchaseOperations = DbService("purchaseOperations") self.products = DbService("products") self.conn = getConnection()
class PurchaseService: def __init__(self): self.purchaseOrders = DbService("purchaseOrders") self.purchaseOperations = DbService("purchaseOperations") self.products = DbService("products") self.conn = getConnection() # 释放连接 def dispose(self): disposeConnection(self.conn) # 新增采购订单 def add(self, order: PurchaseOrder) -> bool: try: cursor = self.conn.cursor() for operation in order.purchaseOperations: paras = [ operation.id, order.id, operation.product.id, operation.salePrice, operation.num ] cursor.execute(self.purchaseOperations.GetOperation("add"), paras) paras = [order.id, order.time, order.state] rows = cursor.execute(self.purchaseOrders.GetOperation("add"), paras) if rows.rowcount != 0: self.conn.commit() return True return False except: return False # 删除采购订单 def delete(self, id: int) -> bool: try: cursor = self.conn.cursor() # 删除此订单的进出库记录 cursor.execute(self.purchaseOperations.GetOperation("delete"), [id]) rows = cursor.execute(self.purchaseOrders.GetOperation("delete"), [id]) if rows.rowcount != 0: self.conn.commit() return True return False except: return False # 更新采购订单 def modify(self, order: PurchaseOrder) -> bool: try: cursor = self.conn.cursor() currentOrder = self.find(order.id) if currentOrder.state: return False cursor.execute(self.purchaseOperations.GetOperation("delete"), [order.id]) for operation in order.purchaseOperations: paras = [ str(uuid.uuid4()), order.id, operation.product.id, operation.salePrice, operation.num ] cursor.execute(self.purchaseOperations.GetOperation("add"), paras) paras = [order.state, order.id] rows = cursor.execute(self.purchaseOrders.GetOperation("modify"), paras) if rows.rowcount != 0: self.conn.commit() return True return False except: return False # 查找采购订单 def find(self, id: str) -> PurchaseOrder: try: cursor = self.conn.cursor() rows = cursor.execute(self.purchaseOperations.GetOperation("find"), [id]).fetchall() operations = list() for row in rows: results = cursor.execute(self.products.GetOperation("find"), [row[3]]) product = None for result in results: product = Product(result[1], result[2], float(result[3]), int(result[4]), result[5], result[6]) break operations.append( ProductOperation(row[1], product, row[4], row[5])) orders = cursor.execute(self.purchaseOrders.GetOperation("find"), [id]) for order in orders: return PurchaseOrder(order[1], order[2], order[3], operations) raise "can't find order" except: return None # 采购订单列表 def list(self, pageIndex: int, pageSize: int) -> list: try: cursor = self.conn.cursor() rows = cursor.execute(self.purchaseOrders.GetOperation("list"), [pageIndex * pageSize, pageSize]).fetchall() results = [] for row in rows: results.append(self.find(row[1])) return results except: return [] # 获取采购订单数量 def count(self) -> int: try: cursor = self.conn.cursor() for row in cursor.execute( self.purchaseOrders.GetOperation("count")): return row[0] return 0 except: return 0 # 完成采购订单 def complete(self, id: str) -> bool: cursor = self.conn.cursor() try: order = self.find(id) if order == None or order.state: return False flag = True for operation in order.purchaseOperations: product = None rows = cursor.execute(self.products.GetOperation("find"), [operation.product.id]) for row in rows: product = Product(row[1], row[2], float(row[3]), int(row[4]) + operation.num, row[5], row[6]) paras = [ product.name, product.price, product.num, product.specifications, product.notes, product.id ] rows = cursor.execute(self.products.GetOperation("modify"), paras) if rows.rowcount == 0: flag = False rows = cursor.execute(self.purchaseOrders.GetOperation("modify"), [True, id]) if rows.rowcount == 0: flag = False if flag: self.conn.commit() return flag except: return False