using DBUtility;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Web;
using Webservice;

namespace WebService
{
    public class FunctionAddColor
    {
        public static string GetProdunctName(string stockNo)
        {
            string res = "";
            try
            {
                string sql = @" select ProductName from tb_Product where StockNo = '" + stockNo + "' ";
                object aa = SqlHelper.ExecuteScalar(SqlHelper.SqlConnString, CommandType.Text, sql, null);
                if (aa != null)
                {
                    res = aa.ToString();
                }
                return res;
            }
            catch (Exception ex)
            {
                LogHelper.WriteLogManager(ex);
                LogHelper.WriteErrLogBase(ex.ToString(), MethodBase.GetCurrentMethod().Name);
                return res;
            }
        }

        public static DataTable GetAllColor()
        {
            DataTable res = new DataTable();
            try
            {
                string sql = @" SELECT CONVERT(NVARCHAR(50),[Des])+','+[ColorCode]+','+[ColorNo] AS ColorInfo
                                FROM tb_Color
                            ";
                res = SqlHelper.GetDataDateTable(SqlHelper.SqlConnString, CommandType.Text, sql, null);
                return res;
            }
            catch (Exception ex)
            {
                LogHelper.WriteLogManager(ex);
                LogHelper.WriteErrLogBase(ex.ToString(), MethodBase.GetCurrentMethod().Name);
                return res;
            }
        }

        public static int SaveBarcodeAndColor(string Barcode, string resultStr, out string errorReason)
        {
            int res = 0;
            try
            {
                string sql = @"
                                INSERT INTO [tb_StockInColor]
                                                   ([ID]
                                                   ,[Barcode]
                                                   ,[ColorInfo])
                                             VALUES
                                                   ((select newid())
                                                   ,'" + Barcode + @"'
                                                   ,'" + resultStr + @"');
                                update tb_InspectResult set productInfo = '" + resultStr + @"' where barcode = '" + Barcode + @"' and createTime >= cast(convert(varchar(10),DATEADD(DAY, -5, GETDATE()),120)+' 00:00:00' as datetime);
                            ";
                res = SqlHelper.ExecuteNonQuery(SqlHelper.SqlConnString, CommandType.Text, sql, null);
                errorReason = "";
                return res;
            }
            catch (Exception ex)
            {
                LogHelper.WriteLogManager(ex);
                LogHelper.WriteErrLogBase(ex.ToString(), MethodBase.GetCurrentMethod().Name);
                errorReason = ex.Message;
                return res;
            }
        }

        public static int InsertSkidInfo(string SkidNo, string BarcodeLeft, string BarcodeRight, string Layer, string ColorInfo, out string errorReason)
        {
            int res = 0;
            try
            {
                #region 分解BarcodeLeft、BarcodeRight保存

                List<string> listA = GetBarCodes(BarcodeLeft);
                List<string> listB = GetBarCodes(BarcodeRight);

                #endregion

                string sql = @"
                                INSERT INTO [dbo].[tb_SkidInfo]
                                           ([ID]
                                           ,[SkidNo]
                                           ,[BarcodeLeft]
                                           ,[BarcodeRight]
                                           ,[Layer]
                                           ,[ColorInfo]
                                           ,[Side_1_BC01]
                                           ,[Side_1_BC02]
                                           ,[Side_1_BC03]
                                           ,[Side_1_BC04]
                                           ,[Side_1_BC05]
                                           ,[Side_1_BC06]
                                           ,[Side_1_BC07]
                                           ,[Side_1_BC08]
                                           ,[Side_1_BC09]
                                           ,[Side_1_BC10]
                                           ,[Side_1_BC11]
                                           ,[Side_1_BC12]
                                           ,[Side_2_BC01]
                                           ,[Side_2_BC02]
                                           ,[Side_2_BC03]
                                           ,[Side_2_BC04]
                                           ,[Side_2_BC05]
                                           ,[Side_2_BC06]
                                           ,[Side_2_BC07]
                                           ,[Side_2_BC08]
                                           ,[Side_2_BC09]
                                           ,[Side_2_BC10]
                                           ,[Side_2_BC11]
                                           ,[Side_2_BC12]
                                            )
                                     VALUES
                                           ((SELECT NEWID())
                                           ,'" + SkidNo + @"'
                                           ,'" + BarcodeLeft + @"'
                                           ,'" + BarcodeRight + @"'
                                           ,'" + Layer + @"'
                                           ,'" + ColorInfo + @"'
                                            ,'" + listA[0] + @"'
                                            ,'" + listA[1] + @"'
                                            ,'" + listA[2] + @"'
                                            ,'" + listA[3] + @"'
                                            ,'" + listA[4] + @"'
                                            ,'" + listA[5] + @"'
                                            ,'" + listA[6] + @"'
                                            ,'" + listA[7] + @"'
                                            ,'" + listA[8] + @"'
                                            ,'" + listA[9] + @"'
                                            ,'" + listA[10] + @"'
                                            ,'" + listA[11] + @"'
                                            ,'" + listB[0] + @"'
                                            ,'" + listB[1] + @"'
                                            ,'" + listB[2] + @"'
                                            ,'" + listB[3] + @"'
                                            ,'" + listB[4] + @"'
                                            ,'" + listB[5] + @"'
                                            ,'" + listB[6] + @"'
                                            ,'" + listB[7] + @"'
                                            ,'" + listB[8] + @"'
                                            ,'" + listB[9] + @"'
                                            ,'" + listB[10] + @"'
                                            ,'" + listB[11] + @"'
                                            )
                            ";
                res = SqlHelper.ExecuteNonQuery(SqlHelper.SqlConnString, CommandType.Text, sql, null);
                errorReason = "";

                return res;
            }
            catch (Exception ex)
            {
                LogHelper.WriteLogManager(ex);
                LogHelper.WriteErrLogBase(ex.ToString(), MethodBase.GetCurrentMethod().Name);
                errorReason = ex.Message;
                return res;
            }
        }


        public static List<string> GetBarCodes(string codes)
        {
            List<string> res = new List<string>();
            try
            {
                string[] codesTmp = codes.Split(',');
                for (int i = 0; i < 12; i++)
                {
                    if (i < codesTmp.Length)
                    {
                        string barcode = codesTmp[i];
                        if (barcode.Contains("."))
                        {
                            barcode = Function.TransToBarCodeOne(barcode);
                        }
                        res.Add(barcode);
                    }
                    else
                    {
                        res.Add("");
                    }
                }
                return res;
            }
            catch (Exception ex)
            {
                LogHelper.WriteLogManager(ex);
                LogHelper.WriteErrLogBase(ex.ToString(), MethodBase.GetCurrentMethod().Name);
                return res;
            }
        }
    }
}