using MESClassLibrary.BLL.Log;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace MESClassLibrary.DAL.Check
{
    public class InspectResultDAL
    {
        public DataTable SearchByPage(int pageIndex, int pageSize, string StartTime, string EndTime, string position, string inspectResult)
        {
            try
            {
                //string sql = @"select top " + pageSize + " *  from (select row_number() over(order by createTime desc) as rownumber,* from tb_InspectResult) temp_row ";
                //sql += " where createTime>='"+ StartTime + "' and createTime<='" + EndTime + "'";
                //if (!string.IsNullOrEmpty(position))
                //{
                //    sql += " and position = '" + position+"'";
                //}
                //if (!string.IsNullOrEmpty(inspectResult))
                //{
                //    sql += " and inspectResult like '%" + inspectResult + "%'";
                //}
                //sql += " and rownumber > " + ((pageIndex - 1) * pageSize);

                string sql = "select * FROM[dbo].[tb_InspectResult] where barcode in ";
                sql += "(select barcode from(select distinct barcode, createTime from [dbo].[tb_InspectResult] ";
                sql += " where barcode is not null and createTime>='" + StartTime + "' and createTime<='" + EndTime + "' ";
                if (!string.IsNullOrEmpty(position))
                {
                    sql += " and position = '" + position + "' ";
                }
                if (!string.IsNullOrEmpty(inspectResult))
                {
                    sql += " and inspectResult like '%" + inspectResult + "%' ";
                }
                sql += " order by createTime ";
                sql += " offset " + ((pageIndex - 1) * pageSize) + " rows ";
                sql += " fetch next " + pageSize + " rows only) temp_row) ";

                return SqlHelper.ExecuteDataset(SqlHelper.GetConnSting(), CommandType.Text, sql, null).Tables[0];

            }
            catch (Exception ex)
            {
                LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod());
                return null;
            }
        }

        public DataTable SearchRepairByPage(int pageIndex, int pageSize, string StartTime, string EndTime, string inspectResult)
        {
            try
            {
                string sql = @" SELECT * FROM dbo.tb_InspectResult ";

                //sql += "(select barcode from(select distinct barcode, createTime from [dbo].[tb_InspectResult] ";
                sql += " where barcode IS NOT NULL AND  createTime BETWEEN '" + StartTime + "' AND '" + EndTime + ".999' and  position='下线二检  点修补'";
                
                if (!string.IsNullOrEmpty(inspectResult))
                {
                    sql += " and inspectResult like '%" + inspectResult + "%' ";
                }
                sql += " order by createTime ";
                sql += " offset " + ((pageIndex - 1) * pageSize) + " rows ";
                sql += " fetch next " + pageSize + " rows only ";

                return SqlHelper.ExecuteDataset(SqlHelper.GetConnSting(), CommandType.Text, sql, null).Tables[0];

            }
            catch (Exception ex)
            {
                LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod());
                return null;
            }
        }

        public DataTable SearchRepairByPage_1(int pageIndex, int pageSize, string StartTime, string EndTime, string inspectResult,string Project,string color)
        {
            try
            {
                string sql = @" SELECT * 
                                FROM dbo.tb_Project RIGHT OUTER JOIN
                                dbo.tb_Product ON dbo.tb_Project.ID = dbo.tb_Product.ProjectID RIGHT OUTER JOIN
                                dbo.tb_InspectResult ON dbo.tb_Product.StockNo = SUBSTRING(dbo.tb_InspectResult.barcode, 1, 10)";

                //sql += "(select barcode from(select distinct barcode, createTime from [dbo].[tb_InspectResult] ";
                sql += " where barcode IS NOT NULL AND  createTime BETWEEN '" + StartTime + "' AND '" + EndTime + ".999' and  position='下线二检 点修补'";

                if (!string.IsNullOrEmpty(inspectResult))
                {
                    sql += " and inspectResult like '%" + inspectResult + "%' ";
                }

                if (!string.IsNullOrEmpty(Project))
                {
                    sql += " and Project like '%" + Project + "%' ";
                }

                if (!string.IsNullOrEmpty(color))
                {
                    sql += " and remark1 like '%" + color + "%' ";
                }

                sql += " order by createTime ";
                sql += " offset " + ((pageIndex - 1) * pageSize) + " rows ";
                sql += " fetch next " + pageSize + " rows only ";

                return SqlHelper.ExecuteDataset(SqlHelper.GetConnSting(), CommandType.Text, sql, null).Tables[0];

            }
            catch (Exception ex)
            {
                LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod());
                return null;
            }
        }



        public int SearchCount(string StartTime, string EndTime, string position, string inspectResult)
        {
            try
            {
                string sql = "select count(1) as num from (select barcode as num FROM[dbo].[tb_InspectResult] where barcode in ";
                sql += "(select distinct barcode from [dbo].[tb_InspectResult] ";
                sql += " where barcode is not null and createTime>='" + StartTime + "' and createTime<='" + EndTime + "' ";
                if (!string.IsNullOrEmpty(position))
                {
                    sql += " and position = '" + position + "' ";
                }
                if (!string.IsNullOrEmpty(inspectResult))
                {
                    sql += " and inspectResult like '%" + inspectResult + "%' ";
                }
                sql += " ) GROUP BY barcode) tem";
                //string sql = "select count(1) as num FROM[dbo].[tb_InspectResult] ";
                //sql += " where barcode is not null and createTime>='" + StartTime + "' and createTime<='" + EndTime + "' ";
                //if (!string.IsNullOrEmpty(position))
                //{
                //    sql += " and position = '" + position + "' ";
                //}
                //if (!string.IsNullOrEmpty(inspectResult))
                //{
                //    sql += " and inspectResult like '%" + inspectResult + "%' ";
                //}

                return Convert.ToInt32(SqlHelper.ExecuteDataset(SqlHelper.GetConnSting(), CommandType.Text, sql, null).Tables[0].Rows[0]["num"].ToString());

            }
            catch (Exception ex)
            {
                LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod());
                return 0;
            }
        }

        public int SearchCountByRepair(string StartTime, string EndTime,  string inspectResult,string Project,string color)
        {
            //string sql = @" SELECT COUNT(*) as num FROM dbo.tb_InspectResult ";
            string sql = @"SELECT COUNT(*) as num 
                            FROM dbo.tb_Project RIGHT OUTER JOIN
                            dbo.tb_Product ON dbo.tb_Project.ID = dbo.tb_Product.ProjectID RIGHT OUTER JOIN
                            dbo.tb_InspectResult ON dbo.tb_Product.StockNo = SUBSTRING(dbo.tb_InspectResult.barcode, 1, 10)";
            //sql += "(select barcode from(select distinct barcode, createTime from [dbo].[tb_InspectResult] ";
            sql += " where barcode IS NOT NULL AND  createTime BETWEEN '" + StartTime + "' AND '" + EndTime + ".999' and  position='下线二检  点修补'";

            //string sql = @" SELECT  COUNT(DISTINCT barcode) as num FROM dbo.tb_InspectResult WHERE barcode IS NOT NULL AND  createTime BETWEEN '"+ StartTime + "' AND '"+ EndTime + ".999' AND position='下线二检  点修补'";
            if (!string.IsNullOrEmpty(inspectResult))
            {
                sql += " and inspectResult like '%" + inspectResult + "%' ";
            }

            if (!string.IsNullOrEmpty(Project))
            {
                sql += " and Project like '%" + Project + "%' ";
            }

            if (!string.IsNullOrEmpty(color))
            {
                sql += " and remark1 like '%" + color + "%' ";
            }

            return Convert.ToInt32(SqlHelper.ExecuteDataset(SqlHelper.GetConnSting(), CommandType.Text, sql, null).Tables[0].Rows[0]["num"].ToString());
        }

        public DataTable Search(string StartTime, string EndTime, string position, string inspectResult)
        {
            try
            {

                string sql = "select * FROM[dbo].[tb_InspectResult] where barcode in ";
                sql += "(select distinct barcode from [dbo].[tb_InspectResult] ";
                sql += " where barcode is not null and createTime<='" + EndTime + "' and createTime>='" + StartTime + "' ";
                if (!string.IsNullOrEmpty(position))
                {
                    sql += " and position = '" + position + "' ";
                }
                if (!string.IsNullOrEmpty(inspectResult))
                {
                    sql += " and inspectResult like '%" + inspectResult + "%' ";
                }
                sql += " ) order by createTime";

                return SqlHelper.ExecuteDataset(SqlHelper.GetConnSting(), CommandType.Text, sql, null).Tables[0];

            }
            catch (Exception ex)
            {
                LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod());
                return null;
            }
        }

        public DataTable SearchByRepair(string StartTime, string EndTime, string inspectResult)
        {
            try
            {

                string sql = "select * FROM dbo.tb_InspectResult ";
                sql += " where barcode IS NOT NULL AND  createTime BETWEEN '" + StartTime + "' AND '" + EndTime + ".999' AND position='下线二检  点修补'";
                
                if (!string.IsNullOrEmpty(inspectResult))
                {
                    sql += " and inspectResult like '%" + inspectResult + "%' ";
                }
                sql += " order by createTime";

                return SqlHelper.ExecuteDataset(SqlHelper.GetConnSting(), CommandType.Text, sql, null).Tables[0];

            }
            catch (Exception ex)
            {
                LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod());
                return null;
            }
        }
        public DataTable SearchByPosition(string StartTime, string EndTime, string side, string product)
        {
            try
            {

                string sql = "select * FROM[dbo].[tb_InspectResult] ";
                sql += " where barcode is not null and createTime BETWEEN '" + StartTime + "' AND '" + EndTime + "'";
                if (!string.IsNullOrEmpty(side))
                {
                    if ("A侧".Equals(side))
                    {
                        sql += " and (side = '" + side + "' or side ='' or side is null) ";
                    }
                    if ("B侧".Equals(side))
                    {
                        sql += " and side = '" + side + "' ";
                    }

                }
                if (!string.IsNullOrEmpty(product))
                {
                    if ("保险杠".Equals(product))
                    {
                        sql += " and (product like '%前保%' or product like '%后保%')";
                    }
                }


                return SqlHelper.ExecuteDataset(SqlHelper.GetConnSting(), CommandType.Text, sql, null).Tables[0];

            }
            catch (Exception ex)
            {
                LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod());
                return null;
            }
        }

        public DataTable SearchByResult(string StartTime, string EndTime, string side)
        {
            try
            {

                string sql = "select ID,barcode,side,position,stationNo,workClass,inspectResult,damnPosition,defectID,reason,productInfo,productOption,createTime,InspectTimes,replace(remark1,' ','') as remark1,remark2,remark3  FROM[dbo].[tb_InspectResult] ";
                sql += " where barcode is not null and createTime BETWEEN '" + StartTime + "' and '" + EndTime + "' ";
                sql += " and remark1 is not null and remark2 is not null";
                if (!string.IsNullOrEmpty(side))
                {
                    if ("A侧".Equals(side))
                    {
                        sql += " and (side = '" + side + "' or side ='' or side is null) ";
                    }
                    if ("B侧".Equals(side))
                    {
                        sql += " and side = '" + side + "' ";
                    }

                }

                return SqlHelper.ExecuteDataset(SqlHelper.GetConnSting(), CommandType.Text, sql, null).Tables[0];

            }
            catch (Exception ex)
            {
                LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod());
                return null;
            }
        }

        public DataTable SearchByResult_1(string StartTime, string EndTime, string side,string Project,string color)
        {
            try
            {

                string sql = @"SELECT dbo.tb_InspectResult.ID, dbo.tb_InspectResult.barcode, dbo.tb_InspectResult.side, dbo.tb_InspectResult.position, 
                            dbo.tb_InspectResult.stationNo, dbo.tb_InspectResult.workClass, dbo.tb_InspectResult.inspectResult, 
                            dbo.tb_InspectResult.damnPosition, dbo.tb_InspectResult.defectID, dbo.tb_InspectResult.reason, 
                            dbo.tb_InspectResult.productInfo, dbo.tb_InspectResult.productOption, dbo.tb_InspectResult.createTime, 
                            dbo.tb_InspectResult.InspectTimes, REPLACE(dbo.tb_InspectResult.remark1, ' ', '') AS remark1, 
                            dbo.tb_InspectResult.remark2, dbo.tb_InspectResult.remark3, dbo.tb_Project.Project
                            FROM dbo.tb_Project RIGHT OUTER JOIN
                            dbo.tb_Product ON dbo.tb_Project.ID = dbo.tb_Product.ProjectID RIGHT OUTER JOIN
                            dbo.tb_InspectResult ON dbo.tb_Product.StockNo = SUBSTRING(dbo.tb_InspectResult.barcode, 1, 10) ";
                sql += " where barcode is not null and createTime BETWEEN '" + StartTime + "' and '" + EndTime + "' ";
                sql += " and remark1 is not null and remark2 is not null";
                if (!string.IsNullOrEmpty(side))
                {
                    if ("A侧".Equals(side))
                    {
                        sql += " and (side = '" + side + "' or side ='' or side is null) ";
                    }
                    if ("B侧".Equals(side))
                    {
                        sql += " and side = '" + side + "' ";
                    }
                }

                if (!string.IsNullOrEmpty(Project))
                {
                    sql += " and Project='" + Project + "'";
                }

                if (!string.IsNullOrEmpty(color))
                {
                    sql += " and remark1='" + color + "'";
                }

                return SqlHelper.ExecuteDataset(SqlHelper.GetConnSting(), CommandType.Text, sql, null).Tables[0];

            }
            catch (Exception ex)
            {
                LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod());
                return null;
            }
        }

        public DataTable SearchByRepair_1(string StartTime, string EndTime, string inspectResult,string Project,string color)
        {
            try
            {

                string sql = @"SELECT dbo.tb_InspectResult.ID,dbo.tb_InspectResult.barcode,dbo.tb_InspectResult.side,dbo.tb_InspectResult.position,dbo.tb_InspectResult.stationNo,dbo.tb_InspectResult.workClass,
                                dbo.tb_InspectResult.inspectResult,dbo.tb_InspectResult.damnPosition,dbo.tb_InspectResult.defectID,dbo.tb_InspectResult.reason,dbo.tb_InspectResult.productInfo,dbo.tb_InspectResult.productOption,
                                dbo.tb_InspectResult.createTime,dbo.tb_InspectResult.InspectTimes,dbo.tb_InspectResult.remark1,dbo.tb_InspectResult.remark2,dbo.tb_InspectResult.remark3 
                                FROM dbo.tb_Project RIGHT OUTER JOIN
                                dbo.tb_Product ON dbo.tb_Project.ID = dbo.tb_Product.ProjectID RIGHT OUTER JOIN
                                dbo.tb_InspectResult ON dbo.tb_Product.StockNo = SUBSTRING(dbo.tb_InspectResult.barcode, 1, 10) ";
                sql += " where barcode IS NOT NULL AND  createTime BETWEEN '" + StartTime + "' AND '" + EndTime + ".999' AND position='下线二检 点修补'";

                if (!string.IsNullOrEmpty(inspectResult))
                {
                    sql += " and inspectResult like '%" + inspectResult + "%' ";
                }

                if (!string.IsNullOrEmpty(Project))
                {
                    sql += " and Project like '%" + Project + "%' ";
                }

                if (!string.IsNullOrEmpty(color))
                {
                    sql += " and remark1 like '%" + color + "%' ";
                }
                sql += " order by createTime";

                return SqlHelper.ExecuteDataset(SqlHelper.GetConnSting(), CommandType.Text, sql, null).Tables[0];

            }
            catch (Exception ex)
            {
                LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod());
                return null;
            }
        }
    }
}