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

namespace SlnMesnac.Repository.service.Impl
{
    public class RecordStaffRealTimeServiceImpl : BaseServiceImpl<RecordStaffRealTime>, IRecordStaffRealTimeService
    {
        private readonly ILogger<RecordStaffRealTime> _logger;

        public RecordStaffRealTimeServiceImpl(Repository<RecordStaffRealTime> rep, ILogger<RecordStaffRealTime> logger) : base(rep)
        {
            _logger = logger;
        }

        /// <summary>
        /// 获取指定工位的实时打卡记录
        /// </summary>
        /// <param name="stationCode"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public List<RecordStaffRealTime> GetRecordStaffRealTime(string stationCode)
        {
            List<RecordStaffRealTime> records = null;
            try
            {
                records = base._rep.GetList(x => x.StationCode == stationCode && x.AttendanceType == "0");
                records = records.OrderByDescending(x => x.CreateTime).Take(20).ToList();
            }
            catch (Exception ex)
            {
                _logger.LogError($"获取员工实时打卡记录异常{ex.Message}");
            }
            return records;
        }

        /// <summary>
        /// 获取班长信息
        /// </summary>
        /// <returns></returns>
        public MonitorInfo GetMonitorInfo(string stationCode)
        {
            var monitorInfo = _rep.Context.Queryable<RecordStaffRealTime>()
                .LeftJoin<BaseStaffInfo>((srt, si) => srt.StaffId == si.StaffId)
                .Where((srt, si) => si.StaffType == "1" && srt.StationCode == stationCode)
                .Select((srt, si) => new MonitorInfo
                {
                    stationCode = srt.StationCode, 
                    staffId = si.StaffId, 
                    staffName = si.StaffName,
                    staffType = si.StaffType
                }).First();
            return monitorInfo;
        }
    }
}
