using Edu.AOP.CustomerAttribute;
using Edu.Common.Enum;
using Edu.Common.Enum.User;
using Edu.Model.CacheModel;
using Edu.Model.ViewModel.User;
using Edu.Repository.System;
using Edu.Repository.User;
using System;
using System.Collections.Generic;
using System.Linq;
using VT.FW.DB;

namespace Edu.Module.User
{
    /// <summary>
    /// 学生处理类
    /// </summary>
    public class StudentModule
    {
        /// <summary>
        /// 学生仓储层对象
        /// </summary>
        private readonly RB_StudentRepository studentRepository = new RB_StudentRepository();

        /// <summary>
        /// 地区仓储层对象
        /// </summary>
        private readonly RB_DestinationRepository destinationRepository = new RB_DestinationRepository();

        /// <summary>
        /// 账号处理类
        /// </summary>
        private readonly AccountModule accountModule = new AccountModule();

        /// <summary>
        /// 获取学生列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Student_ViewModel> GetStudentListModule(RB_Student_ViewModel query)
        {
            return studentRepository.GetStudentListRepository(query);
        }

        /// <summary>
        /// 获取学生分页列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="rowsCount"></param>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Student_ViewModel> GetStudentPageListModule(int pageIndex, int pageSize, out long rowsCount, RB_Student_ViewModel query)
        {
            var list = studentRepository.GetStudentPageListRepository(pageIndex, pageSize, out rowsCount, query);
            if (list != null && list.Count > 0)
            {
                var provideList = list.Where(qitem => qitem.ProviceId > 0).Select(qitem => qitem.ProviceId).ToList();
                var cityList = list.Where(qitem => qitem.CityId > 0).Select(qitem => qitem.CityId).ToList();
                var areaList = list.Where(qitem => qitem.AreaId > 0).Select(qitem => qitem.AreaId).ToList();
                List<int> ids = new List<int>();
                if (provideList != null && provideList.Count > 0)
                {
                    ids.AddRange(provideList);
                }
                if (cityList != null && cityList.Count > 0)
                {
                    ids.AddRange(cityList);
                }
                if (areaList != null && areaList.Count > 0)
                {
                    ids.AddRange(areaList);
                }
                string qIds = "";
                if (ids != null && ids.Count > 0)
                {
                    qIds = string.Join(",", ids);
                }
                else
                {
                    qIds = "0";
                }
                var destinationList = destinationRepository.GetDestinationListRepository(new Model.ViewModel.System.RB_Destination_ViewModel() { Ids = qIds });
                foreach (var item in list)
                {
                    item.AreaName = (destinationList?.FirstOrDefault(qitem => qitem.ID == item.ProviceId)?.Name ?? "")
                        + "/" + (destinationList?.FirstOrDefault(qitem => qitem.ID == item.CityId)?.Name ?? "")
                        + "/" + (destinationList?.FirstOrDefault(qitem => qitem.ID == item.AreaId)?.Name ?? "");
                }
            }
            return list;
        }

        /// <summary>
        /// 添加修改讲师
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool SetStudentModule(RB_Student_ViewModel model)
        {
            bool flag;
            if (model.StuId > 0)
            {
                Dictionary<string, object> fileds = new Dictionary<string, object>()
                {
                    {nameof(RB_Student_ViewModel.StuName),model.StuName.Trim() },
                    {nameof(RB_Student_ViewModel.StuTel),model.StuTel.Trim() },
                    {nameof(RB_Student_ViewModel.StuIcon),model.StuIcon.Trim() },
                    {nameof(RB_Student_ViewModel.StuSex),model.StuSex },
                    {nameof(RB_Student_ViewModel.StuBirth),model.StuBirth },
                    {nameof(RB_Student_ViewModel.ProviceId),model.ProviceId },
                    {nameof(RB_Student_ViewModel.CityId),model.CityId },
                    {nameof(RB_Student_ViewModel.AreaId),model.AreaId },
                };
                flag = studentRepository.Update(fileds, new WhereHelper(nameof(RB_Student_ViewModel.StuId), model.StuId));
            }
            else
            {
                var newId = studentRepository.Insert(model);
                model.StuId = newId;
                flag = newId > 0;
            }
            return flag;
        }

        /// <summary>
        /// 根据学生编号获取学生实体类
        /// </summary>
        /// <param name="StuId"></param>
        /// <returns></returns>
        public RB_Student_ViewModel GetStudentModule(int StuId)
        {
            return studentRepository.GetEntity<RB_Student_ViewModel>(StuId);
        }

        /// <summary>
        /// 更新学生状态
        /// </summary>
        /// <param name="StuId"></param>
        /// <param name="Status"></param>
        /// <returns></returns>
        public bool RemoveStudentModule(int StuId, int Status)
        {
            bool flag = false;
            var model = GetStudentModule(StuId);
            if (model != null && model.StuId > 0)
            {
                Dictionary<string, object> fileds = new Dictionary<string, object>()
                {
                    {nameof(RB_Student_ViewModel.Status),  Status},
                };
                flag = studentRepository.Update(fileds, new WhereHelper(nameof(RB_Student_ViewModel.StuId), StuId));
                var accountList = accountModule.GetAccountListExtModule(new RB_Account_ViewModel()
                {
                    AccountId = model.StuId,
                    Account = model.StuTel,
                    AccountType = AccountTypeEnum.Student
                });
                if (accountList != null && accountList.Count > 0)
                {
                    flag = accountModule.SetAccountStatusModule(new RB_Account_ViewModel()
                    {
                        AccountType = AccountTypeEnum.Student,
                        AccountId = model.StuId,
                        UpdateTime = DateTime.Now,
                        Status = (DateStateEnum)Status
                    });
                }
            }
            return flag;
        }

        /// <summary>
        /// 更新学员【禁用、启用】
        /// </summary>
        /// <param name="StuId"></param>
        /// <param name="IsDisable">0-禁用，1-启用</param>
        /// <returns></returns>
        public bool UpdateStudentIsDisableModule(object StuId, int IsDisable)
        {
            bool flag;
            Dictionary<string, object> fileds = new Dictionary<string, object>()
            {
                {nameof(RB_Student_ViewModel.IsDisable),  IsDisable},
            };
            flag = studentRepository.Update(fileds, new WhereHelper(nameof(RB_Student_ViewModel.StuId), StuId));
            return flag;
        }

        /// <summary>
        /// 创建学生账号
        /// </summary>
        /// <param name="StuId"></param>
        /// <param name="UserInfo"></param>
        /// <returns></returns>
        [TransactionCallHandler]
        public virtual bool CreateStudentAccountModule(int StuId, UserInfo user)
        {
            bool flag = false;
            var model = GetStudentModule(StuId);
            if (model != null && model.StuId > 0)
            {
                var accountList = accountModule.GetAccountListExtModule(new RB_Account_ViewModel()
                {
                    Account = model.StuTel,
                    AccountType = AccountTypeEnum.Student
                });
                if (accountList == null || (accountList != null && accountList.Count == 0))
                {
                    flag = accountModule.SetAccountModule(new RB_Account_ViewModel()
                    {
                        Account = model.StuTel,
                        Password = Common.DES.Encrypt(Common.Config.DefaultPwd),
                        AccountType = AccountTypeEnum.Student,
                        AccountId = model.StuId,
                        CreateBy = user.Id,
                        UpdateBy = user.Id,
                        CreateTime = DateTime.Now,
                        UpdateTime = DateTime.Now,
                        Group_Id = model.Group_Id,
                        School_Id = model.School_Id,
                        Status = model.Status
                    });
                }
            }
            return flag;
        }
    }
}
