using NFine.Data.Extensions;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NFine.Code;
using NFine.Repository;
using System.Data.Common;
using NFine.Domain._03_Entity.SRM;
using ICS.Application.Entity;
using Newtonsoft.Json;
using System.Configuration;
using System.Data.SqlClient;
using ICS.Data;
using Newtonsoft.Json.Linq;

namespace NFine.Application.WMS
{
  public  class ZHYSalesDeliveryApp : RepositoryFactory<ICSVendor>
    {
      
        public DataTable GetGridJson(string queryJson, ref Pagination jqgridparam)
        {
            string WorkPoint = NFine.Code.OperatorProvider.Provider.GetCurrent().Location;
            DataTable dt = new DataTable();
            var queryParam = queryJson.ToJObject();
            List<DbParameter> parameter = new List<DbParameter>();
            string sql = "";
            #region [SQL]

            sql = @"select a.SDNCode ,a.Sequence,a.SOCode,a.SOSequence,a.CusCode,a.CusName,
a.InvCode,b.InvName,b.InvStd,a.Quantity,c.cFree1,b.InvUnit,b.AmountUnit,a.Amount,a.SDNQuantity,a.CreatePerson,a.CreateDateTime,a.MUSERName,a.MTIME,
case when a.SDNQuantity=0 then '未发货'  when a.SDNQuantity=a.Quantity then '发货完成' else '正在发货' end as Status
from ICSSDN a 
left join dbo.ICSInventory b on a.InvCode=b.InvCode and a.WorkPoint=b.WorkPoint
left join dbo.ICSExtension c on a.ExtensionID=c.ID and a.WorkPoint=c.WorkPoint
where a.Type='1' and a.Status='2' ";
            sql = string.Format(sql, WorkPoint);
            sql = string.Format(sql, DbHelper.GetErpIp(), DbHelper.GetErpName());
            #endregion

            if (!string.IsNullOrWhiteSpace(queryJson)) 
            {
                if (!string.IsNullOrWhiteSpace(queryParam["POCode"].ToString()))
                {
                    sql += " and a.SDNCode like '%" + queryParam["POCode"].ToString() + "%' ";
                }
                if (!string.IsNullOrWhiteSpace(queryParam["InvCode"].ToString()))
                {
                    sql += " and a.InvCode like '%" + queryParam["InvCode"].ToString() + "%' ";
                }
                if (!string.IsNullOrWhiteSpace(queryParam["TimeFrom"].ToString()))
                {
                    sql += " and convert(nvarchar(20),a.CreateDateTime,23) >= '" + queryParam["TimeFrom"].ToString() + "' ";
                }
                if (!string.IsNullOrWhiteSpace(queryParam["TimeArrive"].ToString()))
                {
                    sql += " and convert(nvarchar(20),a.CreateDateTime,23) <= '" + queryParam["TimeArrive"].ToString() + "' ";
                }
                if (!string.IsNullOrWhiteSpace(queryParam["Status"].ToString()))
                {
                    string POStatus = queryParam["Status"].ToString();
                    if (POStatus == "1")
                    {
                        sql += " and a.SDNQuantity=a.Quantity ";
                    }
                    else if (POStatus == "2")
                    {
                        sql += "  and  a.SDNQuantity=0";
                    }
                    else
                    {
                        sql += " and a.Quantity-a.SDNQuantity>0  and a.SDNQuantity<>0";
                    }
                }
            }
            return Repository().FindTablePageBySql(sql.ToString(), parameter.ToArray(), ref jqgridparam);
        }



        public DataTable GetSalesDeliveryTemp(string queryJson, ref Pagination jqgridparam, string ID)
        {
            DataTable dt = new DataTable();
            string sql = @"select b.ID,b.LotNo,b.Quantity,d.InvUnit,c.Amount,d.AmountUnit,e.BatchCode,e.cFree1,b.MUSERName,b.MTIME from  ICSSDN a 
inner join dbo.ICSWareHouseLotInfoLog b on a.SDNCode=b.TransCode and a.Sequence=b.TransSequence and a.WorkPoint=b.WorkPoint and b.BusinessCode='19'
left join dbo.ICSInventoryLot c on b.LotNo=c.LotNo and b.WorkPoint=c.WorkPoint
left join dbo.ICSInventory d on c.InvCode=d.InvCode and c.WorkPoint=d.WorkPoint
left join dbo.ICSExtension e on c.ExtensionID=e.ID and c.WorkPoint=e.WorkPoint

where 1=1 ";
            if (ID=="")
            {
                sql+= " and a.SDNCode+a.Sequence='"+ ID + "' ";

            }
            else
            {
                sql += "and a.SDNCode+a.Sequence in (" + ID.TrimEnd(',') + ")";
            }


            return Repository().FindTableBySql(sql.ToString());
        }









    }
}