# generate_locations.py
import os
import django
import sys


def setup_django():
    # 使用原始字符串处理Windows路径
    project_path = "D:/Document/code/vue/greater_wms"
    sys.path.append(project_path)
    
    # 根据实际目录名设置(注意下划线)
    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'greaterwms.settings')
    django.setup()

def main():
    try:
        # 从正确的应用导入模型
        from erp.views import AccessToken ,ProductionInboundAuditSync,PurchaseInboundAuditSync,OtherInboundAuditSync,OtherOutboundAuditSync,ProductionOutboundAuditSync,PurchaseInboundSaveSync,SaleOutboundSaveSync
        from erp.models import InboundBill
        
        token = AccessToken.get_current_token()
        print('【1】token:',token)
        token = AccessToken.get_current_token()
        print('【2】token:',token)
       

        # 所有同步操作统一入口
        inbound_bill = InboundBill.objects.first()
        print(f"ERP单号: {inbound_bill.number}")
        ProductionInboundAuditsync = ProductionInboundAuditSync(inbound_bill)
        PurchaseInboundAuditsync = PurchaseInboundAuditSync(inbound_bill)
        OtherInboundAuditsync = OtherInboundAuditSync(inbound_bill)
        OtherOutboundAuditsync = OtherOutboundAuditSync(inbound_bill)
        ProductionOutboundAuditsync = ProductionOutboundAuditSync(inbound_bill)
        PurchaseInboundSavesync = PurchaseInboundSaveSync(inbound_bill)
        SaleOutboundSavesync = SaleOutboundSaveSync(inbound_bill)

        # 同步生产入库单
        success = ProductionInboundAuditsync.execute_sync()
        # 同步采购入库单
        success = PurchaseInboundAuditsync.execute_sync()
        # 同步其它入库单
        success = OtherInboundAuditsync.execute_sync()
        # 同步其它出库单
        success = OtherOutboundAuditsync.execute_sync()
        # 同步生产出库单
        success = ProductionOutboundAuditsync.execute_sync()
        # 同步采购入库单保存
        success = PurchaseInboundSavesync.execute_sync()
        # 同步销售出库单保存
        success = SaleOutboundSavesync.execute_sync()


        # 通过模型字段直接访问ERP ID
        print(f"ERP审核单号: {inbound_bill.erp_audit_id}")
        print(f"ERP采购单号: {inbound_bill.erp_save_id}")

        print("✅ 方法生成成功!")
    except Exception as e:
        print(f"❌ 生成失败:{str(e)}")
        import traceback
        traceback.print_exc()

if __name__ == "__main__":
    setup_django()
    main()