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

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
//                            ";

                string sql = @"
                                SELECT CONVERT(NVARCHAR(50),[Des])+','+[ColorCode]+','+[ColorNo] AS ColorInfo, CONVERT(NVARCHAR(50),[Des]) [des]
								into #a
                                FROM tb_Color
								where ColorCode is not null
								and ColorNo is not null;

								select max(ColorInfo)
								from #a
								group by [des];

								drop table #a;
                            ";
                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 SaveColor(string Barcode, string resultStr, string createBy, out string errorReason)
        {
            int res = 0;
            try
            {
                string sql = @"
                                INSERT INTO [tb_StockInColor]
                                                   ([ID]
                                                   ,[Barcode]
                                                   ,[ColorInfo],
                                                    CreateBy)
                                             VALUES
                                                   (
                                                    (select newid())
                                                   ,'" + Barcode + @"'
                                                   ,'" + resultStr + @"','" + createBy + @"')";
                res = SqlHelper.ExecuteNonQuery(SqlHelper.SqlConnString, CommandType.Text, sql, null);

                if (!Function.exsitInspectResult(Barcode))
                {
                    //插入一次质检信息

                    InspectModel model = new InspectModel();
                    model.ID = Guid.NewGuid().ToString();
                    model.barcode = Barcode;
                    model.side = Function.GetSide(Barcode); ;
                    model.position = "手持设备";
                    model.stationNo = "S20";
                    model.workClass = Function.GetWorkClass();
                    model.inspectResult = "[合格]";
                    model.productInfo = resultStr;
                    model.InspectTimes = "1";
                    model.productOption = "";
                    model.damnPosition = "";
                    model.defectID = "";
                    model.reason = "";
                    model.remark1 = "";
                    model.remark2 = "";
                    model.remark3 = "";
                    Function.InsertInspect(model);
                }
                else
                {
                    string sql1 = @"update tb_InspectResult set productInfo='" + resultStr + @"' where barcode='" + Barcode + @"'";
                    res = SqlHelper.ExecuteNonQuery(SqlHelper.SqlConnString, CommandType.Text, sql1, 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;
            }
        }
    }
}