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

namespace MESClassLibrary.DAL
{
    public class BasicDAL
    {
        public bool Del_Info(string tableName,string id, string userid)
        {
            try
            {
                string sql = @"update " + tableName + @" set 
                                 IsUseing = @IsUseing,
                                 DisableUserID = @DisableUserID,
                                 DisableTime = @DisableTime
                                 where ID=@ID";

                SqlParameter[] param = new SqlParameter[4];
                param[0] = new SqlParameter("@IsUseing", SqlDbType.Int);
                param[0].Value = 0;

                param[1] = new SqlParameter("@DisableUserID", SqlDbType.VarChar);
                param[1].Value = userid;

                param[2] = new SqlParameter("@DisableTime", SqlDbType.DateTime);
                param[2].Value = DateTime.Now;

                param[3] = new SqlParameter("@ID", SqlDbType.VarChar);
                param[3].Value = id;

                if (SqlHelper.ExecuteNonQuery(SqlHelper.GetConnSting(), CommandType.Text, sql, param) > 0)
                {
                    return true;
                }
                return false;
            }
            catch (Exception ex)
            {
                LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod());
                return false;
            }

        }
        /// <summary>
        /// 生成流水号
        /// </summary>
        /// <param name="type">类型</param>
        /// <param name="len">流水号长度</param>
        /// <returns></returns>
         static string GetBillNo(string type, int len)
        {
            string sql = $"exec p_GetBillNo '{type}'";

            DataTable dt = SqlHelper.ExecuteDataset(SqlHelper.GetConnSting(), CommandType.Text, sql).Tables[0];

            int ret = Convert.ToInt32(dt.Rows[0]["value"].ToString());

            return ret.ToString().PadLeft(len, '0');
        }

        /// <summary>
        /// 生成最新的注塑条码
        /// </summary>
        /// <param name="stockNo"></param>
        /// <param name="batch"></param>
        /// <returns></returns>
        public static string GetSjBarCodeSerialNo(string stockNo,string batch)
        {
            if(stockNo.Length != 10)
            { 
                throw new Exception($"生成塑件条码错误,因为存货代码[{stockNo}]格式不正确,必须10位长度.");
            }
            string s = GetBillNo(stockNo + batch, 4);
            string sjBarCode = stockNo + batch + s;
            try
            {
                SqlHelper.ExecuteNonQuery(SqlHelper.GetConnSting(), CommandType.Text, $" insert into tb_BarCodeUnique(OneBarCode) values('{sjBarCode}')");
                return sjBarCode;
            }
            catch(Exception ex)
            {
               return  GetSjBarCodeSerialNo(stockNo, batch);
            }
        }
        public static DateTime GetServerTime()
        {
            string sql = "";
            DateTime time;
            DataTable dt;
            try
            {
                sql = @"select  convert(char(23),getdate(),121) as time";

                dt = SqlHelper.ExecuteDataset(SqlHelper.GetConnSting(), CommandType.Text, sql, null).Tables[0];
                if (dt != null && dt.Rows.Count > 0)
                {
                    //time = Convert.ToDateTime(dt.Rows[0]["time"].ToString());
                    time = DateTime.ParseExact(dt.Rows[0]["time"].ToString(), "yyyy-MM-dd HH:mm:ss.fff",
                        System.Globalization.CultureInfo.CurrentCulture);

                }
                else
                {
                    time = DateTime.Now;
                }
                return time;
            }
            catch (Exception ex)
            {
                LogErrBLL.AddInfo(ex.ToString(), MethodBase.GetCurrentMethod());
                return DateTime.Now;
            }
        }
    }
}