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

namespace RailMeterSystem
{
    public class BaseDbCls
    {
        Log lg = Log.GetInstance();
        internal bool setBaseDb()
        {
            bool flag = true;

            #region 初始化一次缓存类
            PbStaticRailwayCache.Load();
            #endregion

            #region 读取音频文件名称
            /*
            List<string> ls = new List<string>();
            DirectoryInfo folder = new DirectoryInfo(PbStaticRailwayCache.path + "\\Sound");

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

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

            #region 所有计量点数据
            MeterBaseSpotInfoService service = new MeterBaseSpotInfoService();
            RESTfulResult<List<MeterBaseSpotInfo>> rms = service.doQueryWf(new MeterBaseSpotInfo { validFlag = "1", spotTypeNo = "001002002" });
            if (rms.Succeed && rms.Data != null && rms.Data.Count > 0)
            {
                PbStaticRailwayCache.sportInfos = rms.Data;
            }
            else
            {
                lg.WriteLog(1, "MeterBaseSpotInfo");
                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)
            {
                PbStaticRailwayCache.ltRange = rmR.Data;
            }
            else
            {
                lg.WriteLog(1, "未找到秤体量程配置信息MeterBaseScaleRange");
                return false;
            }
            #endregion

            #region 轨道衡监控表
            MeterWorkStaticMonitorService staticMonitorService = new MeterWorkStaticMonitorService();
            RESTfulResult<List<MeterWorkStaticMonitor>> rr = staticMonitorService.doQueryWf(new MeterWorkStaticMonitor { validFlag = "1" });
            if (rr.Succeed && rr.Data != null && rr.Data.Count > 0)
            {
                PbStaticRailwayCache.ltMonitor = rr.Data;
            }
            else
            {
                lg.WriteLog(1, "未找到监控表信息MeterWorkStaticMonitor");
                return false;
            }
            #endregion

            #region 车型 ltCarType
            string sql = @"SELECT base_code baseCode,base_name baseName,memo FROM Com_Base_Info where p_base_code='001028' 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)
            {
                PbStaticRailwayCache.ltCarType = rmType.Data;
            }
            else
            {
                lg.WriteLog(1, "未找到车型基础信息");
                return false;
            }
            #endregion

            return flag;
        }

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

                Type postType = PbStaticRailwayCache.videoInfo.GetType();
                PropertyInfo[] postTypeInfos = postType.GetProperties(); //返回为当前 Type 的所有公共属性,PropertyInfo[] PropertyInfo 的所有公共属性的 Type 对象数组
                foreach (PropertyInfo p in postTypeInfos)
                {
                    if (p.Name.Contains("videoip")) //2021年4月11日 杨秀东：原来从摄像头截图使用摄像头position；现在从硬盘录像机截图，使用videoip
                    {
                        string str = (string)p.GetValue(PbStaticRailwayCache.videoInfo, null);
                        if (str != "" && str != null)
                        {
                            videoChild.Add(
                                new MeterBaseVideoChild
                                {
                                    pointid = PbStaticRailwayCache.videoInfo.pointid,
                                    p_ip = str,
                                    p_rate = "0",//************解码率直接给定了，这里没取配置值*************
                                    vdo_port = PbStaticRailwayCache.videoInfo.vdoPort,
                                    vdo_pwd = PbStaticRailwayCache.videoInfo.vdoPwd,
                                    vdo_user = PbStaticRailwayCache.videoInfo.vdoUser
                                }
                            );
                        }
                    }
                }

                PbStaticRailwayCache.videoChild = videoChild;
                if (videoChild.Count == 0)
                {
                    return false;
                }
                else
                {
                    //将摄像头操作类存储再缓存中
                    List<DhCameraShot> list = new List<DhCameraShot>();
                    foreach (MeterBaseVideoChild child in PbStaticRailwayCache.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

            return true;
        }
    }
}
