from app.extensions import db
from datetime import datetime

class StocktakeDraft(db.Model):
    __tablename__ = 'stocktake_draft'

    id = db.Column(db.Integer, primary_key=True)
    user_id = db.Column(db.String(100), default='admin')
    uuid = db.Column(db.String(100))
    # ★ 新增 quantity 字段
    quantity = db.Column(db.Numeric(19, 4), default=1)
    scan_time = db.Column(db.DateTime, default=datetime.now)

    def to_dict(self):
        return {
            'id': self.id,
            'user_id': self.user_id,
            'uuid': self.uuid,
            # ★ 返回 quantity
            'quantity': float(self.quantity or 1),
            'scan_time': self.scan_time.strftime('%Y-%m-%d %H:%M:%S')
        }