using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using ConsoleApp;
using KdbndpTypes;
using Microsoft.Extensions.DependencyInjection;
using SlnMesnac.Common;
using SlnMesnac.Model.domain;
using SlnMesnac.Model.dto;
using SlnMesnac.Repository.service;

namespace SlnMesnac.Business.business
{
    /// <summary>
    /// RFID打卡业务逻辑类
    /// </summary>
    public class RfidHandleBusniess
    {
        private IRecordStaffAttendanceService _recordStaffAttendanceService;
        private IRecordStaffCommuteService _recordStaffCommuteService;
        private IRecordStaffRealTimeService _recordStaffRealTimeService;
        private ProdPlanExecuteUserService _prodPlanExecuteUserService;
        private string currentTime;
        private string teamMembers = null;
        private List<string> members = new List<string>();
        public static string stationCode = "";
        public static List<string> stationList;
        public static string staffId = "";
        public static string theNewAmount = "";

        public RfidHandleBusniess(IRecordStaffAttendanceService recordStaffAttendanceService, 
            IRecordStaffCommuteService recordStaffCommuteService, 
            IRecordStaffRealTimeService recordStaffRealTimeService,
            ProdPlanExecuteUserService prodPlanExecuteUserService)
        {
            this._recordStaffAttendanceService = recordStaffAttendanceService;
            this._recordStaffCommuteService = recordStaffCommuteService;
            this._recordStaffRealTimeService = recordStaffRealTimeService;
            _prodPlanExecuteUserService = prodPlanExecuteUserService;
        }


        /// <summary>
        /// 处理并插入打卡记录
        /// </summary>
        /// <param name="staff"></param>
        /// <param name="isCheckOn"></param>
        /// <returns></returns>
        public List<RecordStaffAttendance> HandleAndInsertStaffAttendance(BaseStaffInfo staff,int isCheckOn)
        {
            RecordStaffAttendance recordStaffAttendance = new RecordStaffAttendance
            {
                StaffId = staff.StaffId,
                AttendanceType = isCheckOn.ToString(),
                TeamCode = staff.TeamCode,
                Remark = staff.Remark,
                CreateBy = staff.StaffName,
                CreateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
                StationCode = stationCode
            };
            _recordStaffAttendanceService.Insert(recordStaffAttendance);
            return _recordStaffAttendanceService.GetRecordStaffAttendances(stationCode);
        }

        /// <summary>
        /// 处理并插入打卡记录(强退)
        /// </summary>
        /// <param name="staff"></param>
        /// <param name="isCheckOn"></param>
        /// <returns></returns>
        public List<RecordStaffAttendance> HandleAndInsertRemove(BaseStaffInfo staff, int isCheckOn)
        {
            RecordStaffAttendance recordStaffAttendance = new RecordStaffAttendance
            {
                StaffId = staff.StaffId,
                AttendanceType = isCheckOn.ToString(),
                TeamCode = staff.TeamCode,
                Remark = staffId,
                CreateBy = staff.StaffName,
                CreateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
                StationCode = stationCode
            };
            _recordStaffAttendanceService.Insert(recordStaffAttendance);
            return _recordStaffAttendanceService.GetRecordStaffAttendances(stationCode);
        }

        /// <summary>
        /// 处理上下班记录
        /// </summary>
        public void HandleStaffCommute(BaseStaffInfo staff, int isCheckOn)
        {
            if(isCheckOn == 0)
            {
                RecordStaffCommute recordStaffCommute = new RecordStaffCommute
                {
                    StaffId = staff.StaffId,
                    TeamCode = staff.TeamCode,
                    //Classes = 
                    StartWorkTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
                    Remark = staff.Remark,
                    CreateBy = staff.StaffName,
                    CreateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
                };
                _recordStaffCommuteService.Insert(recordStaffCommute);
            }
            else
            {
                currentTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                //根据id和上班时间不超过九小时的查找对应记录
                RecordStaffCommute recordStaffCommute = _recordStaffCommuteService.GetStaffCommuteByIdAndDuration(staff.StaffId, currentTime);
                //更新这个记录的下班打卡时间和在班时长率
                recordStaffCommute.EndWorkTime = currentTime;
                recordStaffCommute.ClockingRatio = Convert.ToDecimal((DateTime.Parse(recordStaffCommute.EndWorkTime) - DateTime.Parse(recordStaffCommute.StartWorkTime)).TotalHours);
                _recordStaffCommuteService.Update(recordStaffCommute);
            }           
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="baseStaffInfo"></param>
        /// <returns></returns>
        public string SpliceTeamMember()
        {
            teamMembers = "";
            //RecordStaffAttendance recordStaffAttendance = _recordStaffAttendanceService.GetRecordStaffAttendanceByStaffId(baseStaffInfo.StaffId,stationCode);
            List<RecordStaffRealTime> recordStaffRealTimeList = _recordStaffRealTimeService.GetRecordStaffRealTime(stationCode);
            //string type = recordStaffAttendance.AttendanceType;
            //if (type == "0")//上班，添加员工名字转成字符串
            //{
            //    members.Add(baseStaffInfo.StaffName); // Add the staff member's name to the list
            //    teamMembers = string.Join(" // ", members); // Convert the list of names to a string separated by commas
            //}
            //else if (type == "1")//下班,删除下班员工的名字再转为字符串
            //{
            //    members.Remove(baseStaffInfo.StaffName); // Remove the staff member's name from the list
            //    teamMembers = string.Join(" // ", members); // Convert the updated list of names to a string separated by commas
            //}
            foreach(var item in recordStaffRealTimeList)
            {
                teamMembers += item.CreateBy + " / ";
            }
            return teamMembers;
        }

        /// <summary>
        /// 处理并插入实时打卡记录
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public List<RecordStaffRealTime> HandleAndInsertStaffRealTime(BaseStaffInfo?staff,int isCheckOn)
        {
            if (staff != null)
            {
                if (isCheckOn == 0)
                {
                    RecordStaffRealTime recordStaffRealTime = new RecordStaffRealTime
                    {
                        StaffId = staff.StaffId,
                        AttendanceType = isCheckOn.ToString(),
                        TeamCode = staff.TeamCode,
                        Remark = staff.Remark,
                        CreateBy = staff.StaffName,
                        CreateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
                        StationCode = stationCode
                    };
                    _recordStaffRealTimeService.Insert(recordStaffRealTime);
                }
                else if (isCheckOn == 1 || isCheckOn == 2)
                {
                    var realTime = _recordStaffRealTimeService.Query(recordStaffRealTime => recordStaffRealTime.StaffId == staff.StaffId
                    && recordStaffRealTime.StationCode == stationCode
                    && recordStaffRealTime.AttendanceType == "0").FirstOrDefault();
                    _recordStaffRealTimeService.Delete(realTime);
                    SetOffWorkTime(staff.StaffId, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                }
            }        
            return _recordStaffRealTimeService.GetRecordStaffRealTime(stationCode);
        }

        /// <summary>
        /// 设置员工下班时间
        /// </summary>
        public void SetOffWorkTime(string staffId, string dateTime)
        {
            _prodPlanExecuteUserService.SetExecuterOffWorkTime(staffId, dateTime);
        }
    }
}
