using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Edu.Common.API;
using Edu.Common.Enum.Finance;
using Edu.Common.Plugin;
using Edu.Model.ViewModel.Course;
using Edu.Model.ViewModel.Finance;
using Edu.Module.Course;
using Edu.Module.Finance;
using Edu.Module.StudyAbroad;
using Edu.WebApi.Filter;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;

namespace Edu.WebApi.Controllers.Finance
{
    /// <summary>
    /// 系统公用接口
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiExceptionFilter]
    [ApiController]
    [EnableCors("AllowCors")]
    public class FinanceController : BaseController
    {
        /// <summary>
        /// 财务处理类对象
        /// </summary>
        private readonly FinanceModule financeModule = AOP.AOPHelper.CreateAOPObject<FinanceModule>();

        /// <summary>
        /// 订单处理类对象
        /// </summary>
        private readonly OrderModule orderModule = AOP.AOPHelper.CreateAOPObject<OrderModule>();

        /// <summary>
        /// 班级处理类对象
        /// </summary>
        private readonly ClassModule classModule = AOP.AOPHelper.CreateAOPObject<ClassModule>();
        /// <summary>
        /// 留学就业处理类对象
        /// </summary>
        private readonly StudyAbroadModule studyAbroadModule = AOP.AOPHelper.CreateAOPObject<StudyAbroadModule>();

        /// <summary>
        /// 教务奖励处理类对象
        /// </summary>
        private readonly TeachingRewardsModule teachingRewardsModule = AOP.AOPHelper.CreateAOPObject<TeachingRewardsModule>();

        /// <summary>
        /// 获取班级/留学就业收支明细
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public ApiResult GetClassBalanceSheet()
        {
            int classId = base.ParmJObj.GetInt("ClassId", 0);
            int schoolId = base.ParmJObj.GetInt("School_Id");
            int SourceId = base.ParmJObj.GetInt("SourceId");
            if (classId <= 0 && SourceId <= 0)
            {
                return ApiResult.Failed("班级Id不存在");
            }

            RB_Finance_Extend model = new RB_Finance_Extend() { TCID = classId, OrderSource = OrderResourceEnum.Education, RB_Group_Id = base.UserInfo.Group_Id, FinanceType = 2 };
            if (SourceId > 0)
            {
                model.TCID = SourceId;
                model.OrderSource = OrderResourceEnum.EducationStudy;
            }
            var financeList = financeModule.GetFinanceInfoList(model).Where(t => (t.Status == FinanceAuditStatus.InReview || t.Status == FinanceAuditStatus.Pass || t.Status == FinanceAuditStatus.CTemporary)).ToList();

            //收入
            decimal IncomeReceive = 0;//应收
            decimal IncomeActual = 0;//实收
            var orderList = orderModule.GetClassOrderList_V2(classId, base.UserInfo.Group_Id, SourceId).Where(x => (int)x.OrderState < 3);
            IncomeReceive = (orderList != null && orderList.Any()) ? orderList.Sum(x => x.PreferPrice) : 0;
            IncomeActual = (orderList != null && orderList.Any()) ? orderList.Sum(x => x.Income) : 0;

            //其他
            decimal OtherIncomeReceive = 0;//应收
            decimal OtherIncomeActual = 0;//实收
            OtherIncomeReceive = (financeList != null && financeList.Any()) ? financeList.Where(x => x.Type == WFTempLateClassEnum.IN && x.OrderID <= 0).Sum(x => x.Money ?? 0) : 0;
            OtherIncomeActual = (financeList != null && financeList.Any()) ? financeList.Where(x => x.Type == WFTempLateClassEnum.IN && x.OrderID <= 0).Sum(x => x.PayMoney ?? 0) : 0;
            //支出
            decimal PayReceive = 0;//应付
            decimal PayActual = 0;//实付
            PayReceive = (financeList != null && financeList.Any()) ? financeList.Where(x => x.Type == WFTempLateClassEnum.OUT).Sum(x => x.Money ?? 0) : 0;
            PayActual = (financeList != null && financeList.Any()) ? financeList.Where(x => x.Type == WFTempLateClassEnum.OUT).Sum(x => x.PayMoney ?? 0) : 0;

            //提成
            decimal SaleCommission = (orderList != null && orderList.Any()) ? orderList.Where(x => x.IsCommissionGive == 1).Sum(x => x.CommissionMoney + x.ExtraCommissionMoney) : 0;//销售提成
            var teacherProfitList = teachingRewardsModule.GetBonusDetailList(new RB_Teaching_BonusDetail_ViewModel { Group_Id = base.UserInfo.Group_Id, ClassId = classId, School_Id = schoolId });
            decimal TeacherProfitNow = (teacherProfitList != null && teacherProfitList.Any()) ? teacherProfitList.Sum(x => x.Money) : 0;//老师提成

            var meritsProfitList = teachingRewardsModule.GetTeachingPerfList(new RB_Teaching_Perf_ViewModel { Group_Id = base.UserInfo.Group_Id, ClassId = classId, School_Id = schoolId });
            decimal MeritsProfit = (meritsProfitList != null && meritsProfitList.Any()) ? (meritsProfitList.Where(x => x.PerfState == Common.Enum.Course.PerfStateEnum.Confirmed).OrderByDescending(x => x.CreateTime).FirstOrDefault()?.Money ?? 0) : 0;//绩效提成

            PayReceive += (SaleCommission + TeacherProfitNow + MeritsProfit);//支出加上提成的
            PayActual += (SaleCommission + TeacherProfitNow + MeritsProfit);

            //利润
            decimal ProfitActual = 0;//实际利润
            decimal ProfitNow = 0;//当前利润
            ProfitActual = (IncomeActual + OtherIncomeActual) - PayReceive;
            ProfitNow = (IncomeActual + OtherIncomeActual) - PayActual;

            //项目信息
            var projectInfo = new object();
            if (classId > 0)
            {
                var classInfo = classModule.GetClassAndCourseListRepository(new Model.ViewModel.Course.RB_Class_ViewModel { Group_Id = base.UserInfo.Group_Id, ClassId = classId, School_Id = schoolId }).FirstOrDefault();
                projectInfo = new { classInfo?.ClassName, classInfo?.CourseName, classInfo?.TeacherName, OpenTime = classInfo?.OpenTime.ToString("yyyy-MM-dd") };
            }
            else if (SourceId > 0)
            {
                var studyInfo = studyAbroadModule.GetStudyAbroadModule(SourceId);
                projectInfo = new { ClassName = studyInfo?.Name, CourseName = "", TeacherName = "", OpenTime = "" };
            }
            var recultFiniceList = financeList.Select(x => new
            {
                x.FrID,
                x.Type,
                x.TCID,
                x.OrderID,
                x.Money,
                x.PayMoney,
                AirTotalMoney = x.AirTotalMoney ?? 0,
                AirTotalPayMoney = x.AirTotalPayMoney ?? 0,
                CostTypeList = x.CostTypeList.Distinct().ToList(),
                x.OrderSource,
                AuditList = x.AuditList.Select(t => new { t.AuditEmId, t.AuditEmName, AuditDate = t.AuditDate.HasValue ? t.AuditDate.Value.ToString("yyyy/MM/dd") : "" }),
                StatusStr = EnumHelper.ToName(x.Status.Value),
                x.RB_CreateByName,
                CreateDate = x.CreateDate.Value.ToString("yyyy-MM-dd"),
                TradeDate = x.TradeDate.HasValue ? x.TradeDate.Value.ToString("yyyy-MM-dd") : "",
                TradeWayList = x.BankList?.Select(y => new { y.AccountType, y.Alias, y.BankNo, TypeName = EnumHelper.ToName(y.Type), y.CurrencyName, y.OriginalMoney, y.Money, y.Rate })
            });

            var classFinanceModel = classModule.GetClassFinanceListRepository(new RB_Class_Finance_ViewModel { Group_Id = base.UserInfo.Group_Id, ClassId = classId, School_Id = schoolId }).FirstOrDefault();
            if (classFinanceModel == null)
            {
                classFinanceModel = new RB_Class_Finance_ViewModel
                {
                    Group_Id = base.UserInfo.Group_Id,
                    ClassId = classId,
                    School_Id = schoolId
                };
            }
            var resultData = new
            {
                ClassFinance = new { classFinanceModel.ClassId, classFinanceModel.IsOpen, classFinanceModel.Group_Id, classFinanceModel.School_Id, classFinanceModel.FinanceId },
                ClassInfo = projectInfo,
                IncomeReceive,
                IncomeActual,
                OtherIncomeReceive,
                OtherIncomeActual,
                PayReceive,
                PayActual,
                ProfitActual,
                ProfitNow,
                SaleCommission,
                TeacherProfitNow,
                MeritsProfit,
                FiniceReciveList = recultFiniceList.Where(t => t.Type == WFTempLateClassEnum.IN && t.OrderID > 0),//学费收入
                OtherFiniceReciveList = recultFiniceList.Where(t => t.Type == WFTempLateClassEnum.IN && t.OrderID <= 0),//其他收入
                OtherFinicePayList = recultFiniceList.Where(t => t.Type == WFTempLateClassEnum.OUT),//其他支出
                //退费，班级费用（书本，房租等），提成，课时费确定 在弄
            };
            return ApiResult.Success("", resultData);
        }

        /// <summary>
        /// 用户信息下载
        /// </summary>
        [HttpPost]
        [Obsolete]
        public FileContentResult GetClassBalanceSheetToExcel()
        {
            int classId = base.ParmJObj.GetInt("ClassId", 0);
            int schoolId = base.ParmJObj.GetInt("School_Id");
            int SourceId = base.ParmJObj.GetInt("SourceId");
            List<ExcelDataSource> slist = new List<ExcelDataSource>();
            string ExcelName = "用户列表" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls";
            if (classId <= 0 && SourceId <= 0)
            {
                var byteData1 = ExcelTempLateHelper.ToExcelExtend(slist);
                return File(byteData1, "application/octet-stream", ExcelName);
            }
            RB_Finance_Extend model = new RB_Finance_Extend() { TCID = classId, OrderSource = OrderResourceEnum.Education, RB_Group_Id = base.UserInfo.Group_Id, FinanceType = 2 };
            if (SourceId > 0)
            {
                model.TCID = SourceId;
                model.OrderSource = OrderResourceEnum.EducationStudy;
            }
            var financeList = financeModule.GetFinanceInfoList(model).Where(t => (t.Status == FinanceAuditStatus.InReview || t.Status == FinanceAuditStatus.Pass || t.Status == FinanceAuditStatus.CTemporary)).ToList();

            //收入
            decimal IncomeReceive = 0;//应收
            decimal IncomeActual = 0;//实收
            var orderList = orderModule.GetClassOrderList_V2(classId, base.UserInfo.Group_Id, SourceId).Where(x => (int)x.OrderState < 3);
            IncomeReceive = (orderList != null && orderList.Any()) ? orderList.Sum(x => x.PreferPrice) : 0;
            IncomeActual = (orderList != null && orderList.Any()) ? orderList.Sum(x => x.Income) : 0;

            //其他
            decimal OtherIncomeReceive = 0;//应收
            decimal OtherIncomeActual = 0;//实收
            OtherIncomeReceive = (financeList != null && financeList.Any()) ? financeList.Where(x => x.Type == WFTempLateClassEnum.IN && x.OrderID <= 0).Sum(x => x.Money ?? 0) : 0;
            OtherIncomeActual = (financeList != null && financeList.Any()) ? financeList.Where(x => x.Type == WFTempLateClassEnum.IN && x.OrderID <= 0).Sum(x => x.PayMoney ?? 0) : 0;
            //支出
            decimal PayReceive = 0;//应付
            decimal PayActual = 0;//实付
            PayReceive = (financeList != null && financeList.Any()) ? financeList.Where(x => x.Type == WFTempLateClassEnum.OUT).Sum(x => x.Money ?? 0) : 0;
            PayActual = (financeList != null && financeList.Any()) ? financeList.Where(x => x.Type == WFTempLateClassEnum.OUT).Sum(x => x.PayMoney ?? 0) : 0;

            //提成
            decimal SaleCommission = (orderList != null && orderList.Any()) ? orderList.Where(x => x.IsCommissionGive == 1).Sum(x => x.CommissionMoney) : 0; ;//销售提成
            var teacherProfitList = teachingRewardsModule.GetBonusDetailList(new RB_Teaching_BonusDetail_ViewModel { Group_Id = base.UserInfo.Group_Id, ClassId = classId, School_Id = schoolId });
            decimal TeacherProfitNow = (teacherProfitList != null && teacherProfitList.Any()) ? teacherProfitList.Sum(x => x.Money) : 0;//老师提成

            var meritsProfitList = teachingRewardsModule.GetTeachingPerfList(new RB_Teaching_Perf_ViewModel { Group_Id = base.UserInfo.Group_Id, ClassId = classId, School_Id = schoolId });
            decimal MeritsProfit = (meritsProfitList != null && meritsProfitList.Any()) ? (meritsProfitList.OrderByDescending(x => x.CreateTime).FirstOrDefault()?.Money ?? 0) : 0;//绩效提成

            PayReceive += (SaleCommission + TeacherProfitNow + MeritsProfit);//支出加上
            PayActual += (SaleCommission + TeacherProfitNow + MeritsProfit);

            //利润
            decimal ProfitActual = 0;//实际利润
            decimal ProfitNow = 0;//当前利润
            ProfitActual = (IncomeReceive + OtherIncomeReceive) - PayReceive;
            ProfitNow = (IncomeActual + OtherIncomeActual) - PayActual;

            string title = "班级情况";
            string subtitle = "班级名称";
            string thirdTitle = "课程名称：";
            string fourTitle = "开班时间：";

            //项目信息
            var projectInfo = new RB_Class_ViewModel();
            if (classId > 0)
            {
                var classInfo = classModule.GetClassAndCourseListRepository(new Model.ViewModel.Course.RB_Class_ViewModel { Group_Id = base.UserInfo.Group_Id, ClassId = classId, School_Id = schoolId }).FirstOrDefault();
                projectInfo = new RB_Class_ViewModel()
                {
                    ClassName = classInfo?.ClassName,
                    CourseName = classInfo?.CourseName,
                    TeacherName = classInfo?.TeacherName,
                    OpenTimeStr = classInfo?.OpenTime.ToString("yyyy-MM-dd")
                };
            }
            else if (SourceId > 0)
            {
                var studyInfo = studyAbroadModule.GetStudyAbroadModule(SourceId);
                projectInfo = new RB_Class_ViewModel
                {
                    ClassName = studyInfo?.Name,
                    CourseName = "",
                    TeacherName = "",
                    OpenTimeStr = ""
                };
                title = (studyInfo?.Type ?? 0) == 1 ? "留学情况" : "就业情况";
                subtitle = "项目名称";
                thirdTitle = "";
                fourTitle = "";
            }
            List<RB_Finance_Extend> financeReciveList = financeList.Where(t => t.Type == WFTempLateClassEnum.IN).ToList();
            List<RB_Finance_Extend> financePayList = financeList.Where(t => t.Type == WFTempLateClassEnum.OUT).ToList();

            try
            {

                ExcelDataSource teamBalance = new ExcelDataSource(50)
                {
                    ExcelRows = new List<ExcelColumn>()
                    {
                          new ExcelColumn(value: title,colspan:2)
                          {
                               IsBold = true,
                               CellWidth=30,
                               HAlignmentEnum= HAlignmentEnum.LEFT,
                          },
                          new ExcelColumn(value:"学费收入",colspan:2)
                          {
                               IsBold = true,
                               CellWidth=20,
                               HAlignmentEnum= HAlignmentEnum.LEFT
                          },
                          new ExcelColumn(value:"其他收入",colspan:2)
                          {
                               IsBold = true,
                               CellWidth=20,
                               HAlignmentEnum= HAlignmentEnum.LEFT
                          },
                          new ExcelColumn(value:"支出",colspan:2)
                          {
                               IsBold = true,
                                CellWidth=20,
                               HAlignmentEnum= HAlignmentEnum.LEFT
                          },
                          new ExcelColumn(value:"利润",colspan:2)
                          {
                               IsBold = true,
                                CellWidth=20,
                               HAlignmentEnum= HAlignmentEnum.LEFT
                          },
                          new ExcelColumn(value:"提成",colspan:2)
                          {
                               IsBold = true,
                                CellWidth=20,
                               HAlignmentEnum= HAlignmentEnum.LEFT
                          },
                    }
                };
                slist.Add(teamBalance);

                ExcelDataSource firstRow = new ExcelDataSource(30)
                {
                    ExcelRows = new List<ExcelColumn>()
                    {
                        new ExcelColumn(value: $"{subtitle}：{projectInfo.ClassName}",colspan:2)
                        {
                            HAlignmentEnum= HAlignmentEnum.LEFT,
                        },
                        new ExcelColumn(value:$"应收：￥{IncomeReceive}",colspan:2)
                        {
                            CellWidth=15,
                            HAlignmentEnum= HAlignmentEnum.LEFT
                        },
                        new ExcelColumn(value:$"应收：￥{OtherIncomeReceive}",colspan:2)
                        {
                            HAlignmentEnum= HAlignmentEnum.LEFT
                        }, new ExcelColumn(value:$"应付：￥{PayReceive}",colspan:2)
                        {
                            HAlignmentEnum= HAlignmentEnum.LEFT
                        },
                        new ExcelColumn(value:$"实际利润：￥{ProfitActual}",colspan:2)
                        {
                            HAlignmentEnum= HAlignmentEnum.LEFT
                        },
                        new ExcelColumn(value:$"销售提成：￥{SaleCommission}",colspan:2)
                        {
                               HAlignmentEnum= HAlignmentEnum.LEFT
                        }
                    }
                };
                slist.Add(firstRow);
                ExcelDataSource secondRow = new ExcelDataSource(30)
                {
                    ExcelRows = new List<ExcelColumn>()
                    {
                          new ExcelColumn(value: $"{thirdTitle}{projectInfo.CourseName}",colspan:2)
                          {
                               HAlignmentEnum= HAlignmentEnum.LEFT,
                          },
                          new ExcelColumn(value:$"实收：￥{IncomeActual}",colspan:2)
                          {
                               CellWidth=15,
                               HAlignmentEnum= HAlignmentEnum.LEFT
                          },
                          new ExcelColumn(value:$"实收：￥{OtherIncomeActual}",colspan:2)
                          {
                               HAlignmentEnum= HAlignmentEnum.LEFT
                          },
                          new ExcelColumn(value:$"实付：￥{PayActual}",colspan:2)
                          {
                               HAlignmentEnum= HAlignmentEnum.LEFT
                          },
                          new ExcelColumn(value:$"当前利润：￥{ProfitNow}",colspan:2)
                          {
                               HAlignmentEnum= HAlignmentEnum.LEFT
                          },
                          new ExcelColumn(value:$"课时费：￥{TeacherProfitNow}",colspan:2)
                          {
                               HAlignmentEnum= HAlignmentEnum.LEFT
                          }
                    }
                };
                slist.Add(secondRow);
                ExcelDataSource thirdRow = new ExcelDataSource(30)
                {
                    ExcelRows = new List<ExcelColumn>()
                    {
                          new ExcelColumn(value: $"{fourTitle}{projectInfo.OpenTimeStr:yyyy-MM-dd}",colspan:2)
                          {
                               HAlignmentEnum= HAlignmentEnum.LEFT,
                          },
                          new ExcelColumn(value:$"待收：￥{(IncomeReceive-IncomeActual)}",colspan:2)
                          {
                               CellWidth=15,
                               HAlignmentEnum= HAlignmentEnum.LEFT
                          },
                          new ExcelColumn(value:$"待收：￥{(OtherIncomeReceive-OtherIncomeActual)}",colspan:2)
                          {
                               HAlignmentEnum= HAlignmentEnum.LEFT
                          },
                          new ExcelColumn(value:$"待付：￥{(PayReceive-PayActual)}",colspan:2)
                          {
                               HAlignmentEnum= HAlignmentEnum.LEFT
                          },
                           new ExcelColumn(value:"",colspan:2)
                          {
                               HAlignmentEnum= HAlignmentEnum.LEFT
                          }, new ExcelColumn(value:$"绩效奖励：￥{MeritsProfit}",colspan:2)
                          {
                               HAlignmentEnum= HAlignmentEnum.LEFT
                          }
                 }
                };
                slist.Add(thirdRow);
                ExcelDataSource fourRow = new ExcelDataSource(30)
                {
                    ExcelRows = new List<ExcelColumn>()
                    {
                        new ExcelColumn(value: $"带课老师：{projectInfo.TeacherName}",colspan:2)
                        {
                            HAlignmentEnum= HAlignmentEnum.LEFT,
                        },
                        new ExcelColumn(value:"",colspan:2)
                        {
                            HAlignmentEnum= HAlignmentEnum.LEFT
                        },
                        new ExcelColumn(value:"",colspan:2)
                        {
                           HAlignmentEnum= HAlignmentEnum.LEFT
                        },
                        new ExcelColumn(value:"",colspan:2)
                        {
                           HAlignmentEnum= HAlignmentEnum.LEFT
                        },
                        new ExcelColumn(value:"",colspan:2)
                        {
                           HAlignmentEnum= HAlignmentEnum.LEFT
                        },
                        new ExcelColumn(value:"",colspan:2)
                        {
                           HAlignmentEnum= HAlignmentEnum.LEFT
                        }
                    }
                };
                if (classId > 0)
                {
                    slist.Add(fourRow);
                }
                ExcelDataSource splitRow = new ExcelDataSource(40)
                {
                    ExcelRows = new List<ExcelColumn>()
                };
                slist.Add(splitRow);

                #region 收支列表
                ExcelDataSource reciveRow = new ExcelDataSource(40)
                {
                    ExcelRows = new List<ExcelColumn>()
                    {
                          new ExcelColumn(value: "收入",colspan:9)
                          {
                               IsBold = true,
                               IsSetBorder =false,
                               HAlignmentEnum= HAlignmentEnum.LEFT,
                          }
                    }
                };
                slist.Add(reciveRow);
                GenerateTeamBalanceSheet(slist, financeReciveList, WFTempLateClassEnum.IN);
                ExcelDataSource payRow = new ExcelDataSource(40)
                {
                    ExcelRows = new List<ExcelColumn>()
                    {
                          new ExcelColumn(value: "支出",colspan:9)
                          {
                               IsBold = true,
                               IsSetBorder =false,
                               HAlignmentEnum= HAlignmentEnum.LEFT,
                          }
                    }
                };
                slist.Add(payRow);
                GenerateTeamBalanceSheet(slist, financePayList, WFTempLateClassEnum.OUT);
                #endregion

                var byteData = ExcelTempLateHelper.ToExcelExtend(slist);
                return File(byteData, "application/octet-stream", ExcelName);
            }
            catch (Exception ex)
            {
                LogHelper.Write(ex, string.Format("GetFileFromWebApi_requestData: {0}", JsonHelper.Serialize(RequestParm)));
                var byteData1 = ExcelTempLateHelper.ToExcelExtend(slist);
                return File(byteData1, "application/octet-stream", ExcelName);
            }
        }

        /// <summary>
        /// 添加修改班级收支设置
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult SetClassFinance()
        {
            var extModel = Common.Plugin.JsonHelper.DeserializeObject<RB_Class_Finance_ViewModel>(RequestParm.Msg.ToString());
            extModel.CreateTime = DateTime.Now;
            extModel.CreateBy = UserInfo.Id;
            extModel.UpdateBy = UserInfo.Id;
            extModel.UpdateTime = DateTime.Now;
            extModel.Group_Id = this.UserInfo.Group_Id;
            bool flag = classModule.SetClassFinance(extModel);
            return flag ? ApiResult.Success() : ApiResult.Failed();
        }



        /// <summary>
        /// 生成收入支出excel内容
        /// </summary>
        /// <param name="list">excel容器</param>
        /// <param name="financeList">收支列表</param>
        /// <param name="type">收支类型</param>
        private void GenerateTeamBalanceSheet(List<ExcelDataSource> excelDataSourceContainer, List<RB_Finance_Extend> financeList, WFTempLateClassEnum type)
        {
            ExcelDataSource title = new ExcelDataSource(35)
            {
                ExcelRows = new List<ExcelColumn>() {
                      new ExcelColumn(value: "单号"){
                            IsBold = true,
                          CellWidth = 10,
                           HAlignmentEnum= HAlignmentEnum.LEFT,
                      },
                      new ExcelColumn(value: "费用类型"){
                            IsBold = true,
                            CellWidth = 20,
                           HAlignmentEnum= HAlignmentEnum.LEFT,
                      },
                      new ExcelColumn(value: "交易信息",colspan:2){
                            IsBold = true,
                          CellWidth=30,
                           HAlignmentEnum= HAlignmentEnum.LEFT,
                      },
                      new ExcelColumn(value: "金额"){
                            IsBold = true,
                           CellWidth=30,
                           HAlignmentEnum= HAlignmentEnum.LEFT,
                      },
                      new ExcelColumn(value: "交易日期"){
                            IsBold = true,
                           CellWidth=15,
                           HAlignmentEnum= HAlignmentEnum.LEFT,
                      },
                       new ExcelColumn(value: "制单人员"){
                             IsBold = true,
                            CellWidth=15,
                           HAlignmentEnum= HAlignmentEnum.LEFT,
                      },
                        new ExcelColumn(value: "审核状态"){
                              IsBold = true,
                             CellWidth=20,
                           HAlignmentEnum= HAlignmentEnum.LEFT,
                      },
                        new ExcelColumn(value: "当前审核人"){
                              IsBold = true,
                             CellWidth=20,
                           HAlignmentEnum= HAlignmentEnum.LEFT,
                      }
                 }
            };
            excelDataSourceContainer.Add(title);
            if (financeList != null && financeList.Count > 0)
            {
                foreach (var item in financeList)
                {
                    ExcelDataSource content = new ExcelDataSource(30);
                    List<ExcelColumn> colums = new List<ExcelColumn>
                    {
                        new ExcelColumn(value: item.FrID.ToString())
                        {
                            HAlignmentEnum = HAlignmentEnum.LEFT
                        },
                        new ExcelColumn(value: item.CostTypeList != null ? string.Join(",", item.CostTypeList) : "")
                        {
                            HAlignmentEnum = HAlignmentEnum.LEFT
                        },
                        new ExcelColumn(value: item.BankList != null ? GetCostDetial(item.BankList) : "", colspan: 2)
                        {
                            HAlignmentEnum = HAlignmentEnum.LEFT
                        }
                    };
                    if (type == WFTempLateClassEnum.IN)
                    {
                        colums.Add(new ExcelColumn(value: $"应收:￥{item.Money} 实收：￥{item.PayMoney}")
                        {
                            HAlignmentEnum = HAlignmentEnum.LEFT
                        });
                    }
                    else
                    {
                        colums.Add(new ExcelColumn(value: $"应付:￥{item.Money} 实付：￥{item.PayMoney}")
                        {
                            HAlignmentEnum = HAlignmentEnum.LEFT
                        });
                    }
                    colums.Add(new ExcelColumn(value: item.TradeDate.HasValue ? item.TradeDate.Value.ToString("yyyy/MM/dd") : "")
                    {
                        HAlignmentEnum = HAlignmentEnum.LEFT
                    });
                    string EmName = item.RB_CreateByName;
                    colums.Add(new ExcelColumn(value: string.IsNullOrWhiteSpace(EmName) ? "" : EmName)
                    {
                        HAlignmentEnum = HAlignmentEnum.LEFT
                    });
                    colums.Add(new ExcelColumn(value: EnumHelper.ToName(item.Status.Value))
                    {
                        HAlignmentEnum = HAlignmentEnum.LEFT
                    });
                    List<string> auditNameList = new List<string>();
                    foreach (var audit in item.AuditList)
                    {
                        auditNameList.Add(audit.AuditEmName);
                    }
                    colums.Add(new ExcelColumn(value: string.Join(",", auditNameList))
                    {
                        HAlignmentEnum = HAlignmentEnum.LEFT
                    });
                    content.ExcelRows = colums;
                    excelDataSourceContainer.Add(content);
                }
            }
        }

        /// <summary>
        /// 获取交易明细信息
        /// </summary>
        /// <param name="bankList"></param>
        /// <returns></returns>
        private string GetCostDetial(List<RB_TradeWay_Extend> wayList)
        {
            if (wayList.Count > 0)
            {
                List<string> wayDetialList = new List<string>();
                foreach (var item in wayList)
                {
                    wayDetialList.Add($"{item.Alias} {item.CurrencyName}:{item.OriginalMoney} 汇率:{item.Rate}");
                }
                return string.Join(",", wayDetialList);
            }
            else
            {
                return "";
            }
        }

        #region 获取报价单单据列表

        /// <summary>
        /// 获取报价单收支明细
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public ApiResult GetOfferOrderBalanceSheet()
        {
            int OfferId = base.ParmJObj.GetInt("OfferId", 0);
            if (OfferId <= 0)
            {
                return ApiResult.Failed("班级Id不存在");
            }
            RB_Finance_Extend model = new RB_Finance_Extend() { OtherType = 29, ReFinanceId = OfferId, OrderSource = OrderResourceEnum.Education, RB_Group_Id = base.UserInfo.Group_Id, FinanceType = 2, IsSelectNormal = 1 };
            var financeList = financeModule.GetFinanceInfoList(model);

            var recultFiniceList = financeList.Select(x => new
            {
                x.FrID,
                x.Type,
                x.TCID,
                x.OrderID,
                x.Money,
                x.PayMoney,
                AirTotalMoney = x.AirTotalMoney ?? 0,
                AirTotalPayMoney = x.AirTotalPayMoney ?? 0,
                CostTypeList = x.CostTypeList.Distinct().ToList(),
                x.OrderSource,
                AuditList = x.AuditList.Select(t => new { t.AuditEmId, t.AuditEmName, AuditDate = t.AuditDate.HasValue ? t.AuditDate.Value.ToString("yyyy/MM/dd") : "" }),
                StatusStr = EnumHelper.ToName(x.Status.Value),
                x.RB_CreateByName,
                CreateDate = x.CreateDate.Value.ToString("yyyy-MM-dd"),
                TradeDate = x.TradeDate.HasValue ? x.TradeDate.Value.ToString("yyyy-MM-dd") : "",
                TradeWayList = x.BankList?.Select(y => new { y.AccountType, y.Alias, y.BankNo, TypeName = EnumHelper.ToName(y.Type), y.CurrencyName, y.OriginalMoney, y.Money, y.Rate })
            });

            var resultData = new
            {
                IncomeList = recultFiniceList.Where(t => t.Type == WFTempLateClassEnum.IN),//学费收入
                ExpendList = recultFiniceList.Where(t => t.Type == WFTempLateClassEnum.OUT),//其他支出
                IncomeReceive = recultFiniceList.Where(t => t.Type == WFTempLateClassEnum.IN).Sum(x => x.Money),
                IncomeActual = recultFiniceList.Where(t => t.Type == WFTempLateClassEnum.IN).Sum(x => x.PayMoney),
                ExpendReceive = recultFiniceList.Where(t => t.Type == WFTempLateClassEnum.OUT).Sum(x => x.Money),
                ExpendActual = recultFiniceList.Where(t => t.Type == WFTempLateClassEnum.OUT).Sum(x => x.PayMoney)
            };
            return ApiResult.Success("", resultData);
        }

        #endregion

        #region 报表查询

        /// <summary>
        /// 获取营收报表分页列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetRevenueReportPageList()
        {
            var userInfo = base.UserInfo;
            var pageModel = JsonHelper.DeserializeObject<ResultPageModel>(RequestParm.Msg.ToString());
            var dmodel = JsonHelper.DeserializeObject<RB_Edu_RevenueReport_Extend>(RequestParm.Msg.ToString());
            dmodel.Rb_Group_Id = userInfo.Group_Id;

            var list = financeModule.GetRevenueReportPageList(pageModel.PageIndex, pageModel.PageSize, out long count, dmodel);
            pageModel.Count = Convert.ToInt32(count);
            pageModel.PageData = list;

            var fumodel = financeModule.GetUpdateRecordNew();
            int Status = 0;
            string UpdateTime = "";
            if (fumodel != null)
            {
                if (fumodel.EndDate.HasValue)
                {
                    Status = 2;//更新完毕了
                    UpdateTime = fumodel.StartDate.Value.ToString("yyyy-MM-dd HH:mm");
                }
                else
                {
                    Status = 1;//正在更新
                    UpdateTime = fumodel.StartDate.Value.ToString("yyyy-MM-dd HH:mm");
                }
            }
            return ApiResult.Success("", new { Status, UpdateTime, pageModel });
        }

        /// <summary>
        /// 营收报表Excel下载
        /// </summary>
        [HttpPost]
        [Obsolete]
        public FileContentResult GetRevenueReportToExcel()
        {
            var userInfo = base.UserInfo;
            var dmodel = JsonHelper.DeserializeObject<RB_Edu_RevenueReport_Extend>(RequestParm.Msg.ToString());
            dmodel.Rb_Group_Id = userInfo.Group_Id;
            string ExcelName = "营收报表" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls";
            List<ExcelDataSource> slist = new List<ExcelDataSource>();
            ExcelDataSource header = new ExcelDataSource()
            {
                ExcelRows = new List<ExcelColumn>(30) {
                    new ExcelColumn(value: "校区") { CellWidth = 15, HAlignmentEnum = HAlignmentEnum.CENTER, VAlignmentEnum = VAlignmentEnum.CENTER },
                    new ExcelColumn(value: "编号") { CellWidth = 10, HAlignmentEnum = HAlignmentEnum.CENTER, VAlignmentEnum = VAlignmentEnum.CENTER },
                    new ExcelColumn(value: "班号") { CellWidth = 15, HAlignmentEnum = HAlignmentEnum.CENTER, VAlignmentEnum = VAlignmentEnum.CENTER },
                    new ExcelColumn(value: "班级") { CellWidth = 20, HAlignmentEnum = HAlignmentEnum.CENTER, VAlignmentEnum = VAlignmentEnum.CENTER },
                    new ExcelColumn(value: "开班时间") { CellWidth = 15, HAlignmentEnum = HAlignmentEnum.CENTER, VAlignmentEnum = VAlignmentEnum.CENTER },
                    new ExcelColumn(value: "班级状态") { CellWidth = 15, HAlignmentEnum = HAlignmentEnum.CENTER, VAlignmentEnum = VAlignmentEnum.CENTER },
                    new ExcelColumn(value: "课程") { CellWidth = 15, HAlignmentEnum = HAlignmentEnum.CENTER, VAlignmentEnum = VAlignmentEnum.CENTER },
                    new ExcelColumn(value: "教师") { CellWidth = 15, HAlignmentEnum = HAlignmentEnum.CENTER, VAlignmentEnum = VAlignmentEnum.CENTER },
                    new ExcelColumn(value: "原价") { CellWidth = 15, HAlignmentEnum = HAlignmentEnum.CENTER, VAlignmentEnum = VAlignmentEnum.CENTER },
                    new ExcelColumn(value: "实收") { CellWidth = 15, HAlignmentEnum = HAlignmentEnum.CENTER, VAlignmentEnum = VAlignmentEnum.CENTER },
                    new ExcelColumn(value: "退款") { CellWidth = 15, HAlignmentEnum = HAlignmentEnum.CENTER, VAlignmentEnum = VAlignmentEnum.CENTER },
                    new ExcelColumn(value: "手续费") { CellWidth = 15, HAlignmentEnum = HAlignmentEnum.CENTER, VAlignmentEnum = VAlignmentEnum.CENTER },
                    new ExcelColumn(value: "优惠金额") { CellWidth = 15, HAlignmentEnum = HAlignmentEnum.CENTER, VAlignmentEnum = VAlignmentEnum.CENTER },
                    new ExcelColumn(value: "待收") { CellWidth = 15, HAlignmentEnum = HAlignmentEnum.CENTER, VAlignmentEnum = VAlignmentEnum.CENTER },
                    new ExcelColumn(value: "溢收") { CellWidth = 15, HAlignmentEnum = HAlignmentEnum.CENTER, VAlignmentEnum = VAlignmentEnum.CENTER },
                    new ExcelColumn(value: "客人数量") { CellWidth = 15, HAlignmentEnum = HAlignmentEnum.CENTER, VAlignmentEnum = VAlignmentEnum.CENTER },
                    new ExcelColumn(value: "其他收入") { CellWidth = 15, HAlignmentEnum = HAlignmentEnum.CENTER, VAlignmentEnum = VAlignmentEnum.CENTER },
                    new ExcelColumn(value: "提成") { CellWidth = 15, HAlignmentEnum = HAlignmentEnum.CENTER, VAlignmentEnum = VAlignmentEnum.CENTER },
                    new ExcelColumn(value: "课时费") { CellWidth = 15, HAlignmentEnum = HAlignmentEnum.CENTER, VAlignmentEnum = VAlignmentEnum.CENTER },
                    new ExcelColumn(value: "绩效奖金") { CellWidth = 15, HAlignmentEnum = HAlignmentEnum.CENTER, VAlignmentEnum = VAlignmentEnum.CENTER },
                    new ExcelColumn(value: "其他支出") { CellWidth = 15, HAlignmentEnum = HAlignmentEnum.CENTER, VAlignmentEnum = VAlignmentEnum.CENTER },
                    new ExcelColumn(value: "应付") { CellWidth = 15, HAlignmentEnum = HAlignmentEnum.CENTER, VAlignmentEnum = VAlignmentEnum.CENTER },
                    new ExcelColumn(value: "毛利") { CellWidth = 15, HAlignmentEnum = HAlignmentEnum.CENTER, VAlignmentEnum = VAlignmentEnum.CENTER },
                    new ExcelColumn(value: "毛利率") { CellWidth = 15, HAlignmentEnum = HAlignmentEnum.CENTER, VAlignmentEnum = VAlignmentEnum.CENTER },
                    new ExcelColumn(value: "实际利润") { CellWidth = 15, HAlignmentEnum = HAlignmentEnum.CENTER, VAlignmentEnum = VAlignmentEnum.CENTER },
                    new ExcelColumn(value: "备注") { CellWidth = 15, HAlignmentEnum = HAlignmentEnum.CENTER, VAlignmentEnum = VAlignmentEnum.CENTER },
                    new ExcelColumn(value: "班级课时") { CellWidth = 15, HAlignmentEnum = HAlignmentEnum.CENTER, VAlignmentEnum = VAlignmentEnum.CENTER },
                    new ExcelColumn(value: "已上课时") { CellWidth = 15, HAlignmentEnum = HAlignmentEnum.CENTER, VAlignmentEnum = VAlignmentEnum.CENTER },
                    new ExcelColumn(value: "已上课金额") { CellWidth = 15, HAlignmentEnum = HAlignmentEnum.CENTER, VAlignmentEnum = VAlignmentEnum.CENTER },
                    new ExcelColumn(value: "平均收入") { CellWidth = 15, HAlignmentEnum = HAlignmentEnum.CENTER, VAlignmentEnum = VAlignmentEnum.CENTER },
                    new ExcelColumn(value: "平均成本") { CellWidth = 15, HAlignmentEnum = HAlignmentEnum.CENTER, VAlignmentEnum = VAlignmentEnum.CENTER },
                    new ExcelColumn(value: "平均利润") { CellWidth = 15, HAlignmentEnum = HAlignmentEnum.CENTER, VAlignmentEnum = VAlignmentEnum.CENTER },
                }
            };
            slist.Add(header);

            try
            {
                var list = financeModule.GetRevenueReportPageList(1, 100000, out long count, dmodel);
                #region 组装数据
                foreach (var item in list)
                {
                    string ClassState = "结课";
                    if (item.ClassState == 1) { ClassState = "未开班"; }
                    else if (item.ClassState == 2) { ClassState = "学习中"; }
                    if (item.Id == -1)
                    {
                        ClassState = "";
                    }
                    ExcelDataSource datarow = new ExcelDataSource()
                    {
                        ExcelRows = new List<ExcelColumn>(30) {
                            new ExcelColumn(value: item.Id == -1 ? "合计" : item.SchoolName){ },
                            new ExcelColumn(value: item.Id == -1 ? "" : item.ClassId.ToString()){ },
                            new ExcelColumn(value: item.ClassNo){ },
                            new ExcelColumn(value: item.ClassName){ },
                            new ExcelColumn(value: item.ClassBeginsStr){ },
                            new ExcelColumn(value: ClassState){ },
                            new ExcelColumn(value: item.CourseName){ },
                            new ExcelColumn(value: item.TeacherName){ },
                            new ExcelColumn(value: item.YingShou.ToString()){ },
                            new ExcelColumn(value: item.ShiShou.ToString()){ },
                            new ExcelColumn(value: item.TuiKuan.ToString()){ },
                            new ExcelColumn(value: item.PingTaiShuiJin.ToString()){ },
                            new ExcelColumn(value: item.YouHui.ToString()){ },
                            new ExcelColumn(value: item.DaiShou.ToString()){ },
                            new ExcelColumn(value: item.YiShou.ToString()){ },
                            new ExcelColumn(value: item.GuestNum.ToString()){ },
                            new ExcelColumn(value: item.QTShouRu.ToString()){ },
                            new ExcelColumn(value: item.TiCheng.ToString()){ },
                            new ExcelColumn(value: item.ClassFee.ToString()){ },
                            new ExcelColumn(value: item.JiXiaoMoney.ToString()){ },
                            new ExcelColumn(value: item.QTZhiChu.ToString()){ },
                            new ExcelColumn(value: item.YingFu.ToString()){ },
                            new ExcelColumn(value: item.MaoLi.ToString()){ },
                            new ExcelColumn(value: item.MaoLiRate.ToString()){ },
                            new ExcelColumn(value: item.ShiJiLiRun.ToString()){ },
                            new ExcelColumn(value: item.Remark){ },
                            new ExcelColumn(value: item.HoursNum.ToString()){ },
                            new ExcelColumn(value: item.ConsumeNum.ToString()){ },
                            new ExcelColumn(value: item.ConsumeMoney.ToString("#0.00")){ },
                            new ExcelColumn(value: item.AverageIncome.ToString()){ },
                            new ExcelColumn(value: item.AverageCost.ToString()){ },
                            new ExcelColumn(value: item.AverageProfit.ToString()){ },
                        }
                    };
                    slist.Add(datarow);
                }
                #endregion

                var byteData = ExcelTempLateHelper.ToExcelExtend(slist);
                return File(byteData, "application/octet-stream", ExcelName);
            }
            catch (Exception ex)
            {
                LogHelper.Write(ex, string.Format("GetFileFromWebApi_requestData: {0}", JsonHelper.Serialize(RequestParm)));
                var byteData1 = ExcelTempLateHelper.ToExcelExtend(slist);
                return File(byteData1, "application/octet-stream", ExcelName);
            }

        }

        /// <summary>
        /// 获取简易报表列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetEasyReportList()
        {
            var userInfo = base.UserInfo;
            var model = JsonHelper.DeserializeObject<RB_SimpleReport_Extend>(RequestParm.Msg.ToString());
            model.Rb_Group_Id = userInfo.Group_Id;
            var list = financeModule.GetEasyReportList(new RB_SimpleReport_Extend() { Year = model.Year, Rb_Group_Id = userInfo.Group_Id, BranchId = model.BranchId });

            var fumodel = financeModule.GetUpdateRecordNew();
            int Status = 0;
            string UpdateStartTime = "";
            string UpdateEndTime = "";
            if (fumodel != null)
            {
                if (fumodel.EndDate.HasValue)
                {
                    Status = 2;//更新完毕了
                    UpdateEndTime = fumodel.EndDate.Value.ToString("yyyy-MM-dd HH:mm");
                    UpdateStartTime = fumodel.StartDate.Value.ToString("yyyy-MM-dd HH:mm");
                }
                else
                {
                    Status = 1;//正在更新
                    UpdateStartTime = fumodel.StartDate.Value.ToString("yyyy-MM-dd HH:mm");
                }
            }

            var obj = new { Status, UpdateStartTime, UpdateEndTime, list = list?.Select(y => new { y.ID, y.Year, y.BranchId, y.CostIds, y.CostTypeIds, y.Sort, ZhaiYao = y.Abstract, YiYue = y.January, ErYue = y.February, SanYue = y.March, DiYiJiDu = y.FirstQuarter, SiYue = y.April, WuYue = y.May, LiuYue = y.June, DiErJiDu = y.SecondQuarter, ShangBanNianBao = y.HalfReport, QiYue = y.July, BaYue = y.August, JiuYue = y.September, DiSanJiDu = y.ThirdQuarter, ShiYue = y.October, ShiYiYue = y.November, ShiErYue = y.December, DiSiJiDu = y.FourthQuarter, XiaBanNianBao = y.LastHalfReport, ZongJi = y.Total }) };
            return ApiResult.Success("", obj);
        }

        /// <summary>
        /// 导出简易报表列表
        /// </summary>
        /// <returns></returns>
        [Obsolete]
        public FileContentResult OutToExcelEasyReportList()
        {
            List<ExcelDataSource> slist = new List<ExcelDataSource>();
            ExcelDataSource header = new ExcelDataSource()
            {
                ExcelRows = new List<ExcelColumn>(30) {
                            new ExcelColumn(value:"摘要"){CellWidth=20,HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER },
                            new ExcelColumn(value:"一月"){CellWidth=15,HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER },
                            new ExcelColumn(value:"二月"){CellWidth=20,HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER },
                            new ExcelColumn(value:"三月"){CellWidth=15,HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER },
                            new ExcelColumn(value:"第一季度"){CellWidth=15,HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER },
                            new ExcelColumn(value:"四月"){CellWidth=15,HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER },
                            new ExcelColumn(value:"五月"){CellWidth=15,HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER },
                            new ExcelColumn(value:"六月"){CellWidth=15,HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER },
                            new ExcelColumn(value:"第二季度"){CellWidth=15,HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER },
                            new ExcelColumn(value:"上半年报"){CellWidth=15,HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER },
                            new ExcelColumn(value:"七月"){CellWidth=15,HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER },
                            new ExcelColumn(value:"八月"){CellWidth=15,HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER },
                            new ExcelColumn(value:"九月"){CellWidth=15,HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER },
                            new ExcelColumn(value:"第三季度"){CellWidth=15,HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER },
                            new ExcelColumn(value:"十月"){CellWidth=15,HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER },
                            new ExcelColumn(value:"十一月"){CellWidth=15,HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER },
                            new ExcelColumn(value:"十二月"){CellWidth=15,HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER },
                            new ExcelColumn(value:"第四季度"){CellWidth=15,HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER },
                            new ExcelColumn(value:"下半年报"){CellWidth=15,HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER },
                            new ExcelColumn(value:"总计"){CellWidth=15,HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER }

                         }
            };
            slist.Add(header);
            string ExcelName = "简易报表" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls";

            var userInfo = base.UserInfo;
            var model = JsonHelper.DeserializeObject<RB_SimpleReport_Extend>(RequestParm.Msg.ToString());
            model.Rb_Group_Id = userInfo.Group_Id;
            var list = financeModule.GetEasyReportList(new RB_SimpleReport_Extend() { Year = model.Year, Rb_Group_Id = userInfo.Group_Id, BranchId = model.BranchId });
            try
            {
                foreach (var sitem in list)
                {
                    if (sitem.Abstract == "以下项均不计入计算")
                    {
                        ExcelDataSource firstRow = new ExcelDataSource(30)
                        {
                            ExcelRows = new List<ExcelColumn>()
                            {
                                new ExcelColumn(value:sitem.Abstract){HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER},
                                new ExcelColumn(value:""){HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER},
                                new ExcelColumn(value:""){HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER},
                                new ExcelColumn(value:""){HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER},
                                new ExcelColumn(value:""){HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER},
                                new ExcelColumn(value:""){HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER},
                                new ExcelColumn(value:""){HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER},
                                new ExcelColumn(value:""){HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER},
                                new ExcelColumn(value:""){HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER},
                                new ExcelColumn(value:""){HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER},
                                new ExcelColumn(value:""){HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER},
                                new ExcelColumn(value:""){HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER},
                                new ExcelColumn(value:""){HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER},
                                new ExcelColumn(value:""){HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER},
                                new ExcelColumn(value:""){HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER},
                                new ExcelColumn(value:""){HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER},
                                new ExcelColumn(value:""){HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER},
                                new ExcelColumn(value:""){HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER},
                                new ExcelColumn(value:""){HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER},
                                new ExcelColumn(value:""){HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER}
                            }
                        };
                        slist.Add(firstRow);
                    }
                    else
                    {
                        ExcelDataSource firstRow = new ExcelDataSource(30)
                        {
                            ExcelRows = new List<ExcelColumn>()
                            {
                                new ExcelColumn(value:sitem.Abstract){HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER},
                                new ExcelColumn(value:sitem.January.ToString()){HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER},
                                new ExcelColumn(value:sitem.February.ToString()){HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER},
                                new ExcelColumn(value:sitem.March.ToString()){HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER},
                                new ExcelColumn(value:sitem.FirstQuarter.ToString()){HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER},
                                new ExcelColumn(value:sitem.April.ToString()){HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER},
                                new ExcelColumn(value:sitem.May.ToString()){HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER},
                                new ExcelColumn(value:sitem.June.ToString()){HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER},
                                new ExcelColumn(value:sitem.SecondQuarter.ToString()){HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER},
                                new ExcelColumn(value:sitem.HalfReport.ToString()){HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER},
                                new ExcelColumn(value:sitem.July.ToString()){HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER},
                                new ExcelColumn(value:sitem.August.ToString()){HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER},
                                new ExcelColumn(value:sitem.September.ToString()){HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER},
                                new ExcelColumn(value:sitem.ThirdQuarter.ToString()){HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER},
                                new ExcelColumn(value:sitem.October.ToString()){HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER},
                                new ExcelColumn(value:sitem.November.ToString()){HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER},
                                new ExcelColumn(value:sitem.December.ToString()){HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER},
                                new ExcelColumn(value:sitem.FourthQuarter.ToString()){HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER},
                                new ExcelColumn(value:sitem.LastHalfReport.ToString()){HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER},
                                new ExcelColumn(value:sitem.Total.ToString()){HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER}
                            }
                        };
                        slist.Add(firstRow);
                    }
                }

                var byteData = ExcelTempLateHelper.ToExcelExtend(slist);
                return File(byteData, "application/octet-stream", ExcelName);
            }
            catch (Exception ex)
            {
                LogHelper.Write(ex, "OutToExcelEasyReportList");
                var byteData1 = ExcelTempLateHelper.ToExcelExtend(slist);
                return File(byteData1, "application/octet-stream", ExcelName);
            }
        }

        #endregion

        #region 财务查询

        /// <summary>
        /// 获取订单完成情况一览分页列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetOrderStudentClassHoursPageList()
        {
            var userInfo = base.UserInfo;
            var pageModel = JsonHelper.DeserializeObject<ResultPageModel>(RequestParm.Msg.ToString());
            var dmodel = JsonHelper.DeserializeObject<RB_Education_Contract_ViewModel>(RequestParm.Msg.ToString());
            dmodel.Group_Id = userInfo.Group_Id;
            dmodel.CType = Common.Enum.Course.ContractTypeEnum.Train;//这里只看培训的

            var list = orderModule.GetOrderStudentClassHoursPageList(pageModel.PageIndex, pageModel.PageSize, out long count, dmodel);
            pageModel.Count = Convert.ToInt32(count);
            pageModel.PageData = list.Select(x => new
            {
                x.Id,
                x.OrderId,
                CreateTime = x.CreateTime.ToString("yyyy-MM-dd"),
                x.ContractNo,
                x.CourseName,
                x.ClassName,
                x.ClassNo,
                x.StudentName,
                x.CreateByName,
                x.CreateByPhoto,
                x.TotalCourseFee,
                x.TotalClassHours,
                TotalUnitPrice = x.TotalClassHours > 0 ? Math.Round(x.TotalCourseFee / x.TotalClassHours, 2, MidpointRounding.AwayFromZero) : 0,
                x.TotalBookFee,
                x.TotalClassFee,
                x.TotalSub,
                x.TotalDiscountMoney,
                x.TotalMoney,
                x.Status,
                IncomeList = x.FinanceList.Where(t => t.Type == WFTempLateClassEnum.IN).Select(z => new
                {
                    z.FrID,
                    TradeDate = z.TradeDate.HasValue ? z.TradeDate.Value.ToString("yyyy-MM-dd") : "",
                    Money = (z.Money ?? 0) + (z.Fee ?? 0),
                    DetailList = z.detailList.Select(y => new
                    {
                        y.ID,
                        y.CostTypeId,
                        y.CostTypeName,
                        y.CurrencyId,
                        y.CurrencyName,
                        y.Money
                    }),
                    TradeWayList = z.BankList.Select(y => new
                    {
                        y.ID,
                        y.Type,
                        TypeName = y.Type.ToName(),
                        y.AccountId,
                        y.Alias,
                        y.BankNo,
                        y.AccountTypeId
                    })
                }),
                ExpendList = x.FinanceList.Where(t => t.Type == WFTempLateClassEnum.OUT).Select(z => new
                {
                    z.FrID,
                    CreateDate = z.CreateDate.HasValue ? z.CreateDate.Value.ToString("yyyy-MM-dd") : "",
                    Money = (z.Money ?? 0) + (z.Fee ?? 0),
                    DetailList = z.detailList.Select(y => new
                    {
                        y.ID,
                        y.CostTypeId,
                        y.CostTypeName,
                        y.CurrencyId,
                        y.CurrencyName,
                        y.Money
                    }),
                    TradeWayList = z.BankList.Select(y => new
                    {
                        y.ID,
                        y.Type,
                        TypeName = y.Type.ToName(),
                        y.AccountId,
                        y.Alias,
                        y.BankNo,
                        y.AccountTypeId
                    })
                }),
                PreferPrice = x.FinanceList.Where(t => t.Type == WFTempLateClassEnum.IN)?.Sum(x => (x.Money ?? 0) + (x.Fee ?? 0)) ?? 0,
                EffectiveClassHours = x.Unit_Price > 0 ? Math.Round(x.Income / x.Unit_Price, 2, MidpointRounding.AwayFromZero) : 0,
                x.Income,
                x.Expend,
                x.UseBookFee,
                x.UseCoursewareFee,
                Progress = x.TotalClassHours > 0 ? Math.Round(Convert.ToDecimal(x.UseClassHours) / x.TotalClassHours * 100, 2, MidpointRounding.AwayFromZero) : 0,
                x.UseClassHours,
                x.Unit_Price,
                x.UseCourseFee,
                x.AdjustPrice,
                SurplusCourseHours = x.TotalClassHours - x.UseClassHours,
                SurplusMoney = x.Income - x.UseBookFee - x.UseCoursewareFee - x.UseCourseFee - x.AdjustPrice
            });
            return ApiResult.Success("", pageModel);
        }

        /// <summary>
        /// 导出订单完成情况一览
        /// </summary>
        /// <returns></returns>
        [Obsolete]
        public FileContentResult OutToExcelOrderStudentClassHoursList()
        {
            List<ExcelDataSource> slist = new List<ExcelDataSource>();
            ExcelDataSource headerTop = new ExcelDataSource()
            {
                ExcelRows = new List<ExcelColumn>(30) {
                            new ExcelColumn(value:"合同信息"){CellWidth=20,HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER,Colspan=16 },
                            new ExcelColumn(value:"收款信息"){CellWidth=15,HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER,Colspan = 6 },
                            new ExcelColumn(value:"合同有效金额"){CellWidth=20,HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER, Colspan = 2 },
                            new ExcelColumn(value:"消费情况"){CellWidth=15,HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER , Colspan = 7},
                            new ExcelColumn(value:"剩余情况"){CellWidth=15,HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER , Colspan = 3},
                         }
            };
            slist.Add(headerTop);
            ExcelDataSource header = new ExcelDataSource()
            {
                ExcelRows = new List<ExcelColumn>(30) {
                            new ExcelColumn(value:"订单号"){CellWidth=20,HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER },
                            new ExcelColumn(value:"签订时间"){CellWidth=15,HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER },
                            new ExcelColumn(value:"合同号"){CellWidth=20,HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER },
                            new ExcelColumn(value:"合同状态"){CellWidth=15,HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER },
                            new ExcelColumn(value:"产品名称"){CellWidth=15,HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER },
                            new ExcelColumn(value:"班号"){CellWidth=15,HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER },
                            new ExcelColumn(value:"班级"){CellWidth=15,HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER },
                            new ExcelColumn(value:"学员名字"){CellWidth=15,HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER },
                            new ExcelColumn(value:"业务人员"){CellWidth=15,HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER },
                            new ExcelColumn(value:"课时"){CellWidth=15,HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER },
                            new ExcelColumn(value:"单价"){CellWidth=15,HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER },
                            new ExcelColumn(value:"课程费用"){CellWidth=15,HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER },
                            new ExcelColumn(value:"教材费"){CellWidth=15,HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER },
                            new ExcelColumn(value:"课件费"){CellWidth=15,HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER },
                            new ExcelColumn(value:"优惠金额"){CellWidth=15,HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER },
                            new ExcelColumn(value:"合同总金额"){CellWidth=15,HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER },
                            new ExcelColumn(value:"单据号"){CellWidth=15,HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER },
                            new ExcelColumn(value:"收款日期"){CellWidth=15,HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER },
                            new ExcelColumn(value:"收款金额"){CellWidth=15,HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER },
                            new ExcelColumn(value:"收款方式"){CellWidth=15,HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER },
                            new ExcelColumn(value:"收款账户"){CellWidth=15,HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER },
                            new ExcelColumn(value:"应收金额"){CellWidth=15,HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER },
                            new ExcelColumn(value:"有效课时"){CellWidth=15,HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER },
                            new ExcelColumn(value:"有效金额"){CellWidth=15,HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER },
                            new ExcelColumn(value:"消费教材费"){CellWidth=15,HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER },
                            new ExcelColumn(value:"消费课件费"){CellWidth=15,HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER },
                            new ExcelColumn(value:"上课进度"){CellWidth=15,HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER },
                            new ExcelColumn(value:"已上课时"){CellWidth=15,HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER },
                            new ExcelColumn(value:"折后单价"){CellWidth=15,HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER },
                            new ExcelColumn(value:"已上课金额"){CellWidth=15,HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER },
                            new ExcelColumn(value:"退/付款金额"){CellWidth=15,HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER },
                            new ExcelColumn(value:"协调金额"){CellWidth=15,HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER },
                            new ExcelColumn(value:"剩余课时"){CellWidth=15,HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER },
                            new ExcelColumn(value:"剩余金额"){CellWidth=15,HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER },
                         }
            };
            slist.Add(header);
            string ExcelName = "订单完成情况" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls";

            var userInfo = base.UserInfo;
            var dmodel = JsonHelper.DeserializeObject<RB_Education_Contract_ViewModel>(RequestParm.Msg.ToString());
            dmodel.Group_Id = userInfo.Group_Id;
            dmodel.CType = Common.Enum.Course.ContractTypeEnum.Train;//这里只看培训的

            var list = orderModule.GetOrderStudentClassHoursPageList(1, 100000, out long count, dmodel);
            try
            {
                foreach (var item in list)
                {
                    string StatusName = "";//0-草稿，1-提交审核，2-审核通过，3-驳回，4-取消
                    if (item.Status == 0)
                    {
                        StatusName = "草稿";
                    }
                    else if (item.Status == 1)
                    {
                        StatusName = "审核中";
                    }
                    else if (item.Status == 2)
                    {
                        StatusName = "审核通过";
                    }
                    else if (item.Status == 3)
                    {
                        StatusName = "驳回";
                    }
                    var FinanceList = item.FinanceList.Where(x => x.Type == WFTempLateClassEnum.IN).ToList();
                    if (FinanceList.Any())
                    {
                        int Num = 0;
                        decimal PreferPrice = FinanceList.Where(x => x.Type == WFTempLateClassEnum.IN).Sum(x => (x.Money ?? 0) + (x.Fee ?? 0));
                        foreach (var qitem in FinanceList)
                        {
                            var bankModel = qitem.BankList.FirstOrDefault();
                            int Count = FinanceList.Count();
                            if (Num == 0)
                            {
                                ExcelDataSource firstRow = new ExcelDataSource(30)
                                {
                                    ExcelRows = new List<ExcelColumn>()
                                    {
                                        new ExcelColumn(value: item.OrderId.ToString()){ Rowspan = Count },
                                        new ExcelColumn(value: item.CreateTime.ToString("yyyy-MM-dd")){ Rowspan = Count },
                                        new ExcelColumn(value: item.ContractNo){ Rowspan = Count },
                                        new ExcelColumn(value: StatusName){ Rowspan = Count },
                                        new ExcelColumn(value: item.CourseName){ Rowspan = Count },
                                        new ExcelColumn(value: item.ClassNo){ Rowspan = Count },
                                        new ExcelColumn(value: item.ClassName){ Rowspan = Count },
                                        new ExcelColumn(value: item.StudentName){ Rowspan = Count },
                                        new ExcelColumn(value: item.CreateByName){ Rowspan = Count },
                                        new ExcelColumn(value: item.TotalClassHours.ToString()){ Rowspan = Count },
                                        new ExcelColumn(value: (item.TotalClassHours > 0 ? Math.Round(item.TotalCourseFee / item.TotalClassHours, 2, MidpointRounding.AwayFromZero) : 0).ToString()){ Rowspan = Count },
                                        new ExcelColumn(value: item.TotalCourseFee.ToString()){ Rowspan = Count },
                                        new ExcelColumn(value: item.TotalBookFee.ToString()){ Rowspan = Count },
                                        new ExcelColumn(value: item.TotalClassFee.ToString()){ Rowspan = Count },
                                        new ExcelColumn(value: item.TotalDiscountMoney.ToString()){ Rowspan = Count },
                                        new ExcelColumn(value: item.TotalMoney.ToString()){  Rowspan = Count},
                                        new ExcelColumn(value: qitem.FrID.ToString()){ },
                                        new ExcelColumn(value: (qitem.TradeDate.HasValue ? qitem.TradeDate.Value.ToString("yyyy-MM-dd") : "")){ },
                                        new ExcelColumn(value: ((qitem.Money??0)+(qitem.Fee??0)).ToString()){ },
                                        new ExcelColumn(value: bankModel != null ? bankModel.Type.ToName() : ""){ },
                                        new ExcelColumn(value: bankModel != null ? bankModel.Alias + bankModel.BankNo : ""){ },
                                        new ExcelColumn(value: PreferPrice.ToString()){ Rowspan = Count },
                                        new ExcelColumn(value: item.Income.ToString()){ Rowspan = Count },
                                        new ExcelColumn(value: Math.Round(item.Income / item.Unit_Price,2,MidpointRounding.AwayFromZero).ToString()){ Rowspan = Count },
                                        new ExcelColumn(value: item.UseBookFee.ToString("0.######")){ Rowspan = Count },
                                        new ExcelColumn(value: item.UseCoursewareFee.ToString("0.######")){ Rowspan = Count },
                                        new ExcelColumn(value: (item.TotalClassHours > 0 ? Math.Round(Convert.ToDecimal(item.UseClassHours) / item.TotalClassHours * 100, 2, MidpointRounding.AwayFromZero) : 0).ToString() + "%"){ Rowspan = Count },
                                        new ExcelColumn(value: item.UseClassHours.ToString()){ Rowspan = Count },
                                        new ExcelColumn(value: item.Unit_Price.ToString("0.######")){ Rowspan = Count },
                                        new ExcelColumn(value: item.UseCourseFee.ToString("0.######")){ Rowspan = Count },
                                        new ExcelColumn(value: item.Expend.ToString()){ Rowspan = Count },
                                        new ExcelColumn(value: item.AdjustPrice.ToString("0.######")){ Rowspan = Count },
                                        new ExcelColumn(value: (item.TotalClassHours - item.UseClassHours).ToString()){ Rowspan = Count },
                                        new ExcelColumn(value: (item.Income - item.UseBookFee - item.UseCoursewareFee - item.UseCourseFee - item.AdjustPrice).ToString("0.######")){ Rowspan = Count },
                                    }
                                };
                                slist.Add(firstRow);
                            }
                            else
                            {
                                ExcelDataSource firstRow = new ExcelDataSource(30)
                                {
                                    ExcelRows = new List<ExcelColumn>()
                                    {
                                        new ExcelColumn(value: ""){ },
                                        new ExcelColumn(value: ""){ },
                                        new ExcelColumn(value: ""){ },
                                        new ExcelColumn(value: ""){ },
                                        new ExcelColumn(value: ""){ },
                                        new ExcelColumn(value: ""){ },
                                        new ExcelColumn(value: ""){ },
                                        new ExcelColumn(value: ""){ },
                                        new ExcelColumn(value: ""){ },
                                        new ExcelColumn(value: ""){ },
                                        new ExcelColumn(value: ""){ },
                                        new ExcelColumn(value: ""){ },
                                        new ExcelColumn(value: ""){ },
                                        new ExcelColumn(value: ""){ },
                                        new ExcelColumn(value: ""){ },
                                        new ExcelColumn(value: ""){ },
                                        new ExcelColumn(value: qitem.FrID.ToString()){ },
                                        new ExcelColumn(value: (qitem.TradeDate.HasValue ? qitem.TradeDate.Value.ToString("yyyy-MM-dd") : "")){ },
                                        new ExcelColumn(value: ((qitem.Money??0)+(qitem.Fee??0)).ToString()){ },
                                        new ExcelColumn(value: bankModel != null ? bankModel.Type.ToName() : ""){ },
                                        new ExcelColumn(value: bankModel != null ? bankModel.Alias + bankModel.BankNo : ""){ },
                                        new ExcelColumn(value: PreferPrice.ToString()){ },
                                        new ExcelColumn(value: ""){ },
                                        new ExcelColumn(value: ""){ },
                                        new ExcelColumn(value: ""){ },
                                        new ExcelColumn(value: ""){ },
                                        new ExcelColumn(value: ""){ },
                                        new ExcelColumn(value: ""){ },
                                        new ExcelColumn(value: ""){ },
                                        new ExcelColumn(value: ""){ },
                                        new ExcelColumn(value: ""){ },
                                        new ExcelColumn(value: ""){ },
                                        new ExcelColumn(value: ""){ },
                                        new ExcelColumn(value: ""){ },
                                    }
                                };
                                slist.Add(firstRow);
                            }
                            Num++;
                        }
                    }
                    else
                    {
                        ExcelDataSource firstRow = new ExcelDataSource(30)
                        {
                            ExcelRows = new List<ExcelColumn>()
                                {
                                    new ExcelColumn(value: item.OrderId.ToString()){ },
                                    new ExcelColumn(value: item.CreateTime.ToString("yyyy-MM-dd")){ },
                                    new ExcelColumn(value: item.ContractNo){ },
                                    new ExcelColumn(value: StatusName){ },
                                    new ExcelColumn(value: item.CourseName){ },
                                    new ExcelColumn(value: item.ClassNo){ },
                                    new ExcelColumn(value: item.ClassName){ },
                                    new ExcelColumn(value: item.StudentName){ },
                                    new ExcelColumn(value: item.CreateByName){ },
                                    new ExcelColumn(value: item.TotalClassHours.ToString()){ },
                                    new ExcelColumn(value: (item.TotalClassHours > 0 ? Math.Round(item.TotalCourseFee / item.TotalClassHours, 2, MidpointRounding.AwayFromZero) : 0).ToString()){ },
                                    new ExcelColumn(value: item.TotalCourseFee.ToString()){ },
                                    new ExcelColumn(value: item.TotalBookFee.ToString()){ },
                                    new ExcelColumn(value: item.TotalClassFee.ToString()){ },
                                    new ExcelColumn(value: item.TotalDiscountMoney.ToString()){ },
                                    new ExcelColumn(value: item.TotalMoney.ToString()){ },
                                    new ExcelColumn(value: ""){ },
                                    new ExcelColumn(value: ""){ },
                                    new ExcelColumn(value: ""){ },
                                    new ExcelColumn(value: ""){ },
                                    new ExcelColumn(value: ""){ },
                                    new ExcelColumn(value: "0"){ },
                                    new ExcelColumn(value: item.Income.ToString()){ },
                                    new ExcelColumn(value: Math.Round(item.Income / item.Unit_Price,2,MidpointRounding.AwayFromZero).ToString()){ },
                                    new ExcelColumn(value: item.UseBookFee.ToString("0.######")){ },
                                    new ExcelColumn(value: item.UseCoursewareFee.ToString("0.######")){ },
                                    new ExcelColumn(value: (item.TotalClassHours > 0 ? Math.Round(Convert.ToDecimal(item.UseClassHours) / item.TotalClassHours * 100, 2, MidpointRounding.AwayFromZero) : 0).ToString() + "%"){ },
                                    new ExcelColumn(value: item.UseClassHours.ToString()){ },
                                    new ExcelColumn(value: item.Unit_Price.ToString("0.######")){ },
                                    new ExcelColumn(value: item.UseCourseFee.ToString("0.######")){ },
                                    new ExcelColumn(value: item.Expend.ToString()){ },
                                    new ExcelColumn(value: item.AdjustPrice.ToString("0.######")){ },
                                    new ExcelColumn(value: (item.TotalClassHours - item.UseClassHours).ToString()){ },
                                    new ExcelColumn(value: (item.Income - item.UseBookFee - item.UseCoursewareFee - item.UseCourseFee - item.AdjustPrice).ToString("0.######")){ },
                                }
                        };
                        slist.Add(firstRow);
                    }
                }

                var byteData = ExcelTempLateHelper.ToExcelExtend(slist);
                return File(byteData, "application/octet-stream", ExcelName);
            }
            catch (Exception ex)
            {
                LogHelper.Write(ex, "OutToExcelOrderStudentClassHoursList:" + Common.Plugin.JsonHelper.Serialize(dmodel));
                var byteData1 = ExcelTempLateHelper.ToExcelExtend(slist);
                return File(byteData1, "application/octet-stream", ExcelName);
            }
        }

        /// <summary>
        /// 修改合同协调金额
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult SetContractAdjustPrice()
        {
            var userInfo = base.UserInfo;
            var pageModel = JsonHelper.DeserializeObject<ResultPageModel>(RequestParm.Msg.ToString());
            var dmodel = JsonHelper.DeserializeObject<RB_Education_ContractAdjust_ViewModel>(RequestParm.Msg.ToString());
            if (dmodel.ContractId <= 0)
            {
                return ApiResult.ParamIsNull("请传递合同编号");
            }

            dmodel.Group_Id = userInfo.Group_Id;
            bool flag = orderModule.SetContractAdjustPrice(dmodel, userInfo);
            if (flag)
            {
                return ApiResult.Success();
            }
            else
            {
                return ApiResult.Failed();
            }
        }

        #endregion

        #region 月结窗口

        /// <summary>
        /// 获取月结分页列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetFinanceMonthStatementPageList()
        {
            var userInfo = base.UserInfo;
            var pageModel = JsonHelper.DeserializeObject<ResultPageModel>(RequestParm.Msg.ToString());
            var dmodel = JsonHelper.DeserializeObject<RB_RollingAccount_Extend>(RequestParm.Msg.ToString());
            dmodel.GroupId = userInfo.Group_Id;

            var list = financeModule.GetFinanceMonthStatementPageList(pageModel.PageIndex, pageModel.PageSize, out long count, dmodel);
            pageModel.Count = Convert.ToInt32(count);
            pageModel.PageData = list.Select(x => new
            {
                x.Id,
                x.Month,
                x.SchoolIds,
                x.State,
                x.UpdateBy,
                x.UpdateByName,
                UpdateTime = x.UpdateTime.ToString("yyyy-MM-dd HH:mm"),
                SchoolList = x.SchoolList.Select(z => new
                {
                    z.SId,
                    z.SName
                })
            });
            return ApiResult.Success("", pageModel);
        }

        /// <summary>
        /// 新增修改月结设置
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult SetFinanceMonthStatementInfo()
        {
            var userInfo = base.UserInfo;
            var dmodel = JsonHelper.DeserializeObject<RB_RollingAccount_Extend>(RequestParm.Msg.ToString());
            if (string.IsNullOrEmpty(dmodel.Month))
            {
                return ApiResult.ParamIsNull("请选择月份");
            }
            if (dmodel.SchoolList == null || !dmodel.SchoolList.Any())
            {
                return ApiResult.ParamIsNull("请选择校区");
            }
            dmodel.Month = Convert.ToDateTime(dmodel.Month).ToString("yyyy-MM");
            dmodel.SchoolIds = string.Join(",", dmodel.SchoolList.Select(x => x.SId));
            dmodel.State = dmodel.State == 0 ? 1 : dmodel.State;
            dmodel.GroupId = userInfo.Group_Id;
            dmodel.CreateBy = userInfo.Id;
            dmodel.CreateTime = DateTime.Now;
            dmodel.UpdateBy = userInfo.Id;
            dmodel.UpdateTime = DateTime.Now;

            string msg = financeModule.SetFinanceMonthStatementInfo(dmodel);
            if (msg == "") { return ApiResult.Success(); }
            else { return ApiResult.Failed(msg); }
        }

        #endregion

        #region 报表统计  6.28

        /// <summary>
        /// 获取老师课时统计
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetTeacherConsumptionHoursStatistics()
        {
            var userInfo = base.UserInfo;
            var pageModel = JsonHelper.DeserializeObject<ResultPageModel>(RequestParm.Msg.ToString());
            int TeacherId = base.ParmJObj.GetInt("TeacherId", 0);
            int SchoolId = base.ParmJObj.GetInt("SchoolId", 0);
            string StartMonth = base.ParmJObj.GetStringValue("StartMonth");
            string EndMonth = base.ParmJObj.GetStringValue("EndMonth");
            if (string.IsNullOrEmpty(StartMonth))
            {
                return ApiResult.ParamIsNull("请选择开始月份");
            }
            if (string.IsNullOrEmpty(EndMonth))
            {
                return ApiResult.ParamIsNull("请选择结束月份");
            }
            try
            {
                StartMonth = Convert.ToDateTime(StartMonth + "-01").ToString("yyyy-MM-dd");
                EndMonth = Convert.ToDateTime(EndMonth + "-01").AddMonths(1).AddDays(-1).ToString("yyyy-MM-dd");
            }
            catch (Exception)
            {
                return ApiResult.Failed("日期格式有误");
            }

            var list = financeModule.GetTeacherConsumptionHoursStatistics_V2(pageModel.PageIndex, pageModel.PageSize, out long count, TeacherId, SchoolId, StartMonth, EndMonth, userInfo.Group_Id);
            pageModel.Count = Convert.ToInt32(count);
            pageModel.PageData = list;
            return ApiResult.Success("", pageModel);
        }

        /// <summary>
        /// 获取老师课时统计  Excel导出
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Obsolete]
        public FileContentResult GetTeacherConsumptionHoursStatisticsToExcel()
        {
            List<ExcelDataSource> slist = new List<ExcelDataSource>();
            ExcelDataSource header = new ExcelDataSource()
            {
                ExcelRows = new List<ExcelColumn>(30) {
                    new ExcelColumn(value:"序号"){CellWidth=15,HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER },
                    new ExcelColumn(value:"校区"){CellWidth=15,HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER },
                    new ExcelColumn(value:"专职/兼职"){CellWidth=15,HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER },
                    new ExcelColumn(value:"老师姓名"){CellWidth=15,HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER },
                    new ExcelColumn(value:"上课课时"){CellWidth=15,HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER },
                    new ExcelColumn(value:"基础课时"){CellWidth=15,HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER },
                    new ExcelColumn(value:"上课金额"){CellWidth=15,HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER },
                    new ExcelColumn(value:"课时转换/小时"){CellWidth=15,HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER },
                    new ExcelColumn(value:"课时费"){CellWidth=15,HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER }
                }
            };
            slist.Add(header);
            string ExcelName = "课耗统计汇总表" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls";

            var userInfo = base.UserInfo;
            int TeacherId = base.ParmJObj.GetInt("TeacherId", 0);
            int SchoolId = base.ParmJObj.GetInt("SchoolId", 0);
            string StartMonth = base.ParmJObj.GetStringValue("StartMonth");
            string EndMonth = base.ParmJObj.GetStringValue("EndMonth");
            if (string.IsNullOrEmpty(StartMonth))
            {
                var byteData1 = ExcelTempLateHelper.ToExcelExtend(slist);
                return File(byteData1, "application/octet-stream", ExcelName);
            }
            if (string.IsNullOrEmpty(EndMonth))
            {
                var byteData1 = ExcelTempLateHelper.ToExcelExtend(slist);
                return File(byteData1, "application/octet-stream", ExcelName);
            }
            try
            {
                StartMonth = Convert.ToDateTime(StartMonth + "-01").ToString("yyyy-MM-dd");
                EndMonth = Convert.ToDateTime(EndMonth + "-01").AddMonths(1).AddDays(-1).ToString("yyyy-MM-dd");
            }
            catch (Exception)
            {
                var byteData1 = ExcelTempLateHelper.ToExcelExtend(slist);
                return File(byteData1, "application/octet-stream", ExcelName);
            }

            var list = financeModule.GetTeacherConsumptionHoursStatisticsToExcel(TeacherId, SchoolId, StartMonth, EndMonth, userInfo.Group_Id);

            try
            {
                slist.AddRange(list);

                var byteData = ExcelTempLateHelper.ToExcelExtend(slist);
                return File(byteData, "application/octet-stream", ExcelName);
            }
            catch (Exception ex)
            {
                LogHelper.Write(ex, "OutToExcelOrderStudentClassHoursList:" + string.Format("TeacherId:{0},SchoolId:{1},StartMonth:{2},EndMonth:{3},Group_Id:{4}", TeacherId, SchoolId, StartMonth, EndMonth, userInfo.Group_Id));
                var byteData1 = ExcelTempLateHelper.ToExcelExtend(slist);
                return File(byteData1, "application/octet-stream", ExcelName);
            }
        }





        /// <summary>
        /// 获取老师课时统计明细列表
        /// </summary>
        /// <returns></returns>
        public ApiResult GetTeacherConsumptionHoursDetialPageList()
        {
            var userInfo = base.UserInfo;
            var pageModel = JsonHelper.DeserializeObject<ResultPageModel>(RequestParm.Msg.ToString());
            int TeacherId = base.ParmJObj.GetInt("TeacherId", 0);
            int SchoolId = base.ParmJObj.GetInt("SchoolId", 0);
            int ClassId = base.ParmJObj.GetInt("ClassId", 0);
            string StartMonth = base.ParmJObj.GetStringValue("StartMonth");
            string EndMonth = base.ParmJObj.GetStringValue("EndMonth");
            if (string.IsNullOrEmpty(StartMonth))
            {
                return ApiResult.ParamIsNull("请选择开始月份");
            }
            if (string.IsNullOrEmpty(EndMonth))
            {
                return ApiResult.ParamIsNull("请选择结束月份");
            }
            try
            {
                StartMonth = Convert.ToDateTime(StartMonth + "-01").ToString("yyyy-MM-dd");
                EndMonth = Convert.ToDateTime(EndMonth + "-01").AddMonths(1).AddDays(-1).ToString("yyyy-MM-dd");
            }
            catch (Exception)
            {
                return ApiResult.Failed("日期格式有误");
            }

            var list = financeModule.GetTeacherConsumptionHoursDetialPageList(pageModel.PageIndex, pageModel.PageSize, out long count, TeacherId, SchoolId, ClassId, StartMonth, EndMonth, userInfo.Group_Id);
            pageModel.Count = Convert.ToInt32(count);
            pageModel.PageData = list.Select(x => new
            {
                Date = x.Date.ToString("yyyy-MM-dd"),
                x.TimeBucket,
                x.TeacherId,
                x.TeacherName,
                x.ClassId,
                x.ClassName,
                x.ClassNo,
                x.CouseId,
                x.CourseName,
                x.KSNum,
                x.DKNum,
                x.Hours,
                x.HourMoney,
                x.UnitPrice,
                KSMoney = x.Money
            });
            return ApiResult.Success("", pageModel);
        }

        /// <summary>
        /// 获取老师课时统计明细列表  Excel导出
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Obsolete]
        public FileContentResult GetTeacherConsumptionHoursDetialPageListToExcel()
        {
            List<ExcelDataSource> slist = new List<ExcelDataSource>();
            ExcelDataSource header = new ExcelDataSource()
            {
                ExcelRows = new List<ExcelColumn>(30) {
                    new ExcelColumn(value:"上课日期"){CellWidth=15,HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER },
                    new ExcelColumn(value:"上课时间段"){CellWidth=20,HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER },
                    new ExcelColumn(value:"班级编号"){CellWidth=15,HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER },
                    new ExcelColumn(value:"班级"){CellWidth=15,HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER },
                    new ExcelColumn(value:"课程"){CellWidth=15,HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER },
                    new ExcelColumn(value:"老师名称"){CellWidth=15,HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER },
                    new ExcelColumn(value:"课时单价"){CellWidth=15,HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER },
                    new ExcelColumn(value:"上课课时"){CellWidth=15,HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER },
                    new ExcelColumn(value:"基础课时"){CellWidth=15,HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER },
                    new ExcelColumn(value:"课时转换/小时"){CellWidth=15,HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER },
                    new ExcelColumn(value:"课时费单价元/小时"){CellWidth=25,HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER },
                    new ExcelColumn(value:"老师课时费"){CellWidth=15,HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER },
                }
            };
            slist.Add(header);
            string ExcelName = "课耗老师明细表" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls";

            var userInfo = base.UserInfo;
            var pageModel = JsonHelper.DeserializeObject<ResultPageModel>(RequestParm.Msg.ToString());
            int TeacherId = base.ParmJObj.GetInt("TeacherId", 0);
            int SchoolId = base.ParmJObj.GetInt("SchoolId", 0);
            int ClassId = base.ParmJObj.GetInt("ClassId", 0);
            string StartMonth = base.ParmJObj.GetStringValue("StartMonth");
            string EndMonth = base.ParmJObj.GetStringValue("EndMonth");
            if (string.IsNullOrEmpty(StartMonth))
            {
                var byteData1 = ExcelTempLateHelper.ToExcelExtend(slist);
                return File(byteData1, "application/octet-stream", ExcelName);
            }
            if (string.IsNullOrEmpty(EndMonth))
            {
                var byteData1 = ExcelTempLateHelper.ToExcelExtend(slist);
                return File(byteData1, "application/octet-stream", ExcelName);
            }
            try
            {
                StartMonth = Convert.ToDateTime(StartMonth + "-01").ToString("yyyy-MM-dd");
                EndMonth = Convert.ToDateTime(EndMonth + "-01").AddMonths(1).AddDays(-1).ToString("yyyy-MM-dd");
            }
            catch (Exception)
            {
                var byteData1 = ExcelTempLateHelper.ToExcelExtend(slist);
                return File(byteData1, "application/octet-stream", ExcelName);
            }

            var list = financeModule.GetTeacherConsumptionHoursDetialPageListToExcel(TeacherId, SchoolId, ClassId, StartMonth, EndMonth, userInfo.Group_Id);

            try
            {
                slist.AddRange(list);

                var byteData = ExcelTempLateHelper.ToExcelExtend(slist);
                return File(byteData, "application/octet-stream", ExcelName);
            }
            catch (Exception ex)
            {
                LogHelper.Write(ex, "OutToExcelOrderStudentClassHoursList");
                var byteData1 = ExcelTempLateHelper.ToExcelExtend(slist);
                return File(byteData1, "application/octet-stream", ExcelName);
            }
        }





        /// <summary>
        /// 获取学生课时统计明细列表
        /// </summary>
        /// <returns></returns>
        public ApiResult GetStudentConsumptionHoursDetialPageList()
        {
            var userInfo = base.UserInfo;
            var pageModel = JsonHelper.DeserializeObject<ResultPageModel>(RequestParm.Msg.ToString());
            int TeacherId = base.ParmJObj.GetInt("TeacherId", 0);
            int SchoolId = base.ParmJObj.GetInt("SchoolId", 0);
            int ClassId = base.ParmJObj.GetInt("ClassId", 0);
            string StartMonth = base.ParmJObj.GetStringValue("StartMonth");
            string EndMonth = base.ParmJObj.GetStringValue("EndMonth");
            string StudentName = base.ParmJObj.GetStringValue("StudentName");//学生名称
            int StudentId = base.ParmJObj.GetInt("StudentId",0);//学员id
            int OrderId = base.ParmJObj.GetInt("OrderId", 0);//订单
            string ContractNo = base.ParmJObj.GetStringValue("ContractNo");//合同编号
            if (string.IsNullOrEmpty(StartMonth))
            {
                return ApiResult.ParamIsNull("请选择开始月份");
            }
            if (string.IsNullOrEmpty(EndMonth))
            {
                return ApiResult.ParamIsNull("请选择结束月份");
            }
            try
            {
                StartMonth = Convert.ToDateTime(StartMonth + "-01").ToString("yyyy-MM-dd");
                EndMonth = Convert.ToDateTime(EndMonth + "-01").AddMonths(1).AddDays(-1).ToString("yyyy-MM-dd");
            }
            catch (Exception)
            {
                return ApiResult.Failed("日期格式有误");
            }

            var list = financeModule.GetStudentConsumptionHoursDetialPageList(pageModel.PageIndex, pageModel.PageSize, out long count, TeacherId, SchoolId, ClassId, StartMonth, EndMonth, StudentName, OrderId, ContractNo, userInfo.Group_Id, StudentId);
            pageModel.Count = Convert.ToInt32(count);
            pageModel.PageData = list;
            return ApiResult.Success("", pageModel);
        }

        /// <summary>
        /// 获取学生课时统计明细列表  Excel导出
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Obsolete]
        public FileContentResult GetStudentConsumptionHoursDetialPageListToExcel()
        {
            List<ExcelDataSource> slist = new List<ExcelDataSource>();
            ExcelDataSource header = new ExcelDataSource()
            {
                ExcelRows = new List<ExcelColumn>(30) {
                    new ExcelColumn(value:"订单号"){CellWidth=15,HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER },
                    new ExcelColumn(value:"合同号"){CellWidth=20,HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER },
                    new ExcelColumn(value:"上课日期"){CellWidth=15,HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER },
                    new ExcelColumn(value:"上课时间段"){CellWidth=20,HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER },
                    new ExcelColumn(value:"学生名字"){CellWidth=15,HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER },
                    new ExcelColumn(value:"班级编号"){CellWidth=15,HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER },
                    new ExcelColumn(value:"班级"){CellWidth=15,HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER },
                    new ExcelColumn(value:"课程"){CellWidth=15,HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER },
                    new ExcelColumn(value:"老师名称"){CellWidth=15,HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER },
                    new ExcelColumn(value:"上课课时"){CellWidth=15,HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER },
                    new ExcelColumn(value:"合同单价（元/课时）"){CellWidth=25,HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER },
                    new ExcelColumn(value:"课耗（消耗合同金额）"){CellWidth=25,HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER },
                    new ExcelColumn(value:"提成销售"){CellWidth=15,HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER },
                    new ExcelColumn(value:"提成老师"){CellWidth=15,HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER },
                }
            };
            slist.Add(header);
            string ExcelName = "课耗学生明细表" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls";

            var userInfo = base.UserInfo;
            var pageModel = JsonHelper.DeserializeObject<ResultPageModel>(RequestParm.Msg.ToString());
            int TeacherId = base.ParmJObj.GetInt("TeacherId", 0);
            int SchoolId = base.ParmJObj.GetInt("SchoolId", 0);
            int ClassId = base.ParmJObj.GetInt("ClassId", 0);
            string StartMonth = base.ParmJObj.GetStringValue("StartMonth");
            string EndMonth = base.ParmJObj.GetStringValue("EndMonth");
            string StudentName = base.ParmJObj.GetStringValue("StudentName");//学生名称
            int OrderId = base.ParmJObj.GetInt("OrderId", 0);//订单
            string ContractNo = base.ParmJObj.GetStringValue("ContractNo");//合同编号
            if (string.IsNullOrEmpty(StartMonth))
            {
                var byteData1 = ExcelTempLateHelper.ToExcelExtend(slist);
                return File(byteData1, "application/octet-stream", ExcelName);
            }
            if (string.IsNullOrEmpty(EndMonth))
            {
                var byteData1 = ExcelTempLateHelper.ToExcelExtend(slist);
                return File(byteData1, "application/octet-stream", ExcelName);
            }
            try
            {
                StartMonth = Convert.ToDateTime(StartMonth + "-01").ToString("yyyy-MM-dd");
                EndMonth = Convert.ToDateTime(EndMonth + "-01").AddMonths(1).AddDays(-1).ToString("yyyy-MM-dd");
            }
            catch (Exception)
            {
                var byteData1 = ExcelTempLateHelper.ToExcelExtend(slist);
                return File(byteData1, "application/octet-stream", ExcelName);
            }

            var list = financeModule.GetStudentConsumptionHoursDetialPageListToExcel(TeacherId, SchoolId, ClassId, StartMonth, EndMonth, StudentName, OrderId, ContractNo, userInfo.Group_Id);

            try
            {
                slist.AddRange(list);

                var byteData = ExcelTempLateHelper.ToExcelExtend(slist);
                return File(byteData, "application/octet-stream", ExcelName);
            }
            catch (Exception ex)
            {
                LogHelper.Write(ex, "OutToExcelOrderStudentClassHoursList");
                var byteData1 = ExcelTempLateHelper.ToExcelExtend(slist);
                return File(byteData1, "application/octet-stream", ExcelName);
            }
        }





        /// <summary>
        /// 获取销售业绩排名统计
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetSaleAchievementsRankStatistics()
        {
            var userInfo = base.UserInfo;
            var pageModel = JsonHelper.DeserializeObject<ResultPageModel>(RequestParm.Msg.ToString());
            int SaleId = base.ParmJObj.GetInt("SaleId", 0);
            int SchoolId = base.ParmJObj.GetInt("SchoolId", 0);
            string StartMonth = base.ParmJObj.GetStringValue("StartMonth");
            string EndMonth = base.ParmJObj.GetStringValue("EndMonth");
            if (string.IsNullOrEmpty(StartMonth))
            {
                return ApiResult.ParamIsNull("请选择开始月份");
            }
            if (string.IsNullOrEmpty(EndMonth))
            {
                return ApiResult.ParamIsNull("请选择结束月份");
            }
            try
            {
                StartMonth = Convert.ToDateTime(StartMonth + "-01").ToString("yyyy-MM-dd");
                EndMonth = Convert.ToDateTime(EndMonth + "-01").AddMonths(1).AddDays(-1).ToString("yyyy-MM-dd");
            }
            catch (Exception)
            {
                return ApiResult.Failed("日期格式有误");
            }

            var list = financeModule.GetSaleAchievementsRankStatistics(pageModel.PageIndex, pageModel.PageSize, out long count, SaleId, SchoolId, StartMonth, EndMonth, userInfo.Group_Id);
            pageModel.Count = Convert.ToInt32(count);
            pageModel.PageData = list.Select(x => new
            {
                x.EnterID,
                x.EnterName,
                x.PreferPrice,
                x.Income,
                x.PlatformTax,
                x.Refund,
                RIncome = x.Income - x.Refund,
                YSKMoney = Math.Round(x.YSKMoney, 6, MidpointRounding.AwayFromZero),
                x.GuestNum,
                x.JKNum,
                OrderNum = x.CancelNum
            });
            return ApiResult.Success("", pageModel);
        }

        /// <summary>
        /// 获取销售业绩统计
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Obsolete]
        public FileContentResult GetSaleAchievementsRankStatisticsToExcel()
        {
            List<ExcelDataSource> slist = new List<ExcelDataSource>();
            ExcelDataSource header = new ExcelDataSource()
            {
                ExcelRows = new List<ExcelColumn>(30) {
                    new ExcelColumn(value:"序号"){CellWidth=15,HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER },
                    new ExcelColumn(value:"业务员"){CellWidth=20,HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER },
                    new ExcelColumn(value:"业绩（应收金额）"){CellWidth=15,HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER },
                    new ExcelColumn(value:"实收金额"){CellWidth=20,HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER },
                    new ExcelColumn(value:"退费金额"){CellWidth=15,HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER },
                    new ExcelColumn(value:"退后实收金额"){CellWidth=15,HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER },
                    new ExcelColumn(value:"平台税金"){CellWidth=15,HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER },
                    new ExcelColumn(value:"上课金额"){CellWidth=15,HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER },
                    new ExcelColumn(value:"报名人数"){CellWidth=15,HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER },
                    new ExcelColumn(value:"结课人数"){CellWidth=15,HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER },
                    new ExcelColumn(value:"订单数量"){CellWidth=15,HAlignmentEnum= HAlignmentEnum.CENTER,VAlignmentEnum= VAlignmentEnum.CENTER },
                }
            };
            slist.Add(header);
            string ExcelName = "销售业绩统计" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls";

            var userInfo = base.UserInfo;
            var pageModel = JsonHelper.DeserializeObject<ResultPageModel>(RequestParm.Msg.ToString());
            int SaleId = base.ParmJObj.GetInt("SaleId", 0);
            int SchoolId = base.ParmJObj.GetInt("SchoolId", 0);
            string StartMonth = base.ParmJObj.GetStringValue("StartMonth");
            string EndMonth = base.ParmJObj.GetStringValue("EndMonth");
            if (string.IsNullOrEmpty(StartMonth))
            {
                var byteData1 = ExcelTempLateHelper.ToExcelExtend(slist);
                return File(byteData1, "application/octet-stream", ExcelName);
            }
            if (string.IsNullOrEmpty(EndMonth))
            {
                var byteData1 = ExcelTempLateHelper.ToExcelExtend(slist);
                return File(byteData1, "application/octet-stream", ExcelName);
            }
            try
            {
                StartMonth = Convert.ToDateTime(StartMonth + "-01").ToString("yyyy-MM-dd");
                EndMonth = Convert.ToDateTime(EndMonth + "-01").AddMonths(1).AddDays(-1).ToString("yyyy-MM-dd");
            }
            catch (Exception)
            {
                var byteData1 = ExcelTempLateHelper.ToExcelExtend(slist);
                return File(byteData1, "application/octet-stream", ExcelName);
            }

            var list = financeModule.GetSaleAchievementsRankStatisticsToExcel(SaleId, SchoolId, StartMonth, EndMonth, userInfo.Group_Id);

            try
            {
                int Num = 1;
                foreach (var item in list)
                {
                    item.YSKMoney = Math.Round(item.YSKMoney, 6, MidpointRounding.AwayFromZero);
                    ExcelDataSource firstRow = new ExcelDataSource(30)
                    {
                        ExcelRows = new List<ExcelColumn>()
                        {
                            new ExcelColumn(value: Num.ToString()){ },
                            new ExcelColumn(value: item.EnterName){ },
                            new ExcelColumn(value: item.PreferPrice.ToString("#0.00")){ },
                            new ExcelColumn(value: item.Income.ToString("#0.00")){ },
                            new ExcelColumn(value: item.Refund.ToString("#0.00")){ },
                            new ExcelColumn(value: (item.Income - item.Refund).ToString("#0.00")){ },
                            new ExcelColumn(value: item.PlatformTax.ToString("#0.00")){ },
                            new ExcelColumn(value: item.YSKMoney.ToString("#0.000000")){ },
                            new ExcelColumn(value: item.GuestNum.ToString()){ },
                            new ExcelColumn(value: item.JKNum.ToString()){ },
                            new ExcelColumn(value: item.CancelNum.ToString()){ },
                        }
                    };
                    slist.Add(firstRow);
                    Num++;
                }

                ExcelDataSource firstRowT = new ExcelDataSource(30)
                {
                    ExcelRows = new List<ExcelColumn>()
                        {
                            new ExcelColumn(value: "合计"){ },
                            new ExcelColumn(value: ""){ },
                            new ExcelColumn(value: list.Sum(x=>x.PreferPrice).ToString("#0.00")){ },
                            new ExcelColumn(value: list.Sum(x=>x.Income).ToString("#0.00")){ },
                            new ExcelColumn(value: list.Sum(x=>x.Refund).ToString("#0.00")){ },
                            new ExcelColumn(value: list.Sum(x=>x.Income - x.Refund).ToString("#0.00")){ },
                            new ExcelColumn(value: list.Sum(x=>x.PlatformTax).ToString("#0.00")){ },
                            new ExcelColumn(value: list.Sum(x=>x.YSKMoney).ToString("#0.000000")){ },
                            new ExcelColumn(value: list.Sum(x=>x.GuestNum).ToString()){ },
                            new ExcelColumn(value: list.Sum(x=>x.JKNum).ToString()){ },
                            new ExcelColumn(value: list.Sum(x=>x.CancelNum).ToString()){ },
                        }
                };
                slist.Add(firstRowT);

                var byteData = ExcelTempLateHelper.ToExcelExtend(slist);
                return File(byteData, "application/octet-stream", ExcelName);
            }
            catch (Exception ex)
            {
                LogHelper.Write(ex, "OutToExcelOrderStudentClassHoursList");
                var byteData1 = ExcelTempLateHelper.ToExcelExtend(slist);
                return File(byteData1, "application/octet-stream", ExcelName);
            }
        }

        #endregion

        #region 班级相关统计

        /// <summary>
        /// 班级月度课耗统计
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetClassMonthStatistics()
        {
            var userInfo = base.UserInfo;
            int classId = base.ParmJObj.GetInt("ClassId", 0);

            var list = financeModule.GetClassMonthStatistics(classId, userInfo);
            return ApiResult.Success("", list);
        }

        /// <summary>
        /// 获取班级月度分摊统计
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetAllClassNowMonthStatistics()
        {
            var userInfo = base.UserInfo;
            string StartTime = base.ParmJObj.GetStringValue("StartTime");//开始时间
            string EndTime = base.ParmJObj.GetStringValue("EndTime");//结束时间
            int ClassId = base.ParmJObj.GetInt("ClassId", 0);
            string ClassNo = base.ParmJObj.GetStringValue("ClassNo");//班号
            int SchoolId = base.ParmJObj.GetInt("SchoolId", -1);//校区id
            if (string.IsNullOrEmpty(StartTime))
            {
                return ApiResult.ParamIsNull();
            }
            if (string.IsNullOrEmpty(EndTime))
            {
                return ApiResult.ParamIsNull();
            }
            try
            {
                StartTime = Convert.ToDateTime(StartTime).ToString("yyyy-MM-dd");
                EndTime = Convert.ToDateTime(EndTime).ToString("yyyy-MM-dd");
            }
            catch (Exception)
            {
                return ApiResult.Failed("日期格式有误，请核实后再试");
            }

            var Robj = financeModule.GetAllClassNowMonthStatistics(StartTime, EndTime, ClassId, ClassNo, SchoolId, userInfo);
            return ApiResult.Success("", Robj);
        }

        #endregion
    }
}
