using Edu.Repository.Customer;
using Edu.Repository.Reserve;
using Edu.Repository.User;
using System;
using System.Collections.Generic;
using System.Linq;
using Edu.Model.ViewModel.Customer;
using Edu.Repository.Sell;
using Edu.Model.ViewModel.Sell;
using Edu.Repository.DataStatistics;
using Edu.Model.ViewModel.User;
using Edu.Model.ViewModel.DataStatistics;
using Edu.Model.ViewModel.Reserve;
using VT.FW.DB;
using Edu.Common.Enum;
using Edu.Repository.System;
using Edu.Model.ViewModel.System;
using Edu.Common.Enum.User;
using Edu.Common.Plugin;
using Newtonsoft.Json.Linq;
using System.Dynamic;

namespace Edu.Module.Customer
{
    /// <summary>
    /// 市场和课程顾问处理类对象
    /// </summary>
    public class MarketConsultantModule
    {
        /// <summary>
        /// 学生仓储层对象
        /// </summary>
        private readonly RB_StudentRepository studentRepository = new RB_StudentRepository();

        /// <summary>
        /// 学员跟进仓储层对象
        /// </summary>
        private readonly RB_Student_FollowRepository student_FollowRepository = new RB_Student_FollowRepository();

        /// <summary>
        /// 学员试听仓储层对象
        /// </summary>
        private readonly RB_Visitor_ReserveRepository visitor_ReserveRepository = new RB_Visitor_ReserveRepository();

        /// <summary>
        /// 学员到访仓储层对象
        /// </summary>
        private readonly RB_Student_VisitRepository student_VisitRepository = new RB_Student_VisitRepository();

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

        /// <summary>
        /// 员工资料管理仓储层对象
        /// </summary>
        private readonly RB_AccountRepository accountRepository = new RB_AccountRepository();

        /// <summary>
        /// 市场部数据统计仓储层对象
        /// </summary>
        private readonly RB_Market_DataRepository market_DataRepository = new RB_Market_DataRepository();

        /// <summary>
        /// 课程顾问部数据统计仓储层对象
        /// </summary>
        private readonly RB_Consultant_DataRepository consultant_DataRepository = new RB_Consultant_DataRepository();

        /// <summary>
        /// 课程顾问部目标仓储层对象
        /// </summary>
        private readonly RB_Consultant_GoalRepository consultant_GoalRepository = new RB_Consultant_GoalRepository();

        /// <summary>
        /// 部门仓储层对象
        /// </summary>
        private readonly RB_DepartmentRepository departmentRepository = new RB_DepartmentRepository();

        /// <summary>
        /// 渠道仓储层对象
        /// </summary>
        private readonly RB_ChannelRepository channelRepository = new RB_ChannelRepository();

        /// <summary>
        /// 同行列表
        /// </summary>
        private readonly RB_CustomerRepository customerRepository = new RB_CustomerRepository();

        /// <summary>
        /// 清除数据
        /// </summary>
        public void ClearData()
        {
            market_DataRepository.ClearMarketRepository();
            consultant_DataRepository.ClearConsultantRepository();
        }

        /// <summary>
        /// 生成市场部统计数据
        /// </summary>
        public void CreateMarketDataModule(DateTime StartTime)
        {
            var newTime = StartTime.AddDays(-1);
            string start = Common.ConvertHelper.FormatDate(newTime);

            var empList = accountRepository.GetEmployeeListRepository(new Employee_ViewModel()
            {
                //市场部编号4，课程顾问部编号13
                QDeptIds = "4",
                Group_Id = 100000,
                IsLeave = 1,
                UserRole= UserRoleEnum.MarketPersion,
            });
            if (empList != null && empList.Count > 0)
            {
                foreach (var item in empList)
                {
                    RB_Market_Data_Extend model = new RB_Market_Data_Extend()
                    {
                        CreateBy = item.Id,
                        Group_Id = item.Group_Id,
                        Date = newTime,
                        DeptId = item.Dept_Id,
                    };
                    //1.条数(资源量)
                    var dicList = studentRepository.GetStudentClueRepository(new RB_Student_ViewModel() { StartTime = start, CreateBy = item.Id });
                    if (dicList != null && dicList.Count > 0)
                    {
                        foreach (var sItem in dicList)
                        {
                            if (sItem.Key == "today")
                            {
                                model.ClueCount = sItem.Value?.Where(qitem => qitem.CreateBy == item.Id)?.FirstOrDefault()?.ClueCount ?? 0;
                            }
                            else if (sItem.Key == "yestoday")
                            {
                                model.YestodayClue = sItem.Value?.Where(qitem => qitem.CreateBy == item.Id)?.FirstOrDefault()?.ClueCount ?? 0;
                            }
                            else if (sItem.Key == "currentWeek")
                            {
                                model.CurWeekClue = sItem.Value?.Where(qitem => qitem.CreateBy == item.Id)?.FirstOrDefault()?.ClueCount ?? 0;
                            }
                            else if (sItem.Key == "lastWeek")
                            {
                                model.LastWeekClue = sItem.Value?.Where(qitem => qitem.CreateBy == item.Id)?.FirstOrDefault()?.ClueCount ?? 0;
                            }
                            else if (sItem.Key == "curMonth")
                            {
                                model.CurMonthClue = sItem.Value?.Where(qitem => qitem.CreateBy == item.Id)?.FirstOrDefault()?.ClueCount ?? 0;
                            }
                            else if (sItem.Key == "lastMonth")
                            {
                                model.LastMonthClue = sItem.Value?.Where(qitem => qitem.CreateBy == item.Id)?.FirstOrDefault()?.ClueCount ?? 0;
                            }
                        }
                    }

                    //2.有效数据量 (第一次跟进且不等于流失)
                    var followList = student_FollowRepository.GetStudentFollowStaticRepository(new RB_Student_Follow_Extend()
                    {
                        STime = start,
                        CreateBy = item.Id
                    });
                    if (followList != null && followList.Count > 0)
                    {
                        foreach (var sItem in followList)
                        {
                            if (sItem.Key == "today")
                            {
                                model.FollowCount = sItem.Value?.Where(qitem => qitem.CreateBy == item.Id)?.FirstOrDefault()?.FollowCount ?? 0;
                            }
                            else if (sItem.Key == "yestoday")
                            {
                                model.YestodayFollow = sItem.Value?.Where(qitem => qitem.CreateBy == item.Id)?.FirstOrDefault()?.FollowCount ?? 0;
                            }
                            else if (sItem.Key == "currentWeek")
                            {
                                model.CurWeekFollow = sItem.Value?.Where(qitem => qitem.CreateBy == item.Id)?.FirstOrDefault()?.FollowCount ?? 0;
                            }
                            else if (sItem.Key == "lastWeek")
                            {
                                model.LastWeekFollow = sItem.Value?.Where(qitem => qitem.CreateBy == item.Id)?.FirstOrDefault()?.FollowCount ?? 0;
                            }
                            else if (sItem.Key == "curMonth")
                            {
                                model.CurMonthFollow = sItem.Value?.Where(qitem => qitem.CreateBy == item.Id)?.FirstOrDefault()?.FollowCount ?? 0;
                            }
                            else if (sItem.Key == "lastMonth")
                            {
                                model.LastMonthFollow = sItem.Value?.Where(qitem => qitem.CreateBy == item.Id)?.FirstOrDefault()?.FollowCount ?? 0;
                            }
                        }
                    }

                    //3.上门咨询
                    var visitList = student_VisitRepository.GetVisitStaticRepository(new RB_Student_Visit_Extend()
                    {
                        StartDate = start,
                        CreateBy = item.Id,
                    });
                    if (visitList != null && visitList.Count > 0)
                    {
                        foreach (var sItem in visitList)
                        {
                            if (sItem.Key == "today")
                            {
                                model.VisitCount = sItem.Value?.Where(qitem => qitem.CreateBy == item.Id)?.FirstOrDefault()?.VisitCount ?? 0;
                            }
                            else if (sItem.Key == "yestoday")
                            {
                                model.YestodayVisit = sItem.Value?.Where(qitem => qitem.CreateBy == item.Id)?.FirstOrDefault()?.VisitCount ?? 0;
                            }
                            else if (sItem.Key == "currentWeek")
                            {
                                model.CurWeekVisit = sItem.Value?.Where(qitem => qitem.CreateBy == item.Id)?.FirstOrDefault()?.VisitCount ?? 0;
                            }
                            else if (sItem.Key == "lastWeek")
                            {
                                model.LastWeekVisit = sItem.Value?.Where(qitem => qitem.CreateBy == item.Id)?.FirstOrDefault()?.VisitCount ?? 0;
                            }
                            else if (sItem.Key == "curMonth")
                            {
                                model.CurMonthVisit = sItem.Value?.Where(qitem => qitem.CreateBy == item.Id)?.FirstOrDefault()?.VisitCount ?? 0;
                            }
                            else if (sItem.Key == "lastMonth")
                            {
                                model.LastMonthVisit = sItem.Value?.Where(qitem => qitem.CreateBy == item.Id)?.FirstOrDefault()?.VisitCount ?? 0;
                            }
                        }
                    }

                    //4.试听邀约
                    var trialLessonList = visitor_ReserveRepository.GetVisitorStaticRepository(new RB_Visitor_Reserve_Extend()
                    {
                        StartClassDate = start,
                        CreateBy = item.Id,
                    });
                    if (trialLessonList != null && trialLessonList.Count > 0)
                    {
                        foreach (var sItem in trialLessonList)
                        {
                            if (sItem.Key == "today")
                            {
                                model.TrialLessonCount = sItem.Value?.Where(qitem => qitem.CreateBy == item.Id)?.FirstOrDefault()?.TrialLessonCount ?? 0;
                            }
                            else if (sItem.Key == "yestoday")
                            {
                                model.YestodayTrialLesson = sItem.Value?.Where(qitem => qitem.CreateBy == item.Id)?.FirstOrDefault()?.TrialLessonCount ?? 0;
                            }
                            else if (sItem.Key == "currentWeek")
                            {
                                model.CurWeekTrialLesson = sItem.Value?.Where(qitem => qitem.CreateBy == item.Id)?.FirstOrDefault()?.TrialLessonCount ?? 0;
                            }
                            else if (sItem.Key == "lastWeek")
                            {
                                model.LastWeekTrialLesson = sItem.Value?.Where(qitem => qitem.CreateBy == item.Id)?.FirstOrDefault()?.TrialLessonCount ?? 0;
                            }
                            else if (sItem.Key == "curMonth")
                            {
                                model.CurMonthTrialLesson = sItem.Value?.Where(qitem => qitem.CreateBy == item.Id)?.FirstOrDefault()?.TrialLessonCount ?? 0;
                            }
                            else if (sItem.Key == "lastMonth")
                            {
                                model.LastMonthTrialLesson = sItem.Value?.Where(qitem => qitem.CreateBy == item.Id)?.FirstOrDefault()?.TrialLessonCount ?? 0;
                            }
                        }
                    }

                    //5.销售额(签约量)
                    var orderList = orderRepository.GetMarketConsultantStaticRepository(new RB_Order_ViewModel()
                    {
                        StartTime = start,
                        EnterID = item.Id
                    }, qType: 1);
                    if (orderList != null && orderList.Count > 0)
                    {
                        foreach (var sItem in orderList)
                        {
                            if (sItem.Key == "today")
                            {
                                model.SaleMoney = sItem.Value?.Where(qitem => (qitem.EnterID == item.Id || qitem.CourseConsultantId == item.Id))?.Sum(qitem => qitem.Income) ?? 0;
                            }
                            else if (sItem.Key == "yestoday")
                            {
                                model.YestodaySale = sItem.Value?.Where(qitem => (qitem.EnterID == item.Id || qitem.CourseConsultantId == item.Id))?.Sum(qitem => qitem.Income) ?? 0;
                            }
                            else if (sItem.Key == "currentWeek")
                            {
                                model.CurWeekSale = sItem.Value?.Where(qitem => (qitem.EnterID == item.Id || qitem.CourseConsultantId == item.Id))?.Sum(qitem => qitem.Income) ?? 0;
                            }
                            else if (sItem.Key == "lastWeek")
                            {
                                model.LastWeekSale = sItem.Value?.Where(qitem => (qitem.EnterID == item.Id || qitem.CourseConsultantId == item.Id))?.Sum(qitem => qitem.Income) ?? 0;
                            }
                            else if (sItem.Key == "curMonth")
                            {
                                model.CurMonrhSale = sItem.Value?.Where(qitem => (qitem.EnterID == item.Id || qitem.CourseConsultantId == item.Id))?.Sum(qitem => qitem.Income) ?? 0;
                            }
                            else if (sItem.Key == "lastMonth")
                            {
                                model.LastMonthSale = sItem.Value?.Where(qitem => (qitem.EnterID == item.Id || qitem.CourseConsultantId == item.Id))?.Sum(qitem => qitem.Income) ?? 0;
                            }
                        }
                    }
                    market_DataRepository.InsertMarketRepository(model);
                }
            }
        }

        /// <summary>
        /// 生成课程顾问部数据
        /// </summary>
        /// <param name="StartTime"></param>
        public void CreateConsultantDataModule(DateTime StartTime)
        {
            var newTime = StartTime.AddDays(-1);
            string start = Common.ConvertHelper.FormatDate(newTime);

            var empList = accountRepository.GetEmployeeListRepository(new Employee_ViewModel()
            {
                //市场部编号4，课程顾问部编号13
                QDeptIds = "13",
                Group_Id = 100000,
                IsLeave = 1,
                UserRole= UserRoleEnum.CourseConsultant,
            });
            if (empList != null && empList.Count > 0)
            {
                var consultantGoal = GetConsultantGoalListModule(new RB_Consultant_Goal_Extend() { Group_Id = 100000, Dept_Id = 13, YearStr = StartTime.Year, MonthStr = StartTime.Month })?.FirstOrDefault();
                foreach (var item in empList)
                {
                    RB_Consultant_Data_Extend model = new RB_Consultant_Data_Extend()
                    {
                        CreateBy = item.Id,
                        Group_Id = item.Group_Id,
                        Date = newTime,
                        DeptId = item.Dept_Id,
                    };

                    //1.试听邀约
                    var trialLessonList = visitor_ReserveRepository.GetVisitorStaticRepository(new RB_Visitor_Reserve_Extend()
                    {
                        StartClassDate = start,
                        CreateBy = item.Id
                    });
                    if (trialLessonList != null && trialLessonList.Count > 0)
                    {
                        foreach (var sItem in trialLessonList)
                        {
                            if (sItem.Key == "today")
                            {
                                model.TrialLessonCount = sItem.Value?.Where(qitem => qitem.CreateBy == item.Id)?.FirstOrDefault()?.TrialLessonCount ?? 0;
                            }
                            else if (sItem.Key == "yestoday")
                            {
                                model.YestodayTrialLesson = sItem.Value?.Where(qitem => qitem.CreateBy == item.Id)?.FirstOrDefault()?.TrialLessonCount ?? 0;
                            }
                            else if (sItem.Key == "currentWeek")
                            {
                                model.CurWeekTrialLesson = sItem.Value?.Where(qitem => qitem.CreateBy == item.Id)?.FirstOrDefault()?.TrialLessonCount ?? 0;
                            }
                            else if (sItem.Key == "lastWeek")
                            {
                                model.LastWeekTrialLesson = sItem.Value?.Where(qitem => qitem.CreateBy == item.Id)?.FirstOrDefault()?.TrialLessonCount ?? 0;
                            }
                            else if (sItem.Key == "curMonth")
                            {
                                model.CurMonthTrialLesson = sItem.Value?.Where(qitem => qitem.CreateBy == item.Id)?.FirstOrDefault()?.TrialLessonCount ?? 0;
                            }
                            else if (sItem.Key == "lastMonth")
                            {
                                model.LastMonthTrialLesson = sItem.Value?.Where(qitem => qitem.CreateBy == item.Id)?.FirstOrDefault()?.TrialLessonCount ?? 0;
                            }
                        }
                    }

                    //2.到访累计 和 3.转化率~~~市场收集的数据（学生信息和电话号码），邀约上门咨询后算有效单，，报名成交之后叫转化成功。这个转化率能看出课程顾问的能力或者市场给到的数据的有效性
                    var visitList = student_VisitRepository.GetVisitStaticRepository(new RB_Student_Visit_Extend()
                    {
                        StartDate = start,
                        CreateBy = item.Id,
                    });
                    if (visitList != null && visitList.Count > 0)
                    {
                        foreach (var sItem in visitList)
                        {
                            var visitData = sItem.Value?.Where(qitem => qitem.CreateBy == item.Id)?.FirstOrDefault();
                            var VisitCount = visitData?.VisitCount ?? 0;
                            var OrderCount = visitData?.OrderCount ?? 0;
                            decimal Rates = 0;
                            if (VisitCount > 0)
                            {
                                Rates = Math.Round(Convert.ToDecimal(OrderCount) / Convert.ToDecimal(VisitCount) * 100, 2);
                            }
                            if (sItem.Key == "today")
                            {
                                model.VisitCount = VisitCount;
                                model.Rates = Rates;
                            }
                            else if (sItem.Key == "yestoday")
                            {
                                model.YestodayVisit = VisitCount;
                                model.YestodayRates = Rates;
                            }
                            else if (sItem.Key == "currentWeek")
                            {
                                model.CurWeekVisit = VisitCount;
                                model.CurWeekRates = Rates;
                            }
                            else if (sItem.Key == "lastWeek")
                            {
                                model.LastWeekVisit = VisitCount;
                                model.LastWeekRates = Rates;
                            }
                            else if (sItem.Key == "curMonth")
                            {
                                model.CurMonthVisit = VisitCount;
                                model.CurMonthRates = Rates;
                            }
                            else if (sItem.Key == "lastMonth")
                            {
                                model.LastMonthVisit = VisitCount;
                                model.LastMonthRates = Rates;
                            }
                        }
                    }

                    //4.有效回访总量~~~(就是跟客户有实际联系上，不是未接) （有跟进记录）
                    var followList = student_FollowRepository.GetConsultantFollowStaticRepository(new RB_Student_Follow_Extend()
                    {
                        STime = start,
                        CreateBy = item.Id
                    });
                    if (followList != null && followList.Count > 0)
                    {
                        foreach (var sItem in followList)
                        {
                            if (sItem.Key == "today")
                            {
                                model.FollowCount = sItem.Value?.Where(qitem => qitem.CreateBy == item.Id)?.FirstOrDefault()?.FollowCount ?? 0;
                            }
                            else if (sItem.Key == "yestoday")
                            {
                                model.YestodayFollow = sItem.Value?.Where(qitem => qitem.CreateBy == item.Id)?.FirstOrDefault()?.FollowCount ?? 0;
                            }
                            else if (sItem.Key == "currentWeek")
                            {
                                model.CurWeekFollow = sItem.Value?.Where(qitem => qitem.CreateBy == item.Id)?.FirstOrDefault()?.FollowCount ?? 0;
                            }
                            else if (sItem.Key == "lastWeek")
                            {
                                model.LastWeekFollow = sItem.Value?.Where(qitem => qitem.CreateBy == item.Id)?.FirstOrDefault()?.FollowCount ?? 0;
                            }
                            else if (sItem.Key == "curMonth")
                            {
                                model.CurMonthFollow = sItem.Value?.Where(qitem => qitem.CreateBy == item.Id)?.FirstOrDefault()?.FollowCount ?? 0;
                            }
                            else if (sItem.Key == "lastMonth")
                            {
                                model.LastMonthFollow = sItem.Value?.Where(qitem => qitem.CreateBy == item.Id)?.FirstOrDefault()?.FollowCount ?? 0;
                            }
                        }
                    }

                    //5.单资源回访平均数~~~就是一条资源跟进了几次   【平均】
                    var avgFollowList = student_FollowRepository.GetConsultantAvgFollowStaticRepository(new RB_Student_Follow_Extend()
                    {
                        STime = start,
                        CreateBy = item.Id
                    });
                    if (avgFollowList != null && avgFollowList.Count > 0)
                    {
                        foreach (var sItem in avgFollowList)
                        {
                            if (sItem.Key == "today")
                            {
                                model.AgFollow = sItem.Value?.FirstOrDefault()?.FollowEffectiveCount ?? 0;
                            }
                            else if (sItem.Key == "yestoday")
                            {
                                model.YestodayAgFollow = sItem.Value?.FirstOrDefault()?.FollowEffectiveCount ?? 0;
                            }
                            else if (sItem.Key == "currentWeek")
                            {
                                model.CurWeekAgFollow = sItem.Value?.FirstOrDefault()?.FollowEffectiveCount ?? 0;
                            }
                            else if (sItem.Key == "lastWeek")
                            {
                                model.LastWeekAgFollow = sItem.Value?.FirstOrDefault()?.FollowEffectiveCount ?? 0;
                            }
                            else if (sItem.Key == "curMonth")
                            {
                                model.CurMonthAgFollow = sItem.Value?.FirstOrDefault()?.FollowEffectiveCount ?? 0;
                            }
                            else if (sItem.Key == "lastMonth")
                            {
                                model.LastMonthAgFollow = sItem.Value?.FirstOrDefault()?.FollowEffectiveCount ?? 0;
                            }
                        }
                    }

                    //5.销售额(签约量)
                    var orderList = orderRepository.GetMarketConsultantStaticRepository(new RB_Order_ViewModel()
                    {
                        StartTime = start,
                        CourseConsultantId = item.Id
                    }, qType: 2);
                    if (orderList != null && orderList.Count > 0)
                    {
                        foreach (var sItem in orderList)
                        {
                            var tempOrder = sItem.Value?.FirstOrDefault();
                            var orderCount = tempOrder?.OrderCount ?? 0;
                            var Income = tempOrder?.Income ?? 0;
                            if (sItem.Key == "currentWeek")
                            {
                                model.CurWeekOrderNum = orderCount;
                                model.CurWeekOrderSale = Income;
                            }
                            if (sItem.Key == "lastWeek")
                            {
                                model.LastWeekOrderNum = orderCount;
                                model.LastWeekOrderSale = Income;
                            }
                            if (sItem.Key == "curMonth")
                            {
                                model.CurMonthOrderNum = orderCount;
                                model.CurMonthOrderSale = Income;
                                model.CurMonthOrderGoal = consultantGoal?.GoalMoney ?? 0;
                            }
                            if (sItem.Key == "lastMonth")
                            {
                                model.LastMonthOrderNum = orderCount;
                                model.LastMonthOrderSale = Income;
                            }
                        }
                        consultant_DataRepository.InsertConsultantRepository(model);
                    }
                }
            }

        }

        /// <summary>
        /// 市场人员统计
        /// </summary>
        /// <returns></returns>
        public object MarketStaticModule(string EmpIds, string StartTime, string EndTime)
        {
            object obj = new object();
            DateTime start = Convert.ToDateTime(StartTime);
            DateTime end = Convert.ToDateTime(EndTime);
            var days = (end - start).Days;
            var lastList = market_DataRepository.GetMarketListRepository(new RB_Market_Data_Extend()
            {
                StartTime = StartTime,
                EndTime = EndTime,
                QCreateByIds = EmpIds
            });
            //线索列表
            var studentList = studentRepository.GetStudentStaticRepository(EmpIds, StartTime, EndTime);

            //试听列表
            var trialLessonList = visitor_ReserveRepository.GetVisitorReserveStaticRepository(new RB_Visitor_Reserve_Extend()
            {
                QCreateByIds = EmpIds,
                StartClassDate = StartTime,
                EndClassDate = EndTime
            });

            //到访列表
            var visitList = student_VisitRepository.GetStudentVisitStaticRepository(new RB_Student_Visit_Extend()
            {
                QCreateByIds = EmpIds,
                StartDate = StartTime,
                EndDate = EndTime
            });

            //订单列表
            var orderList = orderRepository.GetOrderListRepository(new RB_Order_ViewModel()
            {
                QEnterIds = EmpIds,
                StartTime = StartTime,
                EndTime = EndTime,
            });
            List<object> dayList = new List<object>();
            for (var i = 0; i <= days; i++)
            {
                DateTime newDate = start.AddDays(i);
                object tempObj = new object();
                if (Common.ConvertHelper.FormatDate(newDate) == Common.ConvertHelper.FormatDate(DateTime.Now))
                {
                    var tempStuList = studentList?.Where(qitem => Common.ConvertHelper.FormatMonthDay(qitem.CreateTime) == Common.ConvertHelper.FormatMonthDay(newDate))?.ToList();
                    var tempTrialLessonList = trialLessonList?.Where(qitem => Common.ConvertHelper.FormatMonthDay(qitem.CreateTime) == Common.ConvertHelper.FormatMonthDay(newDate))?.ToList();
                    var tempVisitList = visitList?.Where(qitem => Common.ConvertHelper.FormatMonthDay(qitem.CreateTime) == Common.ConvertHelper.FormatMonthDay(newDate))?.ToList();
                    tempObj = new
                    {
                        DayStr = Common.ConvertHelper.FormatMonthDay(newDate),
                        //新增线索条数
                        ClueCount = tempStuList?.FirstOrDefault()?.ClueCount ?? 0,
                        //试听人数
                        TrialLessonCount = tempTrialLessonList?.Sum(qitem => qitem.TrialLessonCount) ?? 0,
                        //到访人数
                        VisitCount = tempVisitList?.Sum(qitem => qitem.VisitCount) ?? 0,
                    };
                }
                else
                {
                    var tempLast = lastList?.Where(qitem => Common.ConvertHelper.FormatMonthDay(qitem.Date) == Common.ConvertHelper.FormatMonthDay(newDate))?.ToList();
                    tempObj = new
                    {
                        DayStr = Common.ConvertHelper.FormatMonthDay(newDate),
                        //新增线索条数
                        ClueCount = tempLast?.Sum(qitem => qitem.ClueCount) ?? 0,
                        //试听人数
                        TrialLessonCount = tempLast?.Sum(qitem => qitem.TrialLessonCount) ?? 0,
                        //到访人数
                        VisitCount = tempLast?.Sum(qitem => qitem.VisitCount) ?? 0,
                    };
                }
                dayList.Add(tempObj);
            }
            var yesDataList = lastList?.Where(qitem => qitem.Date.Year == end.AddDays(-1).Year && qitem.Date.Month == end.AddDays(-1).Month)?.ToList();

            //当前周
            var curWeekList = lastList?.Where(qitem => Common.ConvertHelper.WeekOfYear(qitem.Date) == Common.ConvertHelper.WeekOfYear(end.AddDays(-1)))?.ToList();
            //上一周
            var lastWeekList = lastList?.Where(qitem => Common.ConvertHelper.WeekOfYear(qitem.Date) == Common.ConvertHelper.WeekOfYear(end.AddDays(-1)) - 1)?.ToList();

            var tempDataList = lastList?.Where(qitem => Common.ConvertHelper.FormatDate(qitem.Date) == Common.ConvertHelper.FormatDate(end.AddDays(-1)))?.ToList();

            //渠道数据统计
            var dataList = studentRepository.GetStudentChannelStaticRepository(new RB_Student_ViewModel()
            {
                StartTime = StartTime,
                EndTime = EndTime,
                CreateIds = Common.ConvertHelper.StringToList(EmpIds)
            });


            //市场部个人数据
            List<MarketChannelStaticModel> barList = new List<MarketChannelStaticModel>();
            var channelList = channelRepository.GetChannelListRepository(new RB_Channel_Extend() { });
            var createTypeList = dataList.GroupBy(qitem => new { qitem.CreateType }).Select(qitem => new { qitem.Key.CreateType });
            foreach (var item in createTypeList)
            {
                var tempList = dataList?.Where(qitem => qitem.CreateType == item.CreateType)?.ToList();
                if (item.CreateType == StuCreateTypeEnum.EmployeeInput)
                {
                    foreach (var sItem in tempList?.GroupBy(qitem => new { qitem.StuChannel })?.Select(qitem => new { qitem.Key.StuChannel }))
                    {
                        barList.Add(new MarketChannelStaticModel()
                        {
                            ChannelId = sItem.StuChannel,
                            ChannelName = channelList?.FirstOrDefault(qitem => qitem.Id == sItem.StuChannel)?.Name ?? "",
                            ClueCount = tempList?.Where(qitem => qitem.StuChannel == sItem.StuChannel)?.Sum(qitem => qitem.ClueCount) ?? 0,
                            VisitCount = tempList?.Where(qitem => qitem.StuChannel == sItem.StuChannel)?.Sum(qitem => qitem.VisitCount) ?? 0,
                            OrderCount = tempList?.Where(qitem => qitem.StuChannel == sItem.StuChannel)?.Sum(qitem => qitem.OrderCount) ?? 0,
                            OrderIncome = tempList?.Where(qitem => qitem.StuChannel == sItem.StuChannel)?.Sum(qitem => qitem.OrderIncome) ?? 0,
                        });
                    }
                }
                else if (item.CreateType == StuCreateTypeEnum.CustomerInput)
                {
                    barList.Add(new MarketChannelStaticModel()
                    {
                        ChannelId = 100002,
                        ChannelName = item.CreateType.ToName(),
                        ClueCount = tempList?.Sum(qitem => qitem.ClueCount) ?? 0,
                        VisitCount = tempList?.Sum(qitem => qitem.VisitCount) ?? 0,
                        OrderCount = tempList?.Sum(qitem => qitem.OrderCount) ?? 0,
                        OrderIncome = tempList?.Sum(qitem => qitem.OrderIncome) ?? 0,
                    });
                }
                else if (item.CreateType == StuCreateTypeEnum.InternalIntroduction)
                {
                    barList.Add(new MarketChannelStaticModel()
                    {
                        ChannelId = 100003,
                        ChannelName = item.CreateType.ToName(),
                        ClueCount = tempList?.Sum(qitem => qitem.ClueCount) ?? 0,
                        VisitCount = tempList?.Sum(qitem => qitem.VisitCount) ?? 0,
                        OrderCount = tempList?.Sum(qitem => qitem.OrderCount) ?? 0,
                        OrderIncome = tempList?.Sum(qitem => qitem.OrderIncome) ?? 0,
                    });
                }
                else if (item.CreateType == StuCreateTypeEnum.TransIntroduction)
                {
                    barList.Add(new MarketChannelStaticModel()
                    {
                        ChannelId = 100004,
                        ChannelName = item.CreateType.ToName(),
                        ClueCount = tempList?.Sum(qitem => qitem.ClueCount) ?? 0,
                        VisitCount = tempList?.Sum(qitem => qitem.VisitCount) ?? 0,
                        OrderCount = tempList?.Sum(qitem => qitem.OrderCount) ?? 0,
                        OrderIncome = tempList?.Sum(qitem => qitem.OrderIncome) ?? 0,
                    });
                }
            }



            //四个渠道饼状图
            List<MarketChannelStaticModel> pieList = new List<MarketChannelStaticModel>();
            foreach (var item in createTypeList)
            {
                var tempList = dataList?.Where(qitem => qitem.CreateType == item.CreateType)?.ToList();
                pieList.Add(new MarketChannelStaticModel()
                {
                    ChannelId = (int)item.CreateType,
                    ChannelName = item.CreateType.ToName(),
                    ClueCount = tempList?.Sum(qitem => qitem.ClueCount) ?? 0,
                    VisitCount = tempList?.Sum(qitem => qitem.VisitCount) ?? 0,
                    OrderCount = tempList?.Sum(qitem => qitem.OrderCount) ?? 0,
                    OrderIncome = tempList?.Sum(qitem => qitem.OrderIncome) ?? 0,
                });
            }
            obj = new
            {
                dayList,
                YesDataObj = new
                {
                    DayStr = Common.ConvertHelper.FormatMonthDay(end.AddDays(-1)),
                    WeekStr = Common.Plugin.StringHelper.GetWeek(end.AddDays(-1)),

                    ClueCount = yesDataList?.Sum(qitem => qitem.ClueCount) ?? 0,
                    YestodayClue = yesDataList?.Sum(qitem => qitem.YestodayClue) ?? 0,
                    CurWeekClue = curWeekList?.Sum(qitem => qitem.CurWeekClue) ?? 0,
                    LastWeekClue = lastWeekList?.Sum(qitem => qitem.LastWeekClue) ?? 0,
                    CurMonthClue = tempDataList?.Sum(qitem => qitem.CurMonthClue) ?? 0,
                    LastMonthClue = tempDataList?.Sum(qitem => qitem.LastMonthClue) ?? 0,

                    FollowCount = yesDataList?.Sum(qitem => qitem.FollowCount) ?? 0,
                    YestodayFollow = yesDataList?.Sum(qitem => qitem.YestodayFollow) ?? 0,
                    CurWeekFollow = curWeekList?.Sum(qitem => qitem.CurWeekFollow) ?? 0,
                    LastWeekFollow = lastWeekList?.Sum(qitem => qitem.LastWeekFollow) ?? 0,
                    CurMonthFollow = tempDataList?.Sum(qitem => qitem.CurMonthFollow) ?? 0,
                    LastMonthFollow = tempDataList?.Sum(qitem => qitem.LastMonthFollow) ?? 0,

                    VisitCount = yesDataList?.Sum(qitem => qitem.VisitCount) ?? 0,
                    YestodayVisit = yesDataList?.Sum(qitem => qitem.YestodayVisit) ?? 0,
                    CurWeekVisit = curWeekList?.Sum(qitem => qitem.CurWeekVisit) ?? 0,
                    LastWeekVisit = lastWeekList?.Sum(qitem => qitem.LastWeekVisit) ?? 0,
                    CurMonthVisit = tempDataList?.Sum(qitem => qitem.CurMonthVisit) ?? 0,
                    LastMonthVisit = tempDataList?.Sum(qitem => qitem.LastMonthVisit) ?? 0,

                    TrialLessonCount = yesDataList?.Sum(qitem => qitem.TrialLessonCount) ?? 0,
                    YestodayTrialLesson = yesDataList?.Sum(qitem => qitem.YestodayTrialLesson) ?? 0,
                    CurWeekTrialLesson = curWeekList?.Sum(qitem => qitem.CurWeekTrialLesson) ?? 0,
                    LastWeekTrialLesson = lastWeekList?.Sum(qitem => qitem.LastWeekTrialLesson) ?? 0,
                    CurMonthTrialLesson = tempDataList?.Sum(qitem => qitem.CurMonthTrialLesson) ?? 0,
                    LastMonthTrialLesson = tempDataList?.Sum(qitem => qitem.LastMonthTrialLesson) ?? 0,

                    SaleMoney = yesDataList?.Sum(qitem => qitem.SaleMoney) ?? 0,
                    YestodaySale = yesDataList?.Sum(qitem => qitem.YestodaySale) ?? 0,
                    CurWeekSale = curWeekList?.Sum(qitem => qitem.CurWeekSale) ?? 0,
                    LastWeekSale = lastWeekList?.Sum(qitem => qitem.LastWeekSale) ?? 0,
                    CurMonrhSale = tempDataList?.Sum(qitem => qitem.CurMonrhSale) ?? 0,
                    LastMonthSale = tempDataList?.Sum(qitem => qitem.LastMonthSale) ?? 0,
                },
                BarData = new
                {
                    ClueData = barList?.OrderByDescending(qitem => qitem.ClueCount)?.Select(qitem => new { qitem.ChannelId, qitem.ChannelName, qitem.ClueCount })?.Take(10),
                    VisitData = barList?.OrderByDescending(qitem => qitem.VisitCount)?.Select(qitem => new { qitem.ChannelId, qitem.ChannelName, qitem.VisitCount })?.Take(10),
                    OrderData = barList?.OrderByDescending(qitem => qitem.OrderCount)?.Select(qitem => new { qitem.ChannelId, qitem.ChannelName, qitem.OrderCount })?.Take(10),
                    IncomeData = barList?.OrderByDescending(qitem => qitem.OrderIncome)?.Select(qitem => new { qitem.ChannelId, qitem.ChannelName, qitem.OrderIncome })?.Take(10),
                },
                PieData = new
                {
                    ClueData = pieList?.OrderByDescending(qitem => qitem.ClueCount)?.Select(qitem => new { qitem.ChannelId, qitem.ChannelName, qitem.ClueCount })?.Take(10),
                    VisitData = pieList?.OrderByDescending(qitem => qitem.VisitCount)?.Select(qitem => new { qitem.ChannelId, qitem.ChannelName, qitem.VisitCount })?.Take(10),
                    OrderData = pieList?.OrderByDescending(qitem => qitem.OrderCount)?.Select(qitem => new { qitem.ChannelId, qitem.ChannelName, qitem.OrderCount })?.Take(10),
                    IncomeData = pieList?.OrderByDescending(qitem => qitem.OrderIncome)?.Select(qitem => new { qitem.ChannelId, qitem.ChannelName, qitem.OrderIncome })?.Take(10),
                }
            };
            return obj;
        }

        /// <summary>
        /// 课程顾问统计
        /// </summary>
        /// <returns></returns>
        public object ConsultantStaticModule(string EmpIds, string StartTime, string EndTime)
        {
            object obj = new object();
            DateTime start = Convert.ToDateTime(StartTime);
            DateTime end = Convert.ToDateTime(EndTime);
            var days = (end - start).Days;
            var lastList = consultant_DataRepository.GetConsultantListRepository(new RB_Consultant_Data_Extend()
            {
                StartTime = StartTime,
                EndTime = EndTime,
                QCreateByIds = EmpIds
            });
            var consultantGoal = GetConsultantGoalListModule(new RB_Consultant_Goal_Extend() { Group_Id = 100000, Dept_Id = 13, YearStr = end.Year, MonthStr = end.Month })?.FirstOrDefault();
            var yesDataList = lastList?.Where(qitem => qitem.Date.Year == end.AddDays(-1).Year && qitem.Date.Month == end.AddDays(-1).Month)?.ToList();
            //当前周
            var curWeekList = lastList?.Where(qitem => Common.ConvertHelper.WeekOfYear(qitem.Date) == Common.ConvertHelper.WeekOfYear(end.AddDays(-1)))?.ToList();
            //上一周
            var lastWeekList = lastList?.Where(qitem => Common.ConvertHelper.WeekOfYear(qitem.Date) == Common.ConvertHelper.WeekOfYear(end.AddDays(-1)) - 1)?.ToList();

            var tempDataList = lastList?.Where(qitem => Common.ConvertHelper.FormatDate(qitem.Date) == Common.ConvertHelper.FormatDate(end.AddDays(-1)))?.ToList();

            var TrialLessonCount = yesDataList?.Sum(qitem => qitem.TrialLessonCount) ?? 0;
            var YestodayTrialLesson = yesDataList?.Sum(qitem => qitem.YestodayTrialLesson) ?? 0;
            var CurWeekTrialLesson = curWeekList?.Sum(qitem => qitem.CurWeekTrialLesson) ?? 0;
            var LastWeekTrialLesson = lastWeekList?.Sum(qitem => qitem.LastWeekTrialLesson) ?? 0;
            var CurMonthTrialLesson = tempDataList?.Sum(qitem => qitem.CurMonthTrialLesson) ?? 0;
            var LastMonthTrialLesson = tempDataList?.Sum(qitem => qitem.LastMonthTrialLesson) ?? 0;

            var VisitCount = yesDataList?.Sum(qitem => qitem.VisitCount) ?? 0;
            var YestodayVisit = yesDataList?.Sum(qitem => qitem.YestodayVisit) ?? 0;
            var CurWeekVisit = curWeekList?.Sum(qitem => qitem.CurWeekVisit) ?? 0;
            var LastWeekVisit = lastWeekList?.Sum(qitem => qitem.LastWeekVisit) ?? 0;
            var CurMonthVisit = tempDataList?.Sum(qitem => qitem.CurMonthVisit) ?? 0;
            var LastMonthVisit = tempDataList?.Sum(qitem => qitem.LastMonthVisit) ?? 0;

            var Rates = yesDataList?.Sum(qitem => qitem.Rates) ?? 0;
            var YestodayRates = yesDataList?.Sum(qitem => qitem.YestodayRates) ?? 0;
            var CurWeekRates = curWeekList?.Sum(qitem => qitem.CurWeekRates) ?? 0;
            var LastWeekRates = lastWeekList?.Sum(qitem => qitem.LastWeekRates) ?? 0;
            var CurMonthRates = tempDataList?.Sum(qitem => qitem.CurMonthRates) ?? 0;
            var LastMonthRates = tempDataList?.Sum(qitem => qitem.LastMonthRates) ?? 0;

            var FollowCount = yesDataList?.Sum(qitem => qitem.FollowCount) ?? 0;
            var YestodayFollow = yesDataList?.Sum(qitem => qitem.YestodayFollow) ?? 0;
            var CurWeekFollow = curWeekList?.Sum(qitem => qitem.CurWeekFollow) ?? 0;
            var LastWeekFollow = lastWeekList?.Sum(qitem => qitem.LastWeekFollow) ?? 0;
            var CurMonthFollow = tempDataList?.Sum(qitem => qitem.CurMonthFollow) ?? 0;
            var LastMonthFollow = tempDataList?.Sum(qitem => qitem.LastMonthFollow) ?? 0;

            var AgFollow = Math.Round(yesDataList?.Average(qitem => qitem.AgFollow) ?? 0, 2);
            var YestodayAgFollow = Math.Round(yesDataList?.Average(qitem => qitem.YestodayAgFollow) ?? 0, 2);
            var CurWeekAgFollow = Math.Round(curWeekList?.Average(qitem => qitem.CurWeekAgFollow) ?? 0, 2);
            var LastWeekAgFollow = Math.Round(lastWeekList?.Average(qitem => qitem.LastWeekAgFollow) ?? 0, 2);
            decimal CurMonthAgFollow = 0;
            decimal LastMonthAgFollow = 0;
            if (tempDataList != null && tempDataList.Count > 0)
            {
                CurMonthAgFollow = Math.Round(tempDataList?.Average(qitem => qitem.CurMonthAgFollow) ?? 0, 2);
                LastMonthAgFollow = Math.Round(tempDataList?.Average(qitem => qitem.LastMonthAgFollow) ?? 0, 2);
            }

            var CurWeekOrderNum = curWeekList?.Sum(qitem => qitem.CurWeekOrderNum) ?? 0;
            var LastWeekOrderNum = lastWeekList?.Sum(qitem => qitem.LastWeekOrderNum) ?? 0;
            var CurWeekOrderSale = tempDataList?.Sum(qitem => qitem.CurWeekOrderSale) ?? 0;
            var LastWeekOrderSale = tempDataList?.Sum(qitem => qitem.LastWeekOrderSale) ?? 0;


            var CurMonthOrderSale = tempDataList?.Sum(qitem => qitem.CurMonthOrderSale) ?? 0;
            var LastMonthOrderSale = tempDataList?.Sum(qitem => qitem.LastMonthOrderSale) ?? 0;
            var CurMonthOrderGoal = consultantGoal?.GoalMoney ?? 0;

            var CurMonthOrderNum = tempDataList?.Sum(qitem => qitem.CurMonthOrderNum) ?? 0;
            var LastMonthOrderNum = tempDataList?.Sum(qitem => qitem.LastMonthOrderNum) ?? 0;
            obj = new
            {
                DayStr = Common.ConvertHelper.FormatMonthDay(end.AddDays(-1)),
                WeekStr = Common.Plugin.StringHelper.GetWeek(end.AddDays(-1)),

                TrialLessonCount,
                YestodayTrialLesson,
                CurWeekTrialLesson,
                LastWeekTrialLesson,
                CurMonthTrialLesson,
                LastMonthTrialLesson,

                VisitCount,
                YestodayVisit,
                CurWeekVisit,
                LastWeekVisit,
                CurMonthVisit,
                LastMonthVisit,

                Rates,
                YestodayRates,
                CurWeekRates,
                LastWeekRates,
                CurMonthRates,
                LastMonthRates,

                FollowCount,
                YestodayFollow,
                CurWeekFollow,
                LastWeekFollow,
                CurMonthFollow,
                LastMonthFollow,

                AgFollow,
                YestodayAgFollow,
                CurWeekAgFollow,
                LastWeekAgFollow,
                CurMonthAgFollow,
                LastMonthAgFollow,

                CurWeekOrderNum,
                LastWeekOrderNum,
                CurWeekOrderSale,
                LastWeekOrderSale,

                CurMonthOrderSale,
                LastMonthOrderSale,
                CurMonthOrderGoal,

                CurMonthOrderNum,
                LastMonthOrderNum,
            };
            return obj;
        }

        #region 课程顾问部目标设置

        /// <summary>
        /// 获取课程顾问部目标分页列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="rowsCount"></param>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Consultant_Goal_Extend> GetConsultantGoalPageModule(int pageIndex, int pageSize, out long rowsCount, RB_Consultant_Goal_Extend query)
        {
            var list = consultant_GoalRepository.GetConsultantGoalPageRepository(pageIndex, pageSize, out rowsCount, query);
            if (list != null && list.Count > 0)
            {
                var deptList = new List<RB_Department_ViewModel>();
                string deptIds = string.Join(",", list.Select(qitem => qitem.Dept_Id));
                if (!string.IsNullOrEmpty(deptIds))
                {
                    deptList = departmentRepository.GetDepartmentListRepository(new RB_Department_ViewModel()
                    {
                        QDeptIds = deptIds
                    });
                }
                foreach (var item in list)
                {
                    item.DeptName = deptList?.FirstOrDefault(qitem => qitem.DeptId == item.Dept_Id)?.DeptName ?? "";
                }
            }
            return list;
        }

        /// <summary>
        /// 获取课程顾问部目标列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Consultant_Goal_Extend> GetConsultantGoalListModule(RB_Consultant_Goal_Extend query)
        {
            var list = consultant_GoalRepository.GetConsultantGoalListRepository(query);
            return list;
        }

        /// <summary>
        /// 修改课程顾问部目标
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool SetConsultantGoalModule(RB_Consultant_Goal_Extend model)
        {
            bool flag = false;
            if (model.Id > 0)
            {
                Dictionary<string, object> fileds = new Dictionary<string, object>()
                {
                    { nameof(RB_Consultant_Goal_Extend.YearStr),model.YearStr},
                    { nameof(RB_Consultant_Goal_Extend.MonthStr),model.MonthStr},
                    { nameof(RB_Consultant_Goal_Extend.GoalMoney),model.GoalMoney},
                    { nameof(RB_Consultant_Goal_Extend.Dept_Id),model.Dept_Id},
                };
                flag = consultant_GoalRepository.Update(fileds, new WhereHelper(nameof(RB_Consultant_Goal_Extend.Id), model.Id));
            }
            return flag;
        }

        /// <summary>
        /// 批量添加课程顾问部目标
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public bool AddConsultantGoalModule(List<RB_Consultant_Goal_Extend> list)
        {
            bool flag = true;
            if (list != null && list.Count > 0)
            {
                foreach (var item in list)
                {
                    var oldModel = consultant_GoalRepository.GetConsultantGoalListRepository(new RB_Consultant_Goal_Extend()
                    {
                        YearStr = item.YearStr,
                        MonthStr = item.MonthStr,
                        Dept_Id = item.Dept_Id
                    })?.FirstOrDefault();
                    item.Id = oldModel?.Id ?? 0;
                    if (flag)
                    {
                        if (item.Id > 0)
                        {
                            Dictionary<string, object> fileds = new Dictionary<string, object>()
                            {
                                { nameof(RB_Consultant_Goal_Extend.YearStr),item.YearStr},
                                { nameof(RB_Consultant_Goal_Extend.MonthStr),item.MonthStr},
                                { nameof(RB_Consultant_Goal_Extend.GoalMoney),item.GoalMoney},
                                { nameof(RB_Consultant_Goal_Extend.Dept_Id),item.Dept_Id},
                            };
                            flag = consultant_GoalRepository.Update(fileds, new WhereHelper(nameof(RB_Consultant_Goal_Extend.Id), item.Id));
                        }
                        else
                        {
                            var newId = consultant_GoalRepository.Insert(item);
                            item.Id = newId;
                            flag = newId > 0;
                        }
                    }
                }
            }
            return flag;
        }

        /// <summary>
        /// 根据编号获取课程顾问部目标
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public RB_Consultant_Goal_Extend GetConsultantGoalModule(object Id)
        {
            var extModel = consultant_GoalRepository.GetEntity<RB_Consultant_Goal_Extend>(Id);
            return extModel;
        }


        /// <summary>
        /// 根据编号删除课程顾问部目标
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public bool RemoveConsultantGoalModule(object Id)
        {
            Dictionary<string, object> fileds = new Dictionary<string, object>()
            {
                {nameof(RB_Consultant_Goal_Extend.Status) ,(int)DateStateEnum.Delete}
            };
            bool flag = consultant_GoalRepository.Update(fileds, new WhereHelper(nameof(RB_Consultant_Goal_Extend.Id), Id));
            return flag;
        }
        #endregion

        #region 市场渠道统计

        /// <summary>
        /// 市场部渠道数据统计
        /// </summary>
        /// <param name="StartTime">开始时间</param>
        /// <param name="EndTime">结束时间</param>
        /// <param name="qEmpIds">员工编号</param>
        /// <returns></returns>
        public List<object> MarketChannelStudentStaticModule(string StartTime, string EndTime, string qEmpIds, bool isHaveAuth = false)
        {
            List<object> list = new List<object>();
            var empList = accountRepository.GetEmployeeListRepository(new Employee_ViewModel()
            {
                //市场部编号4，课程顾问部编号13
                QDeptIds = "4",
                UserRole= UserRoleEnum.MarketPersion,
                Group_Id = 100000,
                IsLeave = 1,
                QIds = qEmpIds
            });
            if (empList != null && empList.Count > 0)
            {
                var channelList = channelRepository.GetChannelListRepository(new RB_Channel_Extend() { });
                var dataList = studentRepository.GetStudentChannelStaticRepository(new RB_Student_ViewModel()
                {
                    StartTime = StartTime,
                    EndTime = EndTime
                });
                foreach (var eItem in empList)
                {
                    List<MarketChannelStaticModel> subList = new List<MarketChannelStaticModel>();
                    var tempList = dataList?.Where(qitem => qitem.CreateBy == eItem.Id)?.ToList();
                    //包含的数据
                    string singleIds = "";
                    string authId = "1";
                    if (isHaveAuth)
                    {
                        authId = "2";
                    }
                    if (channelList != null && channelList.Count > 0)
                    {
                        foreach (var item in channelList)
                        {
                            if (item.StaticTypeList != null && item.StaticTypeList.Contains(authId))
                            {
                                singleIds += "," + item.Id;
                                var channelTempList = tempList?.Where(qitem => qitem.CreateType == StuCreateTypeEnum.EmployeeInput && qitem.StuChannel == item.Id)?.ToList();
                                subList.Add(new MarketChannelStaticModel()
                                {
                                    ChannelName = item.Name,
                                    ChannelId = item.Id,
                                    ClueCount = channelTempList?.Count() ?? 0,
                                    VisitCount = channelTempList?.Sum(qitem => qitem.VisitCount) ?? 0,
                                    OrderCount = channelTempList?.Sum(qitem => qitem.OrderCount) ?? 0,
                                    OrderIncome = channelTempList?.Sum(qitem => qitem.OrderIncome) ?? 0
                                });
                            }
                        }

                    }
                    var customerList = tempList?.Where(qitem => qitem.CreateType == StuCreateTypeEnum.CustomerInput)?.ToList();
                    subList.Add(new MarketChannelStaticModel()
                    {
                        ChannelName = "同行介绍",
                        ChannelId = 10001,
                        ClueCount = customerList?.Count() ?? 0,
                        VisitCount = customerList?.Sum(qitem => qitem.VisitCount) ?? 0,
                        OrderCount = customerList?.Sum(qitem => qitem.OrderCount) ?? 0,
                        OrderIncome = customerList?.Sum(qitem => qitem.OrderIncome) ?? 0
                    });

                    var innerList = tempList?.Where(qitem => qitem.CreateType == StuCreateTypeEnum.InternalIntroduction)?.ToList();
                    subList.Add(new MarketChannelStaticModel()
                    {
                        ChannelName = "内部介绍",
                        ChannelId = 10002,
                        ClueCount = innerList?.Count() ?? 0,
                        VisitCount = innerList?.Sum(qitem => qitem.VisitCount) ?? 0,
                        OrderCount = innerList?.Sum(qitem => qitem.OrderCount) ?? 0,
                        OrderIncome = innerList?.Sum(qitem => qitem.OrderIncome) ?? 0
                    });

                    var transList = tempList?.Where(qitem => qitem.CreateType == StuCreateTypeEnum.TransIntroduction)?.ToList();
                    subList.Add(new MarketChannelStaticModel()
                    {
                        ChannelName = "学员转介绍",
                        ChannelId = 10003,
                        ClueCount = transList?.Count() ?? 0,
                        VisitCount = transList?.Sum(qitem => qitem.VisitCount) ?? 0,
                        OrderCount = transList?.Sum(qitem => qitem.OrderCount) ?? 0,
                        OrderIncome = transList?.Sum(qitem => qitem.OrderIncome) ?? 0
                    });

                    var otherList = tempList?.Where(qitem => qitem.CreateType == StuCreateTypeEnum.EmployeeInput && !singleIds.Contains(qitem.StuChannel.ToString()))?.ToList();
                    subList.Add(new MarketChannelStaticModel()
                    {
                        ChannelName = "其他",
                        ChannelId = 10004,
                        ClueCount = otherList?.Count() ?? 0,
                        VisitCount = otherList?.Sum(qitem => qitem.VisitCount) ?? 0,
                        OrderCount = otherList?.Sum(qitem => qitem.OrderCount) ?? 0,
                        OrderIncome = otherList?.Sum(qitem => qitem.OrderIncome) ?? 0
                    });

                    subList.Add(new MarketChannelStaticModel()
                    {
                        ChannelName = "合计",
                        ChannelId = 10005,
                        ClueCount = tempList?.Count() ?? 0,
                        VisitCount = tempList?.Sum(qitem => qitem.VisitCount) ?? 0,
                        OrderCount = tempList?.Sum(qitem => qitem.OrderCount) ?? 0,
                        OrderIncome = tempList?.Sum(qitem => qitem.OrderIncome) ?? 0
                    });
                    Dictionary<string, object> dicList = new Dictionary<string, object>();

                    for (var i = 0; i < subList.Count; i++)
                    {
                        if (i == 0)
                        {
                            dicList.Add(string.Format("Id"), eItem.Id);
                            dicList.Add(string.Format("EmployeeName"), eItem.EmployeeName);
                        }
                        decimal rate = 0;
                        if (subList[i].ClueCount > 0)
                        {
                            rate = Math.Round(Convert.ToDecimal(subList[i].VisitCount) / Convert.ToDecimal(subList[i].ClueCount) * Convert.ToDecimal(100), 2);
                        }
                        dicList.Add(string.Format("ChannelName{0}", i), subList[i].ChannelName);
                        dicList.Add(string.Format("ClueCount{0}", i), subList[i].ClueCount);
                        dicList.Add(string.Format("VisitCount{0}", i), subList[i].VisitCount);
                        dicList.Add(string.Format("Rates{0}", i), rate + "%");
                    }

                    list.Add(new
                    {
                        eItem.Id,
                        eItem.EmployeeName,
                        dicList,
                        subList,
                    });
                }
            }
            return list;
        }

        /// <summary>
        /// 市场部学员创建类型统计
        /// </summary>
        /// <param name="StartTime"></param>
        /// <param name="EndTime"></param>
        /// <returns></returns>
        public object MarketCreateTypeStaticModule(List<int> queryEmployeeIds,string StartTime, string EndTime)
        {
            var dataList = studentRepository.GetStudentChannelStaticRepository(new RB_Student_ViewModel()
            {
                StartTime = StartTime,
                EndTime = EndTime,
                CreateIds = queryEmployeeIds,
            });
            var enumList = Common.Plugin.EnumHelper.EnumToList(typeof(StuCreateTypeEnum));
            //企业同行
            List<MarketChannelStaticModel> customerDataList = new List<MarketChannelStaticModel>();
            //校代同行
            List<MarketChannelStaticModel> schoolDataList = new List<MarketChannelStaticModel>();
            List<MarketChannelStaticModel> innerDataList = new List<MarketChannelStaticModel>();
            List<MarketChannelStaticModel> transDataList = new List<MarketChannelStaticModel>();
            foreach (var item in enumList)
            {
                if (item.Id == (int)StuCreateTypeEnum.EmployeeInput)
                {

                }
                else if (item.Id == (int)StuCreateTypeEnum.CustomerInput)
                {
                    var cusIds = string.Join(",", dataList.Where(qitem => qitem.CreateType == StuCreateTypeEnum.CustomerInput).Select(qitem => qitem.StuSourceId));
                    List<RB_Customer_Extend> customerList = new List<RB_Customer_Extend>();
                    if (!string.IsNullOrEmpty(cusIds))
                    {
                        customerList = customerRepository.GetCustomerListRepository(new RB_Customer_Extend() { CustomerIds = cusIds });
                    }
                    var sourceTypeList = dataList?.Where(qitem => qitem.CreateType == StuCreateTypeEnum.CustomerInput)
                        ?.GroupBy(qitem => new { qitem.CustomerType })
                        ?.Select(qitem => new { qitem.Key.CustomerType });

                    foreach (var sItem in sourceTypeList)
                    {
                        var sourceIdList = dataList?.Where(qitem => qitem.CreateType == StuCreateTypeEnum.CustomerInput && qitem.CustomerType == sItem.CustomerType)
                            ?.GroupBy(qitem => new { qitem.StuSourceId })
                            ?.Select(qitem => new { qitem.Key.StuSourceId });
                        foreach (var subItem in sourceIdList)
                        {
                            var tempCusList = dataList?.Where(qitem => qitem.CreateType == StuCreateTypeEnum.CustomerInput && qitem.StuSourceId == subItem.StuSourceId)?.ToList();
                            //企业同行
                            if (sItem.CustomerType ==  Common.Enum.Customer.CatetoryTypeEnum.Company)
                            {
                                customerDataList.Add(new MarketChannelStaticModel()
                                {
                                    ChannelName = customerList?.FirstOrDefault(qitem => qitem.CustomerId == subItem.StuSourceId)?.CustomerName ?? "",
                                    ChannelId = subItem.StuSourceId,
                                    ClueCount = tempCusList?.Count() ?? 0,
                                    VisitCount = tempCusList?.Sum(qitem => qitem.VisitCount) ?? 0,
                                    OrderCount = tempCusList?.Sum(qitem => qitem.OrderCount) ?? 0,
                                    OrderIncome = tempCusList?.Sum(qitem => qitem.OrderIncome) ?? 0
                                });
                            }
                            //校代同行
                            else if (sItem.CustomerType == Common.Enum.Customer.CatetoryTypeEnum.School)
                            {
                                schoolDataList.Add(new MarketChannelStaticModel()
                                {
                                    ChannelName = customerList?.FirstOrDefault(qitem => qitem.CustomerId == subItem.StuSourceId)?.CustomerName ?? "",
                                    ChannelId = subItem.StuSourceId,
                                    ClueCount = tempCusList?.Count() ?? 0,
                                    VisitCount = tempCusList?.Sum(qitem => qitem.VisitCount) ?? 0,
                                    OrderCount = tempCusList?.Sum(qitem => qitem.OrderCount) ?? 0,
                                    OrderIncome = tempCusList?.Sum(qitem => qitem.OrderIncome) ?? 0
                                });
                            }
                        }
                    }

                }
                else if (item.Id == (int)StuCreateTypeEnum.InternalIntroduction)
                {
                    var empIds = string.Join(",", dataList.Where(qitem => qitem.CreateType == StuCreateTypeEnum.InternalIntroduction).Select(qitem => qitem.StuSourceId));
                    List<Employee_ViewModel> empList = new List<Employee_ViewModel>();
                    if (!string.IsNullOrEmpty(empIds))
                    {
                        empList = accountRepository.GetEmployeeListRepository(new Employee_ViewModel() { QIds = empIds });
                    }
                    var groupDataList = dataList?.Where(qitem => qitem.CreateType == StuCreateTypeEnum.InternalIntroduction)
                        ?.GroupBy(qitem => new { qitem.StuSourceId })
                        ?.Select(qitem => new { qitem.Key.StuSourceId });
                    foreach (var sItem in groupDataList)
                    {
                        var tempInnerList = dataList?.Where(qitem => qitem.CreateType == StuCreateTypeEnum.InternalIntroduction && qitem.StuSourceId == sItem.StuSourceId)?.ToList();
                        innerDataList.Add(new MarketChannelStaticModel()
                        {
                            ChannelName = empList?.FirstOrDefault(qitem => qitem.Id == sItem.StuSourceId)?.EmployeeName ?? "",
                            ChannelId = sItem.StuSourceId,
                            ClueCount = tempInnerList?.Count() ?? 0,
                            VisitCount = tempInnerList?.Sum(qitem => qitem.VisitCount) ?? 0,
                            OrderCount = tempInnerList?.Sum(qitem => qitem.OrderCount) ?? 0,
                            OrderIncome = tempInnerList?.Sum(qitem => qitem.OrderIncome) ?? 0
                        });
                    }
                }
                else
                {
                    var stuIds = string.Join(",", dataList.Where(qitem => qitem.CreateType == StuCreateTypeEnum.TransIntroduction).Select(qitem => qitem.StuSourceId));
                    List<RB_Student_ViewModel> stuList = new List<RB_Student_ViewModel>();
                    if (!string.IsNullOrEmpty(stuIds))
                    {
                        stuList = studentRepository.GetStudentListRepository(new RB_Student_ViewModel() { StuIds = stuIds });
                    }
                    var sourceTypeList = dataList?.Where(qitem => qitem.CreateType == StuCreateTypeEnum.TransIntroduction)
                        ?.GroupBy(qitem => new { qitem.StuSourceId })
                        ?.Select(qitem => new { qitem.Key.StuSourceId });
                    foreach (var sItem in sourceTypeList)
                    {
                        var tempTransList = dataList?.Where(qitem => qitem.CreateType == StuCreateTypeEnum.TransIntroduction && qitem.StuSourceId == sItem.StuSourceId)?.ToList();
                        transDataList.Add(new MarketChannelStaticModel()
                        {
                            ChannelName = stuList?.FirstOrDefault(qitem => qitem.StuId == sItem.StuSourceId)?.StuName ?? "",
                            ChannelId = sItem.StuSourceId,
                            ClueCount = tempTransList?.Count() ?? 0,
                            VisitCount = tempTransList?.Sum(qitem => qitem.VisitCount) ?? 0,
                            OrderCount = tempTransList?.Sum(qitem => qitem.OrderCount) ?? 0,
                            OrderIncome = tempTransList?.Sum(qitem => qitem.OrderIncome) ?? 0
                        });
                    }
                }
            }
            return new
            {
                customerDataList,
                schoolDataList,
                innerDataList,
                transDataList,
            };
        }

        /// <summary>
        /// 渠道统计汇总
        /// </summary>
        /// <param name="StartTime"></param>
        /// <param name="EndTime"></param>
        /// <returns></returns>
        public List<MarketChannelStaticModel> MarketChannelStaticModule(List<int> queryEmployeeList,string StartTime, string EndTime)
        {
            List<MarketChannelStaticModel> list = new List<MarketChannelStaticModel>();
            var channelList = channelRepository.GetChannelListRepository(new RB_Channel_Extend() { });
            var dataList = studentRepository.GetStudentChannelStaticRepository(new RB_Student_ViewModel()
            {
                StartTime = StartTime,
                EndTime = EndTime,
                CreateIds= queryEmployeeList
            });
            if (channelList != null && channelList.Count > 0)
            {
                foreach (var item in channelList)
                {
                    var channelTempList = dataList?.Where(qitem => qitem.CreateType == StuCreateTypeEnum.EmployeeInput && qitem.StuChannel == item.Id)?.ToList();
                    list.Add(new MarketChannelStaticModel()
                    {
                        ChannelName = item.Name,
                        ChannelId = item.Id,
                        ChannelType =1,
                        ClueCount = channelTempList?.Count()??0,
                        VisitCount = channelTempList?.Sum(qitem => qitem.VisitCount)??0,
                        OrderCount = channelTempList?.Sum(qitem => qitem.OrderCount)??0,
                        OrderIncome = channelTempList?.Sum(qitem => qitem.OrderIncome)??0
                    });
                }
            }


            var cusIds = string.Join(",", dataList.Where(qitem => qitem.CreateType == StuCreateTypeEnum.CustomerInput).Select(qitem => qitem.StuSourceId));
            List<RB_Customer_Extend> customerList = new List<RB_Customer_Extend>();
            if (!string.IsNullOrEmpty(cusIds))
            {
                customerList = customerRepository.GetCustomerListRepository(new RB_Customer_Extend() { CustomerIds = cusIds });
            }
            var sourceTypeList = dataList?.Where(qitem => qitem.CreateType == StuCreateTypeEnum.CustomerInput)
                ?.GroupBy(qitem => new { qitem.CustomerType })
                ?.Select(qitem => new { qitem.Key.CustomerType });

            foreach (var sItem in sourceTypeList)
            {
                var sourceIdList = dataList?.Where(qitem => qitem.CreateType == StuCreateTypeEnum.CustomerInput && qitem.CustomerType == sItem.CustomerType)
                    ?.GroupBy(qitem => new { qitem.StuSourceId })
                    ?.Select(qitem => new { qitem.Key.StuSourceId });
                foreach (var subItem in sourceIdList)
                {
                    var tempCusList = dataList?.Where(qitem => qitem.CreateType == StuCreateTypeEnum.CustomerInput && qitem.StuSourceId == subItem.StuSourceId)?.ToList();
                    //企业同行
                    if (sItem.CustomerType ==  Common.Enum.Customer.CatetoryTypeEnum.Company)
                    {
                        list.Add(new MarketChannelStaticModel()
                        {
                            ChannelName = "(企业)" + (customerList?.FirstOrDefault(qitem => qitem.CustomerId == subItem.StuSourceId)?.CustomerName ?? ""),
                            ChannelId= subItem.StuSourceId,
                            CustomerType= sItem.CustomerType,
                            ChannelType = 2,
                            ClueCount = tempCusList?.Count()??0,
                            VisitCount = tempCusList?.Sum(qitem => qitem.VisitCount)??0,
                            OrderCount = tempCusList?.Sum(qitem => qitem.OrderCount)??0,
                            OrderIncome = tempCusList?.Sum(qitem => qitem.OrderIncome)??0
                        });
                    }
                    //校代同行
                    if (sItem.CustomerType ==  Common.Enum.Customer.CatetoryTypeEnum.School)
                    {
                        list.Add(new MarketChannelStaticModel()
                        {
                            ChannelName = "(校代)" + (customerList?.FirstOrDefault(qitem => qitem.CustomerId == subItem.StuSourceId)?.CustomerName ?? ""),
                            ChannelId=subItem.StuSourceId,
                            CustomerType=sItem.CustomerType,
                            ChannelType = 2,
                            ClueCount = tempCusList?.Count()??0,
                            VisitCount = tempCusList?.Sum(qitem => qitem.VisitCount)??0,
                            OrderCount = tempCusList?.Sum(qitem => qitem.OrderCount)??0,
                            OrderIncome = tempCusList?.Sum(qitem => qitem.OrderIncome)??0
                        });
                    }
                }
            }


            var empIds = string.Join(",", dataList.Where(qitem => qitem.CreateType == StuCreateTypeEnum.InternalIntroduction).Select(qitem => qitem.StuSourceId));
            List<Employee_ViewModel> empList = new List<Employee_ViewModel>();
            if (!string.IsNullOrEmpty(empIds))
            {
                empList = accountRepository.GetEmployeeListRepository(new Employee_ViewModel() { QIds = empIds });
            }
            var groupDataList = dataList?.Where(qitem => qitem.CreateType == StuCreateTypeEnum.InternalIntroduction)
                ?.GroupBy(qitem => new { qitem.StuSourceId })
                ?.Select(qitem => new { qitem.Key.StuSourceId });
            foreach (var sItem in groupDataList)
            {
                var tempInnerList = dataList?.Where(qitem => qitem.CreateType == StuCreateTypeEnum.InternalIntroduction && qitem.StuSourceId == sItem.StuSourceId)?.ToList();
                list.Add(new MarketChannelStaticModel()
                {
                    ChannelName = "(内部介绍)" + (empList?.FirstOrDefault(qitem => qitem.Id == sItem.StuSourceId)?.EmployeeName ?? ""),
                    ChannelId=sItem.StuSourceId,
                    ChannelType = 3,
                    ClueCount = tempInnerList?.Count()??0,
                    VisitCount = tempInnerList?.Sum(qitem => qitem.VisitCount)??0,
                    OrderCount = tempInnerList?.Sum(qitem => qitem.OrderCount)??0,
                    OrderIncome = tempInnerList?.Sum(qitem => qitem.OrderIncome)??0
                });
            }

            var stuIds = string.Join(",", dataList.Where(qitem => qitem.CreateType == StuCreateTypeEnum.TransIntroduction).Select(qitem => qitem.StuSourceId));
            List<RB_Student_ViewModel> stuList = new List<RB_Student_ViewModel>();
            if (!string.IsNullOrEmpty(stuIds))
            {
                stuList = studentRepository.GetStudentListRepository(new RB_Student_ViewModel() { StuIds = stuIds });
            }
            var transTypeList = dataList?.Where(qitem => qitem.CreateType == StuCreateTypeEnum.TransIntroduction)
                ?.GroupBy(qitem => new { qitem.StuSourceId })
                ?.Select(qitem => new { qitem.Key.StuSourceId });
            foreach (var sItem in transTypeList)
            {
                var tempTransList = dataList?.Where(qitem => qitem.CreateType == StuCreateTypeEnum.TransIntroduction && qitem.StuSourceId == sItem.StuSourceId)?.ToList();
                list.Add(new MarketChannelStaticModel()
                {
                    ChannelName = "(学员转介绍)" + stuList?.FirstOrDefault(qitem => qitem.StuId == sItem.StuSourceId)?.StuName ?? "",
                    ChannelId = sItem.StuSourceId,
                    ChannelType = 4,
                    ClueCount = tempTransList?.Count()??0,
                    VisitCount = tempTransList?.Sum(qitem => qitem.VisitCount)??0,
                    OrderCount = tempTransList?.Sum(qitem => qitem.OrderCount)??0,
                    OrderIncome = tempTransList?.Sum(qitem => qitem.OrderIncome)??0
                });
            }

            list.Add(new MarketChannelStaticModel()
            {
                ChannelName = "合计",
                ChannelId = 10005,
                ClueCount = dataList?.Count() ?? 0,
                VisitCount = dataList?.Sum(qitem => qitem.VisitCount) ?? 0,
                OrderCount = dataList?.Sum(qitem => qitem.OrderCount) ?? 0,
                OrderIncome = dataList?.Sum(qitem => qitem.OrderIncome) ?? 0
            });
            return list;
        }

        #endregion
    }
}