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

namespace ICSSoft.DataProject
{
   public class OutsourcingOrderPick
    {
        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"];
        public string Get(List<ICSOutsourcingOrderPick> infos)
        {

            List<ICSOutsourcingOrderPick> szJson = new List<ICSOutsourcingOrderPick>();
            DataTable dt = null;
            string json = "";
            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 (ICSOutsourcingOrderPick info in infos)
                {
                    if (info.MTime < new DateTime(2000, 01, 01))
                        throw new Exception("请输入正确的操作时间:" + info.MTime);
                    sql = @"  SELECT a.MOID,a.cCode,a.cDepCode,e.cDepName, a.csrccode ,a.cMaker ,a.dCreateTime,a.cVerifier ,a.dVerifyTime 
                              ,b.MODetailsID  ODetailsID ,b.iVouchRowNo ,b.cInvCode ,b.iQuantity ,b.iArrQTY ,b.dArriveDate ,b.iTaxPrice ,d.AutoID
                              FROM  dbo.OM_MOMain   a 
                              INNER JOIN dbo.OM_MODetails b ON a.MOID=b.MOID 
		                      left join PU_AppVouch c on a.csrccode=c.cCode
		                      left join PU_AppVouchs d on c.ID =d.ID 
		                      left join Department  e on a.cDepCode =e.cDepCode ";
                    if (!string.IsNullOrWhiteSpace(info.OOCode))
                    {
                        sql += " and a.cCode='{0}'";
                    }
                    if (!string.IsNullOrWhiteSpace(info.MTime.ToString()))
                    {
                        sql += " and ISNULL(a.cModifyTime,ISNULL(a.cAuditTime, ISNULL(a.cModifyTime, a.cmaketime)))>='{2}'";
                    }
                    if (!string.IsNullOrWhiteSpace(info.User))
                    {
                        sql += "and a.CMAKER='{3}'";
                    }
                    sql = string.Format(sql, info.OOCode, info.MTime, info.User);
                    dt = DBHelper.SQlReturnData(sql, cmd);
                    if (dt.Rows.Count <= 0 || dt == null)
                        throw new Exception("委外订单号:" + info.OOCode + ",无信息!");
                    json = JsonConvert.SerializeObject(dt);
                }
                cmd.Transaction.Commit();
                return json;
            }
            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();
            }
        }
    }
}