using System;
using System.Collections.Generic;
using System.Data;
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 IT_RP_StationPara_110_1Service _tr_stationPara_110_1Service;
        private IT_RP_StationPara_110_2Service _tr_stationPara_110_2Service;
        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)
        {
            _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;
        }
        public Task Execute(IJobExecutionContext context)
        {
            _logger.LogError($"任务执行:{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}");
            var flag = _appConfig.sqlConfig.Find(x => x.configId == "Local");
            if (flag.connStr.Contains("EData"))
            {
                LoadTaskInfo();
            }
            else if (flag.connStr.Contains("FData"))
            {
                LoadEDataInfo();
            }
            else if (flag.connStr.Contains("HData"))
            {

            }
            else if (flag.connStr.Contains("LSHData"))
            {

            }
            return Task.CompletedTask;
        }
        private async void LoadTaskInfo()
        {
            try
            {

                List<data1_Table> _data1Tables = _data1TableService.Getdata1TableListAsync().Result;
                if (_data1Tables != null && _data1Tables.Count > 0)
                {
                    List<T_RP_StationPara_110_1> newList = new List<T_RP_StationPara_110_1>();

                    foreach (var data1Table in _data1Tables)
                    {
                        newList.Add(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,
                            inserttime = DateTime.Parse(data1Table.time)
                        });
                        data1Table.flag = "F";
                        var iflag = _data1TableService.UpdateAsync(data1Table).Result;
                        if (iflag)
                        {
                            Console.WriteLine(StringChange.ModeToJson(data1Table));
                            _logger.LogDebug(StringChange.ModeToJson(data1Table));
                        }
                    }
                    if (newList != null && newList.Count != 0)
                    {
                        bool insertflag = await _tr_stationPara_110_1Service.AddTaskAsync(newList);
                    }
                }
            }
            catch (Exception ex)
            {
                _logger.LogError($"异常:{ex.Message}");
            }
        }

        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<data2_Table> _data1Tables = _data2TableService.Getdata2TableListAsync(start,end).Result;
                if (_data1Tables != null && _data1Tables.Count > 0)
                {
                    for (global::System.Int32 i = 0; i < _data1Tables.Count; i++)
                    {
                        T_RP_StationPara_110_2 t_RP_StationPara_110_2 = new T_RP_StationPara_110_2()
                        {
                            scanbarcode = _data1Tables[i].qrcode,
                            state = _data1Tables[i].totaljudge == "OK" ? "1" : "2",
                            para1 = _data1Tables[i].f1value,
                            para1State = _data1Tables[i].f1judge == "OK" ? "1" : "2",
                            para2 = _data1Tables[i].f2value,
                            para2State = _data1Tables[i].f2judge == "OK" ? "1" : "2",
                            para3 = _data1Tables[i].f3value,
                            para3State = _data1Tables[i].f3judge == "OK" ? "1" : "2",
                            para4 = _data1Tables[i].f4value,
                            para4State = _data1Tables[i].f4judge == "OK" ? "1" : "2",
                            para5 = _data1Tables[i].frangevalue,
                            para5State = _data1Tables[i].frange == "OK" ? "1" : "2",
                            inserttime = _data1Tables[i].time

                        };
                        bool insertflag = await _tr_stationPara_110_2Service.AddTaskAsync(t_RP_StationPara_110_2);

                        if (insertflag)
                        {
                            _data1Tables[i].flag = "F";
                            var iflag = _data2TableService.UpdateAsync(_data1Tables[i]).Result;
                            if (iflag)
                            {
                                Console.WriteLine("》》》更新前数量:" + _data1Tables.Count);
                                //_data1Tables.Remove(_data1Tables[i]);
                                if (_data1Tables.Count > 0)
                                {
                                    Console.WriteLine("》》》更新后数量:" + _data1Tables.Count);
                                }
                                Console.WriteLine(StringChange.ModeToJson(_data1Tables[i]));
                                _logger.LogDebug(StringChange.ModeToJson(_data1Tables[i]));
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                _logger.LogError($"异常:{ex.Message}");
            }
        }

    }
}