using ICSSoft.Common;
using ICSSoft.Entity;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ICSSoft.DataProject
{
    /// <summary>
    /// 自定义语言服务
    /// </summary>
    public class ICSDefiniteLanguageService
    {
        private static log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        private static string connString = System.Configuration.ConfigurationManager.AppSettings["ConnStr"];
        private static string ERPDB = System.Configuration.ConfigurationManager.AppSettings["ERPDB"];
        DataTable table = null;
        SqlConnection conn = new System.Data.SqlClient.SqlConnection(connString);
        string sql = string.Empty;
        string sqlInfo = string.Empty;
        VerificationMethod verification = new VerificationMethod();

        public static string GetLanguage()
        {
            using (SqlConnection conn = new System.Data.SqlClient.SqlConnection(connString))
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand();
                SqlTransaction sqlTran = conn.BeginTransaction();
                cmd.Transaction = sqlTran;
                cmd.Connection = conn;
                try
                {
                    string sql = @"select top 1 b.F_Define1 as DefineCode,b.F_itemCode as ItemCode,b.F_ItemName as ItemName
                            from Sys_SRM_Items a left join Sys_SRM_ItemsDetail b on a.F_Id=b.F_ItemId 
                            where a.F_EnCode='LGG0001' and b.F_EnabledMark='1' order by b.F_ItemCode";

                    DataTable table = DBHelper.SQlReturnData(sql, cmd);
                    string json = JsonConvert.SerializeObject(table);
                    List<LanguageModel> modelList = JsonConvert.DeserializeObject<List<LanguageModel>>(json);
//                    LanguageModel model = JsonConvert.DeserializeObject<LanguageModel>(json);
                    cmd.Transaction.Commit();
                    return modelList[0].DefineCode;
//                    return model.DefineCode;
                }
                catch(Exception ex)
                {
                    if (cmd.Transaction != null)
                        cmd.Transaction.Rollback();
                    log.Error(ex.Message);
                    throw new Exception(ex.Message);
                }
                finally
                {
                    if (conn.State == ConnectionState.Open)
                    {
                        conn.Close();
                    }
                    conn.Dispose();
                }
            }
        }
    }
}