using Edu.AOP.CustomerAttribute;
using Edu.Common.Enum.Sale;
using Edu.Model.CacheModel;
using Edu.Model.ViewModel.StudyAbroad;
using Edu.Repository.Course;
using Edu.Repository.StudyAbroad;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VT.FW.DB;

namespace Edu.Module.StudyAbroad
{
    /// <summary>
    /// 留学就业处理类
    /// </summary>
    public class StudyAbroadModule
    {
        /// <summary>
        /// 留学就业仓储层对象
        /// </summary>
        private readonly RB_StudyAbroadRepository studyAbroadRepository = new RB_StudyAbroadRepository();

        /// <summary>
        /// 留学就业优惠信息仓储层对象
        /// </summary>
        private readonly RB_StudyaBroad_PreferentialRepository studyaBroad_PreferentialRepository = new RB_StudyaBroad_PreferentialRepository();

        /// <summary>
        /// 订单仓储层对象
        /// </summary>
        private readonly RB_OrderRepository orderRepository = new RB_OrderRepository();

        /// <summary>
        /// 获取留学就业分页列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="rowsCount"></param>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_StudyAbroad_ViewModel> GetStudyAbroadPageModule(int pageIndex, int pageSize, out long rowsCount, RB_StudyAbroad_ViewModel query)
        {
            var list= studyAbroadRepository.GetStudyAbroadPageRepository(pageIndex, pageSize, out rowsCount, query);
            if (list != null && list.Count > 0)
            {
                string ids = string.Join(",", list.Select(qitem => qitem.Id));
                List<RB_StudyaBroad_Preferential_ViewModel> preferentialList = new List<RB_StudyaBroad_Preferential_ViewModel>();
                if (query.IsQPrice == 1)
                {
                    preferentialList= GetStudyaBroadPreferentialListModule(new RB_StudyaBroad_Preferential_ViewModel()
                    {
                        QStudyabroadIds = ids
                    });
                }
                foreach (var item in list)
                {
                    item.PreferentialList = preferentialList?.Where(qitem => qitem.StudyabroadId == item.Id)?.ToList() ?? new List<RB_StudyaBroad_Preferential_ViewModel>();
                }
            }
            return list;
        }

        /// <summary>
        /// 获取留学就业审核分页列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="rowsCount"></param>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_StudyAbroad_ViewModel> GetStudyAbroadAuditPageModule(int pageIndex, int pageSize, out long rowsCount, RB_StudyAbroad_ViewModel query)
        {
            var list = studyAbroadRepository.GetStudyAbroadAuditPageRepository(pageIndex, pageSize, out rowsCount, query);
            if (list != null && list.Count > 0)
            {
                string ids = string.Join(",", list.Select(qitem => qitem.Id));
                List<RB_StudyaBroad_Preferential_ViewModel> preferentialList = new List<RB_StudyaBroad_Preferential_ViewModel>();
                if (query.IsQPrice == 1)
                {
                    preferentialList = GetStudyaBroadPreferentialListModule(new RB_StudyaBroad_Preferential_ViewModel()
                    {
                        QStudyabroadIds = ids
                    });
                }
                foreach (var item in list)
                {
                    item.PreferentialList = preferentialList?.Where(qitem => qitem.StudyabroadId == item.Id)?.ToList() ?? new List<RB_StudyaBroad_Preferential_ViewModel>();
                }
            }
            return list;
        }


        /// <summary>
        /// 获取留学就业列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_StudyAbroad_ViewModel> GetStudyAbroadListModule(RB_StudyAbroad_ViewModel query)
        {
            var list= studyAbroadRepository.GetStudyAbroadListRepository(query);
            if (list != null && list.Count > 0)
            {
                string ids = string.Join(",", list.Select(qitem => qitem.Id));
                List<RB_StudyaBroad_Preferential_ViewModel> preferentialList = new List<RB_StudyaBroad_Preferential_ViewModel>();
                if (query.IsQPrice == 1)
                {
                    preferentialList = GetStudyaBroadPreferentialListModule(new RB_StudyaBroad_Preferential_ViewModel()
                    {
                        QStudyabroadIds = ids
                    });
                }
                foreach (var item in list)
                {
                    item.PreferentialList = preferentialList?.Where(qitem => qitem.StudyabroadId == item.Id)?.ToList() ?? new List<RB_StudyaBroad_Preferential_ViewModel>();
                }
            }
            return list;
        }

        /// <summary>
        /// 获取留学就业优惠列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_StudyaBroad_Preferential_ViewModel> GetStudyaBroadPreferentialListModule(RB_StudyaBroad_Preferential_ViewModel query)
        {
            return studyaBroad_PreferentialRepository.GetStudyaBroadPreferentialListRepostory(query);
        }


        /// <summary>
        /// 新增修改留学就业产品
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool SetStudyAbroadModule(RB_StudyAbroad_ViewModel model)
        {
            bool flag = false;
            if (model.Id > 0)
            {
                Dictionary<string, object> fileds = new Dictionary<string, object>()
                {
                    { nameof(RB_StudyAbroad_ViewModel.Type),model.Type},
                    { nameof(RB_StudyAbroad_ViewModel.Name),model.Name.Trim()},
                    { nameof(RB_StudyAbroad_ViewModel.SupplierId),model.SupplierId},
                    { nameof(RB_StudyAbroad_ViewModel.SupplierContract),model.SupplierContract},
                    { nameof(RB_StudyAbroad_ViewModel.Remark),model.Remark},
                    { nameof(RB_StudyAbroad_ViewModel.School_Id),model.School_Id},
                    { nameof(RB_StudyAbroad_ViewModel.ImgCover),model.ImgCover},
                };
                flag = studyAbroadRepository.Update(fileds, new WhereHelper(nameof(RB_StudyAbroad_ViewModel.Id), model.Id));
            }
            else
            {
                var newId = studyAbroadRepository.Insert(model);
                model.Id = newId;
                flag = newId > 0;
            }
            return flag;
        }

   
        /// <summary>
        /// 批量新增修改留学就业优惠信息
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        [TransactionCallHandler]
        public virtual bool SetStudyAbroadPreferentialListModule(RB_StudyAbroad_ViewModel model, List<RB_StudyaBroad_Preferential_ViewModel> list)
        {
            bool flag = false;
            if (model.Id > 0)
            {
                Dictionary<string, object> fileds = new Dictionary<string, object>()
                {
                    {nameof(RB_StudyAbroad_ViewModel.SuggestPrice),model.SuggestPrice },
                    {nameof(RB_StudyAbroad_ViewModel.SellPrice),model.SellPrice },
                };
                flag = studyAbroadRepository.Update(fileds, new WhereHelper(nameof(RB_StudyAbroad_ViewModel.Id), model.Id));
            }
            var oldList = GetStudyaBroadPreferentialListModule(new RB_StudyaBroad_Preferential_ViewModel() { StudyabroadId=model.Id });
            //原来没有优惠数据
            if (oldList == null || (oldList != null && oldList.Count == 0))
            {
                foreach (var item in list)
                {
                    flag = studyaBroad_PreferentialRepository.Insert(item) > 0;
                }
            }
            //原来有优惠数据
            else
            {
                if (list == null || (list != null && list.Count == 0))
                {
                    flag = studyaBroad_PreferentialRepository.DeleteStudyaBroadPreferentialRepostory(model.Id);
                }
                else
                {
                    //找出差异的数据
                    var deleteList = oldList.Where(qitem => !list.Any(oldItem => qitem.Id == oldItem.Id)).ToList();
                    foreach (var dItem in deleteList)
                    {
                        if (dItem.Id > 0)
                        {
                            flag = studyaBroad_PreferentialRepository.Delete(dItem.Id) > 0;
                        }
                    }
                    foreach (var priceItem in list)
                    {
                        if (priceItem.Id == 0)
                        {
                            flag = studyaBroad_PreferentialRepository.Insert(priceItem) > 0;
                        }
                        else
                        {
                            flag = studyaBroad_PreferentialRepository.Update(priceItem);
                        }
                    }
                }
            }
            return flag;
        }

        /// <summary>
        /// 根据编号获取留学就业产品信息
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public RB_StudyAbroad_ViewModel GetStudyAbroadModule(int Id,int IsGetPrice=0)
        {
            var model = studyAbroadRepository.GetStudyAbroadListExtRepository(new RB_StudyAbroad_ViewModel()
            {
                Id = Id,
                School_Id = -1
            })?.FirstOrDefault();
            if (model != null && IsGetPrice == 1)
            {
                model.PreferentialList = GetStudyaBroadPreferentialListModule(new RB_StudyaBroad_Preferential_ViewModel()
                {
                    StudyabroadId = model.Id
                });
            }
            return model;
        }

        /// <summary>
        /// 更新留学就业产品状态
        /// </summary>
        /// <param name="Id">供应商编号</param>
        /// <param name="Status">状态(0-正常，1-删除)</param>
        /// <returns></returns>
        public bool RemoveStudyAbroadModule(int Id, int Status,out string message)
        {
            message = "";
            var orderList = orderRepository.GetStudyOrderPageListRepository(1, 1, out _, new Model.ViewModel.Course.RB_Order_ViewModel()
            {
                SourceId = Id,
                Q_OrderState = 1
            });
            if (orderList != null && orderList.Count > 0)
            {
                message = "此产品下存在正常订单!不能删除!";
                return false;
            }
            Dictionary<string, object> fileds = new Dictionary<string, object>()
            {
                { nameof(RB_StudyAbroad_ViewModel.Status),Status},
            };
            bool flag = studyAbroadRepository.Update(fileds, new WhereHelper(nameof(RB_StudyAbroad_ViewModel.Id), Id));
            return flag;
        }

        /// <summary>
        /// 更新留学就业产品提交审核
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public bool SetSaleStateModule(int Id,int SaleState)
        {
            Dictionary<string, object> fileds = new Dictionary<string, object>()
            {
                { nameof(RB_StudyAbroad_ViewModel.SaleState),SaleState},
            };
            if (SaleState == 2)
            {
                fileds.Add(nameof(RB_StudyAbroad_ViewModel.DirectorAuditTime), null);
                fileds.Add(nameof(RB_StudyAbroad_ViewModel.DirectorId), Common.Config.StudyAbroadDirectorId);
                fileds.Add(nameof(RB_StudyAbroad_ViewModel.DirectorOpinion), "");
                fileds.Add(nameof(RB_StudyAbroad_ViewModel.DirectorStatus), 0);
            }
            bool flag = studyAbroadRepository.Update(fileds, new WhereHelper(nameof(RB_StudyAbroad_ViewModel.Id), Id));
            return flag;
        }

        /// <summary>
        /// 部门主管审核
        /// </summary>
        /// <param name="model"></param>
        /// <param name="userInfo"></param>
        /// <returns></returns>
        [TransactionCallHandler]
        public virtual bool SetDirectorAuditModule(RB_StudyAbroad_ViewModel model, UserInfo userInfo)
        {
            Dictionary<string, object> fileds = new Dictionary<string, object>()
            {
                { nameof(RB_StudyAbroad_ViewModel.DirectorId),userInfo.Id},
                { nameof(RB_StudyAbroad_ViewModel.DirectorOpinion),model.DirectorOpinion},
                { nameof(RB_StudyAbroad_ViewModel.DirectorStatus),model.DirectorStatus},
                { nameof(RB_StudyAbroad_ViewModel.DirectorAuditTime),model.DirectorAuditTime},
            };
            //拒绝
            if (model.DirectorStatus == 2)
            {
                fileds.Add(nameof(RB_StudyAbroad_ViewModel.SaleState), (int)SaleStateEnum.Fail);
            }
            else if (model.DirectorStatus == 1)
            {
                fileds.Add(nameof(RB_StudyAbroad_ViewModel.ManagerId), Common.Config.StudyAbroadManagerId);
                fileds.Add(nameof(RB_StudyAbroad_ViewModel.ManagerOpinion), "");
                fileds.Add(nameof(RB_StudyAbroad_ViewModel.ManagerStatus),0 );
                fileds.Add(nameof(RB_StudyAbroad_ViewModel.ManagerAuditTime), null);
            }
            bool flag = studyAbroadRepository.Update(fileds, new WhereHelper(nameof(RB_StudyAbroad_ViewModel.Id), model.Id));
            return flag;
        }

        /// <summary>
        /// 部门负责人审核
        /// </summary>
        /// <param name="model"></param>
        /// <param name="userInfo"></param>
        /// <returns></returns>
        [TransactionCallHandler]
        public virtual bool SetManagerAuditModule(RB_StudyAbroad_ViewModel model, UserInfo userInfo)
        {
            Dictionary<string, object> fileds = new Dictionary<string, object>()
            {
                { nameof(RB_StudyAbroad_ViewModel.ManagerId),userInfo.Id},
                { nameof(RB_StudyAbroad_ViewModel.ManagerOpinion),model.ManagerOpinion},
                { nameof(RB_StudyAbroad_ViewModel.ManagerStatus),model.ManagerStatus},
                { nameof(RB_StudyAbroad_ViewModel.ManagerAuditTime),model.ManagerAuditTime},
            };
            //拒绝
            if (model.DirectorStatus == 2)
            {
                fileds.Add(nameof(RB_StudyAbroad_ViewModel.SaleState), (int)SaleStateEnum.Fail);
            }
            //通过
            else
            {
                fileds.Add(nameof(RB_StudyAbroad_ViewModel.SaleState), (int)SaleStateEnum.Sell);
            }
            bool flag = studyAbroadRepository.Update(fileds, new WhereHelper(nameof(RB_StudyAbroad_ViewModel.Id), model.Id));
            return flag;
        }
    }
}
