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;
using NFine.Domain._03_Entity.WMS;
using System.Net;
using System.IO;

namespace NFine.Application.WMS
{
    public class ICSRCVIQCsApp : RepositoryFactory<ICSVendor>
    {
        /// <summary>
        /// 采购
        /// </summary>
        /// <param name="jqgridparam"></param>
        /// <returns></returns>
        public DataTable GetICSInspection(ref Pagination jqgridparam, string queryJson)
        {
               string ERPSign = Configs.GetValue("ERPSign");
        string WorkPoint = NFine.Code.OperatorProvider.Provider.GetCurrent().Location;
            DataTable dt = new DataTable();
            var queryParam = queryJson.ToJObject();
            List<DbParameter> parameter = new List<DbParameter>();
            string sql = string.Empty;
            if (ERPSign=="U8")
            {
                 sql = @"  select DISTINCT
                                                a.ID as DHID,
                                                d.ID ,
                                                h.ID as JYID,
                                                a.DNCode ,
                                                a.Sequence ,
                                                a.ASNCode ,
                								l.POCode,
                                                c.InvCode ,
                                                c.InvName ,
                                                c.INVSTD ,
                                                c.ClassName,
                                                CAST(b.DNQuantity as decimal(18,4)) as AllNumber, 
                                                CAST(ISNULL(h.QualifiedQuantity, b.DNQuantity)as decimal(18,4))as YLOTQTY ,
                                                CAST(ISNULL(h.UnqualifiedQuantity, 0) as decimal(18,4)) as  NLOTQTY,
                                                CAST(ISNULL(h.WaiveQuantity, 0) as decimal(18,4)) as SpecialQTY,
                                                CAST(ISNULL(h.SampleQuantity, 0) as decimal(18,4)) as SampleQuantity,
                                                c.InvUnit ,
                                                b.LotNo ,
                                                g.BadReasonDesc as BadReasonDesc ,
                								g.BadReasonCode as BRCodeValue,
                								j.BadCode as BCCodeValue,
                                                j.BadDesc as BadDesc, 
                                                k.ContainerID,
                                                isnull(h.MUSERName,'开发者') MUSERName,
                                                CONVERT(VARCHAR(100),d.ProductDate,23) as ProductTime, 
                								 CONVERT(VARCHAR(100),a.CreateDateTime,23) as CreateDateTime,
                								CONVERT(VARCHAR(100),h.MTIME,23) as MTIME,
                                                CASE WHEN isnull(h.ID,'') =''THEN '未检验' ELSE '已检验' END AS TestState,
                								a.VenCode,
                								m.VenName,
                                                isnull(h.MUSER,'') as Surveyor,
                                                h.MUSERName as ProvingTime 
                                                ,f.Colspan	,f.ProjectCode	,f.BatchCode	,f.Version	,f.Brand	,f.cFree1	,f.cFree2	,f.cFree3	,f.cFree4	,f.cFree5	,f.cFree6	,f.cFree7	,f.cFree8	,f.cFree9	,f.cFree10
                                                ,CASE WHEN h.Result='0' THEN '不合格' when ISNULL(h.Result,'')='' THEN ''   ELSE '合格' END  AS Result,
                                                n.FileName
                                                FROM ICSDeliveryNotice a
                                                inner JOIN ICSASNDetail b ON a.ASNCode=b.ASNCode  AND a.WorkPoint=b.WorkPoint
                                                inner JOIN ICSInventory c ON a.InvCode=c.InvCode and a.WorkPoint=c.WorkPoint --and c.InvIQC='1'
                								inner JOIN ICSInventoryLot d ON b.LotNo=d.LotNo  AND a.InvCode=d.InvCode AND b.WorkPoint=d.WorkPoint
                								left join ICSInventoryLotDetail e on d.LotNo=e.LotNo and d.WorkPoint=e.WorkPoint
                                                LEFT JOIN dbo.ICSInspection h ON h.LotNo=b.LotNo  AND h.WorkPoint=b.WorkPoint --and Enable='1'
                								left join ICSBadReason g on h.BRCode =g.BadReasonCode and h.WorkPoint=g.WorkPoint
                                                left join ICSBadCode j on h.BCCode =j.BadCode and h.WorkPoint=j.WorkPoint
                                                LEFT JOIN ICSContainerLot k ON b.LotNo=k.LotNo AND b.WorkPoint=k.WorkPoint
                                                inner JOIN ICSPurchaseOrder l ON e.TransCode =l.POCode AND e.TransSequence=l.Sequence AND a.PODetailID=l.PODetailID AND d.WorkPoint=l.WorkPoint
                								left join ICSExtension f on l.ExtensionID=f.ID and l.WorkPoint=f.WorkPoint
                								left join dbo.ICSVendor m on a.VenCode=m.VenCode and a.WorkPoint=m.WorkPoint
                                                left join ICSInspectionFile n on h.id=n.InspectionID and h.WorkPoint=n.WorkPoint
                                                where b.LotNo is not null  and a.DNType='1' {0} 
                
                 ";
            }
            else
            {
                 sql = @"    select DISTINCT
                                a.ID as DHID,
                                d.ID ,
                                h.ID as JYID,
                                a.DNCode ,
                                a.Sequence ,
                                a.ASNCode ,
								l.POCode,
                                c.InvCode ,
                                c.InvName ,
                                c.INVSTD ,
                                c.ClassName,
                                CAST(d.Quantity as decimal(18,4)) as AllNumber, 
                                CAST(ISNULL(h.QualifiedQuantity, d.Quantity)as decimal(18,4))as YLOTQTY ,
                                CAST(ISNULL(h.UnqualifiedQuantity, 0) as decimal(18,4)) as  NLOTQTY,
                                CAST(ISNULL(h.WaiveQuantity, 0) as decimal(18,4)) as SpecialQTY,
                                CAST(ISNULL(h.SampleQuantity, 0) as decimal(18,4)) as SampleQuantity,
                                c.InvUnit ,
                                d.LotNo ,
                                g.BadReasonDesc as BadReasonDesc ,
								g.BadReasonCode as BRCodeValue,
								j.BadCode as BCCodeValue,
                                j.BadDesc as BadDesc, 
                                k.ContainerID,
                                isnull(h.MUSERName,'开发者') MUSERName,
                                CONVERT(VARCHAR(100),d.ProductDate,23) as ProductTime, 
								 CONVERT(VARCHAR(100),a.CreateDateTime,23) as CreateDateTime,
								CONVERT(VARCHAR(100),h.MTIME,23) as MTIME,
                                CASE WHEN isnull(h.ID,'') =''THEN '未检验' ELSE '已检验' END AS TestState,
								a.VenCode,
								m.VenName,
                                isnull(h.MUSER,'') as Surveyor,
                                h.MUSERName as ProvingTime 
                                ,f.Colspan	,f.ProjectCode	,f.BatchCode	,f.Version	,f.Brand	,f.cFree1	,f.cFree2	,f.cFree3	,f.cFree4	,f.cFree5	,f.cFree6	,f.cFree7	,f.cFree8	,f.cFree9	,f.cFree10
                                 ,CASE WHEN h.Result='0' THEN '不合格' when ISNULL(h.Result,'')='' THEN ''   ELSE '合格' END  AS Result,
                                n.FileName
                                FROM ICSDeliveryNotice a
                                --inner JOIN ICSASNDetail b ON a.ASNCode=b.ASNCode  AND a.WorkPoint=b.WorkPoint
								left join ICSInventoryLotDetail e on a.DNCode=e.TransCode and a.Sequence=e.TransSequence and a.WorkPoint=e.WorkPoint
                                inner JOIN ICSInventory c ON a.InvCode=c.InvCode and a.WorkPoint=c.WorkPoint and c.InvIQC='1'
								inner JOIN ICSInventoryLot d ON e.LotNo=d.LotNo  AND a.InvCode=d.InvCode AND a.WorkPoint=d.WorkPoint
                                LEFT JOIN dbo.ICSInspection h ON h.LotNo=d.LotNo  AND h.WorkPoint=a.WorkPoint and Enable='1'
								left join ICSBadReason g on h.BRCode =g.BadReasonCode and h.WorkPoint=g.WorkPoint
                                left join ICSBadCode j on h.BCCode =j.BadCode and h.WorkPoint=j.WorkPoint
                                LEFT JOIN ICSContainerLot k ON d.LotNo=k.LotNo AND a.WorkPoint=k.WorkPoint
                                inner JOIN ICSPurchaseOrder l ON a.PODetailID=l.PODetailID AND a.WorkPoint=l.WorkPoint
								left join ICSExtension f on l.ExtensionID=f.ID and l.WorkPoint=f.WorkPoint
								left join dbo.ICSVendor m on a.VenCode=m.VenCode and a.WorkPoint=m.WorkPoint
                                left join ICSInspectionFile n on h.id=n.InspectionID and h.WorkPoint=n.WorkPoint
                                where d.LotNo is not null  and a.DNType='1' AND a.WorkPoint='{1}' {0} 

 ";
            }
         
            string wheresql = "";
            if (!string.IsNullOrWhiteSpace(queryJson))
            {
                if (!string.IsNullOrWhiteSpace(queryParam["POCode"].ToString()))
                {
                    wheresql += " and a.DNCode like '%" + queryParam["POCode"].ToString() + "%' ";
                }
                if (!string.IsNullOrWhiteSpace(queryParam["ASNCode"].ToString()))
                {
                    wheresql += " and a.ASNCode like '%" + queryParam["ASNCode"].ToString() + "%' ";
                }
                if (!string.IsNullOrWhiteSpace(queryParam["CaiGouCode"].ToString()))
                {
                    wheresql += " and l.POCode like '%" + queryParam["CaiGouCode"].ToString() + "%' ";
                }
                if (!string.IsNullOrWhiteSpace(queryParam["VenCode"].ToString()))
                {
                    wheresql += " and a.VenCode like '%" + queryParam["VenCode"].ToString() + "%' ";
                }
                if (!string.IsNullOrWhiteSpace(queryParam["VenName"].ToString()))
                {
                    wheresql += " and m.VenName like '%" + queryParam["VenName"].ToString() + "%' ";
                }
                if (!string.IsNullOrWhiteSpace(queryParam["InvCode"].ToString()))
                {
                    wheresql += " and c.InvCode like '%" + queryParam["InvCode"].ToString() + "%' ";
                }
                if (!string.IsNullOrWhiteSpace(queryParam["InvName"].ToString()))
                {
                    wheresql += " and c.InvName like '%" + queryParam["InvName"].ToString() + "%' ";
                }
                if (!string.IsNullOrWhiteSpace(queryParam["BatchCode"].ToString()))
                {
                    wheresql += " and f.BatchCode like '%" + queryParam["BatchCode"].ToString() + "%' ";
                }
                if (!string.IsNullOrWhiteSpace(queryParam["TimeFrom"].ToString()))
                {
                    wheresql += " and a.CreateDateTime >= '" + queryParam["TimeFrom"].ToString() + "' ";
                }
                if (!string.IsNullOrWhiteSpace(queryParam["TimeArrive"].ToString()))
                {
                    wheresql += " and a.CreateDateTime <= '" + queryParam["TimeArrive"].ToString() + "' ";
                }

                if (!string.IsNullOrWhiteSpace(queryParam["ReleaseState"].ToString()))
                {
                    string ReleaseState = queryParam["ReleaseState"].ToString();
                    if (ReleaseState == "1")
                        wheresql += " and CASE WHEN isnull(h.ID,'') =''THEN '未检验' ELSE '已检验' END='已检验'";
                    else if (ReleaseState == "0")
                        wheresql += " and CASE WHEN isnull(h.ID,'') =''THEN '未检验' ELSE '已检验' END='未检验'";
                    else 
                        wheresql += " ";
                }
                if (!string.IsNullOrWhiteSpace(queryParam["selEnableShow"].ToString()))
                {
                    string selEnableShow = queryParam["selEnableShow"].ToString();
                    if (selEnableShow == "1")
                        wheresql += " and ISNULL(h.UnqualifiedQuantity, 0) <= 0";
                    else if (selEnableShow == "0")
                        wheresql += " and ISNULL(h.UnqualifiedQuantity, 0) > 0";
                }
            }
            sql = string.Format(sql, wheresql, WorkPoint);

            return Repository().FindTablePageBySql(sql.ToString(), parameter.ToArray(), ref jqgridparam);
        }
        public DataTable GetInspectionFile(ref Pagination jqgridparam, string queryJson,string JYID)
        {
            var queryParam = queryJson.ToJObject();
            List<DbParameter> parameter = new List<DbParameter>();
            string sql = @"SELECT * FROM dbo.ICSInspectionFile WHERE InspectionID='{0}'";
            sql = string.Format(sql, JYID);
            return Repository().FindTablePageBySql(sql.ToString(), parameter.ToArray(), ref jqgridparam);
        }
        //委外
        public DataTable GetICSInspection2(ref Pagination jqgridparam, string queryJson)
        {
            DataTable dt = new DataTable();
            var queryParam = queryJson.ToJObject();
            List<DbParameter> parameter = new List<DbParameter>();
            string sql = @"select DISTINCT a.ID as DHID, d.ID ,h.ID as JYID, a.ODNCode as DNCode ,a.Sequence ,a.OASNCode ,l.OOCode,c.InvCode ,   c.InvName , c.INVSTD , c.ClassName,
                                f.Colspan	,f.ProjectCode	,f.BatchCode	,f.Version	,f.Brand	,f.cFree1	,f.cFree2	,f.cFree3	,f.cFree4	,f.cFree5	,f.cFree6	,f.cFree7	,f.cFree8	,f.cFree9	,f.cFree10,
                                CAST(b.ODNQuantity as NUMERIC(10,4)) as AllNumber, 
                                CAST(ISNULL(h.QualifiedQuantity, b.ODNQuantity)as NUMERIC(10,4))as YLOTQTY ,
                                CAST(ISNULL(h.UnqualifiedQuantity, 0) as NUMERIC(10,4)) as  NLOTQTY,
                                CAST(ISNULL(h.WaiveQuantity, 0) as NUMERIC(10,4)) as SpecialQTY,
                                CAST(ISNULL(h.SampleQuantity, 0) as NUMERIC(10,4)) as SampleQuantity,
                                c.InvUnit , b.LotNo , g.BadReasonDesc as BadReasonDesc ,g.BadReasonCode as BRCodeValue,j.BadCode as BCCodeValue, j.BadDesc as BadDesc,
                                k.ContainerID,isnull(h.MUSERName,'开发者') MUSERName, CONVERT(VARCHAR(100),d.ProductDate,23) as ProductTime,
								 CONVERT(VARCHAR(100),a.CreateDateTime,23) as CreateDateTime,CONVERT(VARCHAR(100),h.MTIME,23) as MTIME,
                                CASE WHEN isnull(h.ID,'') =''THEN '未检验' ELSE '已检验' END AS TestState,a.VenCode,m.VenName,
                                 isnull(h.MUSER,'') as Surveyor, h.MUSERName as ProvingTime
                                 ,CASE WHEN h.Result='0' THEN '不合格' when ISNULL(h.Result,'')='' THEN ''   ELSE '合格' END  AS Result,
                                n.FileName,a.EATTRIBUTE1,a.EATTRIBUTE2,a.EATTRIBUTE3,a.EATTRIBUTE4,a.EATTRIBUTE5,a.EATTRIBUTE6,a.EATTRIBUTE7,a.EATTRIBUTE8,
                                a.EATTRIBUTE9,a.EATTRIBUTE10 
                                FROM ICSODeliveryNotice a
                                LEFT JOIN ICSOASNDetail b ON a.OASNCode=b.OASNCode  AND a.WorkPoint=b.WorkPoint
                                inner JOIN ICSInventory c ON a.InvCode=c.InvCode and a.WorkPoint=c.WorkPoint and  c.InvIQC='1'
                                inner JOIN ICSInventoryLot d ON b.LotNo=d.LotNo  AND a.InvCode=d.InvCode AND b.WorkPoint=d.WorkPoint
								left join ICSInventoryLotDetail e on d.LotNo=e.LotNo and d.WorkPoint=e.WorkPoint
                                LEFT JOIN dbo.ICSInspection h ON h.LotNo=b.LotNo  AND h.WorkPoint=b.WorkPoint and Enable='1'
								left join ICSBadReason g on h.BRCode =g.BadReasonCode and h.WorkPoint=g.WorkPoint
                                left join ICSBadCode j on h.BCCode =j.BadCode and h.WorkPoint=j.WorkPoint
                                LEFT JOIN ICSContainerLot k ON b.LotNo=k.LotNo AND b.WorkPoint=k.WorkPoint
                                inner JOIN ICSOutsourcingOrder l ON e.TransCode =l.OOCode AND e.TransSequence=l.Sequence  AND a.OODetailID=l.OODetailID AND d.WorkPoint=l.WorkPoint
								left join ICSExtension f on l.ExtensionID=f.ID and l.WorkPoint=f.WorkPoint
								left join dbo.ICSVendor m on a.VenCode=m.VenCode and a.WorkPoint=m.WorkPoint
                                left join ICSInspectionFile n on h.id=n.InspectionID and h.WorkPoint=n.WorkPoint
                                where b.LotNo is not null and  ISNULL(CAST(b.ODNQuantity as VARCHAR), '')! ='' AND ISNULL(b.ODNQuantity, 0)<>0  {0}

 ";
            #region 无SRM逻辑
            //            sql = @"UNION all
            //select 	 
            //                                a.ID as DHID,
            //                                d.ID ,
            //                                h.ID as JYID,
            //                                a.ODNCode as DNCode ,
            //                                a.Sequence ,
            //                                a.OASNCode ,
            //                                c.InvCode ,
            //                                c.InvName ,
            //                                c.INVSTD ,
            //                                c.ClassName,
            //                                f.BatchCode ,
            //                                CAST(d.Quantity as NUMERIC(10,4)) as AllNumber, 
            //                                CAST(ISNULL(h.QualifiedQuantity, d.Quantity)as NUMERIC(10,4))as YLOTQTY ,
            //                                CAST(ISNULL(h.UnqualifiedQuantity, 0) as NUMERIC(10,4)) as  NLOTQTY,
            //                                CAST(ISNULL(h.WaiveQuantity, 0) as NUMERIC(10,4)) as SpecialQTY,
            //                                c.InvUnit ,
            //                                d.LotNo ,
            //                                g.BadReasonDesc as BadReasonDesc ,
            //								g.BadReasonCode as BRCodeValue,
            //								j.BadCode as BCCodeValue,
            //                                j.BadDesc as BadDesc,
            //                                k.ContainerID,
            //                                isnull(h.MUSERName,'开发者') MUSERName,
            //                                CONVERT(VARCHAR(100),d.ProductDate,23) as ProductTime,
            //								CONVERT(VARCHAR(100),h.MTIME,23) as MTIME,
            //                                CASE WHEN isnull(h.ID,'') =''THEN '未检验' ELSE '已检验' END AS TestState
            //                                FROM ICSODeliveryNotice a
            //								inner join ICSInventoryLotDetail e on e.TransCode =a.ODNCode AND e.TransSequence=a.Sequence AND  e.WorkPoint=a.WorkPoint
            //								inner JOIN ICSInventoryLot d ON d.LotNo=e.LotNo and d.WorkPoint=e.WorkPoint
            //                                LEFT JOIN ICSInventory c ON a.InvCode=c.InvCode and a.WorkPoint=c.WorkPoint
            //                                LEFT JOIN dbo.ICSInspection h ON h.LotNo=d.LotNo  AND h.WorkPoint=d.WorkPoint
            //								left join ICSBadReason g on h.BRCode =g.BadReasonCode and h.WorkPoint=g.WorkPoint
            //                                left join ICSBadCode j on h.BCCode =j.BadCode and h.WorkPoint=j.WorkPoint
            //                                LEFT JOIN ICSContainerLot k ON d.LotNo=k.LotNo AND d.WorkPoint=k.WorkPoint
            //								left join ICSExtension f on a.ExtensionID=f.ID and a.WorkPoint=f.WorkPoint
            //                                where 1=1 {0}"; 
            #endregion
            string wheresql = "";
            if (!string.IsNullOrWhiteSpace(queryJson))
            {
                if (!string.IsNullOrWhiteSpace(queryParam["POCode"].ToString()))
                {
                    wheresql += " and a.ODNCode like '%" + queryParam["POCode"].ToString() + "%' ";
                }
                if (!string.IsNullOrWhiteSpace(queryParam["ASNCode"].ToString()))
                {
                    wheresql += " and a.OASNCode like '%" + queryParam["ASNCode"].ToString() + "%' ";
                }
                if (!string.IsNullOrWhiteSpace(queryParam["CaiGouCode"].ToString()))
                {
                    wheresql += " and l.OOCode like '%" + queryParam["CaiGouCode"].ToString() + "%' ";
                }
                if (!string.IsNullOrWhiteSpace(queryParam["VenCode"].ToString()))
                {
                    wheresql += " and a.VenCode like '%" + queryParam["VenCode"].ToString() + "%' ";
                }
                if (!string.IsNullOrWhiteSpace(queryParam["VenName"].ToString()))
                {
                    wheresql += " and m.VenName like '%" + queryParam["VenName"].ToString() + "%' ";
                }
                if (!string.IsNullOrWhiteSpace(queryParam["InvCode"].ToString()))
                {
                    wheresql += " and c.InvCode like '%" + queryParam["InvCode"].ToString() + "%' ";
                }
                if (!string.IsNullOrWhiteSpace(queryParam["InvName"].ToString()))
                {
                    wheresql += " and c.InvName like '%" + queryParam["InvName"].ToString() + "%' ";
                }
                if (!string.IsNullOrWhiteSpace(queryParam["BatchCode"].ToString()))
                {
                    wheresql += " and f.BatchCode like '%" + queryParam["BatchCode"].ToString() + "%' ";
                }
                if (!string.IsNullOrWhiteSpace(queryParam["TimeFrom"].ToString()))
                {
                    wheresql += " and a.CreateDateTime >= '" + queryParam["TimeFrom"].ToString() + "' ";
                }
                if (!string.IsNullOrWhiteSpace(queryParam["TimeArrive"].ToString()))
                {
                    wheresql += " and a.CreateDateTime <= '" + queryParam["TimeArrive"].ToString() + "' ";
                }

                if (!string.IsNullOrWhiteSpace(queryParam["ReleaseState"].ToString()))
                {
                    string ReleaseState = queryParam["ReleaseState"].ToString();
                    if (ReleaseState == "1")
                        wheresql += " and CASE WHEN isnull(h.ID,'') =''THEN '未检验' ELSE '已检验' END='已检验'";
                    else if (ReleaseState == "0")
                        wheresql += " and CASE WHEN isnull(h.ID,'') =''THEN '未检验' ELSE '已检验' END='未检验'";
                    else
                        wheresql += " ";
                }
                if (!string.IsNullOrWhiteSpace(queryParam["selEnableShow"].ToString()))
                {
                    string selEnableShow = queryParam["selEnableShow"].ToString();
                    if (selEnableShow == "1")
                        wheresql += " and ISNULL(h.UnqualifiedQuantity, 0) <= 0";
                    else if (selEnableShow == "0")
                        wheresql += " and ISNULL(h.UnqualifiedQuantity, 0) > 0";
                }
            }
            sql = string.Format(sql, wheresql);
            return Repository().FindTablePageBySql(sql.ToString(), parameter.ToArray(), ref jqgridparam);
        }


        //工单
        public DataTable GetICSInspection3(ref Pagination jqgridparam, string queryJson)
        {
            DataTable dt = new DataTable();
            var queryParam = queryJson.ToJObject();
            List<DbParameter> parameter = new List<DbParameter>();
            string sql = @"select DISTINCT a.ID as GDID, e.ID , a.MOCode as DNCode, a.Sequence , h.ID as JYID, c.InvCode , c.InvName , c.INVSTD , c.ClassName,c.InvDesc,
                                f.Colspan	,f.ProjectCode	,f.BatchCode	,f.Version	,f.Brand	,f.cFree1	,f.cFree2	,f.cFree3	,f.cFree4	,f.cFree5	,f.cFree6	,f.cFree7	,f.cFree8	,f.cFree9	,f.cFree10, e.Quantity as AllNumber, 
                                CAST(ISNULL(h.QualifiedQuantity,e.Quantity)as NUMERIC(10,4))as YLOTQTY ,
                                CAST(ISNULL(h.UnqualifiedQuantity, 0) as NUMERIC(10,4)) as  NLOTQTY,
                                CAST(ISNULL(h.WaiveQuantity, 0) as NUMERIC(10,4)) as SpecialQTY,
                                CAST(ISNULL(h.SampleQuantity, 0) as NUMERIC(10,4)) as SampleQuantity,
                                c.InvUnit , e.LotNo , g.BadReasonDesc as BadReasonDesc ,
								g.BadReasonCode as BRCodeValue,j.BadCode as BCCodeValue, j.BadDesc as BadDesc, i.ContainerID,
                                isnull(h.MUSERName,'开发者') MUSERName, CONVERT(VARCHAR(100),e.ProductDate,23) as ProductTime,CONVERT(VARCHAR(100),h.MTIME,23) as MTIME,
                                CASE WHEN isnull(h.ID,'') =''THEN '未检验' ELSE '已检验' END AS TestState,
                                isnull(h.MUSER,'') as Surveyor, h.MUSERName as ProvingTime
                                 ,CASE WHEN h.Result='0' THEN '不合格' when ISNULL(h.Result,'')='' THEN ''   ELSE '合格' END  AS Result,
                                n.FileName,a.EATTRIBUTE1,a.EATTRIBUTE2,a.EATTRIBUTE3,a.EATTRIBUTE4,a.EATTRIBUTE5,a.EATTRIBUTE6,a.EATTRIBUTE7,a.EATTRIBUTE8,
a.EATTRIBUTE9,a.EATTRIBUTE10
                            from ICSMO a
                            inner JOIN dbo.ICSInventory c ON a.InvCode = c.InvCode AND a.WorkPoint=c.WorkPoint and c.InvIQC='1'
							left join ICSInventoryLotDetail b on a.MOCode=b.TransCode and a.Sequence=b.TransSequence and a.WorkPoint=b.WorkPoint
							left join ICSExtension f on a.ExtensionID=f.ID and a.WorkPoint=f.WorkPoint                          
                            LEFT JOIN dbo.ICSInventoryLot e ON e.LotNo=b.LotNo  AND e.WorkPoint=b.WorkPoint
                            LEFT JOIN dbo.ICSInspection h ON h.LotNO=e.LotNO and h.InvCode=e.InvCode and h.WorkPoint=e.WorkPoint  and Enable='1'
							left join ICSBadReason g on h.BRCode =g.BadReasonCode and h.WorkPoint=g.WorkPoint
                            left join ICSBadCode j on h.BCCode =j.BadCode and h.WorkPoint=j.WorkPoint
                            LEFT JOIN dbo.ICSContainerLot i ON e.LotNO=i.LotNO AND e.WorkPoint=i.WorkPoint
                            left join ICSInspectionFile n on h.id=n.InspectionID and h.WorkPoint=n.WorkPoint
                            where a.MOStatus<>'3' and e.LotNO is not null {0}  ";
            string wheresql = "";
            if (!string.IsNullOrWhiteSpace(queryJson))
            {
                if (!string.IsNullOrWhiteSpace(queryParam["POCode"].ToString()))
                {
                    wheresql += " and a.MOCode like '%" + queryParam["POCode"].ToString() + "%' ";
                }
                if (!string.IsNullOrWhiteSpace(queryParam["InvCode"].ToString()))
                {
                    wheresql += " and c.InvCode like '%" + queryParam["InvCode"].ToString() + "%' ";
                }
                if (!string.IsNullOrWhiteSpace(queryParam["InvName"].ToString()))
                {
                    wheresql += " and c.InvName like '%" + queryParam["InvName"].ToString() + "%' ";
                }
                if (!string.IsNullOrWhiteSpace(queryParam["BatchCode"].ToString()))
                {
                    wheresql += " and f.BatchCode like '%" + queryParam["BatchCode"].ToString() + "%' ";
                }

                if (!string.IsNullOrWhiteSpace(queryParam["ReleaseState"].ToString()))
                {
                    string ReleaseState = queryParam["ReleaseState"].ToString();
                    if (ReleaseState == "1")
                        wheresql += " and CASE WHEN isnull(h.ID,'') =''THEN '未检验' ELSE '已检验' END='已检验'";
                    else if (ReleaseState == "0")
                        wheresql += " and CASE WHEN isnull(h.ID,'') =''THEN '未检验' ELSE '已检验' END='未检验'";
                    else
                        wheresql += " ";
                }
                if (!string.IsNullOrWhiteSpace(queryParam["selEnableShow"].ToString()))
                {
                    string selEnableShow = queryParam["selEnableShow"].ToString();
                    if (selEnableShow == "1")
                        wheresql += " and ISNULL(h.UnqualifiedQuantity, 0) <= 0";
                    else if (selEnableShow == "0")
                        wheresql += " and ISNULL(h.UnqualifiedQuantity, 0) > 0";
                }
            }
            sql = string.Format(sql, wheresql);
            return Repository().FindTablePageBySql(sql.ToString(), parameter.ToArray(), ref jqgridparam);
        }

        //其他入库
        public DataTable GetICSInspection4(ref Pagination jqgridparam, string queryJson)
        {
            DataTable dt = new DataTable(); 
            var queryParam = queryJson.ToJObject();
            List<DbParameter> parameter = new List<DbParameter>();
            string sql = @"select DISTINCT a.ID as GDID, e.ID , a.InCode as DNCode, a.Sequence , h.ID as JYID, c.InvCode , c.InvName , c.INVSTD , c.ClassName,
                                f.Colspan	,f.ProjectCode	,f.BatchCode	,f.Version	,f.Brand	,f.cFree1	,f.cFree2	,f.cFree3	,f.cFree4	,f.cFree5	,f.cFree6	,f.cFree7	,f.cFree8	,f.cFree9	,f.cFree10, e.Quantity as AllNumber, 
                                CAST(ISNULL(h.QualifiedQuantity,e.Quantity)as NUMERIC(10,4))as YLOTQTY ,
                                CAST(ISNULL(h.UnqualifiedQuantity, 0) as NUMERIC(10,4)) as  NLOTQTY,
                                CAST(ISNULL(h.WaiveQuantity, 0) as NUMERIC(10,4)) as SpecialQTY,
                                CAST(ISNULL(h.SampleQuantity, 0) as NUMERIC(10,4)) as SampleQuantity,
                                c.InvUnit , e.LotNo , g.BadReasonDesc as BadReasonDesc ,
								g.BadReasonCode as BRCodeValue,j.BadCode as BCCodeValue, j.BadDesc as BadDesc, i.ContainerID,
                                isnull(h.MUSERName,'开发者') MUSERName, CONVERT(VARCHAR(100),e.ProductDate,23) as ProductTime,CONVERT(VARCHAR(100),h.MTIME,23) as MTIME,
                                CASE WHEN isnull(h.ID,'') =''THEN '未检验' ELSE '已检验' END AS TestState,
                                isnull(h.MUSER,'') as Surveyor, h.MUSERName as ProvingTime
                             ,CASE WHEN h.Result='0' THEN '不合格' when ISNULL(h.Result,'')='' THEN ''   ELSE '合格' END  AS Result,
                                n.FileName,a.EATTRIBUTE1,a.EATTRIBUTE2,a.EATTRIBUTE3,a.EATTRIBUTE4,a.EATTRIBUTE5,a.EATTRIBUTE6,a.EATTRIBUTE7,a.EATTRIBUTE8,
a.EATTRIBUTE9,a.EATTRIBUTE10
                            from ICSOtherIn a
                            inner JOIN dbo.ICSInventory c ON a.InvCode = c.InvCode AND a.WorkPoint=c.WorkPoint and c.InvIQC='1'
							left join ICSInventoryLotDetail b on a.InCode=b.TransCode and a.Sequence=b.TransSequence and a.WorkPoint=b.WorkPoint
							left join ICSExtension f on a.ExtensionID=f.ID and a.WorkPoint=f.WorkPoint                          
                            LEFT JOIN dbo.ICSInventoryLot e ON e.LotNo=b.LotNo  AND e.WorkPoint=b.WorkPoint
                            LEFT JOIN dbo.ICSInspection h ON h.LotNO=e.LotNO and h.InvCode=e.InvCode and h.WorkPoint=e.WorkPoint  and Enable='1'
							left join ICSBadReason g on h.BRCode =g.BadReasonCode and h.WorkPoint=g.WorkPoint
                            left join ICSBadCode j on h.BCCode =j.BadCode and h.WorkPoint=j.WorkPoint
                            LEFT JOIN dbo.ICSContainerLot i ON e.LotNO=i.LotNO AND e.WorkPoint=i.WorkPoint
                            left join ICSInspectionFile n on h.id=n.InspectionID and h.WorkPoint=n.WorkPoint
                            where e.LotNO is not null {0}  ";
            string wheresql = "";
            if (!string.IsNullOrWhiteSpace(queryJson))
            {
                if (!string.IsNullOrWhiteSpace(queryParam["POCode"].ToString()))
                {
                    wheresql += " and a.InCode like '%" + queryParam["POCode"].ToString() + "%' ";
                }
                if (!string.IsNullOrWhiteSpace(queryParam["InvCode"].ToString()))
                {
                    wheresql += " and c.InvCode like '%" + queryParam["InvCode"].ToString() + "%' ";
                }
                if (!string.IsNullOrWhiteSpace(queryParam["InvName"].ToString()))
                {
                    wheresql += " and c.InvName like '%" + queryParam["InvName"].ToString() + "%' ";
                }
                if (!string.IsNullOrWhiteSpace(queryParam["BatchCode"].ToString()))
                {
                    wheresql += " and f.BatchCode like '%" + queryParam["BatchCode"].ToString() + "%' ";
                }

                if (!string.IsNullOrWhiteSpace(queryParam["ReleaseState"].ToString()))
                {
                    string ReleaseState = queryParam["ReleaseState"].ToString();
                    if (ReleaseState == "1")
                        wheresql += " and CASE WHEN isnull(h.ID,'') =''THEN '未检验' ELSE '已检验' END='已检验'";
                    else if (ReleaseState == "0")
                        wheresql += " and CASE WHEN isnull(h.ID,'') =''THEN '未检验' ELSE '已检验' END='未检验'";
                    else
                        wheresql += " ";
                }
                if (!string.IsNullOrWhiteSpace(queryParam["selEnableShow"].ToString()))
                {
                    string selEnableShow = queryParam["selEnableShow"].ToString();
                    if (selEnableShow == "1")
                        wheresql += " and ISNULL(h.UnqualifiedQuantity, 0) <= 0";
                    else if (selEnableShow == "0")
                        wheresql += " and ISNULL(h.UnqualifiedQuantity, 0) > 0";
                }
            }
            sql = string.Format(sql, wheresql);
            return Repository().FindTablePageBySql(sql.ToString(), parameter.ToArray(), ref jqgridparam);
        }

        //生产退料
        public DataTable GetICSInspection5(ref Pagination jqgridparam, string queryJson)
        {
            DataTable dt = new DataTable();
            var queryParam = queryJson.ToJObject();
            List<DbParameter> parameter = new List<DbParameter>();
            string sql = @"select DISTINCT a.ID as GDID, e.ID , a.ApplyNegCode as DNCode, ad.Sequence , h.ID as JYID, c.InvCode , c.InvName , c.INVSTD , c.ClassName,
                                f.Colspan	,f.ProjectCode	,f.BatchCode	,f.Version	,f.Brand	,f.cFree1	,f.cFree2	,f.cFree3	,f.cFree4	,f.cFree5	,f.cFree6	,f.cFree7	,f.cFree8	,f.cFree9	,f.cFree10, e.Quantity as AllNumber, 
                                CAST(ISNULL(h.QualifiedQuantity,e.Quantity)as NUMERIC(10,4))as YLOTQTY ,
                                CAST(ISNULL(h.UnqualifiedQuantity, 0) as NUMERIC(10,4)) as  NLOTQTY,
                                CAST(ISNULL(h.WaiveQuantity, 0) as NUMERIC(10,4)) as SpecialQTY,
                                CAST(ISNULL(h.SampleQuantity, 0) as NUMERIC(10,4)) as SampleQuantity,
                                c.InvUnit , e.LotNo , g.BadReasonDesc as BadReasonDesc ,
								g.BadReasonCode as BRCodeValue,j.BadCode as BCCodeValue, j.BadDesc as BadDesc, i.ContainerID,
                                isnull(h.MUSERName,'开发者') MUSERName, CONVERT(VARCHAR(100),e.ProductDate,23) as ProductTime,CONVERT(VARCHAR(100),h.MTIME,23) as MTIME,
                                CASE WHEN isnull(h.ID,'') =''THEN '未检验' ELSE '已检验' END AS TestState,
                                isnull(h.MUSER,'') as Surveyor, h.MUSERName as ProvingTime
                                 ,CASE WHEN h.Result='0' THEN '不合格' when ISNULL(h.Result,'')='' THEN ''   ELSE '合格' END  AS Result,
                                n.FileName ,a.EATTRIBUTE1,a.EATTRIBUTE2,a.EATTRIBUTE3,a.EATTRIBUTE4,a.EATTRIBUTE5,a.EATTRIBUTE6,a.EATTRIBUTE7,a.EATTRIBUTE8,
a.EATTRIBUTE9,a.EATTRIBUTE10
                            from ICSMOApplyNeg a
														LEFT JOIN ICSMOApplyNegDetail ad ON a.ApplyNegCode=ad.ApplyNegCode AND a.WorkPoint=ad.WorkPoint
                            inner JOIN dbo.ICSInventory c ON ad.InvCode = c.InvCode AND ad.WorkPoint=c.WorkPoint and c.InvIQC='1'
							left join ICSInventoryLotDetail b on a.ApplyNegCode=b.TransCode and ad.Sequence=b.TransSequence and ad.WorkPoint=b.WorkPoint
							left join ICSExtension f on ad.ExtensionID=f.ID and ad.WorkPoint=f.WorkPoint                          
                            LEFT JOIN dbo.ICSInventoryLot e ON e.LotNo=b.LotNo  AND e.WorkPoint=b.WorkPoint
                            LEFT JOIN dbo.ICSInspection h ON h.LotNO=e.LotNO and h.InvCode=e.InvCode and h.WorkPoint=e.WorkPoint  and Enable='1'
							left join ICSBadReason g on h.BRCode =g.BadReasonCode and h.WorkPoint=g.WorkPoint
                            left join ICSBadCode j on h.BCCode =j.BadCode and h.WorkPoint=j.WorkPoint
                            LEFT JOIN dbo.ICSContainerLot i ON e.LotNO=i.LotNO AND e.WorkPoint=i.WorkPoint
                            left join ICSInspectionFile n on h.id=n.InspectionID and h.WorkPoint=n.WorkPoint
                            where e.LotNO is not null {0}  ";
            string wheresql = "";
            if (!string.IsNullOrWhiteSpace(queryJson))
            {
                if (!string.IsNullOrWhiteSpace(queryParam["POCode"].ToString()))
                {
                    wheresql += " and a.ApplyNegCode like '%" + queryParam["POCode"].ToString() + "%' ";
                }
                if (!string.IsNullOrWhiteSpace(queryParam["InvCode"].ToString()))
                {
                    wheresql += " and c.InvCode like '%" + queryParam["InvCode"].ToString() + "%' ";
                }
                if (!string.IsNullOrWhiteSpace(queryParam["InvName"].ToString()))
                {
                    wheresql += " and c.InvName like '%" + queryParam["InvName"].ToString() + "%' ";
                }
                if (!string.IsNullOrWhiteSpace(queryParam["BatchCode"].ToString()))
                {
                    wheresql += " and f.BatchCode like '%" + queryParam["BatchCode"].ToString() + "%' ";
                }

                if (!string.IsNullOrWhiteSpace(queryParam["ReleaseState"].ToString()))
                {
                    string ReleaseState = queryParam["ReleaseState"].ToString();
                    if (ReleaseState == "1")
                        wheresql += " and CASE WHEN isnull(h.ID,'') =''THEN '未检验' ELSE '已检验' END='已检验'";
                    else if (ReleaseState == "0")
                        wheresql += " and CASE WHEN isnull(h.ID,'') =''THEN '未检验' ELSE '已检验' END='未检验'";
                    else
                        wheresql += " ";
                }
                if (!string.IsNullOrWhiteSpace(queryParam["selEnableShow"].ToString()))
                {
                    string selEnableShow = queryParam["selEnableShow"].ToString();
                    if (selEnableShow == "1")
                        wheresql += " and ISNULL(h.UnqualifiedQuantity, 0) <= 0";
                    else if (selEnableShow == "0")
                        wheresql += " and ISNULL(h.UnqualifiedQuantity, 0) > 0";
                }
            }
            sql = string.Format(sql, wheresql);
            return Repository().FindTablePageBySql(sql.ToString(), parameter.ToArray(), ref jqgridparam);
        }

        //委外退料
        public DataTable GetICSInspection6(ref Pagination jqgridparam, string queryJson)
        {
            DataTable dt = new DataTable();
            var queryParam = queryJson.ToJObject();
            List<DbParameter> parameter = new List<DbParameter>();
            string sql = @"select DISTINCT a.ID as GDID, e.ID , a.OApplyNegCode as DNCode, ad.Sequence , h.ID as JYID, c.InvCode , c.InvName , c.INVSTD , c.ClassName,
                                f.Colspan	,f.ProjectCode	,f.BatchCode	,f.Version	,f.Brand	,f.cFree1	,f.cFree2	,f.cFree3	,f.cFree4	,f.cFree5	,f.cFree6	,f.cFree7	,f.cFree8	,f.cFree9	,f.cFree10, e.Quantity as AllNumber, 
                                CAST(ISNULL(h.QualifiedQuantity,e.Quantity)as NUMERIC(10,4))as YLOTQTY ,
                                CAST(ISNULL(h.UnqualifiedQuantity, 0) as NUMERIC(10,4)) as  NLOTQTY,
                                CAST(ISNULL(h.WaiveQuantity, 0) as NUMERIC(10,4)) as SpecialQTY,
                                CAST(ISNULL(h.SampleQuantity, 0) as NUMERIC(10,4)) as SampleQuantity,
                                c.InvUnit , e.LotNo , g.BadReasonDesc as BadReasonDesc ,
								g.BadReasonCode as BRCodeValue,j.BadCode as BCCodeValue, j.BadDesc as BadDesc, i.ContainerID,
                                isnull(h.MUSERName,'开发者') MUSERName, CONVERT(VARCHAR(100),e.ProductDate,23) as ProductTime,CONVERT(VARCHAR(100),h.MTIME,23) as MTIME,
                                CASE WHEN isnull(h.ID,'') =''THEN '未检验' ELSE '已检验' END AS TestState,
                                isnull(h.MUSER,'') as Surveyor, h.MUSERName as ProvingTime
                                 ,CASE WHEN h.Result='0' THEN '不合格' when ISNULL(h.Result,'')='' THEN ''   ELSE '合格' END  AS Result,
                                n.FileName,a.EATTRIBUTE1,a.EATTRIBUTE2,a.EATTRIBUTE3,a.EATTRIBUTE4,a.EATTRIBUTE5,a.EATTRIBUTE6,a.EATTRIBUTE7,a.EATTRIBUTE8,
a.EATTRIBUTE9,a.EATTRIBUTE10
                            from ICSOApplyNeg a
														LEFT JOIN ICSOApplyNegDetail ad ON a.OApplyNegCode=ad.OApplyNegCode AND a.WorkPoint=ad.WorkPoint
                            inner JOIN dbo.ICSInventory c ON ad.InvCode = c.InvCode AND ad.WorkPoint=c.WorkPoint and c.InvIQC='1'
							left join ICSInventoryLotDetail b on a.OApplyNegCode=b.TransCode and ad.Sequence=b.TransSequence and ad.WorkPoint=b.WorkPoint
							left join ICSExtension f on ad.ExtensionID=f.ID and ad.WorkPoint=f.WorkPoint                          
                            LEFT JOIN dbo.ICSInventoryLot e ON e.LotNo=b.LotNo  AND e.WorkPoint=b.WorkPoint
                            LEFT JOIN dbo.ICSInspection h ON h.LotNO=e.LotNO and h.InvCode=e.InvCode and h.WorkPoint=e.WorkPoint   and Enable='1'
							left join ICSBadReason g on h.BRCode =g.BadReasonCode and h.WorkPoint=g.WorkPoint
                            left join ICSBadCode j on h.BCCode =j.BadCode and h.WorkPoint=j.WorkPoint
                            LEFT JOIN dbo.ICSContainerLot i ON e.LotNO=i.LotNO AND e.WorkPoint=i.WorkPoint
                            left join ICSInspectionFile n on h.id=n.InspectionID and h.WorkPoint=n.WorkPoint
                            where e.LotNO is not null {0}  ";
            string wheresql = "";
            if (!string.IsNullOrWhiteSpace(queryJson))
            {
                if (!string.IsNullOrWhiteSpace(queryParam["POCode"].ToString()))
                {
                    wheresql += " and a.OApplyNegCode like '%" + queryParam["POCode"].ToString() + "%' ";
                }
                if (!string.IsNullOrWhiteSpace(queryParam["InvCode"].ToString()))
                {
                    wheresql += " and c.InvCode like '%" + queryParam["InvCode"].ToString() + "%' ";
                }
                if (!string.IsNullOrWhiteSpace(queryParam["InvName"].ToString()))
                {
                    wheresql += " and c.InvName like '%" + queryParam["InvName"].ToString() + "%' ";
                }
                if (!string.IsNullOrWhiteSpace(queryParam["BatchCode"].ToString()))
                {
                    wheresql += " and f.BatchCode like '%" + queryParam["BatchCode"].ToString() + "%' ";
                }

                if (!string.IsNullOrWhiteSpace(queryParam["ReleaseState"].ToString()))
                {
                    string ReleaseState = queryParam["ReleaseState"].ToString();
                    if (ReleaseState == "1")
                        wheresql += " and CASE WHEN isnull(h.ID,'') =''THEN '未检验' ELSE '已检验' END='已检验'";
                    else if (ReleaseState == "0")
                        wheresql += " and CASE WHEN isnull(h.ID,'') =''THEN '未检验' ELSE '已检验' END='未检验'";
                    else
                        wheresql += " ";
                }
                if (!string.IsNullOrWhiteSpace(queryParam["selEnableShow"].ToString()))
                {
                    string selEnableShow = queryParam["selEnableShow"].ToString();
                    if (selEnableShow == "1")
                        wheresql += " and ISNULL(h.UnqualifiedQuantity, 0) <= 0";
                    else if (selEnableShow == "0")
                        wheresql += " and ISNULL(h.UnqualifiedQuantity, 0) > 0";
                }
            }
            sql = string.Format(sql, wheresql);
            return Repository().FindTablePageBySql(sql.ToString(), parameter.ToArray(), ref jqgridparam);
        }

        //销售退货
        public DataTable GetICSInspection7(ref Pagination jqgridparam, string queryJson)
        {
            DataTable dt = new DataTable();
            var queryParam = queryJson.ToJObject();
            List<DbParameter> parameter = new List<DbParameter>();
            string sql = @"select DISTINCT a.ID as GDID, e.ID , a.SDNCode as DNCode, a.Sequence , h.ID as JYID, c.InvCode , c.InvName , c.INVSTD , c.ClassName,
                                f.Colspan	,f.ProjectCode	,f.BatchCode	,f.Version	,f.Brand	,f.cFree1	,f.cFree2	,f.cFree3	,f.cFree4	,f.cFree5	,f.cFree6	,f.cFree7	,f.cFree8	,f.cFree9	,f.cFree10, e.Quantity as AllNumber, 
                                CAST(ISNULL(h.QualifiedQuantity,e.Quantity)as NUMERIC(10,4))as YLOTQTY ,
                                CAST(ISNULL(h.UnqualifiedQuantity, 0) as NUMERIC(10,4)) as  NLOTQTY,
                                CAST(ISNULL(h.WaiveQuantity, 0) as NUMERIC(10,4)) as SpecialQTY,
                                CAST(ISNULL(h.SampleQuantity, 0) as NUMERIC(10,4)) as SampleQuantity,
                                c.InvUnit , e.LotNo , g.BadReasonDesc as BadReasonDesc ,
								g.BadReasonCode as BRCodeValue,j.BadCode as BCCodeValue, j.BadDesc as BadDesc, i.ContainerID,
                                isnull(h.MUSERName,'开发者') MUSERName, CONVERT(VARCHAR(100),e.ProductDate,23) as ProductTime,CONVERT(VARCHAR(100),h.MTIME,23) as MTIME,
                                CASE WHEN isnull(h.ID,'') =''THEN '未检验' ELSE '已检验' END AS TestState,
                                isnull(h.MUSER,'') as Surveyor, h.MUSERName as ProvingTime
                                 ,CASE WHEN h.Result='0' THEN '不合格' when ISNULL(h.Result,'')='' THEN ''   ELSE '合格' END  AS Result,
                                n.FileName,a.EATTRIBUTE1,a.EATTRIBUTE2,a.EATTRIBUTE3,a.EATTRIBUTE4,a.EATTRIBUTE5,a.EATTRIBUTE6,a.EATTRIBUTE7,a.EATTRIBUTE8,
a.EATTRIBUTE9,a.EATTRIBUTE10

                            from ICSSDN a
                            inner JOIN dbo.ICSInventory c ON a.InvCode = c.InvCode AND a.WorkPoint=c.WorkPoint and c.InvIQC='1'
							left join ICSInventoryLotDetail b on a.SDNCode=b.TransCode and a.Sequence=b.TransSequence and a.WorkPoint=b.WorkPoint
							left join ICSExtension f on a.ExtensionID=f.ID and a.WorkPoint=f.WorkPoint                          
                            LEFT JOIN dbo.ICSInventoryLot e ON e.LotNo=b.LotNo  AND e.WorkPoint=b.WorkPoint
                            LEFT JOIN dbo.ICSInspection h ON h.LotNO=e.LotNO and h.InvCode=e.InvCode and h.WorkPoint=e.WorkPoint  and Enable='1'
							left join ICSBadReason g on h.BRCode =g.BadReasonCode and h.WorkPoint=g.WorkPoint
                            left join ICSBadCode j on h.BCCode =j.BadCode and h.WorkPoint=j.WorkPoint
                            LEFT JOIN dbo.ICSContainerLot i ON e.LotNO=i.LotNO AND e.WorkPoint=i.WorkPoint
                            left join ICSInspectionFile n on h.id=n.InspectionID and h.WorkPoint=n.WorkPoint
                            where e.LotNO is not null AND a.Type='2' {0}  ";
            string wheresql = "";
            if (!string.IsNullOrWhiteSpace(queryJson))
            {
                if (!string.IsNullOrWhiteSpace(queryParam["POCode"].ToString()))
                {
                    wheresql += " and a.SDNCode like '%" + queryParam["POCode"].ToString() + "%' ";
                }
                if (!string.IsNullOrWhiteSpace(queryParam["InvCode"].ToString()))
                {
                    wheresql += " and c.InvCode like '%" + queryParam["InvCode"].ToString() + "%' ";
                }
                if (!string.IsNullOrWhiteSpace(queryParam["InvName"].ToString()))
                {
                    wheresql += " and c.InvName like '%" + queryParam["InvName"].ToString() + "%' ";
                }
                if (!string.IsNullOrWhiteSpace(queryParam["BatchCode"].ToString()))
                {
                    wheresql += " and f.BatchCode like '%" + queryParam["BatchCode"].ToString() + "%' ";
                }

                if (!string.IsNullOrWhiteSpace(queryParam["ReleaseState"].ToString()))
                {
                    string ReleaseState = queryParam["ReleaseState"].ToString();
                    if (ReleaseState == "1")
                        wheresql += " and CASE WHEN isnull(h.ID,'') =''THEN '未检验' ELSE '已检验' END='已检验'";
                    else if (ReleaseState == "0")
                        wheresql += " and CASE WHEN isnull(h.ID,'') =''THEN '未检验' ELSE '已检验' END='未检验'";
                    else
                        wheresql += " ";
                }
                if (!string.IsNullOrWhiteSpace(queryParam["selEnableShow"].ToString()))
                {
                    string selEnableShow = queryParam["selEnableShow"].ToString();
                    if (selEnableShow == "1")
                        wheresql += " and ISNULL(h.UnqualifiedQuantity, 0) <= 0";
                    else if (selEnableShow == "0")
                        wheresql += " and ISNULL(h.UnqualifiedQuantity, 0) > 0";
                }
            }
            sql = string.Format(sql, wheresql);
            return Repository().FindTablePageBySql(sql.ToString(), parameter.ToArray(), ref jqgridparam);
        }



        //获取不良原因
        public DataTable Select_ICSBadReason(string BCCode)
        {
            string sql = string.Empty;

            DataTable dt = null;
            sql = @"select '' as BadReasonCode,'' as BadReasonDesc from ICSBadReasonGroup a inner join ICSBadReason b on a.ID=b.BRGroupID
				union
				select  BadReasonCode, BadReasonDesc from ICSBadReasonGroup a inner join ICSBadReason b on a.ID=b.BRGroupID
				where 1=1 ";

            dt = SqlHelper.GetDataTableBySql(sql);
            return dt;
        }

        public DataTable Select_ChangeA(string BCCode)
        {
            string sql = string.Empty;

            DataTable dt = null;
            sql = @"select '' as BadReasonCode,'' as BadReasonDesc from ICSBadReasonGroup a inner join ICSBadReason b on a.ID=b.BRGroupID
				union
				select  BadReasonCode, BadReasonDesc from ICSBadReasonGroup a inner join ICSBadReason b on a.ID=b.BRGroupID
				where 1=1 ";
            if (!string.IsNullOrWhiteSpace(BCCode))
            {
                sql += " and a.BRGCode= '" + BCCode + "' ";
            }
            dt = SqlHelper.GetDataTableBySql(sql);
            return dt;
        }

        //获取不良代码组
        public DataTable GetSelectICSBadCode(string BRCode)
        {
            string sql = string.Empty;

            DataTable dt = null;

            sql = @"select '' as BRGCode,'' as BRGDesc from ICSBadReasonGroup a inner join ICSBadReason b on a.ID=b.BRGroupID
				union
				select  BRGCode, BRGDesc from ICSBadReasonGroup a inner join ICSBadReason b on a.ID=b.BRGroupID
                where 1=1 ";


            dt = SqlHelper.GetDataTableBySql(sql);
            return dt;
        }


        public DataTable Select_ChangeB(string BRCode)
        {
            string sql = string.Empty;

            DataTable dt = null;

            sql = @"
				select  BRGCode, BRGDesc from ICSBadReasonGroup a inner join ICSBadReason b on a.ID=b.BRGroupID
                where 1=1 ";

            if (!string.IsNullOrWhiteSpace(BRCode))
            {
                sql += " and b.BadReasonCode= '" + BRCode + "' ";
            }
            dt = SqlHelper.GetDataTableBySql(sql);
            return dt;
        }

        /// <summary>
        /// 新增检验
        /// </summary>
        /// <param name="queryJson"></param>
        /// <returns></returns>
        public string CreateICSInspection(string keyValue, string ICSInspections)
        {
            string MUSER = NFine.Code.OperatorProvider.Provider.GetCurrent().UserCode;
            string MUSERNAME = NFine.Code.OperatorProvider.Provider.GetCurrent().UserName;
            string WorkPoint = NFine.Code.OperatorProvider.Provider.GetCurrent().Location;
            string msg = "";
            string sql = string.Empty;
            JArray res = (JArray)JsonConvert.DeserializeObject(ICSInspections);
            foreach (var item in res)
            {
                JObject jo = (JObject)item;
                string sqls = string.Empty;
                sqls = @"update ICSInspection set Enable='0' WHERE LotNO='{0}' and Enable='1' and WorkPoint='{1}'";
                sqls = string.Format(sqls, jo["LotNo"].ToString(), WorkPoint);
                SqlHelper.CmdExecuteNonQueryLi(sqls);
                //if (jo["TestState"].ToString()=="已检验")
                //{
                //    sql += @"UPDATE dbo.ICSInspection set QualifiedQuantity='{0}',UnqualifiedQuantity='{1}',WaiveQuantity='{2}',BCCode='{3}',BRCode='{4}',MTIME=GETDATE(),MUSER='{5}',MUSERName='{6}',WorkPoint='{7}' WHERE ID='{8}'";
                //    sql = string.Format(sql, jo["YLOTQTY"].ToString(), jo["NLOTQTY"].ToString(), jo["SpecialQTY"].ToString(), jo["BCCode"].ToString(), jo["BRCode"].ToString(), MUSER, MUSERNAME, WorkPoint, jo["JYID"].ToString());
                //}
                //else
                //{
                sql += @"
                                      --  IF  EXISTS(select LotNo from dbo.ICSWareHouseLotInfo where LotNo='{0}' and WorkPoint='{11}')
                                                                      --  BEGIN
	                                                                      --  RAISERROR('条码:{0}已入库!',16,1);
                                                                        --    RETURN
                                                                     --   end
                                        INSERT INTO dbo.ICSInspection
                                            ( ID ,LotNo,InvCode ,Quantity,QualifiedQuantity ,UnqualifiedQuantity,WaiveQuantity,BCCode,BRCode,Type,MTIME,MUSER ,MUSERName ,WorkPoint ,Enable,SampleQuantity) 
                											Values(NEWID(),'{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}',getdate(),'{9}','{10}','{11}','1','{12}')
                ";
                sql = string.Format(sql, jo["LotNo"].ToString(), jo["InvCode"].ToString(), jo["AllNumber"].ToString(), jo["YLOTQTY"].ToString(), jo["NLOTQTY"].ToString(), jo["SpecialQTY"].ToString(), jo["BCCode"].ToString(), jo["BRCode"].ToString(), jo["Type"].ToString(), MUSER, MUSERNAME, WorkPoint, jo["SampleQuantity"].ToString());
            }
            //}

            try
            {
                if (SqlHelper.ExecuteNonQuery(sql) > 0)
                {

                }
                else
                {
                    msg = "新增失败";
                }
            }
            catch (Exception ex)
            {
                msg=ex.Message;
            }

            return msg;
        }




        //生成采购拒收单
        public string CreateRejection(string keyValue)
        {
            string msg = "";
            string WorkPoint = NFine.Code.OperatorProvider.Provider.GetCurrent().Location;

            string sqlAsn = @"select distinct a.VenCode,a.DNID,a.DepCode,a.DNCode from ICSDeliveryNotice a
            		inner join ICSASNDetail b on a.ASNCode=b.ASNCode and a.WorkPoint=b.WorkPoint
            		left join dbo.ICSInventoryLot c on b.LotNo=c.LotNo and a.InvCode=c.InvCode and b.WorkPoint=c.WorkPoint
            		left join ICSInventoryLotDetail e on c.LotNo=e.LotNo and c.WorkPoint=e.WorkPoint
            		inner JOIN ICSPurchaseOrder l ON e.TransCode =l.POCode AND e.TransSequence=l.Sequence AND a.PODetailID=l.PODetailID AND c.WorkPoint=l.WorkPoint
            		WHERE b.LotNo  IN (" + keyValue.TrimEnd(',') + ")";
            //          string sqlAsn = @"select distinct a.VenCode,a.DNID,a.DepCode,a.DNCode from ICSDeliveryNotice a
            //left join ICSInventoryLotDetail e on a.DNCode=e.TransCode and a.Sequence=e.TransSequence and a.WorkPoint=e.WorkPoint
            //left join dbo.ICSInventoryLot c on e.LotNo=c.LotNo and a.InvCode=c.InvCode and a.WorkPoint=c.WorkPoint
            //inner JOIN ICSPurchaseOrder l ON  a.PODetailID=l.PODetailID AND c.WorkPoint=l.WorkPoint
            //WHERE c.LotNo  IN (" + keyValue.TrimEnd(',') + ")";
            DataTable dt = Repository().FindTableBySql(sqlAsn.ToString());

            string sqlAsnD = @"select distinct c.InvCode,d.UnqualifiedQuantity-d.WaiveQuantity as Quantity,isnull(c.Amount,0) as Amount,a.Currency,isnull(a.UnitPrice,0) as UnitPrice,DNDetailID,DNID,b.LotNo
            								from ICSDeliveryNotice a 
            								left join ICSASNDetail b on a.ASNCode=b.ASNCode and a.WorkPoint=b.WorkPoint
            								left join ICSInventoryLot c on b.LotNo=c.LotNo  and a.InvCode=c.InvCode and b.WorkPoint=c.WorkPoint
            								left join ICSInventoryLotDetail e on c.LotNo=e.LotNo and c.WorkPoint=e.WorkPoint
            		                        inner JOIN ICSPurchaseOrder l ON e.TransCode =l.POCode AND e.TransSequence=l.Sequence AND a.PODetailID=l.PODetailID AND c.WorkPoint=l.WorkPoint
            								left join ICSInspection d on b.LotNo=d.LotNo and b.WorkPoint=d.WorkPoint
            								WHERE b.LotNo in  (" + keyValue.TrimEnd(',') + ")   ";
        //    string sqlAsnD = @"select distinct c.InvCode,d.UnqualifiedQuantity-d.WaiveQuantity as Quantity,isnull(c.Amount,0) as Amount,a.Currency,isnull(a.UnitPrice,0) as UnitPrice,DNDetailID,DNID,d.LotNo
								//from ICSDeliveryNotice a 
								//left join ICSInventoryLotDetail e on a.DNCode=e.TransCode and a.Sequence=e.TransSequence and a.WorkPoint=e.WorkPoint
								//left join ICSInventoryLot c on e.LotNo=c.LotNo  and a.InvCode=c.InvCode and a.WorkPoint=c.WorkPoint
		      //                  inner JOIN ICSPurchaseOrder l ON  a.PODetailID=l.PODetailID AND c.WorkPoint=l.WorkPoint
								//left join ICSInspection d on c.LotNo=d.LotNo and a.WorkPoint=d.WorkPoint and d.Enable='1'
								//WHERE d.LotNo in  (" + keyValue.TrimEnd(',') + ")   ";
            DataTable dtD = Repository().FindTableBySql(sqlAsnD.ToString());

            List<RejectionHead> asn = new List<RejectionHead>();

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                RejectionHead ass = new RejectionHead();
                ass.VenCode = dt.Rows[i]["VenCode"].ToString();
                ass.ID = dt.Rows[i]["DNID"].ToString();
                ass.DepCode = dt.Rows[i]["DepCode"].ToString();
                ass.DNCode = dt.Rows[i]["DNCode"].ToString();
                ass.User = NFine.Code.OperatorProvider.Provider.GetCurrent().UserCode;
                ass.MTIME = System.DateTime.Now.ToString("s");
                ass.WorkPoint = WorkPoint;
                DataRow[] drs = dtD.Select("DNID='" + dt.Rows[i]["DNID"].ToString() + "'");

                for (int j = 0; j < drs.Length; j++)
                {
                    RejectionBody DetailList = new RejectionBody();
                    DetailList.Sequence = (j + 1).ToString();
                    DetailList.InvCode = drs[j]["InvCode"].ToString();
                    DetailList.Quantity = drs[j]["Quantity"].ToString();
                    DetailList.Amount = drs[j]["Amount"].ToString();
                    DetailList.Currency = drs[j]["Currency"].ToString();
                    DetailList.UnitPrice = drs[j]["UnitPrice"].ToString();
                    DetailList.DNDetailID = drs[j]["DNDetailID"].ToString();
                    DetailList.LotNo = drs[j]["LotNo"].ToString();

                    ass.detail.Add(DetailList);
                }
                asn.Add(ass);
            }
            string input = JsonConvert.SerializeObject(asn);
            string APIURL = ConfigurationManager.ConnectionStrings["APIURL"].ConnectionString + "PurchaseRejectDoc/Create";
            string result = HttpPost(APIURL, input);
            JObject Obj = (JObject)JsonConvert.DeserializeObject(result);//或者JObject jo = JObject.Parse(jsonText);
            string MessAge = Obj["Message"].ToString();
            string Success = Obj["Success"].ToString();
            if (Success.ToUpper() == "FALSE")
            {
                msg = MessAge;
            }
            return msg;
        }



        //生成采购拒收单(无来源)
        public string CreateWLYRejection(string keyValue)
        {
            string msg = "";
            string WorkPoint = NFine.Code.OperatorProvider.Provider.GetCurrent().Location;

            //string sqlAsn = @"select distinct a.VenCode,a.DNID,a.DepCode,a.DNCode from ICSDeliveryNotice a
            //		inner join ICSASNDetail b on a.ASNCode=b.ASNCode and a.WorkPoint=b.WorkPoint
            //		left join dbo.ICSInventoryLot c on b.LotNo=c.LotNo and a.InvCode=c.InvCode and b.WorkPoint=c.WorkPoint
            //		left join ICSInventoryLotDetail e on c.LotNo=e.LotNo and c.WorkPoint=e.WorkPoint
            //		inner JOIN ICSPurchaseOrder l ON e.TransCode =l.POCode AND e.TransSequence=l.Sequence AND a.PODetailID=l.PODetailID AND c.WorkPoint=l.WorkPoint
            //		WHERE b.LotNo  IN (" + keyValue.TrimEnd(',') + ")";
            string sqlAsn = @"select distinct a.VenCode,a.DNID,a.DepCode,a.DNCode from ICSDeliveryNotice a
            left join ICSInventoryLotDetail e on a.DNCode=e.TransCode and a.Sequence=e.TransSequence and a.WorkPoint=e.WorkPoint
            left join dbo.ICSInventoryLot c on e.LotNo=c.LotNo and a.InvCode=c.InvCode and a.WorkPoint=c.WorkPoint
           -- left JOIN ICSPurchaseOrder l ON  a.PODetailID=l.PODetailID AND c.WorkPoint=l.WorkPoint
            WHERE c.LotNo  IN (" + keyValue.TrimEnd(',') + ")";
            DataTable dt = Repository().FindTableBySql(sqlAsn.ToString());

            //string sqlAsnD = @"select distinct c.InvCode,d.UnqualifiedQuantity-d.WaiveQuantity as Quantity,isnull(c.Amount,0) as Amount,a.Currency,isnull(a.UnitPrice,0) as UnitPrice,DNDetailID,DNID,b.LotNo
            //								from ICSDeliveryNotice a 
            //								left join ICSASNDetail b on a.ASNCode=b.ASNCode and a.WorkPoint=b.WorkPoint
            //								left join ICSInventoryLot c on b.LotNo=c.LotNo  and a.InvCode=c.InvCode and b.WorkPoint=c.WorkPoint
            //								left join ICSInventoryLotDetail e on c.LotNo=e.LotNo and c.WorkPoint=e.WorkPoint
            //		                        inner JOIN ICSPurchaseOrder l ON e.TransCode =l.POCode AND e.TransSequence=l.Sequence AND a.PODetailID=l.PODetailID AND c.WorkPoint=l.WorkPoint
            //								left join ICSInspection d on b.LotNo=d.LotNo and b.WorkPoint=d.WorkPoint
            //								WHERE b.LotNo in  (" + keyValue.TrimEnd(',') + ")   ";
            string sqlAsnD = @"select distinct c.InvCode,d.UnqualifiedQuantity-d.WaiveQuantity as Quantity,isnull(c.Amount,0) as Amount,a.Currency,isnull(a.UnitPrice,0) as UnitPrice,DNDetailID,DNID,d.LotNo
            from ICSDeliveryNotice a 
            left join ICSInventoryLotDetail e on a.DNCode=e.TransCode and a.Sequence=e.TransSequence and a.WorkPoint=e.WorkPoint
            left join ICSInventoryLot c on e.LotNo=c.LotNo  and a.InvCode=c.InvCode and a.WorkPoint=c.WorkPoint
                              inner JOIN ICSPurchaseOrder l ON  a.PODetailID=l.PODetailID AND c.WorkPoint=l.WorkPoint
            left join ICSInspection d on c.LotNo=d.LotNo and a.WorkPoint=d.WorkPoint and d.Enable='1'
            WHERE d.LotNo in  (" + keyValue.TrimEnd(',') + ")   ";
            DataTable dtD = Repository().FindTableBySql(sqlAsnD.ToString());

            List<RejectionHead> asn = new List<RejectionHead>();

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                RejectionHead ass = new RejectionHead();
                ass.VenCode = dt.Rows[i]["VenCode"].ToString();
                ass.ID = dt.Rows[i]["DNID"].ToString();
                ass.DepCode = dt.Rows[i]["DepCode"].ToString();
                ass.DNCode = dt.Rows[i]["DNCode"].ToString();
                ass.User = NFine.Code.OperatorProvider.Provider.GetCurrent().UserCode;
                ass.MTIME = System.DateTime.Now.ToString("s");
                ass.WorkPoint = WorkPoint;
                DataRow[] drs = dtD.Select("DNID='" + dt.Rows[i]["DNID"].ToString() + "'");

                for (int j = 0; j < drs.Length; j++)
                {
                    RejectionBody DetailList = new RejectionBody();
                    DetailList.Sequence = (j + 1).ToString();
                    DetailList.InvCode = drs[j]["InvCode"].ToString();
                    DetailList.Quantity = drs[j]["Quantity"].ToString();
                    DetailList.Amount = drs[j]["Amount"].ToString();
                    DetailList.Currency = drs[j]["Currency"].ToString();
                    DetailList.UnitPrice = drs[j]["UnitPrice"].ToString();
                    DetailList.DNDetailID = drs[j]["DNDetailID"].ToString();
                    DetailList.LotNo = drs[j]["LotNo"].ToString();

                    ass.detail.Add(DetailList);
                }
                asn.Add(ass);
            }
            string input = JsonConvert.SerializeObject(asn);
            string APIURL = ConfigurationManager.ConnectionStrings["APIURL"].ConnectionString + "PurchaseRejectDocNoTrans/Create";
            string result = HttpPost(APIURL, input);
            JObject Obj = (JObject)JsonConvert.DeserializeObject(result);//或者JObject jo = JObject.Parse(jsonText);
            string MessAge = Obj["Message"].ToString();
            string Success = Obj["Success"].ToString();
            if (Success.ToUpper() == "FALSE")
            {
                msg = MessAge;
            }
            return msg;
        }






        //生成委外拒收单
        public string CreateWWRejection(string keyValue)
        {
            string msg = "";
            string WorkPoint = NFine.Code.OperatorProvider.Provider.GetCurrent().Location;

            string sqlAsn = @"select distinct a.VenCode,a.ODNID,a.DepCode,a.ODNCode from ICSODeliveryNotice a
		left join ICSOASNDetail b on a.OASNCode=b.OASNCode and a.WorkPoint=b.WorkPoint
		 LEFT JOIN ICSInventory c ON a.InvCode=c.InvCode and a.WorkPoint=c.WorkPoint
         inner JOIN ICSInventoryLot d ON b.LotNo=d.LotNo AND a.InvCode=d.InvCode AND b.WorkPoint=d.WorkPoint
		left join ICSInventoryLotDetail e on d.LotNo=e.LotNo and d.WorkPoint=e.WorkPoint
		  inner JOIN ICSOutsourcingOrder l ON e.TransCode =l.OOCode AND e.TransSequence=l.Sequence  AND a.OODetailID=l.OODetailID AND d.WorkPoint=l.WorkPoint
		 WHERE b.LotNo in (" + keyValue.TrimEnd(',') + ")";
            DataTable dt = Repository().FindTableBySql(sqlAsn.ToString());

            string sqlAsnD = @"select distinct c.InvCode,d.UnqualifiedQuantity-d.WaiveQuantity as Quantity,isnull(c.Amount,0) as Amount,a.Currency,isnull(a.UnitPrice,0) as UnitPrice,ODNDetailID,ODNID,b.LotNo
								from ICSODeliveryNotice a 
								inner join ICSOASNDetail b on a.OASNCode=b.OASNCode and a.WorkPoint=b.WorkPoint
								inner join ICSInventoryLot c on b.LotNo=c.LotNo  and a.InvCode=c.InvCode and b.WorkPoint=c.WorkPoint
								left join ICSInventoryLotDetail e on c.LotNo=e.LotNo and c.WorkPoint=e.WorkPoint
								 inner JOIN ICSOutsourcingOrder l ON e.TransCode =l.OOCode AND e.TransSequence=l.Sequence  AND a.OODetailID=l.OODetailID AND c.WorkPoint=l.WorkPoint
								inner join ICSInspection d on c.LotNo=d.LotNo and b.WorkPoint=d.WorkPoint and d.Enable='1'
								WHERE b.LotNo IN  (" + keyValue.TrimEnd(',') + ")   ";
            DataTable dtD = Repository().FindTableBySql(sqlAsnD.ToString());

            List<WWRejectionHead> asn = new List<WWRejectionHead>();

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                WWRejectionHead ass = new WWRejectionHead();
                ass.VenCode = dt.Rows[i]["VenCode"].ToString();
                ass.ID = dt.Rows[i]["ODNID"].ToString();
                ass.DepCode = dt.Rows[i]["DepCode"].ToString();
                ass.ODNCode = dt.Rows[i]["ODNCode"].ToString();
                ass.User = NFine.Code.OperatorProvider.Provider.GetCurrent().UserCode;
                ass.MTIME = System.DateTime.Now.ToString("s");
                ass.WorkPoint = WorkPoint;

                DataRow[] drs = dtD.Select("ODNID='" + dt.Rows[i]["ODNID"].ToString() + "'");

                for (int j = 0; j < drs.Length; j++)
                {
                    WWRejectionBody DetailList = new WWRejectionBody();
                    DetailList.Sequence = (j + 1).ToString();
                    DetailList.InvCode = drs[j]["InvCode"].ToString();
                    DetailList.Quantity = drs[j]["Quantity"].ToString();
                    DetailList.Amount = drs[j]["Amount"].ToString();
                    DetailList.Currency = drs[j]["Currency"].ToString();
                    DetailList.UnitPrice = drs[j]["UnitPrice"].ToString();
                    DetailList.ODNDetailID = drs[j]["ODNDetailID"].ToString();
                    DetailList.LotNo = drs[j]["LotNo"].ToString();

                    ass.detail.Add(DetailList);
                }
                asn.Add(ass);
            }
            string input = JsonConvert.SerializeObject(asn);
            string APIURL = ConfigurationManager.ConnectionStrings["APIURL"].ConnectionString + "OutsourcingRejectDoc/Create";
            string result = HttpPost(APIURL, input);
            JObject Obj = (JObject)JsonConvert.DeserializeObject(result);//或者JObject jo = JObject.Parse(jsonText);
            string MessAge = Obj["Message"].ToString();
            string Success = Obj["Success"].ToString();
            if (Success.ToUpper() == "FALSE")
            {
                msg = MessAge;
            }
            return msg;
        }

        //生成委外拒收单(无来源)
        public string CreateWWWLYRejection(string keyValue)
        {
            string msg = "";
            string WorkPoint = NFine.Code.OperatorProvider.Provider.GetCurrent().Location;

            string sqlAsn = @"select distinct a.VenCode,a.ODNID,a.DepCode,a.ODNCode 
from ICSODeliveryNotice a
			left join ICSInventoryLotDetail e on a.ODNCode=e.TransCode and a.Sequence=e.TransSequence and a.WorkPoint=e.WorkPoint
			 inner JOIN ICSInventoryLot d ON e.LotNo=d.LotNo AND a.InvCode=d.InvCode AND e.WorkPoint=d.WorkPoint
		      left JOIN ICSInventory c ON a.InvCode=c.InvCode and a.WorkPoint=c.WorkPoint
		 WHERE d.LotNo in (" + keyValue.TrimEnd(',') + ")";
            DataTable dt = Repository().FindTableBySql(sqlAsn.ToString());

            string sqlAsnD = @"select distinct c.InvCode,d.UnqualifiedQuantity-d.WaiveQuantity as Quantity,isnull(c.Amount,0) as Amount,a.Currency,isnull(a.UnitPrice,0) as UnitPrice,ODNDetailID,ODNID,c.LotNo
								from ICSODeliveryNotice a 
									left join ICSInventoryLotDetail e on a.ODNCode=e.TransCode and a.Sequence=e.TransSequence and a.WorkPoint=e.WorkPoint
								inner join ICSInventoryLot c on e.LotNo=c.LotNo  and a.InvCode=c.InvCode and e.WorkPoint=c.WorkPoint
								inner join ICSInspection d on c.LotNo=d.LotNo and a.WorkPoint=d.WorkPoint and d.Enable='1'
								WHERE c.LotNo IN  (" + keyValue.TrimEnd(',') + ")   ";
            DataTable dtD = Repository().FindTableBySql(sqlAsnD.ToString());

            List<WWRejectionHead> asn = new List<WWRejectionHead>();

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                WWRejectionHead ass = new WWRejectionHead();
                ass.VenCode = dt.Rows[i]["VenCode"].ToString();
                ass.ID = dt.Rows[i]["ODNID"].ToString();
                ass.DepCode = dt.Rows[i]["DepCode"].ToString();
                ass.ODNCode = dt.Rows[i]["ODNCode"].ToString();
                ass.User = NFine.Code.OperatorProvider.Provider.GetCurrent().UserCode;
                ass.MTIME = System.DateTime.Now.ToString("s");
                ass.WorkPoint = WorkPoint;

                DataRow[] drs = dtD.Select("ODNID='" + dt.Rows[i]["ODNID"].ToString() + "'");

                for (int j = 0; j < drs.Length; j++)
                {
                    WWRejectionBody DetailList = new WWRejectionBody();
                    DetailList.Sequence = (j + 1).ToString();
                    DetailList.InvCode = drs[j]["InvCode"].ToString();
                    DetailList.Quantity = drs[j]["Quantity"].ToString();
                    DetailList.Amount = drs[j]["Amount"].ToString();
                    DetailList.Currency = drs[j]["Currency"].ToString();
                    DetailList.UnitPrice = drs[j]["UnitPrice"].ToString();
                    DetailList.ODNDetailID = drs[j]["ODNDetailID"].ToString();
                    DetailList.LotNo = drs[j]["LotNo"].ToString();

                    ass.detail.Add(DetailList);
                }
                asn.Add(ass);
            }
            string input = JsonConvert.SerializeObject(asn);
            string APIURL = ConfigurationManager.ConnectionStrings["APIURL"].ConnectionString + "OutsourcingRejectDocNoTrans/Create";
            string result = HttpPost(APIURL, input);
            JObject Obj = (JObject)JsonConvert.DeserializeObject(result);//或者JObject jo = JObject.Parse(jsonText);
            string MessAge = Obj["Message"].ToString();
            string Success = Obj["Success"].ToString();
            if (Success.ToUpper() == "FALSE")
            {
                msg = MessAge;
            }
            return msg;
        }


        public static string HttpPost(string url, string body)
        {
            try
            {
                Encoding encoding = Encoding.UTF8;
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                request.Method = "POST";
                request.Accept = "application/json, text/javascript, */*"; //"text/html, application/xhtml+xml, */*";
                request.ContentType = "application/json; charset=utf-8";

                byte[] buffer = encoding.GetBytes(body);
                request.ContentLength = buffer.Length;
                request.GetRequestStream().Write(buffer, 0, buffer.Length);
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                using (StreamReader reader = new StreamReader(response.GetResponseStream(), encoding))
                {
                    return reader.ReadToEnd();
                }
            }
            catch (WebException ex)
            {
                throw new Exception(ex.Message);
            }
        }

        /// <summary>
        /// 删除拒收单
        /// </summary>
        /// <param name="keyValue">传入ID</param>
        /// <returns></returns>
        public string DelectRejection(string keyValue)
        {
            //站点信息
            string WorkPoint = NFine.Code.OperatorProvider.Provider.GetCurrent().Location;
            string msg = "";
            keyValue = keyValue.Substring(1, keyValue.Length - 2);
            string sql = string.Empty;
            sql = string.Format(@"DELETE FROM dbo.ICSDeliveryNotice  WHERE ID IN ({0}) and WorkPoint ='{1}'", keyValue.TrimEnd(','), WorkPoint);
            try
            {
                SqlHelper.ExecuteNonQuery(sql);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            return msg;
        }
        /// <summary>
        /// 删除委外拒收单
        /// </summary>
        /// <param name="keyValue">传入ID</param>
        /// <returns></returns>
        public string DelectWWRejection(string keyValue)
        {
            //站点信息
            string WorkPoint = NFine.Code.OperatorProvider.Provider.GetCurrent().Location;
            string msg = "";
            keyValue = keyValue.Substring(1, keyValue.Length - 2);
            string sql = string.Empty;
            sql = string.Format(@"DELETE FROM dbo.ICSODeliveryNotice  WHERE ID IN ({0}) and WorkPoint ='{1}'", keyValue.TrimEnd(','), WorkPoint);
            try
            {
                SqlHelper.ExecuteNonQuery(sql);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            return msg;
        }



        public DataTable GetInventoryInspection(ref Pagination jqgridparam, string InvCode, int SampleQuantity, string ResultINp)
        {
            string WorkPoint = NFine.Code.OperatorProvider.Provider.GetCurrent().Location;
            DataTable dt = new DataTable();
            List<DbParameter> parameter = new List<DbParameter>();
            string sql = string.Empty;
            if (ResultINp == "")
            {
                sql = "SELECT 1 AS NUM INTO #TEmpType";
                for (int i = 2; i <= SampleQuantity; i++)
                {
                    sql += @" UNION 
                            SELECT " + i + " AS NUM";
                }

                sql += @"                select c.ID,a.GroupCode,a.GroupName,c.InspectionID,b.ListCode, b.ListName,b.Unit,b.SetValueMax,b.SetValueMin,c.SetValue,CASE WHEN c.Result='0' THEN '不合格' when ISNULL(c.Result,'')='' THEN ''   ELSE '合格' END  AS Result
                                                        INTO #TEmpTypedetail from ICSInventoryInspectionGroup a 
                                                        left join ICSInventoryInspectionList b on a.ID=b.InvGroupID and a.WorkPoint=b.WorkPoint 
                                                        left join ICSInspectionDetail c on b.ListCode=c.ListCode and b.ListName=c.ListName and b.WorkPoint=c.WorkPoint
                                                        where a.InvCode='{0}' and b.Enable='1' and a.WorkPoint='{1}' 

                            SELECT a.NUM, NEWID() AS ID,b.GroupCode,b.GroupName,b.InspectionID,b.ListCode, b.ListName,b.Unit,b.SetValueMax,b.SetValueMin,b.SetValue,CASE WHEN b.Result='0' THEN '不合格' when ISNULL(b.Result,'')='' THEN ''   ELSE '合格' END  AS Result INTO ##SUMCount FROM #TEmpType a ,#TEmpTypedetail b  ORDER BY a.NUM,b.GroupCode ASC

                            ";
                sql = string.Format(sql, InvCode, WorkPoint);
                return SqlHelper.FindTablePageBySql_OtherTemp(sql.ToString(), " " + "##SUMCount" + " ", " DROP TABLE #TEmpType;DROP TABLE #TEmpTypedetail;DROP TABLE ##SUMCount", parameter.ToArray(), ref jqgridparam);
            }
            else
            {
                sql += @"   select c.EATTRIBUTE1 as NUM, c.ID,a.GroupCode,a.GroupName,c.InspectionID,b.ListCode, b.ListName,b.Unit,b.SetValueMax,b.SetValueMin,c.SetValue,CASE WHEN c.Result='0' THEN '不合格' when ISNULL(c.Result,'')='' THEN ''   ELSE '合格' END  AS Result
                                                        from ICSInventoryInspectionGroup a 
                                                        left join ICSInventoryInspectionList b on a.ID=b.InvGroupID and a.WorkPoint=b.WorkPoint 
                                                        left join ICSInspectionDetail c on b.ListCode=c.ListCode and b.ListName=c.ListName and b.WorkPoint=c.WorkPoint
                                                        where a.InvCode='{0}' and b.Enable='1' and a.WorkPoint='{1}' 
                                                        
                            ";
                sql = string.Format(sql, InvCode, WorkPoint);
                return Repository().FindTablePageBySql(sql.ToString(), parameter.ToArray(), ref jqgridparam);
            }
        }

        public string SaveICSInspectionDetail(string ICSMTDOC, string InvCode, string ResultINp)
        {
            string msg = "";
            try
            {
                string MUSER = NFine.Code.OperatorProvider.Provider.GetCurrent().UserCode;
                string MUSERNAME = NFine.Code.OperatorProvider.Provider.GetCurrent().UserName;
                string WorkPoint = NFine.Code.OperatorProvider.Provider.GetCurrent().Location;

                string sql = string.Empty;
                JArray res = (JArray)JsonConvert.DeserializeObject(ICSMTDOC);
                bool ISOK = false;
                string ID = string.Empty;
                foreach (var item in res)
                {
                    JObject jo = (JObject)item;
                    ID = jo["InspectionID"].ToString();
                    if (string.IsNullOrWhiteSpace(ResultINp))
                    {
                        sql += @"
                            INSERT INTO dbo.ICSInspectionDetail
                             ( ID ,InspectionID ,ListCode ,ListName ,
                               Unit ,SetValueMax ,SetValueMin ,SetValue ,Result, MUSER ,MUSERName ,MTIME, WorkPoint,GroupCode,GroupName,EATTRIBUTE1)
                    values(newid(),'{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}',getdate(),'{10}','{11}','{12}','{13}')";
                        sql = string.Format(sql, jo["InspectionID"].ToString(), jo["ListCode"].ToString(), jo["ListName"].ToString(), jo["Unit"].ToString(), jo["SetValueMax"].ToString(), jo["SetValueMin"].ToString(), jo["SetValue"].ToString(), jo["Result"].ToString(), MUSER, MUSERNAME, WorkPoint, jo["GroupCode"].ToString(), jo["GroupName"].ToString(), jo["NUM"].ToString());
                    }
                    else
                    {
                        sql += @" update ICSInspectionDetail set InspectionID='{0}',ListCode='{1}',ListName='{2}', Unit='{3}' ,SetValueMax='{4}' ,SetValueMin='{5}' ,SetValue='{6}' ,Result='{7}', MUSER='{8}' ,MUSERName='{9}' ,MTIME=getdate(), WorkPoint='{10}', GroupCode='{11}', GroupName='{12}',EATTRIBUTE1='{14}' where ID='{13}'";
                        sql = string.Format(sql, jo["InspectionID"].ToString(), jo["ListCode"].ToString(), jo["ListName"].ToString(), jo["Unit"].ToString(), jo["SetValueMax"].ToString(), jo["SetValueMin"].ToString(), jo["SetValue"].ToString(), jo["Result"].ToString(), MUSER, MUSERNAME, WorkPoint, jo["GroupCode"].ToString(), jo["GroupName"].ToString(), jo["ID"].ToString(), jo["NUM"].ToString());
                    }
                    if (jo["Result"].ToString() == "0")
                    {
                        ISOK = true;
                    }
                   
                }
                if (ISOK == true)
                {
                    sql += @" update ICSInspection set Result='{0}' where ID='{1}'";
                    sql = string.Format(sql, 0, ID);
                }
                else
                {
                    sql += @" update ICSInspection set Result='{0}' where ID='{1}'";
                    sql = string.Format(sql, 1, ID);
                }
                if (SqlHelper.CmdExecuteNonQueryLi(sql) > 0)
                {

                }
                else
                {
                    msg = "绑定失败";
                }
            }
            catch (Exception ex)
            {
                msg = ex.Message;
            }

            return msg;
        }
        /// <summary>
        /// 上传招标文件
        /// </summary>
        /// <param name="keyValue"></param>
        /// <returns></returns>
        public int InsertICSInspectionFile(string ID,string UploadfileName, string fileName)
        {
            DataTable dt = new DataTable();
            string WorkPoint = NFine.Code.OperatorProvider.Provider.GetCurrent().Location;
            string MUSER = NFine.Code.OperatorProvider.Provider.GetCurrent().UserCode;
            string MUSERName = NFine.Code.OperatorProvider.Provider.GetCurrent().UserName;
            string sql = "";
            sql += string.Format(@"Insert into  ICSInspectionFile(ID,InspectionID,FileCode,FileName,MUSER,MUSERName,MTIME,WorkPoint) values(NEWID(),'{0}','{1}','{2}','{3}','{4}',GetDate(),'{5}')", ID, UploadfileName, fileName, MUSER, MUSERName, WorkPoint);
            sql += "\r\n";
            StringBuilder Str = new StringBuilder(sql);
            return Repository().ExecuteBySql(Str);
        }


        public DataTable GetICSInspection8(ref Pagination jqgridparam, string queryJson)
        {
           //string ERPSign = Configs.GetValue("ERPSign");
            string WorkPoint = NFine.Code.OperatorProvider.Provider.GetCurrent().Location;
            DataTable dt = new DataTable();
            var queryParam = queryJson.ToJObject();
            List<DbParameter> parameter = new List<DbParameter>();
            string sql = string.Empty;

                sql = @"  select DISTINCT
                                a.ID as DHID,
                                d.ID ,
                                h.ID as JYID,
                                a.DNCode ,
                                a.Sequence ,
                                a.ASNCode ,
								--l.POCode,
                                c.InvCode ,
                                c.InvName ,
                                c.INVSTD ,
                                c.ClassName,
                                CAST(d.Quantity as decimal(18,4)) as AllNumber, 
                                CAST(ISNULL(h.QualifiedQuantity, d.Quantity)as decimal(18,4))as YLOTQTY ,
                                CAST(ISNULL(h.UnqualifiedQuantity, 0) as decimal(18,4)) as  NLOTQTY,
                                CAST(ISNULL(h.WaiveQuantity, 0) as decimal(18,4)) as SpecialQTY,
                                CAST(ISNULL(h.SampleQuantity, 0) as decimal(18,4)) as SampleQuantity,
                                c.InvUnit ,
                                d.LotNo ,
                                g.BadReasonDesc as BadReasonDesc ,
								g.BadReasonCode as BRCodeValue,
								j.BadCode as BCCodeValue,
                                j.BadDesc as BadDesc, 
                                k.ContainerID,
                                isnull(h.MUSERName,'开发者') MUSERName,
                                CONVERT(VARCHAR(100),d.ProductDate,23) as ProductTime, 
								 CONVERT(VARCHAR(100),a.CreateDateTime,23) as CreateDateTime,
								CONVERT(VARCHAR(100),h.MTIME,23) as MTIME,
                                CASE WHEN isnull(h.ID,'') =''THEN '未检验' ELSE '已检验' END AS TestState,
								a.VenCode,
								m.VenName,
                                isnull(h.MUSER,'') as Surveyor,
                                h.MUSERName as ProvingTime 
                                ,f.Colspan	,f.ProjectCode	,f.BatchCode	,f.Version	,f.Brand	,f.cFree1	,f.cFree2	,f.cFree3	,f.cFree4	,f.cFree5	,f.cFree6	,f.cFree7	,f.cFree8	,f.cFree9	,f.cFree10
                                 ,CASE WHEN h.Result='0' THEN '不合格' when ISNULL(h.Result,'')='' THEN ''   ELSE '合格' END  AS Result,
                                n.FileName
                                FROM ICSDeliveryNotice a
                                --inner JOIN ICSASNDetail b ON a.ASNCode=b.ASNCode  AND a.WorkPoint=b.WorkPoint
								left join ICSInventoryLotDetail e on a.DNCode=e.TransCode and a.Sequence=e.TransSequence and a.WorkPoint=e.WorkPoint 
                                inner JOIN ICSInventory c ON a.InvCode=c.InvCode and a.WorkPoint=c.WorkPoint and c.InvIQC='1'
								inner JOIN ICSInventoryLot d ON e.LotNo=d.LotNo  AND a.InvCode=d.InvCode AND a.WorkPoint=d.WorkPoint and d.type='7'
                                LEFT JOIN dbo.ICSInspection h ON h.LotNo=d.LotNo  AND h.WorkPoint=a.WorkPoint and Enable='1'
								left join ICSBadReason g on h.BRCode =g.BadReasonCode and h.WorkPoint=g.WorkPoint
                                left join ICSBadCode j on h.BCCode =j.BadCode and h.WorkPoint=j.WorkPoint
                                LEFT JOIN ICSContainerLot k ON d.LotNo=k.LotNo AND a.WorkPoint=k.WorkPoint
								left join ICSExtension f on a.ExtensionID=f.ID and a.WorkPoint=f.WorkPoint
								left join dbo.ICSVendor m on a.VenCode=m.VenCode and a.WorkPoint=m.WorkPoint
                                left join ICSInspectionFile n on h.id=n.InspectionID and h.WorkPoint=n.WorkPoint
                                where d.LotNo is not null  and a.DNType='1' {0} 
                
                 ";

        

         
            string wheresql = "";
            if (!string.IsNullOrWhiteSpace(queryJson))
            {
                if (!string.IsNullOrWhiteSpace(queryParam["POCode"].ToString()))
                {
                    wheresql += " and a.DNCode like '%" + queryParam["POCode"].ToString() + "%' ";
                }
                //if (!string.IsNullOrWhiteSpace(queryParam["ASNCode"].ToString()))
                //{
                //    wheresql += " and a.ASNCode like '%" + queryParam["ASNCode"].ToString() + "%' ";
                //}
                //if (!string.IsNullOrWhiteSpace(queryParam["CaiGouCode"].ToString()))
                //{
                //    wheresql += " and l.POCode like '%" + queryParam["CaiGouCode"].ToString() + "%' ";
                //}
                if (!string.IsNullOrWhiteSpace(queryParam["VenCode"].ToString()))
                {
                    wheresql += " and a.VenCode like '%" + queryParam["VenCode"].ToString() + "%' ";
                }
                if (!string.IsNullOrWhiteSpace(queryParam["VenName"].ToString()))
                {
                    wheresql += " and m.VenName like '%" + queryParam["VenName"].ToString() + "%' ";
                }
                if (!string.IsNullOrWhiteSpace(queryParam["InvCode"].ToString()))
                {
                    wheresql += " and c.InvCode like '%" + queryParam["InvCode"].ToString() + "%' ";
                }
                if (!string.IsNullOrWhiteSpace(queryParam["InvName"].ToString()))
                {
                    wheresql += " and c.InvName like '%" + queryParam["InvName"].ToString() + "%' ";
                }
                if (!string.IsNullOrWhiteSpace(queryParam["BatchCode"].ToString()))
                {
                    wheresql += " and f.BatchCode like '%" + queryParam["BatchCode"].ToString() + "%' ";
                }

                if (!string.IsNullOrWhiteSpace(queryParam["ReleaseState"].ToString()))
                {
                    string ReleaseState = queryParam["ReleaseState"].ToString();
                    if (ReleaseState == "1")
                        wheresql += " and CASE WHEN isnull(h.ID,'') =''THEN '未检验' ELSE '已检验' END='已检验'";
                    else if (ReleaseState == "0")
                        wheresql += " and CASE WHEN isnull(h.ID,'') =''THEN '未检验' ELSE '已检验' END='未检验'";
                    else
                        wheresql += " ";
                }
                if (!string.IsNullOrWhiteSpace(queryParam["selEnableShow"].ToString()))
                {
                    string selEnableShow = queryParam["selEnableShow"].ToString();
                    if (selEnableShow == "1")
                        wheresql += " and ISNULL(h.UnqualifiedQuantity, 0) <= 0";
                    else if (selEnableShow == "0")
                        wheresql += " and ISNULL(h.UnqualifiedQuantity, 0) > 0";
                }
            }
            sql = string.Format(sql, wheresql, WorkPoint);

            return Repository().FindTablePageBySql(sql.ToString(), parameter.ToArray(), ref jqgridparam);
        }



        public DataTable GetICSInspection9(ref Pagination jqgridparam, string queryJson)
        {
            DataTable dt = new DataTable();
            var queryParam = queryJson.ToJObject();
            List<DbParameter> parameter = new List<DbParameter>();

            #region 
          string  sql = @"
            select 	 
                                            a.ID as DHID,
                                            d.ID ,
                                            h.ID as JYID,
                                            a.ODNCode as DNCode ,
                                            a.Sequence ,
                                            a.OASNCode ,
                                            c.InvCode ,
                                            c.InvName ,
                                            c.INVSTD ,
                                            c.ClassName,
                                            f.BatchCode ,
                                            CAST(d.Quantity as NUMERIC(10,4)) as AllNumber, 
                                            CAST(ISNULL(h.QualifiedQuantity, d.Quantity)as NUMERIC(10,4))as YLOTQTY ,
                                            CAST(ISNULL(h.UnqualifiedQuantity, 0) as NUMERIC(10,4)) as  NLOTQTY,
                                            CAST(ISNULL(h.WaiveQuantity, 0) as NUMERIC(10,4)) as SpecialQTY,
                                          CAST(ISNULL(h.SampleQuantity, 0) as decimal(18,4)) as SampleQuantity,
                                            c.InvUnit ,
                                            d.LotNo ,
                                            g.BadReasonDesc as BadReasonDesc ,
            								g.BadReasonCode as BRCodeValue,
            								j.BadCode as BCCodeValue,
                                            j.BadDesc as BadDesc,
                                            k.ContainerID,
                                            isnull(h.MUSERName,'开发者') MUSERName,
                                            CONVERT(VARCHAR(100),d.ProductDate,23) as ProductTime,
            								CONVERT(VARCHAR(100),h.MTIME,23) as MTIME,
                                            CASE WHEN isnull(h.ID,'') =''THEN '未检验' ELSE '已检验' END AS TestState
                                            FROM ICSODeliveryNotice a
            								inner join ICSInventoryLotDetail e on e.TransCode =a.ODNCode AND e.TransSequence=a.Sequence AND  e.WorkPoint=a.WorkPoint
            								inner JOIN ICSInventoryLot d ON d.LotNo=e.LotNo and d.WorkPoint=e.WorkPoint  and d.type='8'
                                            inner JOIN ICSInventory c ON a.InvCode=c.InvCode and a.WorkPoint=c.WorkPoint and c.InvIQC='1'
                                            LEFT JOIN dbo.ICSInspection h ON h.LotNo=d.LotNo  AND h.WorkPoint=d.WorkPoint
            								left join ICSBadReason g on h.BRCode =g.BadReasonCode and h.WorkPoint=g.WorkPoint
                                            left join ICSBadCode j on h.BCCode =j.BadCode and h.WorkPoint=j.WorkPoint
                                            LEFT JOIN ICSContainerLot k ON d.LotNo=k.LotNo AND d.WorkPoint=k.WorkPoint
            								left join ICSExtension f on a.ExtensionID=f.ID and a.WorkPoint=f.WorkPoint
                                            where 1=1 {0}";
            #endregion
            string wheresql = "";
            if (!string.IsNullOrWhiteSpace(queryJson))
            {
                if (!string.IsNullOrWhiteSpace(queryParam["POCode"].ToString()))
                {
                    wheresql += " and a.ODNCode like '%" + queryParam["POCode"].ToString() + "%' ";
                }
                //if (!string.IsNullOrWhiteSpace(queryParam["ASNCode"].ToString()))
                //{
                //    wheresql += " and a.OASNCode like '%" + queryParam["ASNCode"].ToString() + "%' ";
                //}
                //if (!string.IsNullOrWhiteSpace(queryParam["CaiGouCode"].ToString()))
                //{
                //    wheresql += " and l.OOCode like '%" + queryParam["CaiGouCode"].ToString() + "%' ";
                //}
                if (!string.IsNullOrWhiteSpace(queryParam["VenCode"].ToString()))
                {
                    wheresql += " and a.VenCode like '%" + queryParam["VenCode"].ToString() + "%' ";
                }
                if (!string.IsNullOrWhiteSpace(queryParam["VenName"].ToString()))
                {
                    wheresql += " and m.VenName like '%" + queryParam["VenName"].ToString() + "%' ";
                }
                if (!string.IsNullOrWhiteSpace(queryParam["InvCode"].ToString()))
                {
                    wheresql += " and c.InvCode like '%" + queryParam["InvCode"].ToString() + "%' ";
                }
                if (!string.IsNullOrWhiteSpace(queryParam["InvName"].ToString()))
                {
                    wheresql += " and c.InvName like '%" + queryParam["InvName"].ToString() + "%' ";
                }
                if (!string.IsNullOrWhiteSpace(queryParam["BatchCode"].ToString()))
                {
                    wheresql += " and f.BatchCode like '%" + queryParam["BatchCode"].ToString() + "%' ";
                }

                if (!string.IsNullOrWhiteSpace(queryParam["ReleaseState"].ToString()))
                {
                    string ReleaseState = queryParam["ReleaseState"].ToString();
                    if (ReleaseState == "1")
                        wheresql += " and CASE WHEN isnull(h.ID,'') =''THEN '未检验' ELSE '已检验' END='已检验'";
                    else if (ReleaseState == "0")
                        wheresql += " and CASE WHEN isnull(h.ID,'') =''THEN '未检验' ELSE '已检验' END='未检验'";
                    else
                        wheresql += " ";
                }
                if (!string.IsNullOrWhiteSpace(queryParam["selEnableShow"].ToString()))
                {
                    string selEnableShow = queryParam["selEnableShow"].ToString();
                    if (selEnableShow == "1")
                        wheresql += " and ISNULL(h.UnqualifiedQuantity, 0) <= 0";
                    else if (selEnableShow == "0")
                        wheresql += " and ISNULL(h.UnqualifiedQuantity, 0) > 0";
                }
            }
            sql = string.Format(sql, wheresql);
            return Repository().FindTablePageBySql(sql.ToString(), parameter.ToArray(), ref jqgridparam);
        }
    }
}