using Common;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using com.hnshituo.core.webapp.vo;

namespace MeterModelLibrary
{
    /// <summary>
    /// 车号修正类
    /// </summary>
    public class CarNoModfiy
    {
        /// <summary>
        /// 车号修正
        /// </summary>
        /// <param name="strCarNO">识别的车号</param>
        /// <param name="ModifyMethods">修正方法用逗号分隔 CarNoTrustCorrect1,CarNoTrustCorrect2,CarNoTrustCorrect3</param>
        /// <returns></returns>
        public string CarNoCorrect(string strCarNO,string ModifyMethods)
        {
            try
            {
                if (string.IsNullOrEmpty(strCarNO) && strCarNO.Length < 7)
                {
                    return strCarNO;
                }

                if (ModifyMethods.Trim() == "") 
                {
                    return strCarNO;
                }

                string[] strTemp = ModifyMethods.Split(',');
                
                //////////////////查出所有预报及内倒表中的车号/////////////////
                string sql = @"select carNo from (select CAR_NO as carNo
                                  from (select CAR_NO
                                          from PRE_TRACK_SCALE
                                         where value_Flag in ('0', '1','4') and car_No is not null
                                        union all
                                        select CAR_NO
                                          from METER_BASE_CAR_CARD
                                         where value_Flag = '1' and car_No is not null)
                                 group by CAR_NO) where ";

                PbModelDbService<List<MeterBaseCarCard>> pb = new PbModelDbService<List<MeterBaseCarCard>>();

                RESTfulResult<string> strRm = new RESTfulResult<string>();
                ExecuteMethod em = new ExecuteMethod();

                //根据级别调用对应的方法个数，若其中有正确返回车号的则终止循环
                foreach (string str in strTemp)
                {
                    strRm = em.GetAndExecuteMethod<string>("MeterModelLibrary", "CarNoModfiy", str, new object[] { strCarNO });
                    if (strRm.Succeed)
                    {
                        if (strRm.Data != "")
                        {
                            string sSql = sql + strRm.Data;
                            RESTfulResult<List<MeterBaseCarCard>> rm = pb.executeSqlDataWf(sSql);
                            if (rm.Succeed && rm.Data != null)// && rm.Data.Count == 1)
                            {
                                if (rm.Data.Count > 0) 
                                {
                                    if (rm.Data.Count == 1)
                                    {
                                        WriteLog("车号结果：修正前车号" + strCarNO + "，修正后车号:" + rm.Data[0].carNo);
                                        strCarNO = rm.Data[0].carNo;
                                    }
                                    else if (str == "CarNoTrustCorrect1")
                                    {
                                        //一级修正补充
                                        string stCarNo = "";
                                        int i = 0;
                                        foreach (MeterBaseCarCard mbcc in rm.Data)
                                        {
                                            if (mbcc.carNo.Substring(1, mbcc.carNo.Length - 1) == strCarNO.Substring(1, strCarNO.Length - 1))
                                            {
                                                stCarNo = mbcc.carNo;
                                                i++;
                                            }
                                        }
                                        if (i == 1)
                                        {
                                            WriteLog("车号结果：修正前车号" + strCarNO + "，修正后车号:" + stCarNo);
                                            strCarNO = stCarNo;
                                        }
                                    }
                                    if (rm.Data.Count > 1)
                                    {
                                        //2021年3月7日 杨秀东添加的逻辑；将识别错误的车号通过默认表给他纠正回来
                                        String sqlModify = @"select card_modfiy from Meter_Base_Car_Modify  where card_no ='" + strCarNO + "'";
                                        PbModelDbService<List<MeterBaseCarModify>> pbModify = new PbModelDbService<List<MeterBaseCarModify>>();
                                        RESTfulResult < List <MeterBaseCarModify>> rmModify = pbModify.executeSqlDataWf(sqlModify);
                                        if (rmModify.Succeed && rmModify.Data != null&& rmModify.Data.Count == 1)
                                        {
                                            WriteLog("车号结果：修正前车号" + strCarNO + "，修正后车号:" + rmModify.Data[0].card_modfiy);
                                            strCarNO = rmModify.Data[0].card_modfiy;
                                        }
                                    }                                    
                                    break;
                                }
                            }
                            if (rm.Succeed && rm.Data == null && str == "CarNoTrustCorrect2")
                            {
                                //二级修正补充;杨秀东2021年3月13日添加
                                strRm = em.GetAndExecuteMethod<string>("MeterModelLibrary", "CarNoModfiy", "CarNoTrustCorrect2Complement", new object[] { strCarNO });
                                if (strRm.Succeed && strRm.Data != "")
                                {
                                    string sSql2 = sql + strRm.Data;
                                    RESTfulResult<List<MeterBaseCarCard>> rm2 = pb.executeSqlDataWf(sSql2);
                                    if (rm2 !=null && rm2.Succeed && rm2.Data != null && rm2.Data.Count == 1)
                                    {
                                        string stCarNo = rm2.Data[0].carNo;
                                        WriteLog("车号结果：修正前车号" + strCarNO + "，修正后车号:" + stCarNo);
                                        strCarNO = stCarNo;
                                    }
                                }
                            }
                        }
                    }
                    else
                    {
                        //这里是未找到方法的情况
                        WriteLog("车号修正异常！" + strRm.ResultMessage);
                        break;
                    }
                }
                return strCarNO;
            }
            catch (Exception exp)
            {
                WriteLog("车号修正异常！" + exp.Message);
                return strCarNO;
            }
        }

        /// <summary>
        /// 车号修正 第1梯队
        /// </summary>
        /// <param name="strCarNO"></param>
        /// <returns></returns>
        public string CarNoTrustCorrect0(string strCarNO)
        {
            return "carNo='" + strCarNO + "'";
        }

        /// <summary>
        /// 车号修正 第1梯队
        /// </summary>
        /// <param name="strCarNO"></param>
        /// <returns></returns>
        public string CarNoTrustCorrect1(string strCarNO)
        {
            try
            {
                string sWhere = "";
                //////////////////第1梯队(6位)////////////////////////       
                for (int i = 0; i < strCarNO.Length; i++)
                {
                    if (i == 0)
                    {
                        sWhere += "carNo like '_" + strCarNO.Substring(1, strCarNO.Length - 1) + "'";
                    }
                    else if (i < strCarNO.Length - 1)
                    {
                        sWhere += " or carNo like '" + strCarNO.Substring(0, i) + "_" + strCarNO.Substring(i + 1, strCarNO.Length - (i + 1)) + "'";
                    }
                    else
                    {
                        sWhere += " or carNo like '" + strCarNO.Substring(0, i) + "_'";
                    }
                }
                return sWhere;
            }
            catch (Exception exp)
            {
                return "";
            }
        }

        /// <summary>
        /// 车号修正 第2梯队
        /// </summary>
        /// <param name="strCarNO"></param>
        /// <returns></returns>
        public string CarNoTrustCorrect2(string strCarNO)
        {
            try
            {
                //连续length-2位相同
                string sWhere = "";
                for (int i = 0; i < 3; i++)
                {
                    if (i == 0)
                    {
                        sWhere += "carNo like '" + strCarNO.Substring(i, strCarNO.Length - 2) + "%'";
                    }
                    else
                    {
                        sWhere += " or carNo like '%" + strCarNO.Substring(i, strCarNO.Length - 2) + "%'";
                    }
                }
                return sWhere;
            }
            catch (Exception exp)
            {
                return "";
            }
        }
        /// <summary>
        /// 车号修正 第2梯队的补充，针对中间三个车号错两个进行处理
        /// </summary>
        /// <param name="strCarNO"></param>
        /// <returns></returns>
        public string CarNoTrustCorrect2Complement(string strCarNO)
        {
            try
            {
                //连续length-2位相同；--carNo like '甘A__270' or carNo like '甘A3__70' or carNo like '甘A33__0'
                string sWhere = "";
                sWhere += " carNo like '" + strCarNO.Substring(0, 2) + "__" + strCarNO.Substring(4, 3) + "'";
                sWhere += "or carNo like '" + strCarNO.Substring(0, 3) + "__" + strCarNO.Substring(5, 2) + "'";
                sWhere += "or carNo like '" + strCarNO.Substring(0, 4) + "__" + strCarNO.Substring(6, 1) + "'";
                return sWhere;
            }
            catch (Exception exp)
            {
                return "";
            }
        }

        /// <summary>
        /// 车号修正 第3梯队
        /// </summary>
        /// <param name="strCarNO"></param>
        /// <returns></returns>
        public string CarNoTrustCorrect3(string strCarNO)
        {
            try
            {
                //连续length-3位相同
                string sWhere = "";
                for (int i = 0; i < 4; i++)
                {
                    if (i == 0)
                    {
                        sWhere += "carNo like '" + strCarNO.Substring(i, strCarNO.Length - 3) + "%'";
                    }
                    else
                    {
                        sWhere += " or carNo like '%" + strCarNO.Substring(i, strCarNO.Length - 3) + "%'";
                    }
                }
                return sWhere;
            }
            catch (Exception exp)
            {
                return "";
            }
        }


        private void WriteLog(string str)
        {
            string m_szRunPath;
            m_szRunPath = System.Environment.CurrentDirectory;
            if (System.IO.Directory.Exists(m_szRunPath + "\\log") == false)
            {
                System.IO.Directory.CreateDirectory(m_szRunPath + "\\log");
            }
            string strDate = System.DateTime.Now.ToString("yyyyMMdd");
            string strPathFile = m_szRunPath + "\\log\\" + strDate;
            if (!Directory.Exists(strPathFile))//如果不存在就创建file文件夹
            {
                Directory.CreateDirectory(strPathFile);
            }
            System.IO.TextWriter tw = new System.IO.StreamWriter(strPathFile + "\\车号修正_" + strDate + ".log", true);

            tw.WriteLine(System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
            tw.WriteLine(str);
            tw.WriteLine("\r\n");

            tw.Close();
        }

    }
}
