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
{

    namespace ICSSoft.DataProject
    {
        public class CreatePOTest
        {
            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": "PO200224008100011",
            //    "YLOTQTY": "11",
            //     "NLOTQTY": "11",
            //     "BadCause": "破损",
            //    "User": "CC001",
            //    "MTime": "2021-08-26 17:20:13"
            //}]
            //返回参数
            //{
            //"Success": true,
            //"Message": "接口调用成功!",
            //"Data": null
            //}

            /// <summary>
            /// 生成物料检验单
            /// </summary>
            /// <param name="infos"></param>
            /// <returns></returns>
            public string Create(List<ICSPOTest> 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 (ICSPOTest info in infos)
                    {
                        if (info.MTime < new DateTime(2000, 01, 01))
                            throw new Exception("请输入正确的操作时间:" + info.MTime);
                            sql = @" IF EXISTS(SELECT LotNO  FROM ICSINSPECTION WHERE LotNO='{0}' )
	                                         RAISERROR('条码:{0},已检验过的条码不能再次检验!',16,1)
                            else
                            insert into ICSINSPECTION(ID,TransNO,TransLine,LotNO,INVCODE,VENDORITEMCODE,VENDORCODE,VenderLotNO,PRODUCTDATE,
							LOTQTY,YLOTQTY,NLOTQTY,BadCause,MUSER,MUSERName,MTIME,WorkPoint
							)
							select  newid(),c.cCode,c.irowno,'{0}',c.cInvCode,b.ITEMCODE,a.VENDORCODE,c.Batch,getdate(),c.iQuantity ,'{1}','{2}','{3}','{4}',UserName ,'{5}',b.WorkPoint from ICSASN a
							left join dbo.ICSASNDETAIL b on a.STNO=b.STNO 
							left join ICSPOArrive c on b.STNO=c.STNO
							left join ICSINVENTORY d on b.itemcode=d.INVCODE 
                            inner join dbo. Sys_User on USERCODE='{4}'
                            where b.lotno='{0}'";
                            sql = string.Format(sql, info.LotNO, info.YLOTQTY, info.NLOTQTY, info.BadCause, info.User, info.MTime);
                            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();
                }
            }







        }
    }
}