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,string barCode)
        {
            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 + "%' ";
                }
                if (!string.IsNullOrEmpty(barCode))
                {
                    sql += " and barcode like '%" + barCode + @"%' ";
                }
                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>='" + StartTime + "' and createTime<='" + EndTime + "' and   REPLACE(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 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 sql = @" SELECT  COUNT(DISTINCT barcode) as num FROM dbo.tb_InspectResult WHERE barcode IS NOT NULL AND  createTime BETWEEN '"+ StartTime + "' AND '"+ EndTime + ".999' AND  REPLACE(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());
        }

        public DataTable Search(string StartTime, string EndTime, string position, string inspectResult,string barCode)
        {
            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 + "%' ";
                }
                if (!string.IsNullOrEmpty(barCode))
                {
                    sql += " and barcode like '%" + barCode + @"%'";
                }
                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<='" + EndTime + "' and createTime>='" + StartTime + "' 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 productInfo!='' and productInfo is not null and createTime>='" + StartTime + "' and createTime<='" + EndTime + "' and inspectResult in ('[合格]','[抛光]','[报废]') and substring(productInfo,1,1) != ',' ";
                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 * FROM[dbo].[tb_InspectResult] ";
                sql += " where barcode is not null and createTime>='" + StartTime + "' and createTime<='" + 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;
            }
        }
    }
}