using Microsoft.Extensions.Logging;
using SlnMesnac.Model.domain;
using SlnMesnac.Repository.service.@base;
using System;
using System.Collections.Generic;
using System.Text;

namespace SlnMesnac.Repository.service.Impl
{
    public class ProdPlanExecuteUserServiceImpl: BaseServiceImpl<ProdPlanExecuteUser>, ProdPlanExecuteUserService
    {
        private ILogger<ProdPlanExecuteUserServiceImpl> _logger;
        public ProdPlanExecuteUserServiceImpl(Repository<ProdPlanExecuteUser> repository, ILogger<ProdPlanExecuteUserServiceImpl> logger) : base(repository)
        {
            _logger = logger;
        }

        /// <summary>
        /// 根据计划号和批次号删除所有执行信息
        /// </summary>
        /// <param name="planCode"></param>
        /// <param name="batch"></param>
        /// <returns></returns>
        public bool DeleteByPlanCodeAndTheBatchNumber(string planCode, int batch)
        {
            bool result = false;
            try
            {
                List<ProdPlanExecuteUser> prodPlanExecuteUsers = _rep.AsQueryable().Where(x => x.PlanCode == planCode && x.BatchNumber == batch).ToList();
                foreach (var execute in prodPlanExecuteUsers)
                {
                    _rep.Delete(execute);
                }
                result = true;
            }
            catch (Exception ex)
            {
                _logger.LogError($"根据计划编号删指定锅数的数据异常{ex.Message}");
            }
            return result;
        }

        /// <summary>
        /// 设置员工下班时间
        /// </summary>
        public int SetExecuterOffWorkTime(string staffId,string dateTime)
        {
            int executers = 0;
            try
            {
                executers = _rep.Context.Updateable<ProdPlanExecuteUser>()
                        .SetColumns(x => x.OffWorkTime == dateTime)
                        .Where(x => x.OffWorkTime == null && x.StaffId == staffId)
                        .ExecuteCommand();
            }
            catch (Exception ex)
            {
                _logger.LogError($"设置员工下班时间异常:{ex.Message}");
            }
            return executers;
        }
    }
}
