using System;
using System.Collections.Generic;
using System.Linq;
using CK.SCP.Models;
using CK.SCP.Models.Enums;
using CK.SCP.Models.ScpEntity;
using CK.SCP.Models.UniApiEntity;

namespace CK.SCP.UniApi
{
    public static class UniApiController
    {

        public static void RemoveList(ScpEntities db, List<TS_UNI_API> uniApiList)
        {
            db.TS_UNI_API.RemoveRange(uniApiList);
        }
        public static void AddHisList(ScpEntities db, List<TS_UNI_API_HIS> uniApiHisList)
        {

            db.TS_UNI_API_HIS.AddRange(uniApiHisList);
        }

        public static void AddApiData(ScpEntities wdb, TS_UNI_API apiData)
        {
            wdb.TS_UNI_API.Add(apiData);
        }


        public static void AddApiDataList(ScpEntities wdb, List<TS_UNI_API> apiDataList)
        {
            wdb.TS_UNI_API.AddRange(apiDataList);
        }


        public static WmsTableName GetTableName(UniApiType uniApiType)
        {
            WmsTableName tableName;
            switch (uniApiType)
            {
//                case UniApiType.StockMove:
//                    tableName = WmsTableName.xxwms_tr_det;
//                    break;
//                case UniApiType.BackFlush:
//                    tableName = WmsTableName.xxwms_bk_mstr;
//                    break;
//                case UniApiType.MaterialReceive:
//                    tableName = WmsTableName.xxwms_rc_det;
//                    break;
//                case UniApiType.MaterialReturn:
//                    tableName = WmsTableName.xxwms_rt_det;
//                    break;
//                case UniApiType.OtherIn:
//                    tableName = WmsTableName.xxwms_rct_det;
//                    break;
//                case UniApiType.OtherOut:
//                    tableName = WmsTableName.xxwms_iss_det;
//                    break;
//                case UniApiType.CheckUpdate:
//                    tableName = WmsTableName.xxwms_tag_det;
//                    break;
//                case UniApiType.ProductSell:
//                    tableName = WmsTableName.xxwms_soiss_det;
//                    break;
//                case UniApiType.StockDetail:
//                    tableName = WmsTableName.xxwms_ld_det;
//                    break;
                default:
                    throw new ArgumentOutOfRangeException(nameof(uniApiType), uniApiType, null);
            }
            return tableName;
        }

        ////创建采购订单
        //public static TS_UNI_API CreateBy(TB_PO bill, TB_PO_DETAIL detail, UniApiType uniApiType)
        //{
        //    var tableName = GetTableName(uniApiType);
        //    var apiData = new TS_UNI_API
        //    {
        //        InterfaceType = uniApiType.ToString(),
        //        TableName = tableName.ToString(),
        //        BillNum = bill.BillNum,
        //        PartCode = detail.PartCode,
        //        Batch = "",
        //        Qty = detail.BillQty,
        //        PoUnit = detail.PoUnit,
        //        LocUnit = detail.LocUnit,
        //        State = detail.State,
        //        CreateOper = bill.OperName,
        //        CreateTime = bill.BillTime,
        //        PutTime = ScpCache.GetServerTime(),
        //        VendId = bill.VendId,
        //        BillType = (int)BillType.PuchaseOrder,
        //        SubBillType = 0,
        //        ValidDate = detail.DueDate,
        //        ErpBillNum = bill.ErpBillNum,
        //        ErpLineNum = detail.LineNum,
        //        VendBatch = "",
        //        SourceBillNum = "",
        //        Price = detail.Price,
        //        PackQty = detail.PackQty,
        //        Currency = detail.Currency,
        //        Attn = "",//TODO
        //        Buyer = bill.Buyer,
        //        BuyerPhone = bill.BuyerPhone,
        //        ModType = bill.ModType,
        //        Receiver = "",//TODO

        //    };
        //    return apiData;
        //}

        //创建发货单
        public static TS_UNI_API CreateBy(TB_ASN bill, TB_ASN_DETAIL detail, UniApiType uniApiType)
        {
            var tableName = GetTableName(uniApiType);
            var apiData = new TS_UNI_API
            {
                InterfaceType = uniApiType.ToString(),
                TableName = tableName.ToString(),
                BillNum = bill.AskBillNum,
                PartCode = detail.PartCode,
                Batch = detail.Batch,
                Qty = detail.Qty,
                PoUnit = detail.PoUnit,
                LocUnit = detail.LocUnit,
                State = (int)detail.State,
                CreateOper = bill.CreateUser,
                CreateTime = bill.CreateTime,
                PutTime = ScpCache.GetServerTime(),
                VendId = bill.VendId,
                BillType = (int)BillType.PuchaseOrder,
                SubBillType = 0,
                ValidDate = (DateTime)detail.ProduceDate,
                ErpBillNum = bill.PoBillNum,
                ErpLineNum = detail.PoLine,
                VendBatch = detail.VendBatch,
                Price = detail.Price,
                PackQty = detail.PackQty,
                Currency = detail.Currency,
//                UmConv = detail.UmConv,
            };
            return apiData;
        }

        //创建条码
        public static TS_UNI_API CreateBy(TB_ASN bill, TS_BARCODE detail, UniApiType uniApiType)
        {
            var tableName = GetTableName(uniApiType);
            var apiData = new TS_UNI_API
            {
                Barcode = detail.BarCode,
                InterfaceType = uniApiType.ToString(),
                TableName = tableName.ToString(),
                BillNum = bill.AsnBillNum,
                PartCode = detail.PartCode,
                Batch = detail.Batch,
                Qty = detail.Qty,
                PoUnit = detail.PoUnit,
                LocUnit = detail.LocUnit,
                State = detail.State,
                CreateOper = bill.CreateUser,
                CreateTime = bill.CreateTime,
                PutTime = ScpCache.GetServerTime(),
                VendId = bill.VendId,
                BillType = (int)BillType.PuchaseOrder,
                SubBillType = 0,
                ValidDate = detail.ProduceDate,
                ErpBillNum = bill.PoBillNum,
                ErpLineNum = detail.PoBillLine,
                VendBatch = detail.VendBatch,
                SourceBillNum = detail.FullBarCode,
                PackQty = detail.PackQty,
            };
            return apiData;
        }

        //创建发票
        public static TS_UNI_API CreateBy(TB_INVOICE bill, TB_INVOICE_DETAIL detail, UniApiType uniApiType)
        {
            var tableName = GetTableName(uniApiType);
            var apiData = new TS_UNI_API
            {
                InterfaceType = uniApiType.ToString(),
                TableName = tableName.ToString(),
                Domain=bill.Site,
                Site=bill.Site,
                //BillNum = bill.BillNum,
                PartCode = detail.PartCode,
                Batch = "",
                Qty = detail.Qty,
                PoUnit = detail.PoUnit,
                //LocUnit = detail.LocUnit,
                State = detail.State,
                //CreateOper = bill.OperName,
                CreateTime = bill.CreateTime,
                PutTime = ScpCache.GetServerTime(),
                VendId = bill.VendId,
                BillType = (int)BillType.PuchaseOrder,
                SubBillType = 0,
                ValidDate = detail.ProduceDate,
                ErpBillNum = detail.PoBillNum,
                ErpLineNum = detail.PoLineNum,
                VendBatch = "",
                SourceBillNum = "",
                Price = detail.Price,
                PackQty = detail.PackQty,
                Currency = detail.Currency,
                Invoice = bill.InvoiceNum,
                //Tax = detail.Tax,
                //TaxAmt = detail.TaxAmt,
            };
            return apiData;
        }

        public static List<TS_UNI_API> GetNewInterfaceList(ScpEntities wdb)
        {
            return wdb.TS_UNI_API.Where(p => p.State == (int)DataState.Enabled).OrderBy(p => p.UID).ToList();
        }
    }
}