using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Quartz;
using SlnMesnac.Business.@base;
using SlnMesnac.Common;
using SlnMesnac.Config;
using SlnMesnac.Repository;
using SlnMesnac.Repository.service;

namespace SlnMesnac.Quartz.Job
{
    public class GetTaskInfo_Job : IJob
    {
        private readonly ILogger<GetTaskInfo_Job> _logger;
        private Idata1_TableService _data1TableService;
        private Idata2_TableService _data2TableService;
        private Idata3_TableService _data3TableService;
        private Idata4_TableService _data4TableService;
        private IT_RP_StationPara_110_1Service _tr_stationPara_110_1Service;
        private IT_RP_StationPara_110_2Service _tr_stationPara_110_2Service;
        private IT_RP_StationPara_130Service _tr_stationPara_130Service;
        private IT_RP_StationPara_160_1Service _tr_stationPara_160_1Service;
        private readonly AppConfig _appConfig;
        public GetTaskInfo_Job(AppConfig appConfig, ILogger<GetTaskInfo_Job> logger, Idata1_TableService service,Idata2_TableService idata2_TableService, IT_RP_StationPara_110_1Service tr_stationPara_110_1Service, IT_RP_StationPara_110_2Service tr_stationPara_110_2Service, Idata3_TableService data3TableService,Idata4_TableService idata4_TableService,IT_RP_StationPara_130Service rP_StationPara_130Service,IT_RP_StationPara_160_1Service tr_stationPara_160_1Service)
        {
            _appConfig = appConfig;
            _logger = logger;
            _data1TableService = service;
            _tr_stationPara_110_1Service = tr_stationPara_110_1Service;
            _data2TableService = idata2_TableService;
            _tr_stationPara_110_2Service = tr_stationPara_110_2Service;
            _data3TableService = data3TableService;
            _tr_stationPara_130Service = rP_StationPara_130Service;
            _data4TableService = idata4_TableService;
            _tr_stationPara_160_1Service = tr_stationPara_160_1Service;
        }
        public Task Execute(IJobExecutionContext context)
        {
            _logger.LogInformation($"任务执行:{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}");
            var flag = _appConfig.sqlConfig.Find(x => x.configId == "Local").connStr.Split("\\").Last();
            if (flag == "EData.db3")
            {
                LoadEDataInfo();
            }
            else if (flag == "FData.db3")
            {
                LoadFDataInfo();
            }
            else if (flag == "HData.db3")
            {
                LoadHDataInfo();
            }
            else if (flag == "LSHData.db3")
            {
                LoadLSHDataInfo();
            }
            return Task.CompletedTask;
        }
        private async void LoadEDataInfo()
        {
            try
            {
                DateTime start = Convert.ToDateTime(DateTime.Now.ToString("D").ToString());
                DateTime end = Convert.ToDateTime(DateTime.Now.AddDays(1).ToString("D").ToString()).AddSeconds(-1);
                List<data1_Table> _data1Tables = _data1TableService.Getdata1TableListAsync().Result;
                if (_data1Tables != null && _data1Tables.Count > 0)
                {
                    foreach (var data1Table in _data1Tables)
                    {
                        T_RP_StationPara_110_1 T_RP_StationPara_110_1 = new T_RP_StationPara_110_1()
                        {
                            semibarcode = data1Table.dpqrcode,
                            scanbarcode = data1Table.jpqrcode,
                            state = data1Table.etotaljudge == "OK" ? "1" : "2",
                            para1 = data1Table.evalue,
                            para2 = data1Table.erangevalue,
                            para3 = data1Table.erank,
                            para4 = data1Table.bprank,
                            para5 = data1Table.e1value,
                            para6 = data1Table.e2value,
                            para7 = data1Table.e3value,
                            para8 = data1Table.e4value,
                            uploadflag = "0",
                            finalflag = "1",
                            inserttime = DateTime.Parse(data1Table.time)
                        };
                        bool insertflag = false;
                        var GetFlag = await _tr_stationPara_110_1Service.GetResultAsync(data1Table.jpqrcode, data1Table.dpqrcode);
                        if (GetFlag != null)
                        {
                            T_RP_StationPara_110_1.id = GetFlag.id;
                            insertflag = _tr_stationPara_110_1Service.Update(T_RP_StationPara_110_1);
                        }
                        else
                        {
                            insertflag = _tr_stationPara_110_1Service.Insert(T_RP_StationPara_110_1);
                        }
                        if (insertflag)
                        {
                            data1Table.flag = "F";
                            var iflag = _data1TableService.UpdateAsync(data1Table).Result;
                            if (iflag)
                            {
                                _logger.LogInformation(StringChange.ModeToJson(data1Table));
                            }
                        }


                    }

                }
            }
            catch (Exception ex)
            {
                _logger.LogError($"异常:{ex.Message}");
            }
        }

        private async void LoadFDataInfo()
        {
            try
            {
                DateTime start = Convert.ToDateTime(DateTime.Now.ToString("D").ToString());
                DateTime end = Convert.ToDateTime(DateTime.Now.AddDays(1).ToString("D").ToString()).AddSeconds(-1);
                List<data2_Table> _data1Tables = _data2TableService.Getdata2TableListAsync(start,end).Result;
                if (_data1Tables != null && _data1Tables.Count > 0)
                {
                    foreach (var data1Table in _data1Tables)
                    {
                        T_RP_StationPara_110_2 t_RP_StationPara_110_2 = new T_RP_StationPara_110_2()
                        {
                            scanbarcode = data1Table.qrcode,
                            state = data1Table.totaljudge == "OK" ? "1" : "2",
                            para1 = data1Table.f1value,
                            para1State = data1Table.f1judge == "OK" ? "1" : "2",
                            para2 = data1Table.f2value,
                            para2State = data1Table.f2judge == "OK" ? "1" : "2",
                            para3 = data1Table.f3value,
                            para3State = data1Table.f3judge == "OK" ? "1" : "2",
                            para4 = data1Table.f4value,
                            para4State = data1Table.f4judge == "OK" ? "1" : "2",
                            para5 = data1Table.frangevalue,
                            para5State = data1Table.frange == "OK" ? "1" : "2",
                            inserttime = data1Table.time,
                            uploadflag = "0",
                            finalflag = "1",

                        };
                        //查询是否同条码多次测量
                        bool insertflag = false;
                        var GetFlag = await _tr_stationPara_110_2Service.GetResultAsync(data1Table.qrcode);
                        if (GetFlag != null)
                        {
                            t_RP_StationPara_110_2.id = GetFlag.id;
                            insertflag = _tr_stationPara_110_2Service.Update(t_RP_StationPara_110_2);
                        }
                        else
                        {
                            insertflag = _tr_stationPara_110_2Service.Insert(t_RP_StationPara_110_2);
                        }
                        if (insertflag)
                        {
                            data1Table.flag = "F";
                            var iflag = _data2TableService.UpdateAsync(data1Table).Result;
                            if (iflag)
                            {
                                _logger.LogInformation(StringChange.ModeToJson(data1Table));
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                _logger.LogError($"异常:{ex.Message}");
            }
        }

        private async void LoadHDataInfo()
        {
            try
            {
                DateTime start = Convert.ToDateTime(DateTime.Now.ToString("D").ToString());
                DateTime end = Convert.ToDateTime(DateTime.Now.AddDays(1).ToString("D").ToString()).AddSeconds(-1);
                List<data3_Table> _data1Tables = _data3TableService.GetTableListAsync(start,end).Result;
                if (_data1Tables != null && _data1Tables.Count > 0)
                {
                    foreach (var data1Table in _data1Tables)
                    {
                        T_RP_StationPara_130 T_RP_StationPara_110_1 = new T_RP_StationPara_130()
                        {
                            scanbarcode = data1Table.qrcode,
                            state = data1Table.totaljudge == "OK" ? "1" : "2",
                            para1 = data1Table.h1value,
                            para1State = data1Table.h1judge == "OK" ? "1" : "2",
                            para2 = data1Table.h2value,
                            para2State = data1Table.h2judge == "OK" ? "1" : "2",
                            para3 = data1Table.h3value,
                            para3State = data1Table.h3judge == "OK" ? "1" : "2",
                            para4 = data1Table.h4value,
                            para4State = data1Table.h4judge == "OK" ? "1" : "2",
                            para5 = data1Table.hrangevalue,
                            para5State = data1Table.hrangejudge == "OK" ? "1" : "2",
                            inserttime = data1Table.time,
                            uploadflag = "0",
                            finalflag = "1",
                        };
                        bool insertflag = false;
                        var GetFlag = await _tr_stationPara_130Service.GetResultAsync(data1Table.qrcode);
                        if (GetFlag != null)
                        {
                            T_RP_StationPara_110_1.id = GetFlag.id;
                            insertflag = _tr_stationPara_130Service.Update(T_RP_StationPara_110_1);
                        }
                        else
                        {
                            insertflag = _tr_stationPara_130Service.Insert(T_RP_StationPara_110_1);
                        }
                        if (insertflag)
                        {
                            data1Table.flag = "F";
                            var iflag = _data3TableService.UpdateAsync(data1Table).Result;
                            if (iflag)
                            {
                                _logger.LogInformation(StringChange.ModeToJson(data1Table));
                            }
                        }


                    }

                }
            }
            catch (Exception ex)
            {
                _logger.LogError($"异常:{ex.Message}");
            }

        }
        private async void LoadLSHDataInfo()
        {
            try
            {
                DateTime start = Convert.ToDateTime(DateTime.Now.ToString("D").ToString());
                DateTime end = Convert.ToDateTime(DateTime.Now.AddDays(1).ToString("D").ToString()).AddSeconds(-1);
                List<data4_Table> _data1Tables = _data4TableService.GetTableListAsync(start, end).Result;
                if (_data1Tables != null && _data1Tables.Count > 0)
                {
                    foreach (var data1Table in _data1Tables)
                    {
                        T_RP_StationPara_160_1 T_RP_StationPara_110_1 = new T_RP_StationPara_160_1()
                        {
                            scanbarcode = data1Table.qrcode,
                            state = data1Table.totaljudge == "OK" ? "1" : "2",
                            para1 = data1Table.lsh1value,
                            para1State = data1Table.lsh1judge == "OK" ? "1" : "2",
                            para2 = data1Table.lsh2value,
                            para2State = data1Table.lsh2judge == "OK" ? "1" : "2",
                            para3 = data1Table.lsh3value,
                            para3State = data1Table.lsh3judge == "OK" ? "1" : "2",
                            para4 = data1Table.lsh4value,
                            para4State = data1Table.lsh4judge == "OK" ? "1" : "2",
                            para5 = data1Table.lsh5value,
                            para5State = data1Table.lsh5judge == "OK" ? "1" : "2",
                            para6 = data1Table.hrangevalue,
                            para6State = data1Table.hrangejudge == "OK" ? "1" : "2",
                            inserttime = data1Table.time,
                            uploadflag = "0",
                            finalflag = "1",
                        };
                        bool insertflag = false;
                        var GetFlag = await _tr_stationPara_160_1Service.GetResultAsync(data1Table.qrcode);
                        if (GetFlag != null)
                        {
                            T_RP_StationPara_110_1.id = GetFlag.id;
                            insertflag = _tr_stationPara_160_1Service.Update(T_RP_StationPara_110_1);
                        }
                        else
                        {
                            insertflag = _tr_stationPara_160_1Service.Insert(T_RP_StationPara_110_1);
                        }
                        if (insertflag)
                        {
                            data1Table.flag = "F";
                            var iflag = _data4TableService.UpdateAsync(data1Table).Result;
                            if (iflag)
                            {
                                _logger.LogInformation(StringChange.ModeToJson(data1Table));
                            }
                        }


                    }

                }
            }
            catch (Exception ex)
            {
                _logger.LogError($"异常:{ex.Message}");
            }

        }

    }
}