using MediII.Adapter.ReceiveToScanModel;
using MediII.Adapter.Scanner;
using System;
using System.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using IL.Common;

namespace MediII.Adapter.BizComponent.UE
{
    public class HL7_TimeOut_Scanner : DBScaner
    {

        private int TaskRunTime = 5;

        public HL7_TimeOut_Scanner()
        {

            var tm = ConfigurationManager.AppSettings["TaskRunTime"];
            if (!int.TryParse(tm, out TaskRunTime))
            {
                TaskRunTime = 5;
            }

        }


        protected override long OnDo(DateTime dtEventTime, Action<LogModel> setLog)
        {
            if (setLog != null)
                setLog(new LogModel { Message = "开始一次数据库错误消息处理扫描 " });
            var sql = @" 
                        SELECT *   
                        FROM    HL7_Receive
                        WHERE   1 = 1 
                                AND ( TaskMsg LIKE '%Timeout 时间已到。%'
                                        OR TaskMsg LIKE '%此操作对该事务的状态无效%'
                                        OR TaskMsg LIKE '%请重新运行该事务。%' )
                                AND (ErrNum<4 or ErrNum is null) 
                                AND TaskStatus = 3
                                AND ReceivingTime > DATE_ADD(SYSDATE(),interval -{0} DAY)
                        ORDER BY SeqNo DESC  ";
            sql = string.Format(sql, TaskRunTime);//操作中数据
            try
            {
                var listRow = MySqlDataAccess.ExecuteSqlStringAccessor<HL7_ReceiveEntity>(ctx, sql).Take(100).ToList();
                if (listRow.Any())
                {
                    process(listRow, setLog); 
                }
            }
            catch (Exception ex)
            {
                if (setLog != null)
                    setLog(new LogModel { Message = "重置数据库错误消息发生错误", ErrorMsg ="sql:"+ sql+"\r\n"+ ex.Message + "\r\n" + ex.StackTrace });
            }
            return 1;
        }


        private void process(IEnumerable<HL7_ReceiveEntity> listRow, Action<LogModel> setLog)
        {
            listRow.ToList().ForEach(o =>
            {
                try
                {
                    if (setLog != null)
                        setLog(new LogModel { Message = "重置数据库错误消息:" + o.MsgContent, MessageID = o.MessageID });

                    var sql = "UPDATE HL7_Receive SET TaskStatus=1,StartTime=NULL,TaskIP=NULL,TaskMsg = null, TaskMac=NULL,ErrNum=ISNULL(ErrNum,0)+1 WHERE ID=?ID";
                    var cmd = MySqlDataAccess.GetSqlStringCommand(ctx, sql);
                    MySqlDataAccess.AddInParameter(ctx, cmd, "ID", DbType.String, o.ID);
                    MySqlDataAccess.ExecuteNonQuery(ctx, cmd);
                }
                catch (Exception ex)
                {
                    if (setLog != null)
                        setLog(new LogModel
                        {
                            Message = string.Format("重置数据库错误消息发生错误:HL7ReceiveID:{0}", o.ID),
                            ErrorMsg = ex.Message + "\r\n" + ex.StackTrace
                        });
                }

            });
        }



    }
}
