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

namespace CarRemoteMeter
{
    /// <summary>
    /// 基础数据获取
    /// </summary>
    public class BaseDbCls
    {
        Log lg = Log.GetInstance();

        public bool getBaseDb() 
        {
            #region 读取音频文件名称
            List<string> ls = new List<string>();
            DirectoryInfo folder = new DirectoryInfo(PbCache.path + "\\Sound");

            foreach (FileInfo file in folder.GetFiles("*.wav"))
            {
                ls.Add(file.Name.Replace(".wav", ""));
            }
            PbCache.voiceInfo = ls;
            #endregion

            #region 获取计量类型
            string sql = @"SELECT base_code baseCode,base_name baseName,memo FROM Com_Base_Info where p_base_code='001006' and valid_Flag='1'";
            PbModelDbService<List<ComBaseInfo>> pb = new PbModelDbService<List<ComBaseInfo>>();
            RESTfulResult<List<ComBaseInfo>> rmType = pb.executeSqlDataWf(sql);
            if (rmType.Succeed && rmType.Data != null && rmType.Data.Count > 0)
            {
                PbCache.businessType = rmType.Data;
            }
            else
            {
                lg.WriteLog(5, "未找到计量类型基础信息");
                return false;
            }
            //*/
            #endregion

            #region 获取一次计量数据过期时间
            sql = @"SELECT base_code baseCode,base_name baseName,memo FROM Com_Base_Info where p_base_code='001031' and valid_Flag='1'";
            PbModelDbService<List<ComBaseInfo>> pb1 = new PbModelDbService<List<ComBaseInfo>>();
            RESTfulResult<List<ComBaseInfo>> rmType1 = pb.executeSqlDataWf(sql);
            if (rmType1.Succeed && rmType1.Data != null && rmType1.Data.Count > 0)
            {
                try
                {
                    PbCache.isEndHour = Convert.ToDouble(rmType.Data[0].baseName);
                }
                catch
                {
                    PbCache.isEndHour = 9999;
                }
            }
            #endregion

            #region 所有摄像头配置信息
            MeterBaseVideoinfoService mbvs = new MeterBaseVideoinfoService();
            RESTfulResult<List<MeterBaseVideoinfo>> rm = mbvs.doQueryWf(new MeterBaseVideoinfo { });
            if (rm.Succeed && rm.Data != null && rm.Data.Count > 0)
            {
                PbCache.ltVideoInfo = rm.Data;
            }
            else
            {
                lg.WriteLog(5, "未找到摄像头配置信息MeterBaseVideoinfo");
                return false;
            }
            #endregion

            #region 秤体量程配置 
            //*
            MeterBaseScaleRangeService rangeService = new MeterBaseScaleRangeService();
            RESTfulResult<List<MeterBaseScaleRange>> rmR = rangeService.doQueryWf(new MeterBaseScaleRange { });
            if (rmR.Succeed && rmR.Data != null && rmR.Data.Count > 0)
            {
                PbCache.ltRange = rmR.Data;
            }
            else
            {
                lg.WriteLog(5, "未找到秤体量程配置信息MeterBaseScaleRange");
                return false;
            }
            //*/
            #endregion

            #region 获取计量场景信息
            MeterWorkSceneRegisterService sceneRegisterService = new MeterWorkSceneRegisterService();
            RESTfulResult<List<MeterWorkSceneRegister>> rmSceneRegister = sceneRegisterService.doQueryWf(new MeterWorkSceneRegister { valueFlag = "0" });
            if (rmSceneRegister.Succeed)
            {
                PbCache.ltSceneRegister = rmSceneRegister.Data;
            }
            else
            {
                lg.WriteLog(1, "未找到计量场景信息配置信息sceneRegisterService");
                return false;
            }
            #endregion

            return true;
        }

        /// <summary>
        /// 将基础数据全部载入缓存中
        /// </summary>
        /// <returns></returns>
        public bool setBaseDb()
        {
            #region 初始化一次缓存类
            PbCache.monitor = new MeterWorkMonitor();
            PbCache.Load();
            #endregion

            #region 历史皮重计算配置表
            //*
            MeterBaseHisTareCountService service = new MeterBaseHisTareCountService();
            RESTfulResult<List<MeterBaseHisTareCount>> rmH = service.doQueryWf(new MeterBaseHisTareCount { valueFlag = "0", typeNo = "001033001" }); //0有效 1无效 //db.doOption<List<MeterBaseHisTareCount>>("MeterBaseHisTareCountService", "doQueryWf", new object[] { mbhtc }, 0);
            if (rmH.Succeed && rmH.Data != null && rmH.Data.Count > 0)
            {
                PbCache.tareCount = rmH.Data[0];
            }
            else 
            {
                lg.WriteLog(5, "未找到历史皮重计算配置信息MeterBaseHisTareCount");
                return false;
            }
            //*/
            #endregion

            #region 期限皮重计算配置表
            MeterBaseTermTareCountService termTareCountService = new MeterBaseTermTareCountService();
            RESTfulResult<List<MeterBaseTermTareCount>> rmTe = termTareCountService.doQueryWf(new MeterBaseTermTareCount { valueFlag = "0", typeNo = "001033001" }); //0有效 1无效 //db.doOption<List<MeterBaseHisTareCount>>("MeterBaseHisTareCountService", "doQueryWf", new object[] { mbhtc }, 0);
            if (rmTe.Succeed && rmTe.Data != null && rmTe.Data.Count > 0)
            {
                PbCache.termTareCount = rmTe.Data[0];
            }
            else
            {
                lg.WriteLog(1, "未找到期限皮重配置信息MeterBaseHisTareCount");
                return false;
            }
            #endregion

            #region 危化品限载配置
            /*
            MeterBaseLimitChemicalService limitService = new MeterBaseLimitChemicalService();
            RESTfulResult<List<MeterBaseLimitChemical>> rmL = limitService.doQueryWf(new MeterBaseLimitChemical { valueFlag = "1" });
            if (rmL.Succeed)
            {
                PbCache.limit = rmL.Data;
            }
            else 
            {
                lg.WriteLog(5, "未找到危化品限载配置信息MeterBaseLimitChemical");
                return false;
            }
            //*/
            #endregion

            #region 两次计量时间间隔配置
            /*
            MeterBaseTimeSpaceService spaceService = new MeterBaseTimeSpaceService();
            RESTfulResult<List<MeterBaseTimeSpace>> rmS = spaceService.doQueryWf(new MeterBaseTimeSpace { spotTypeNo = "001002001" });//汽车衡
            if (rmS.Succeed)
            {
                PbCache.timeSpace = rmS.Data;
            }
            else 
            {
                lg.WriteLog(5, "未找到两次计量时间间隔配置信息MeterBaseTimeSpace");
                return false;
            }
            //*/
            #endregion

            #region 期限皮重时间配置
            /*
            MeterBaseTermTareTimeService tareTime = new MeterBaseTermTareTimeService();
            RESTfulResult<List<MeterBaseTermTareTime>> rmT = tareTime.doQueryWf(new MeterBaseTermTareTime { valueFlag = "0" });//0：有效；1：无效
            if (rmS.Succeed)
            {
                PbCache.tareTime = rmT.Data;
            }
            else
            {
                lg.WriteLog(5, "未找到期限皮重时间配置信息MeterBaseTermTareTime");
                return false;
            }
            //*/
            #endregion

            #region 所有秤体信息
            //*
            MeterWorkMonitorService monitorService = new MeterWorkMonitorService();
            RESTfulResult<List<MeterWorkMonitor>> rmMonitor = monitorService.doQueryOneWf(new MeterWorkMonitor { validFlag = "1" });
            if (rmMonitor.Succeed)
            {
                PbCache.ltWorkMonitor = rmMonitor.Data;
            }
            else 
            {
                lg.WriteLog(1, "未找到秤体信息配置信息MeterWorkMonitor");
                return false;
            }
            //*/
            #endregion

            #region 所有计量点数据
            MeterBaseSpotInfoService Pservice = new MeterBaseSpotInfoService();
            RESTfulResult<List<MeterBaseSpotInfo>> rms = Pservice.doQueryWf(new MeterBaseSpotInfo { validFlag = "1" });
            if (rms.Succeed && rms.Data != null && rms.Data.Count > 0)
            {
                PbCache.sportInfos = rms.Data;
            }
            else
            {
                lg.WriteLog(1, "MeterBaseSpotInfo");
                return false;
            }
            #endregion

            #region 计量卸货时间
            MeterBaseUnloadTimeService unloadTimeService = new MeterBaseUnloadTimeService();
            RESTfulResult<List<MeterBaseUnloadTime>> rmU = unloadTimeService.doQueryWf(new MeterBaseUnloadTime { });
            if (rmU.Succeed)
            {
                PbCache.unloadTimes = rmU.Data;
            }
            else
            {
                lg.WriteLog(1, "未找到计量卸货时间间隔配置信息MeterBaseUnloadTime");
                return false;
            }
            #endregion

            #region 计量车卡信息
            /*
            MeterBaseCarCardService carCardService = new MeterBaseCarCardService();
            RESTfulResult<List<MeterBaseCarCard>> rmCar = carCardService.doQueryWf(new MeterBaseCarCard { valueFlag = "1" });
            if (rmCar.Succeed)
            {
                PbCache.ltCarCard = rmCar.Data;
            }
            else
            {
                lg.WriteLog(1, "未找到计量卡车辆配置信息MeterBaseCarCard");
                return false;
            }
            //*/
            #endregion

            #region 校秤计量关联，用于获取是否限制
            string sql = @"select base_spot_no baseSpotNo, 
                           base_spot_name baseSpotName, 
                           up_weight upWeight, 
                           is_meter isMeter
                      from METER_BASE_CALIBRA_NUM_REL 
                     group by base_spot_no, base_spot_name, UP_WEIGHT, is_meter";
            PbModelDbService<List<MeterBaseCalibraNumRel>> pbNumRel = new PbModelDbService<List<MeterBaseCalibraNumRel>>();
            RESTfulResult<List<MeterBaseCalibraNumRel>> rmNumRel = pbNumRel.executeSqlDataWf(sql);
            if (rmNumRel.Succeed && rmNumRel.Data != null && rmNumRel.Data.Count > 0)
            {
                PbCache.ltBaseCalibraNumRel = rmNumRel.Data;
            }
            else
            {
                lg.WriteLog(1, "未找到秤点关联表METER_BASE_CALIBRA_NUM_REL信息");
                return false;
            }
            #endregion

            return true;
        }

         
        public bool getSpot() 
        {
            #region 获取摄像头数据用于截图
            if (PbCache.ltVideoInfo != null && PbCache.ltVideoInfo.Count > 0) 
            {
                PbCache.videoInfo = PbCache.ltVideoInfo.Where(s => s.pointid == PbCache.sportInfo.baseSpotNo).FirstOrDefault();
                List<MeterBaseVideoChild> videoChild = new List<MeterBaseVideoChild>();

                Type postType = PbCache.videoInfo.GetType();
                PropertyInfo[] postTypeInfos = postType.GetProperties(); //返回为当前 Type 的所有公共属性,PropertyInfo[] PropertyInfo 的所有公共属性的 Type 对象数组
                foreach (PropertyInfo p in postTypeInfos)
                {
                    if (p.Name.Contains("position"))
                    {
                        string str = (string)p.GetValue(PbCache.videoInfo, null);
                        if (str != "" && str != null)
                        {
                            videoChild.Add(
                                new MeterBaseVideoChild
                                {
                                    pointid = PbCache.videoInfo.pointid,
                                    p_ip = str,
                                    p_rate = "0",//************解码率直接给定了，这里没取配置值*************
                                    vdo_port = PbCache.videoInfo.vdoPort,
                                    vdo_pwd = PbCache.videoInfo.vdoPwd,
                                    vdo_user = PbCache.videoInfo.vdoUser
                                }
                            );
                        }
                    }
                }

                PbCache.videoChild = videoChild;
                if (videoChild.Count == 0)
                {
                    return false;
                }
                else
                {
                    //将摄像头操作类存储再缓存中
                    List<DhCameraShot> list = new List<DhCameraShot>();
                    foreach (MeterBaseVideoChild child in PbCache.videoChild)
                    {
                        DhCameraShot cameraShot = new DhCameraShot();
                        cameraShot.ip = child.p_ip;
                        cameraShot.port = child.vdo_port;
                        cameraShot.uid = child.vdo_user;
                        cameraShot.pwd = child.vdo_pwd;
                        list.Add(cameraShot);
                    }

                    CarCache.cameraShots = list;
                }
            }
            #endregion

            #region 秤体量程 
            if (PbCache.ltRange != null && PbCache.ltRange.Count > 0)
            {
                PbCache.range = PbCache.ltRange.Where(s => s.baseSpotNo == PbCache.sportInfo.baseSpotNo).FirstOrDefault();
            }
            else 
            {
                return false;
            }
            #endregion

            #region 校秤基础数据
            PbModelDbService<List<MeterBaseCalibraRelation>> pbRelation = new PbModelDbService<List<MeterBaseCalibraRelation>>();
            string sql = @"select *
                      from (select t2.base_Spot_No baseSpotNo,
                                   t2.base_Spot_Name baseSpotName,
                                   t2.calibration_Time_Unit calibrationTimeUnit,
                                   to_char(t2.start_time, 'HH24:mi:ss') startTime,
                                   to_char(t2.end_time, 'HH24:mi:ss') endTime,
                                   t2.day_Num dayNum
                              from METER_BASE_CALIBRA_RELATION t2
                             where where t2.base_Spot_No = '" + PbCache.sportInfo.baseSpotNo + @"'
                             order by t2.end_time desc)
                     where rownum = 1";
            RESTfulResult<List<MeterBaseCalibraRelation>> rmRelation = pbRelation.executeSqlDataWf(sql);
            if (rmRelation.Succeed && rmRelation.Data != null && rmRelation.Data.Count > 0)
            {
                PbCache.relations = rmRelation.Data;
            }
            else
            {
                lg.WriteLog(5, "未找到秤校基础数据MeterBaseCalibraRelation");
                return false;
            }
            #endregion

            return true;
        }
    }
}
