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

namespace ICSSoft.DataProject
{
    public class ISPO
    {
        private static log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        private static string connString = System.Configuration.ConfigurationManager.AppSettings["ERPConnStr"];
        public string Check(List<POModel> Bills)
        {
            string QTY = "";
            SqlConnection conn = new SqlConnection();
            SqlCommand cmd = new SqlCommand();
            try
            {
                conn = new System.Data.SqlClient.SqlConnection(connString);
                conn.Open();
                SqlTransaction sqlTran = conn.BeginTransaction();
                cmd = new SqlCommand();
                cmd.Transaction = sqlTran;
                cmd.Connection = conn;
                string sql = @"SELECT  FLOOR( MIN(d.IssuedSetableQty) )
                                  FROM PM_PurchaseOrder a
                                LEFT JOIN PM_POLine b ON b.PurchaseOrder=a.ID
                                LEFT JOIN CBO_SCMPickHead c ON c.POLine=b.ID
                                LEFT JOIN CBO_SCMPickList d ON d.PicKHead=c.ID
                                LEFT JOIN Base_Organization org ON a.Org=org.ID
                                WHERE    b.ID='{1}' AND org.Code='{0}'";
                //sql = string.Format(sql, Bills.WorkPoint, Bills.PODocNo, Bills.LineNo);
                sql = string.Format(sql, Bills[0].WorkPoint, Bills[0].DetailID);

                DataTable dt = DBHelper.SQlReturnData(sql, cmd);
                if (dt.Rows.Count == 0)
                {
                    throw new Exception("传入的单号：" + Bills[0].DetailID + "无法查到对应的数据，请确认U9是否存在!");
                }
                else
                {
                    QTY = dt.Rows[0][0].ToString();
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                if (conn.State == ConnectionState.Open)
                {
                    conn.Close();
                }
                conn.Dispose();
            }
            return QTY;

        }
    }
}
