using Edu.Model.ViewModel.Sell;
using Edu.Repository.Sell;
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using VT.FW.DB;
using Edu.Common.Enum;
using Edu.Repository.User;
using Edu.Model.ViewModel.User;
using Edu.Repository.Course;
using Edu.Model.ViewModel.Course;

namespace Edu.Module.Course
{
    /// <summary>
    /// 获取课程顾问、市场达标奖金配置处理类
    /// </summary>
    public class BonusConfigModule
    {
        /// <summary>
        /// 获取课程顾问、市场达标奖金配置仓储层
        /// </summary>
        private readonly RB_Bonus_PlanRepository bonus_PlanRepository = new RB_Bonus_PlanRepository();

        /// <summary>
        /// 获取课程顾问、市场达标奖金配置阶梯仓储层
        /// </summary>
        private readonly RB_Bouns_PlanDetailRepository bouns_PlanDetailRepository = new RB_Bouns_PlanDetailRepository();

        /// <summary>
        /// 员工账号仓储层对象
        /// </summary>
        private readonly RB_AccountRepository accountRepository = new RB_AccountRepository();

        /// <summary>
        /// 课程仓储层对象
        /// </summary>
        private readonly RB_CourseRepository courseRepository = new RB_CourseRepository();

        /// <summary>
        ///  获取课程顾问、市场达标奖金配置分页列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="rowsCount"></param>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Bonus_PlanExtend> GetBounsPlanPageModule(int pageIndex, int pageSize, out long rowsCount, RB_Bonus_PlanExtend query)
        {
            var list = bonus_PlanRepository.GetBounsPlanPageRepository(pageIndex, pageSize, out rowsCount, query);
            if (list != null && list.Count>0)
            {
                string Ids = string.Join(",", list.Select(qitem => qitem.Id));
                var detailsList = bouns_PlanDetailRepository.GetBounsPlanDetailListRepository(new RB_Bouns_PlanDetail_Extend()
                {
                    QPlanIds = Ids
                });
                //适用人员
                string saleIds = string.Join(",", list.Where(qitem=>!string.IsNullOrEmpty(qitem.SaleIds)).Select(qitem => qitem.SaleIds));
                List<Employee_ViewModel> empList = new List<Employee_ViewModel>();
                if (!string.IsNullOrEmpty(saleIds))
                {
                    empList = accountRepository.GetEmployeeListRepository(new Employee_ViewModel()
                    {
                        QIds = saleIds
                    });
                }

                List<int> courseIdList = new List<int>();
                var useCourseList = Common.ConvertHelper.StringToList(string.Join(",", list.Where(qitem => !string.IsNullOrEmpty(qitem.UseCourseIds)).Select(qitem => qitem.UseCourseIds)));
                if (useCourseList != null)
                {
                    courseIdList.AddRange(useCourseList);
                }
                var notUseCourseList= Common.ConvertHelper.StringToList(string.Join(",", list.Where(qitem => !string.IsNullOrEmpty(qitem.NotUseCourseIds)).Select(qitem => qitem.NotUseCourseIds)));
                if (notUseCourseList != null)
                {
                    courseIdList.AddRange(notUseCourseList);
                }
                List<RB_Course_ViewModel> courseList = new List<RB_Course_ViewModel>();
                if (courseIdList != null && courseIdList.Count > 0)
                {
                    courseList= courseRepository.GetCourseListRepository(new RB_Course_ViewModel()
                    {
                        QCourseIds = string.Join(",", courseIdList)
                    });
                }
                foreach (var item in list)
                {
                    item.SaleList = new List<SaleManItem>();
                    item.UseCourseList = new List<CourseItem>();
                    item.NotUseCourseList = new List<CourseItem>();
                    var saleList = Common.ConvertHelper.StringToList(item.SaleIds);
                    foreach (var sItem in saleList)
                    {
                        var tempEmp = empList?.FirstOrDefault(qitem => qitem.Id == sItem);
                        if (tempEmp != null)
                        {
                            item.SaleList.Add(new SaleManItem()
                            {
                                SaleId = tempEmp.Id,
                                SaleManName = tempEmp.EmployeeName
                            });
                        }
                    }

                    var useList= Common.ConvertHelper.StringToList(item.UseCourseIds);
                    foreach (var cItem in useList)
                    {
                        var tempCourse = courseList?.FirstOrDefault(qitem => qitem.CourseId == cItem);
                        if (tempCourse != null)
                        {
                            item.UseCourseList.Add(new CourseItem()
                            {
                                CourseId = tempCourse.CourseId,
                                CourseName = tempCourse.CourseName
                            });
                        }
                    }

                    var notuseList = Common.ConvertHelper.StringToList(item.NotUseCourseIds);
                    foreach (var cItem in notuseList)
                    {
                        var tempCourse = courseList?.FirstOrDefault(qitem => qitem.CourseId == cItem);
                        if (tempCourse != null)
                        {
                            item.NotUseCourseList.Add(new CourseItem()
                            {
                                CourseId = tempCourse.CourseId,
                                CourseName = tempCourse.CourseName
                            });
                        }
                    }
                    item.DetailList = detailsList?.Where(qitem => qitem.PlanId == item.Id)?.ToList();
                }
            }
            return list;
        }

        /// <summary>
        /// 新增修改课程顾问、市场达标奖金配置
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool SetBonusPlanModule(RB_Bonus_PlanExtend model)
        {
            bool flag = false;
            if (model.Id > 0)
            {
                Dictionary<string, object> fileds = new Dictionary<string, object>()
                {
                    {nameof(RB_Bonus_PlanExtend.PlanType),model.PlanType },
                    {nameof(RB_Bonus_PlanExtend.PlanName),model.PlanName },
                    {nameof(RB_Bonus_PlanExtend.BasicSalary),model.BasicSalary },
                    {nameof(RB_Bonus_PlanExtend.OpenBonus),model.OpenBonus },
                    {nameof(RB_Bonus_PlanExtend.WorkAgeSalary),model.WorkAgeSalary },
                    {nameof(RB_Bonus_PlanExtend.ClueNumSalary),model.ClueNumSalary },
                    {nameof(RB_Bonus_PlanExtend.SaleIds),model.SaleIds },
                    {nameof(RB_Bonus_PlanExtend.UseCourseIds),model.UseCourseIds },
                    {nameof(RB_Bonus_PlanExtend.NotUseCourseIds),model.NotUseCourseIds },
                    {nameof(RB_Bonus_PlanExtend.UpdateBy),model.UpdateBy },
                    {nameof(RB_Bonus_PlanExtend.UpdateTime),model.UpdateTime },
                    {nameof(RB_Bonus_PlanExtend.School_Id),model.School_Id },
                    {nameof(RB_Bonus_PlanExtend.MonthGoalMoney),model.MonthGoalMoney },
                };
                flag = bonus_PlanRepository.Update(fileds, new WhereHelper(nameof(RB_Bonus_PlanExtend.Id), model.Id));
            }
            else
            {
                var newId = bonus_PlanRepository.Insert(model);
                model.Id = newId;
                flag = newId > 0;
            }
            var oldList = bouns_PlanDetailRepository.GetBounsPlanDetailListRepository(new RB_Bouns_PlanDetail_Extend()
            {
                PlanId = model.Id
            });
           
            //以前没有阶梯人员信息【直接新增】
            if (oldList == null || (oldList != null && oldList.Count == 0))
            {
                if (model.DetailList != null && model.DetailList.Count > 0)
                {
                    foreach (var item in model.DetailList)
                    {
                        item.Status = DateStateEnum.Normal;
                        item.DetailId = 0;
                        item.PlanId = model.Id;
                        bouns_PlanDetailRepository.Insert(item);
                    }
                }
            }
            else//以前有阶梯人员信息
            {
                //现在没有阶梯人员信息了【直接删除以前的阶梯人员】
                if (model.DetailList == null || (model.DetailList != null && model.DetailList.Count == 0))
                {
                    bouns_PlanDetailRepository.RemoveBounsPlanDetailByPlanIdRepository(model.Id);
                }
                //找出差异的数据
                var deleteList = oldList.Where(qitem => !model.DetailList.Any(oldItem => qitem.DetailId == oldItem.DetailId)).ToList();
                foreach (var dItem in deleteList)
                {
                    if (dItem.DetailId > 0)
                    {
                        bouns_PlanDetailRepository.RemoveBounsPlanDetailByPlanIdRepository(dItem.PlanId, dItem.DetailId);
                    }
                }
                foreach (var priceItem in model.DetailList)
                {
                    priceItem.PlanId = model.Id;
                    if (priceItem.DetailId == 0)
                    {
                        bouns_PlanDetailRepository.Insert(priceItem);
                    }
                    else
                    {
                        bouns_PlanDetailRepository.Update(priceItem);
                    }
                }
            }
            return flag;
        }

        /// <summary>
        /// 根据编号获取课程顾问、市场达标奖金配置
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public object GetBonusPlanModule(int Id)
        {
            var extModel = bonus_PlanRepository.GetEntity<RB_Bonus_PlanExtend>(Id);
            if (extModel != null)
            {
                extModel.DetailList = bouns_PlanDetailRepository.GetBounsPlanDetailListRepository(new RB_Bouns_PlanDetail_Extend()
                {
                    PlanId = extModel.Id
                });
            }
            var obj = new
            {
                extModel?.Id,
                extModel?.PlanType,
                extModel?.PlanName,
                extModel?.BasicSalary,
                extModel?.OpenBonus,
                extModel?.WorkAgeSalary,
                extModel?.ClueNumSalary,
                extModel?.SaleIds,
                SaleIdList = Common.ConvertHelper.StringToList(extModel?.SaleIds),
                extModel?.UseCourseIds,
                UseCourseIdList = Common.ConvertHelper.StringToList(extModel?.UseCourseIds),
                NotUseCourseIdList = Common.ConvertHelper.StringToList(extModel?.NotUseCourseIds),
                DetailList = extModel?.DetailList ?? new List<RB_Bouns_PlanDetail_Extend>(),
                extModel?.MonthGoalMoney,
            };
            return obj;
        }

        /// <summary>
        /// 根据编号删除课程顾问、市场达标奖金配置
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public bool RemoveBonusPlanModule(int Id)
        {
            Dictionary<string, object> fileds = new Dictionary<string, object>()
            {
                {nameof(RB_Bonus_PlanExtend.Status),(int)DateStateEnum.Delete }
            };
            bool flag = bonus_PlanRepository.Update(fileds, new WhereHelper(nameof(RB_Bonus_PlanExtend.Id), Id));
            return flag;
        }
    }
}
