using System;
using System.Collections.Generic;
using System.Linq;
using Edu.Cache.User;
using Edu.Common.API;
using Edu.Common.Enum.Course;
using Edu.Common.Plugin;
using Edu.Model.ViewModel.Course;
using Edu.Model.ViewModel.Log;
using Edu.Model.ViewModel.Sell;
using Edu.Module.Course;
using Edu.WebApi.Filter;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json.Linq;

namespace Edu.WebApi.Controllers.Course
{
    [Route("api/[controller]/[action]")]
    [ApiExceptionFilter]
    [ApiController]
    [EnableCors("AllowCors")]
    public class SellAchievementsController : BaseController
    {
        /// <summary>
        /// 市场顾问
        /// </summary>
        private readonly SellAchievementsModule sellAchievementsModule = new SellAchievementsModule();

        #region 规则配置

        /// <summary>
        /// 获取规则列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetSellAchievementsRuleList()
        {
            var userInfo = base.UserInfo;
            var list = sellAchievementsModule.GetSellAchievementsRuleList(userInfo.Group_Id);
            return ApiResult.Success("", list.Select(x => new
            {
                x.Id,
                x.Name,
                x.Type,
                x.CourseType,
                x.CourseIds,
                CourseList = x.CourseList.Select(y => new
                {
                    y.CourseId,
                    y.CourseName
                }),
                x.EmpIds,
                EmpList = x.EmpList.Select(y => new
                {
                    y.Id,
                    y.EmployeeName
                }),
                x.ChildEmpIds,
                ChildEmpList = x.ChildEmpList.Select(y => new
                {
                    y.Id,
                    y.EmployeeName
                }),
                x.CourseRate,
                x.FirstRate,
                x.RateList,
                x.SchoolIds,
                x.SchoolName,
                CreateTime = x.CreateTime.ToString("yyyy-MM-dd HH:mm:ss")
            }));
        }

        /// <summary>
        /// 保存规则配置
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult SetSellAchievementsRule()
        {
            var userInfo = base.UserInfo;
            RB_Sell_Achievements_Rule_ViewModel demodel = JsonHelper.DeserializeObject<RB_Sell_Achievements_Rule_ViewModel>(base.RequestParm.Msg.ToString());
            if (string.IsNullOrEmpty(demodel.Name)) {
                return ApiResult.ParamIsNull("请传递方案名称");
            }
            if (string.IsNullOrEmpty(demodel.SchoolIds))
            {
                return ApiResult.ParamIsNull("请传递校区");
            }
            if (demodel.CourseList != null && demodel.CourseList.Any()) {
                demodel.CourseIds = string.Join(",", demodel.CourseList.Select(x => x.CourseId));
            }
            if (demodel.EmpList == null || !demodel.EmpList.Any()) {
                return ApiResult.ParamIsNull("请传递人员列表");
            }
            demodel.EmpIds = string.Join(",", demodel.EmpList.Select(x => x.Id));
            if (demodel.ChildEmpList != null && demodel.ChildEmpList.Any()) { 
                demodel.ChildEmpIds = string.Join(",", demodel.ChildEmpList.Select(x => x.Id));
            }

            if (demodel.CourseRate <= 0)
            {
                return ApiResult.ParamIsNull("请传递课程比例");
            }
            if (demodel.FirstRate <= 0)
            {
                return ApiResult.ParamIsNull("请传递首次发放比例");
            }

            if (demodel.RateList == null || !demodel.RateList.Any())
            {
                return ApiResult.ParamIsNull("请传递业绩提成比例");
            }
            int length = demodel.RateList.Count();
            for (var i = 0; i < length; i++) {
                if (i < length - 1) {
                    if (demodel.RateList[i].EndValue > demodel.RateList[i + 1].StartValue) {
                        return ApiResult.ParamIsNull("业绩金额有误，请核实后再试");
                    }
                }
                if (demodel.RateList[i].Rate < 0) {
                    return ApiResult.ParamIsNull("业绩提成比例有误");
                }
            }
            demodel.Content = JsonHelper.Serialize(demodel.RateList);

            demodel.Status = 0;
            demodel.Group_Id = userInfo.Group_Id;
            demodel.CreateBy = userInfo.Id;
            demodel.CreateTime = DateTime.Now;
            demodel.UpdateBy = userInfo.Id;
            demodel.UpdateTime = DateTime.Now;
            var flag = sellAchievementsModule.SetSellAchievementsRule(demodel);
            if (flag)
            {
                return ApiResult.Success();
            }
            else {
                return ApiResult.Failed();
            }
        }

        /// <summary>
        /// 删除规则
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult DelSellAchievementsRule() {
            var userInfo = base.UserInfo;
            JObject parms = JObject.Parse(base.RequestParm.Msg.ToString());
            int RuleId = parms.GetInt("RuleId", 0);
            if (RuleId <= 0) {
                return ApiResult.ParamIsNull();
            }

            string msg = sellAchievementsModule.DelSellAchievementsRule(RuleId,userInfo);
            if (msg == "")
            {
                return ApiResult.Success();
            }
            else {
                return ApiResult.Failed(msg);
            }
        }

        #endregion

        #region 提成列表

        /// <summary>
        /// 获取提成期数分页列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetSellAchievementsPeriodsPageList()
        {
            var userInfo = base.UserInfo;
            var pageModel = JsonHelper.DeserializeObject<ResultPageModel>(RequestParm.Msg.ToString());
            var dmodel = JsonHelper.DeserializeObject<RB_Sell_Achievements_Periods_ViewModel>(RequestParm.Msg.ToString());
            dmodel.Group_Id = userInfo.Group_Id;

            var list = sellAchievementsModule.GetSellAchievementsPeriodsPageList(pageModel.PageIndex, pageModel.PageSize, out long count, dmodel);
            pageModel.Count = Convert.ToInt32(count);
            pageModel.PageData = list.Select(x => new
            {
                x.Id,
                x.Periods,
                x.SumPrice,
                x.SchooldCommission,
                x.Name,
                FrIdList = string.IsNullOrEmpty(x.Financials) ? new List<string>() : x.Financials.Split(',').ToList(),
                x.CreateBy,
                CreateByName = UserReidsCache.GetUserLoginInfo(x.CreateBy)?.AccountName ?? "",
                CreateTime = x.CreateTime.ToString("yyyy-MM-dd")
            });

            return ApiResult.Success("", pageModel);
        }

        /// <summary>
        /// 获取期数列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetSellAchievementsPeriodsList() {
            var userInfo = base.UserInfo;
            var dmodel = JsonHelper.DeserializeObject<RB_Sell_Achievements_Periods_ViewModel>(RequestParm.Msg.ToString());
            dmodel.Group_Id = userInfo.Group_Id;

            var list = sellAchievementsModule.GetSellAchievementsPeriodsList(dmodel);
            return ApiResult.Success("", list.Select(x => new
            {
                x.Id,
                x.Periods,                
                x.Name,
            }));
        }

        /// <summary>
        /// 获取提成用户列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetSellAchievementsUserList() {
            var userInfo = base.UserInfo;
            var dmodel = JsonHelper.DeserializeObject<RB_Sell_Achievements_Details_ViewModel>(RequestParm.Msg.ToString());
            dmodel.Group_Id = userInfo.Group_Id;
            if (dmodel.PeriodId <= 0) {
                return ApiResult.ParamIsNull("请传递期数id");
            }

            var list = sellAchievementsModule.GetSellAchievementsUserList(dmodel);
            return ApiResult.Success("", list.Select(x => new
            {
                x.School_Id,
                x.SchoolName,
                x.Depart_Id,
                x.DeptName,
                x.UserId,
                x.UserName,
                x.StudentCount,
                x.CurrentPeriodMoney
            }));
        }

        /// <summary>
        /// 获取提成用户明细列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetSellAchievementsUserDetailList()
        {
            var userInfo = base.UserInfo;
            var dmodel = JsonHelper.DeserializeObject<RB_Sell_Achievements_Details_ViewModel>(RequestParm.Msg.ToString());
            dmodel.Group_Id = userInfo.Group_Id;
            if (dmodel.PeriodId <= 0)
            {
                return ApiResult.ParamIsNull("请传递期数id");
            }

            var list = sellAchievementsModule.GetSellAchievementsUserDetailList_V2(dmodel);
            return ApiResult.Success("", list.Select(x => new
            {
                x.School_Id,
                x.SchoolName,
                x.Depart_Id,
                x.DeptName,
                x.UserId,
                x.UserName,
                x.ClassId,
                x.ClassName,
                x.ClassNo,
                x.ClassType,
                x.ClassTypeName,
                x.OrderId,
                x.IssueNum,
                x.StudentCount,
                x.Periods,
                TotalCommissionMoney = x.CommissionMoney,
                x.GiveOutMoney,
                x.IsFirstCommission,
                x.TotalHours,
                x.TotalLearn,
                x.CurrentHours,
                x.Remark,
                x.CurrentPeriodMoney,
                x.DeductionMoney,
                YFMoney = x.CurrentPeriodMoney + x.DeductionMoney,
                x.UserType,
                x.IsDept
            }));
        }

        /// <summary>
        /// 获取提成订单明细列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetSellAchievementsForOrderList()
        {
            var userInfo = base.UserInfo;
            var dmodel = JsonHelper.DeserializeObject<RB_Sell_Achievements_Details_ViewModel>(RequestParm.Msg.ToString());
            dmodel.Group_Id = userInfo.Group_Id;
            if (dmodel.OrderId <= 0)
            {
                return ApiResult.ParamIsNull("请传递订单id");
            }

            var list = sellAchievementsModule.GetSellAchievementsUserDetailList(dmodel);
            return ApiResult.Success("", list.Select(x => new
            {
                x.School_Id,
                x.SchoolName,
                x.Depart_Id,
                x.DeptName,
                x.UserId,
                x.UserName,
                x.ClassId,
                x.ClassName,
                x.ClassNo,
                x.ClassType,
                x.ClassTypeName,
                x.OrderId,
                x.StudentCount,
                x.Periods,
                TotalCommissionMoney = x.CommissionMoney,
                x.GiveOutMoney,
                x.IsFirstCommission,
                x.TotalHours,
                x.TotalLearn,
                x.CurrentHours,
                x.Remark,
                x.CurrentPeriodMoney,
                x.DeductionMoney,
                YFMoney = x.CurrentPeriodMoney + x.DeductionMoney,
                x.UserType,
                x.IsDept
            }));
        }

        /// <summary>
        /// 提成统计
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetSellAchievementsStatistics() {
            var userInfo = base.UserInfo;
            var pageModel = JsonHelper.DeserializeObject<ResultPageModel>(RequestParm.Msg.ToString());
            var dmodel = JsonHelper.DeserializeObject<RB_Sell_Achievements_Details_ViewModel>(RequestParm.Msg.ToString());
            dmodel.Group_Id = userInfo.Group_Id;

            var list = sellAchievementsModule.GetSellAchievementsStatisticsPageList(pageModel.PageIndex, pageModel.PageSize, out long count, dmodel);
            decimal TotalMoney = sellAchievementsModule.GetSellAchievementsStatistics(dmodel);
            pageModel.Count = Convert.ToInt32(count);
            pageModel.PageData = new
            {
                TotalCommission = TotalMoney,
                List = list.Select(x => new
                {
                    x.School_Id,
                    x.SchoolName,
                    x.Depart_Id,
                    x.DeptName,
                    x.UserId,
                    x.UserName,
                    x.ClassId,
                    x.ClassName,
                    x.ClassNo,
                    x.ClassType,
                    x.ClassTypeName,
                    x.OrderId,
                    x.StudentCount,
                    x.Periods,
                    TotalCommissionMoney = x.CommissionMoney,
                    x.GiveOutMoney,
                    x.IsFirstCommission,
                    x.TotalHours,
                    x.TotalLearn,
                    x.CurrentHours,
                    x.Remark,
                    x.CurrentPeriodMoney,
                    x.DeductionMoney,
                    YFMoney = x.CurrentPeriodMoney + x.DeductionMoney,
                    x.UserType,
                    x.IsDept
                })
            };
            return ApiResult.Success("", pageModel);
        }

        /// <summary>
        /// 导出提成统计 Excel
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Obsolete]
        public FileContentResult GetSellAchievementsStatisticsToExcel() {
            var userInfo = base.UserInfo;
            var dmodel = JsonHelper.DeserializeObject<RB_Sell_Achievements_Details_ViewModel>(RequestParm.Msg.ToString());
            dmodel.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 = 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 = 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 },
                    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 = sellAchievementsModule.GetSellAchievementsStatisticsPageList(1, 1000000, out long count, dmodel);
                decimal TotalMoney = sellAchievementsModule.GetSellAchievementsStatistics(dmodel);
                #region 组装数据
                foreach (var item in list)
                {
                    ExcelDataSource datarow = new ExcelDataSource()
                    {
                        ExcelRows = new List<ExcelColumn>(30) {
                            new ExcelColumn(value: item.SchoolName){ },
                            new ExcelColumn(value: item.DeptName){ },
                            new ExcelColumn(value: item.UserType == 1?"市场":item.UserType ==2?"顾问":"教师"){ },
                            new ExcelColumn(value: item.UserName){ },
                            new ExcelColumn(value: item.IsDept==1?"部门":"个人"){ },
                            new ExcelColumn(value: item.ClassNo){ },
                            new ExcelColumn(value: item.ClassName){ },
                            new ExcelColumn(value: item.ClassTypeName){ },
                            new ExcelColumn(value: item.StudentCount.ToString()){ },
                            new ExcelColumn(value: item.OrderId.ToString()){ },
                            new ExcelColumn(value: (item.CommissionMoney).ToString("#0.00")){ },
                            new ExcelColumn(value: (item.GiveOutMoney).ToString("#0.00")){ },
                            new ExcelColumn(value: item.IsFirstCommission == 1 ?"是":"否"){ },
                            new ExcelColumn(value: item.TotalHours.ToString()){ },
                            new ExcelColumn(value: item.TotalLearn.ToString()){ },
                            new ExcelColumn(value: item.CurrentHours == -1 ?"-": item.CurrentHours.ToString()){ },
                            new ExcelColumn(value: (item.CurrentPeriodMoney).ToString("#0.00")){ },
                            new ExcelColumn(value: (item.DeductionMoney).ToString("#0.00")){ },
                            new ExcelColumn(value: (item.CurrentPeriodMoney + item.DeductionMoney).ToString("#0.00")){ },
                            new ExcelColumn(value: item.Periods){ },
                            new ExcelColumn(value: item.Remark){ },
                        },
                        ColumnHight = 30
                    };
                    slist.Add(datarow);
                }

                //统计
                ExcelDataSource datarows = new ExcelDataSource()
                {
                    ExcelRows = new List<ExcelColumn>(30) {
                            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: ""){ },
                            new ExcelColumn(value: ""){ },
                            new ExcelColumn(value: TotalMoney.ToString("#0.00")){ },
                            new ExcelColumn(value: ""){ },
                            new ExcelColumn(value: ""){ },
                        },
                    ColumnHight = 30
                };
                slist.Add(datarows);
                #endregion

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

        /// <summary>
        /// 创建业绩提成
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult SetAchievementsCommissionInfo() {
            var userInfo = base.UserInfo;
            JObject parms = JObject.Parse(RequestParm.Msg.ToString());
            string Month = parms.GetStringValue("Month");//月份
            if (string.IsNullOrEmpty(Month))
            {
                return ApiResult.ParamIsNull();
            }
            try
            {
                Month = Convert.ToDateTime(Month + "-01").ToString("yyyy-MM");
            }
            catch (Exception)
            {
                return ApiResult.Failed("日期格式有误");
            }

            string msg = sellAchievementsModule.SetAchievementsCommissionInfo(Month, userInfo);
            if (msg == "")
            {
                return ApiResult.Success();
            }
            else {
                return ApiResult.Failed(msg);
            }
        }

        /// <summary>
        /// 获取提成可发放人员列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetSellAchievementsSendEmployeeList() {
            var userInfo = base.UserInfo;
            JObject parms = JObject.Parse(RequestParm.Msg.ToString());
            int PeriodId = parms.GetInt("PeriodId", 0);//期数id
            if (PeriodId <= 0)
            {
                return ApiResult.ParamIsNull();
            }

            var list = sellAchievementsModule.GetSellAchievementsSendEmployeeList(PeriodId, userInfo);
            return ApiResult.Success("", list);
        }

        /// <summary>
        /// 设置提成发放人员
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult SetSellAchievementsSendEmployee() {
            var userInfo = base.UserInfo;
            JObject parms = JObject.Parse(RequestParm.Msg.ToString());
            int PeriodId = parms.GetInt("PeriodId", 0);//期数id
            string EmpIds = parms.GetStringValue("EmpIds");//人员列表
            if (PeriodId <= 0)
            {
                return ApiResult.ParamIsNull();
            }
            if (string.IsNullOrEmpty(EmpIds))
            {
                return ApiResult.ParamIsNull("请传递用户id");
            }

            string msg = sellAchievementsModule.SetSellAchievementsSendEmployee(PeriodId, EmpIds, userInfo);
            if (msg == "")
            {
                return ApiResult.Success("");
            }
            else {
                return ApiResult.Failed(msg);
            }
        }

        /// <summary>
        /// 获取应发提成变更记录
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetSellAchievementsChangeLog() {
            var userInfo = base.UserInfo;
            JObject parms = JObject.Parse(RequestParm.Msg.ToString());
            int OrderId = parms.GetInt("OrderId", 0);
            int UserId = parms.GetInt("UserId", 0);
            int UserType = parms.GetInt("UserType", 0);
            int IsDept = parms.GetInt("IsDept", 0);
            if (OrderId <= 0 || UserId <= 0 || UserType <= 0 || IsDept <= 0) {
                return ApiResult.ParamIsNull();
            }

            var Robj = sellAchievementsModule.GetSellAchievementsChangeLog(OrderId, UserId, UserType, IsDept, userInfo.Group_Id, out string msg);
            if (msg == "")
            {
                return ApiResult.Success("", Robj);
            }
            else {
                return ApiResult.Failed(msg);
            }
        }

        #endregion

        #region 销售中心
        /// <summary>
        /// 获取市场中心业绩提成
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetSellCenterAchievementsStatistics() {
            var userInfo = base.UserInfo;
            JObject parms = JObject.Parse(base.RequestParm.Msg.ToString());
            string Month = parms.GetStringValue("Month");
            if (string.IsNullOrEmpty(Month)) {
                return ApiResult.ParamIsNull();
            }
            
            Month = Convert.ToDateTime(Month).ToString("yyyy-MM");
            var Robj = sellAchievementsModule.GetSellCenterAchievementsStatistics(Month, userInfo);
            return ApiResult.Success("", Robj);
        }

        /// <summary>
        /// 获取业绩发放明细分页列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetAchievementsEmpPageList() {
            var userInfo = base.UserInfo;
            ResultPageModel pmodel = JsonHelper.DeserializeObject<ResultPageModel>(base.RequestParm.Msg.ToString());
            RB_Sell_Achievements_Emp_ViewModel demodel = JsonHelper.DeserializeObject<RB_Sell_Achievements_Emp_ViewModel>(base.RequestParm.Msg.ToString());
            demodel.Group_Id = userInfo.Group_Id;
            demodel.EmpId = userInfo.Id;
            demodel.IsDept = 2;//非部门的

            var list = sellAchievementsModule.GetAchievementsEmpPageList(pmodel.PageIndex, pmodel.PageSize, out long count, demodel);
            pmodel.Count = Convert.ToInt32(count);
            pmodel.PageData = list.Select(x => new
            {
                x.Id,
                x.OrderId,
                x.Rate,
                x.PushMoney,
                x.GiveOutMoney,
                x.SaleMoney,
                x.Remark
            });
            return ApiResult.Success("", pmodel);
        }
        #endregion
    }
}
