using ICSSoft.Entity;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using Newtonsoft.Json;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ICSSoft.Common;

namespace ICSSoft.DataProject
{
    public class CreateWareHouseLotInfo
    {
        private static log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        private static string connString = System.Configuration.ConfigurationManager.AppSettings["ConnStr"];
        private static string ERPDB = System.Configuration.ConfigurationManager.AppSettings["ERPDB"];

        //参数示例
        //[{
        //    "LotNO": "PO200219004100005",
        //    "BinCode": "MAT-BLCA03601",
        //    "LotQty": "11",
        //    "User": "CC001",
        //    "MTime": "2021-08-26 17:20:13",
        //    "ReturnDoc": "U8000000001",
        //    "ReturnDocLine": "10"
        //}]
        //返回参数
        //{
        //"Success": true,
        //"Message": "接口调用成功!",
        //"Data": null
        //}

        /// <summary>
        /// 生成条码上架入库
        /// </summary>
        /// <param name="infos"></param>
        /// <returns></returns>
        public string Create(List<ICSWareHouseLotInfo> infos)
        {
            if (infos.Count <= 0)
            {
                throw new Exception("传送数据为空!");
            }
            string res = string.Empty;
            SqlConnection conn = new System.Data.SqlClient.SqlConnection(connString);
            conn.Open();
            SqlTransaction sqlTran = conn.BeginTransaction();
            SqlCommand cmd = new SqlCommand();
            cmd.Transaction = sqlTran;
            cmd.Connection = conn;
            try
            {
                string sql = string.Empty;
                foreach (ICSWareHouseLotInfo info in infos)
                {
                    if (info.MTime < new DateTime(2000, 01, 01))
                        throw new Exception("请输入正确的操作时间:" + info.MTime);

                    sql = @"	insert into ICSWareHouseLotInfo(ID,LotNO,WHGUID,WHCode,BinGUID,BinCode,INVGUID,INVCode,LotQty,ReceiveDate,MUSER,MTIME,WorkPoint,MUSERName)
	                            select newid(),'{0}',d.Serial,d.StorageCode,e.Serial,'{1}',f.ID,c.ItemCode,'{2}',getdate(),'{3}','{4}',a.WorkPoint,USERName 
	                            from ICSPOArrive a --到货单
	                            left join ICSASNDetail b on a.STNO=b.STNO and a.WorkPoint=b.WorkPoint --送货单
	                            left join ICSITEMLot c on b.LOTNO=c.LotNO and b.WorkPoint=c.WorkPoint --条码
								left join ICSINVENTORY f on c.ItemCode=f.INVCODE and c.WorkPoint= f.WorkPoint  --物料
	                            left join ICSStorage d on a.cWhCode=d.StorageCode and a.WorkPoint=d.WorkPoint --仓库
	                            left join ICSStack e on d.Serial=e.Storage_Serial and d.WorkPoint=e.WorkPoint --库位
                                inner join dbo. Sys_User on USERCODE='{3}'  
                                where c.LotNO='{0} 'and e.StackCode='{1}' 
                            --插入出入库记录
	                            insert into ICSWareHouseLotInfoLog(ID,TransNO,TransLine,ITEMCODE,LotNO,TOStorageCODE,TOStackCODE,TransQTY,MUSER,MTIME,WorkPoint,MUSERName,TransType,BusinessCode,ReturnDoc,ReturnDocLine)
	                            select newid(),c.TransNO,c.TransLine,c.ItemCODE,'{0}',d.StorageCode,'{1}','{2}','{3}','{4}',a.WorkPoint ,USERName,'收','采购入库','{5}','{6}'
	                            from ICSPOArrive a --到货单
                                left join ICSASNDetail b on a.STNO=b.STNO and a.WorkPoint=b.WorkPoint --送货单
	                            left join ICSITEMLot c on b.LOTNO=c.LotNO and b.WorkPoint=c.WorkPoint --条码
								left join ICSINVENTORY f on c.ItemCode=f.INVCODE and c.WorkPoint= f.WorkPoint  --物料
	                            left join ICSStorage d on a.cWhCode=d.StorageCode and a.WorkPoint=d.WorkPoint --仓库
	                            left join ICSStack e on d.Serial=e.Storage_Serial and d.WorkPoint=e.WorkPoint --库位
                                inner join dbo. Sys_User on USERCODE='{3}'  
                                where c.LotNO='{0} '  and e.StackCode='{1}' ";
                    sql = string.Format(sql, info.LotNO, info.BinCode, info.LotQty, info.User ,info.MTime,info.ReturnDoc,info.ReturnDocLine);
                    DBHelper.CmdExecuteNonQuery(sql, cmd, "条码:" + info.LotNO + "未查询到对应数据!");
                  
                }
                cmd.Transaction.Commit();
                return res;
            }
            catch (Exception ex)
            {
                cmd.Transaction.Rollback();
                log.Error(ex.Message);
                throw new Exception(ex.Message);
            }
            finally
            {
                if (conn.State == ConnectionState.Open)
                {
                    conn.Close();
                }
                conn.Dispose();
            }
        }







    }
}