using Microsoft.Extensions.Logging;
using SlnMesnac.Model.domain;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static Dm.net.buffer.ByteArrayBuffer;

namespace SlnMesnac.Repository.service.Impl
{
    public class LogoIdentifyImpl : ILogoIdentifyService
    {
        private readonly ILogger<LogoIdentify> _logger;

        private readonly Repository<LogoIdentify> _rep;

        public LogoIdentifyImpl(ILogger<LogoIdentify> logger, Repository<LogoIdentify> rep)
        {
            _logger = logger;
            _rep = rep;
        }

       public async  Task<List<LogoIdentify>> GetAllRecordAsync()
        {

            //List<LogoIdentify> list = null;
            //list = await _rep.GetListAsync(x => x.RecordTime >= DateTime.Now.AddDays(-3));    
            //return list;

            List<LogoIdentify> list = null;
            list = await _rep.Context.Ado.SqlQueryAsync<LogoIdentify>("select  ID,PRODUCT_CODE,MATERIAL_TYPE,IS_CHECKED,RESULT,RECORD_TIME,MATERIAL_NAME,PRODUCT_LINE from LOGO_IDENTIFY where RECORD_TIME >= @RecordTime ", new { RecordTime = DateTime.Now.AddDays(-3) });
            return list;
        }


        /// <summary>
        /// 指定条码查询数据
        /// </summary>
        /// <returns></returns>
        public async Task<List<LogoIdentify>> GetRecordByCodeAsync(string code)
        {
            List<LogoIdentify> list = null;
            list = await _rep.Context.Ado.SqlQueryAsync<LogoIdentify>("select  ID,PRODUCT_CODE,MATERIAL_TYPE,IS_CHECKED,RESULT,RECORD_TIME,MATERIAL_NAME,PRODUCT_LINE from LOGO_IDENTIFY where PRODUCT_CODE = @Code",new {Code =code});
            return list;
        }

        /// <summary>
        /// 时间段条件查询
        /// </summary>
        /// <returns></returns>
        public async Task<List<LogoIdentify>> QueryAllByTime(DateTime time1, DateTime time2)
        {
            try
            {
                List<LogoIdentify> list = null;
              
               
               list = await _rep.Context.Ado.SqlQueryAsync<LogoIdentify>("select  ID,PRODUCT_CODE,MATERIAL_TYPE,IS_CHECKED,RESULT,RECORD_TIME,MATERIAL_NAME,PRODUCT_LINE from LOGO_IDENTIFY where RECORD_TIME >= @RecordTime1 AND RECORD_TIME <= @RecordTime2 ", new { RecordTime1 = time1, RecordTime2 = time2 });
  
                return list;

            }
            catch (Exception ex)
            {
                _logger.LogError("时间段条件查询QueryAllByTime()出现异常:" + ex);
                return null;
            }

        }



        public bool InsertRecord(LogoIdentify ocrRecord)
        {
            return _rep.Insert(ocrRecord);
            
        }


        /// <summary>
        /// 人工修正结果
        /// </summary>
        /// <returns></returns>
        public bool ChangeResult()
        {
            LogoIdentify logoIdentify;
            var  list =  _rep.Context.Ado.SqlQuery<LogoIdentify>("SELECT * FROM ( SELECT ID, PRODUCT_CODE, MATERIAL_TYPE, IS_CHECKED, RESULT, RECORD_TIME, MATERIAL_NAME, PRODUCT_LINE  FROM LOGO_IDENTIFY ORDER BY RECORD_TIME DESC) WHERE ROWNUM <= 1;");
            logoIdentify = list.FirstOrDefault();
            if (logoIdentify.Result == 0)
            { //仅修正ng的，防止误触
                return _rep.UpdateSetColumnsTrue(x => new LogoIdentify { Result = 3 }, x => x.Id == logoIdentify.Id);
            }
            return true;
           
        }


    }
}
