class DriverOrderBase(Common, OrderIdModel, db.Model, OrderBaseInfo): """驾驶员订单列表""" _privacy_fields = {'status', 'user_id', 'id'} __tablename__ = 'driver_order' driver_uuid = db.Column(db.String(length=40), db.ForeignKey('driver.uuid'), nullable=False, comment='驾驶员UUID') factory_order_uuid = db.Column(db.String(12), db.ForeignKey('factory_order.order_uuid'), comment='订单编号') description = db.Column(db.Text, comment='订单详情') images = db.Column(db.JSON, comment='订单图片') date = db.Column(db.Date, default=datetime.date.today, comment='订单开始日期') time = db.Column(db.Time, comment='订单具体时间') driver_schedule = db.Column(db.SMALLINT, default=1, comment='驾驶员进度:-1:订单已取消,0:未接单1:已接单,2:已出发,3:已到达厂家,4:返程中,5:已送达,6:已验收') order = db.relationship(OrderBase, foreign_keys=[factory_order_uuid]) driver = db.relationship("DriverBase", foreign_keys=[driver_uuid]) def order_infos(self, result: dict, *args, **kwargs): """添加厂家详情与进度详情""" # self.factory_info(result, *args, **kwargs) self.schedule_info(result, *args, **kwargs) def schedule_info(self, result: dict, *args, **kwargs): """进度详情""" result['schedules'] = [item.serialization(remove={'driver_order', 'id'}) for item in self.schedules] def customize_serialization(self): """自定义序列化""" result = dict() result.update({'schedules': [item.serialization(remove={'driver_order', 'id'}) for item in self.schedules]}) result.update({'order_info': self.serialization()}) result.update({'factory_info': self.order.factory.serialization()}) return result
class OrderBase(Common, OrderIdModel, db.Model, OrderBaseInfo): """厂家订单""" """update_time:司机接单时,提交订单原更新时间.原更新时间与订单现更新时间一致,接单通过.否则返回特有错误.""" __tablename__ = 'factory_order' _privacy_fields = {'factory_uuid', 'status', 'id'} factory_uuid = db.Column(db.String(length=40), db.ForeignKey('factory.uuid'), nullable=False, comment='厂家UUID') description = db.Column(db.Text, comment='订单详情') images = db.Column(db.JSON, comment='订单图片') date = db.Column(db.Date, default=datetime.date.today, comment='订单开始日期') time = db.Column(db.Time, comment='订单具体时间') schedule = db.Column(db.SMALLINT, default=0, comment='订单进度:0:待接单,1:已接单,2:已完成', index=True) # 增加索引 -> 事务锁,不会造成表锁 update_time = db.Column(db.DateTime, default=datetime.datetime.now, comment='订单内容更新时间') driver_order_uuid = db.Column(db.String(12), db.ForeignKey('driver_order.order_uuid'), comment='驾驶员订单编号') # driver_order_uuid = db.Column(db.String(12), comment='驾驶员订单编号') # 厂家订单的驾驶员订单编号外键不做约束 factory = db.relationship('FactoryBase', backref='orders') driver_order = db.relationship('DriverOrderBase', lazy='joined', foreign_keys=[driver_order_uuid]) def factory_info(self, result: dict, *args, **kwargs): """厂家详情""" result['factory_info'] = self.factory.serialization(remove={'create_time'}) def driver_info(self, result: dict, *args, **kwargs): """驾驶员详情""" if self.driver_order: result['driver_info'] = self.driver_order.driver.serialization(remove={'verify', 'create_time'}) result['driver_schedule'] = [item.serialization(remove={'driver_order_uuid', 'id'}) for item in self.driver_order.schedules] else: result['driver_info'] = {} result['driver_schedule'] = [] def driver_serialization(self, increase: set = None, remove: set = None): """驾驶员端序列化""" if increase is None: increase = set() if remove is None: remove = set() # 自定义需要去除的字段可以写成 remove = remove | {'field'} remove = remove | set() increase = increase | set() result = dict() result.update({'order_info': self.serialization(increase=increase, remove=remove)}) self.factory_info(result=result) return result def factory_serialization(self, increase: set = None, remove: set = None): """厂家端序列化""" if increase is None: increase = set() if remove is None: remove = set() result = dict() result.update({'order_info': self.serialization(increase=increase, remove=remove)}) self.driver_info(result=result) return result
class AdminBase(Common, db.Model, UUIDModel, UserToken): """管理员模型""" """管理员注册流程:数据库写入手机号,通过手机号匹配短信验证码最终关联open_id""" __tablename__ = 'admin' _privacy_fields = {'status', 'open_id', 'id'} open_id = db.Column(db.String(length=32), unique=True, comment='用户微信uuid') name = db.Column(db.String(length=10), comment='管理员姓名') phone = db.Column(db.String(length=13), nullable=False, comment='手机号') sms_status = db.Column(db.Boolean, default=True, comment='是否接收短信的状态')
class ImagesBase(Common, db.Model): """图片记录""" __tablename__ = 'images' id = db.Column(db.Integer, primary_key=True) user_uuid = db.Column(db.String(40), nullable=False, comment='用户ID') url = db.Column(db.String(length=255), nullable=False, unique=True, comment='图片地址') genre = db.Column(db.String(length=10), nullable=False, comment='图片用途') def __init__(self, **kwargs): super().__init__(**kwargs)
class FactoryBase(Common, db.Model, UUIDModel, UserToken, Coordinate, AddressInfo): """厂家用户""" __tablename__ = 'factory' _privacy_fields = {'status', 'open_id', 'id'} open_id = db.Column(db.String(length=32), unique=True, nullable=False, comment='用户微信uuid') name = db.Column(db.String(length=50), nullable=False, comment='用户名:厂家名') phone = db.Column(db.String(length=13), nullable=False, comment='手机号') def save_contact(self, contact_name: str, contact_phone: str, address: str, address_replenish: str, longitude: float, latitude: float): """保存联系人 如果存在相同的名称与手机号,则更新地址与坐标. 如果不存在相同的名称与手机号,则新增联系人. :param contact_name: 名称 :param contact_phone: 手机号 :param address: 地址 :param address_replenish:地址详情 :param longitude: 经度 :param latitude: 纬度 :return: """ query = business.FactoryContactBase.query contact = query.filter_by(factory_uuid=self.uuid, contact_name=contact_name, contact_phone=contact_phone).first() if contact: contact.address = address contact.address_replenish = address_replenish contact.longitude = longitude contact.latitude = latitude else: business.FactoryContactBase(factory_uuid=self.uuid, contact_name=contact_name, contact_phone=contact_phone, address=address, address_replenish=address_replenish, longitude=longitude, latitude=latitude).direct_add_() self.direct_update_()
class DriverOrderScheduleLogBase(Common, db.Model): """驾驶员订单进度日志""" __tablename__ = 'driver_order_schedule_log' driver_order_uuid = db.Column(db.String(length=12), db.ForeignKey('driver_order.order_uuid'), comment='订单编号') schedule = db.Column(db.SMALLINT, default=1, comment='驾驶员进度:0:未接单1:已接单,2:已出发,3:已到达厂家,4:返程中,5:已送达,6:已验收,-1:订单已取消') order = db.relationship(DriverOrderBase, backref='schedules')
class DriverBase(Common, db.Model, UUIDModel, UserToken): """驾驶员用户""" __tablename__ = 'driver' _privacy_fields = {'status', 'open_id', 'id', 'remark'} open_id = db.Column(db.String(length=32), unique=True, nullable=False, comment='用户微信uuid') name = db.Column(db.String(length=10), nullable=False, comment='用户名:驾驶员名') phone = db.Column(db.String(length=13), nullable=False, comment='手机号') number_plate = db.Column(db.String(length=10), default='', comment='车牌号:驾驶员特有字段') verify = db.Column(db.SMALLINT, default=False, comment='账号审核状态. -2:封禁 -1:审核失败 0:待审核 1:审核通过')
class SystemParameterBase(Common, db.Model): """系统参数 持续增加""" __tablename__ = 'system_parameter' visitors_code = db.Column(db.String(255), nullable=False, comment='审核码') remark = db.Column(db.String(255), nullable=False, comment='备注')
class FactoryContactBase(Common, OrderBaseInfo, db.Model): """厂家常用联系人""" __tablename__ = 'factory_contact' factory_uuid = db.Column(db.String(length=40), db.ForeignKey('factory.uuid'), nullable=False, comment='厂家UUID')