using OASystem.Domain.Entities.PersonnelModule;
using OASystem.Infrastructure.Repositories.PersonnelModule;
using ILogger = Microsoft.Extensions.Logging.ILogger;
namespace OASystem.API.OAMethodLib.Quartz.Business
{
    /// 
    /// 任务指派
    /// 定时任务
    /// 
    public static class TaskAssignment
    {
        private readonly static TaskAllocationRepository _taskAllocationRep = AutofacIocManager.Instance.GetService();
        //private readonly static SqlSugarClient _sqlSugar = AutofacIocManager.Instance.GetService();
        private readonly static ILogger _logger;
        /// 
        /// 定时任务更改状态
        /// 每天下午六点定时更新
        /// 
        public static async void PostTaskUpdateStatus()
        {
            //_taskAllocationRep.ChangeDataBase(DBEnum.OA2023DB);
            var data = _taskAllocationRep._sqlSugar
                                         .Queryable()
                                         .Where(it => it.IsDel == 0 &&
                                                      !string.IsNullOrEmpty(it.PredictEndTime) &&
                                                      Convert.ToDateTime(it.PredictEndTime) >= DateTime.Now
                                               )
                                         .ToList();
            if (data.Count > 0)
            {
                //处理要变更状态的员工
                List primaryIds = new List();
                primaryIds = data.Select(it => it.Id).ToList();
                var subData = _taskAllocationRep._sqlSugar
                                                .Queryable()
                                                .Where(it => 
                                                             it.IsDel == 0 &&
                                                             primaryIds.Contains(it.TAId) &&
                                                             it.TaskStatus <= TaskerEnum.UnderWay
                                                      )
                                                .ToList();
               
                if (subData.Count > 0)
                {
                    foreach (var item in subData)
                    {
                        item.OverTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                        item.TaskStatus = TaskerEnum.UnFinished;
                        item.Cause = string.Format(@"任务没有提交完成或者超时,由系统设置任务未完成");
                    }
                    var updateStatus = _taskAllocationRep._sqlSugar
                                                         .Updateable(subData)
                                                         .WhereColumns(it => it.Id)
                                                         .ExecuteCommand();
                    //_taskAllocationRep._sqlSugar.Close();
                }
            }
        }
    }
}