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 RecordStaffAttendanceServiceImpl : BaseServiceImpl<RecordStaffAttendance>,IRecordStaffAttendanceService
    {
        private readonly ILogger<RecordStaffAttendance> _logger;

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

        /// <summary>
        /// 获取员工上班打卡信息
        /// </summary>
        /// <param name="stationCode"></param>
        /// <param name="attendanceType"></param>
        /// <returns></returns>
        public List<RecordStaffAttendance> GetRecordStaffAttendances(string stationCode)
        {
            List<RecordStaffAttendance> 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;
        }

        public RecordStaffAttendance GetRecordStaffAttendanceByStaffId(string staffId,string stationCode)
        {
            RecordStaffAttendance recordStaffAttendances = _rep.AsQueryable().Where(x => x.StaffId == staffId)
                .Where(x => x.StationCode == stationCode)
                .OrderByDescending(x => x.CreateTime).First();
            if (recordStaffAttendances != null)
            {
                return recordStaffAttendances;
            }
            else
            {
                return null;
            }
        }

        public bool InsertRecordStaffAttendance(List<RecordStaffAttendance> records)
        {
            bool result = false;
            try
            {
                base._rep.AsTenant().BeginTran();
                result = base._rep.InsertRange(records);
                base._rep.AsTenant().CommitTran();
            }
            catch (Exception ex)
            {
                base._rep.AsTenant().RollbackTran();
                _logger.LogError($"员工打卡信息添加异常:{ex.Message}");
            }
            return result;
        }

        public RecordStaffAttendance GetLastestOnRecord()
        {
            RecordStaffAttendance recordStaffAttendances = _rep.AsQueryable().Where(x => x.AttendanceType == "0")
                .OrderByDescending(x => x.CreateTime).First();
            return recordStaffAttendances;
        }

        public RecordStaffAttendance GetLastestOffRecord()
        {
            RecordStaffAttendance recordStaffAttendances = _rep.AsQueryable().Where(x => x.AttendanceType == "1")
                .OrderByDescending(x => x.CreateTime).First();
            return recordStaffAttendances;
        }
    }
}
