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

namespace StorageMeterSystem
{
    public class BaseDbCls
    {
        Log lg = Log.GetInstance();

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

            foreach (FileInfo file in folder.GetFiles("*.wav"))
            {
                ls.Add(file.Name.Replace(".wav", ""));
            }
            PbStorageCache.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)
            {
                PbStorageCache.ltVideoInfo = rm.Data;
            }
            else
            {
                lg.WriteLog(8, "未找到摄像头配置信息MeterBaseVideoinfo");
                return false;
            }
            #endregion

            #region 所有计量点数据
            MeterBaseSpotInfoService service = new MeterBaseSpotInfoService();
            RESTfulResult<List<MeterBaseSpotInfo>> rms = service.doQueryWf(new MeterBaseSpotInfo { validFlag = "1", spotTypeNo = "001002003" }); //获取成品秤数据
            if (rms.Succeed && rms.Data != null && rms.Data.Count > 0)
            {
                PbStorageCache.sportInfos = rms.Data;
                if (PbStorageCache.ltMonitor != null) 
                {
                    foreach (MeterBaseSpotInfo sp in PbStorageCache.sportInfos)
                    {
                        string pNo = PbStorageCache.ltMonitor?.FirstOrDefault(s => s.baseSpotNo == sp.baseSpotNo)?.pointNo;
                        if (!string.IsNullOrEmpty(pNo))
                            sp.deleteManNo = pNo;
                    }
                }
            }
            else
            {
                lg.WriteLog(8, "MeterBaseSpotInfo");
                return false;
            }
            #endregion

            #region 秤点配置表,针对一个秤多块表的情况
            PbModelDbService<List<MeterBaseScalePoint>> pbRelation = new PbModelDbService<List<MeterBaseScalePoint>>();
            string sql = @"select t2.scale_point_no scalePointNo,
                                   t2.scale_point_name scalePointName,
                                   t2.base_spot_no baseSpotNo,
                                   t2.base_spot_name baseSpotName
                              from meter_base_spot_info t1,
                                   meter_base_scale_point t2
                             where t1.base_Spot_No=t2.base_Spot_No and t2.valid_flag='1' and t1.valid_flag='1' and t1.spot_type_no='001002003'
                             order by t2.base_spot_no";
            RESTfulResult<List<MeterBaseScalePoint>> rmRelation = pbRelation.executeSqlDataWf(sql);
            if (rmRelation.Succeed && rmRelation.Data != null && rmRelation.Data.Count > 0)
            {
                PbStorageCache.scalePoints = rmRelation.Data;
            }
            else
            {
                lg.WriteLog(8, "未找到秤校基础数据MeterBaseCalibraRelation");
                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)
            {
                PbStorageCache.ltRange = rmR.Data;
            }
            else
            {
                lg.WriteLog(8, "未找到秤体量程配置信息MeterBaseScaleRange");
                return false;
            }
            #endregion

            return true;
        }

        internal bool setBaseDb()
        {
            #region 初始化一次缓存类
            PbStorageCache.Load();
            #endregion

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

        internal bool setBaseDbScalePoint()
        {
            #region 初始化一次缓存类
            PbStorageCache.Load();
            #endregion

            #region 监控表
            MeterBaseScalePointService monitorService = new MeterBaseScalePointService();
            RESTfulResult<List<MeterBaseScalePoint>> rmMonitor = monitorService.doQueryWf(new MeterBaseScalePoint { validFlag = "1" });
            if (rmMonitor.Succeed)
            {
                PbCacheMonitor.ltMeterBaseScalePoint = rmMonitor.Data;
                return true;
            }
            else
            {
                lg.WriteLog(4, "未找到秤体基础配置信息MeterBaseScalePoint");
                return false;
            }
            #endregion
        }

        /// <summary>
        /// 热送基础数据
        /// </summary>
        /// <returns></returns>
        internal bool setBaseDbScalePointHot()
        {
            #region 初始化一次缓存类
            PbStorageCache.Load();
            #endregion

            #region 热送磅监控表
            MeterBaseScalePointService monitorService = new MeterBaseScalePointService();
            RESTfulResult<List<MeterBaseScalePoint>> rmMonitor = monitorService.doQueryWf(new MeterBaseScalePoint { validFlag = "1" ,spotTypeNo= "001002004" });
            if (rmMonitor.Succeed)
            {
                PbCacheMonitor.ltMeterBaseScalePoint = rmMonitor.Data;
                return true;
            }
            else
            {
                lg.WriteLog(4, "未找到秤体基础配置信息MeterBaseScalePoint");
                return false;
            }
            #endregion
        }

        public bool getSpot()
        {
            #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 t2.base_Spot_No = '" + PbStorageCache.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)
            {
                PbStorageCache.relations = rmRelation.Data;
            }
            else
            {
                lg.WriteLog(8, "未找到秤校基础数据MeterBaseCalibraRelation");
                return false;
            }
            #endregion

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

                Type postType = PbStorageCache.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(PbStorageCache.videoInfo, null);
                        if (str != "" && str != null)
                        {
                            videoChild.Add(
                                new MeterBaseVideoChild
                                {
                                    pointid = PbStorageCache.videoInfo.pointid,
                                    p_ip = str,
                                    p_rate = "0",//************解码率直接给定了，这里没取配置值*************
                                    vdo_port = PbStorageCache.videoInfo.vdoPort,
                                    vdo_pwd = PbStorageCache.videoInfo.vdoPwd,
                                    vdo_user = PbStorageCache.videoInfo.vdoUser
                                }
                            );
                        }
                    }
                }

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

        public bool getDb()
        {
            #region 实时获取监控信息
            MeterWorkMonitorService monitorService = new MeterWorkMonitorService();
            RESTfulResult<List<MeterWorkMonitor>> rmMonitor = monitorService.doQueryAllWf(new MeterWorkMonitor { validFlag = "1" });
            if (rmMonitor.Succeed)
            {
                PbCacheMonitor.ltWorkMonitor = rmMonitor.Data;
                return true;
            }
            else
            {
                lg.WriteLog(4, "未找到秤体量程配置信息MeterBaseScaleRange");
                return false;
            }
            #endregion
        }

        /// <summary>
        /// 查询MeterBaseScalePoint表的基础信息
        /// </summary>
        /// <returns></returns>
        public bool getDbBaseScale()
        {
            #region 实时获取监控信息
            MeterBaseScalePointService monitorService = new MeterBaseScalePointService();
            RESTfulResult<List<MeterBaseScalePoint>> rmMonitor = monitorService.doQueryWf(new MeterBaseScalePoint { validFlag = "1" });
            if (rmMonitor.Succeed)
            {
                PbCacheMonitor.ltMeterBaseScalePoint = rmMonitor.Data;
                return true;
            }
            else
            {
                lg.WriteLog(4, "未找到秤体基础配置信息MeterBaseScalePoint");
                return false;
            }
            #endregion
        }

        /// <summary>
        /// 查询MeterBaseScalePoint表的基础信息:热送
        /// </summary>
        /// <returns></returns>
        public bool getDbBaseScaleHot()
        {
            #region 实时获取热送磅监控信息
            MeterBaseScalePointService monitorService = new MeterBaseScalePointService();
            RESTfulResult<List<MeterBaseScalePoint>> rmMonitor = monitorService.doQueryWf(new MeterBaseScalePoint { validFlag = "1", spotTypeNo = "001002004" });
            if (rmMonitor.Succeed)
            {
                PbCacheMonitor.ltMeterBaseScalePoint = rmMonitor.Data;
                return true;
            }
            else
            {
                lg.WriteLog(4, "未找到秤体基础配置信息MeterBaseScalePoint");
                return false;
            }
            #endregion
        }
    }
}
