using System;
using System.Collections.Generic;
using Edu.Cache.User;
using Edu.Common.API;
using Edu.Common.Enum.Course;
using Edu.Common.Plugin;
using Edu.Model.ViewModel.Course;
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
{
    /// <summary>
    /// 学员相关
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiExceptionFilter]
    [ApiController]
    [EnableCors("AllowCors")]
    public class StuController : BaseController
    {
        /// <summary>
        /// 学员单据处理类对象
        /// </summary>
        private readonly StudentBillModule studentBillModule = AOP.AOPHelper.CreateAOPObject<StudentBillModule>();

        /// <summary>
        /// 获取学员退课单据分页列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetStudentBackClassPage()
        {
            var pageModel = Common.Plugin.JsonHelper.DeserializeObject<ResultPageModel>(RequestParm.Msg.ToString());
            var query = new RB_Student_BackClass_ViewModel()
            {
                ClassName = base.ParmJObj.GetStringValue("ClassName"),
                QStartTime=base.ParmJObj.GetStringValue("QStartTime"),
                QEndTime=base.ParmJObj.GetStringValue("QEndTime"),
                AuditStatus=(BackClassAuditStatusEnum)base.ParmJObj.GetInt("AuditStatus"),
            };
            query.CreateBy = base.UserInfo.Id;
            var Conditon = base.ParmJObj.GetInt("Conditon");
            if (Conditon > 0)
            {
                query.Conditon = (BackClassConditionEnum)Conditon;
            }
            query.Group_Id = base.UserInfo.Group_Id;
            List<object> result = new List<object>();
            var list = studentBillModule.GetStudentBackClassPageListModule(pageModel.PageIndex, pageModel.PageSize, out long rowsCount, query);
            foreach (var item in list)
            {
                if (item.CreateBy > 0)
                {
                    item.CreateByName = UserReidsCache.GetUserLoginInfo(item.CreateBy)?.AccountName ?? "";
                }
                if (item.EnterID > 0)
                {
                    item.EnterName = UserReidsCache.GetUserLoginInfo(item.EnterID)?.AccountName ?? "";
                }
                result.Add(new
                {
                    item.BackId,
                    item.BackNum,
                    item.GuestId,
                    item.GuestName,
                    item.SchoolId,
                    item.SchoolName,
                    item.ClassId,
                    item.ClassName,
                    item.TeacherId,
                    item.TeacherName,
                    item.FinishHours,
                    item.BackMoney,
                    item.CreateTimeStr,
                    item.CreateByName,
                    item.AuditStatus,
                    item.EnterID,
                    item.EnterName,
                    item.CourseName,
                    item.ApplyReason,
                    item.OrderId,
                    AuditStatusName=item.AuditStatus.ToName(),
                    item.SpecialNode,
                    item.RealityBackMoney,
                    item.FinanceId,
                });
            }
            pageModel.Count = rowsCount;
            pageModel.PageData = result;
            return ApiResult.Success(data: pageModel);
        }

        /// <summary>
        /// 获取退款单据审核信息
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult SetBackBillAduit()
        {
            //退课单据号
            var BackId = base.ParmJObj.GetInt("BackId");
            //审核状态
            var AuditStatus = base.ParmJObj.GetInt("AuditStatus");
            //审核备注
            string Description = base.ParmJObj.GetStringValue("Description");
            //退课金额
            decimal BackMoney= base.ParmJObj.GetDecimal("BackMoney");
            //是否是特殊节点
            var SpecialNode = base.ParmJObj.GetInt("SpecialNode");
            //抄送人
            string RecipientIds = base.ParmJObj.GetStringValue("RecipientIds");
            var flag = studentBillModule.SetBackBillAduitModule(BackId, AuditStatus, Description, base.UserInfo,out string message, SpecialNode: SpecialNode, BackMoney: BackMoney, RecipientIds: RecipientIds);
            return flag ? ApiResult.Success(message: message) : ApiResult.Failed(message: message);
        }

        /// <summary>
        /// 获取学员预计退课金额
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetBackBillMoney()
        {
            //退课单据号
            var BackId = base.ParmJObj.GetInt("BackId");
            var data = studentBillModule.GetBackBillMoneyModule(BackId);
            return ApiResult.Success(data: data);
        }

        /// <summary>
        /// 获取单据详情
        /// </summary>
        /// <returns></returns>
        public ApiResult GetBackBillAduitInfo()
        {
            //退课单据号
            var BackId = base.ParmJObj.GetInt("BackId");
            var extModel = studentBillModule.GetStuBackInfoModule(BackId);
            return ApiResult.Success(data: extModel);
        }

        /// <summary>
        /// 退学  一键生成财务单据
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult SetStudentBackFinance()
        {
            var userInfo = base.UserInfo;
            JObject parms = JObject.Parse(RequestParm.Msg.ToString());
            int BackClassId = parms.GetInt("BackClassId", 0);
            int IsPublic = parms.GetInt("IsPublic", 0);
            int CurrencyId = parms.GetInt("CurrencyId", 0);
            int ClientType = parms.GetInt("ClientType", 0);
            int ClientId = parms.GetInt("ClientId", 0);
            if (BackClassId <= 0)
            {
                return ApiResult.ParamIsNull(message:"请传递退课编号!");
            }
            if (ClientId <= 0)
            {
                return ApiResult.ParamIsNull(message:"请传递客户编号!");
            }
            string msg = studentBillModule.SetStudentBackFinance(BackClassId, IsPublic, CurrencyId, ClientType, ClientId, userInfo);
            return string.IsNullOrEmpty(msg) ? ApiResult.Success() : ApiResult.Failed(msg);
        }

        /// <summary>
        /// 学员续费订单
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult RenewOrder()
        {
            //订单编号
            int OrderId = base.ParmJObj.GetInt("OrderId");
            //学员编号
            int guestId = base.ParmJObj.GetInt("GuestId");
            var orderModel = new RB_Order_ViewModel()
            {
                Class_Price = base.ParmJObj.GetDecimal("Class_Price"),//单价
                Unit_Price = base.ParmJObj.GetDecimal("Class_Price"),//成交单价
                GuestNum = 1,
                PreferPrice = base.ParmJObj.GetDecimal("PreferPrice"),//应收，
                Income = 0,
                ClassId = base.ParmJObj.GetInt("ClassId"),//班级编号
                SaleRemark =base.ParmJObj.GetStringValue("SaleRemark"),//销售备注
                CreateBy=base.UserInfo.Id,
                CreateTime=DateTime.Now,
                UpdateBy=base.UserInfo.Id,
                UpdateTime=DateTime.Now,
                Dept_Id=base.UserInfo.DeptId,
                Group_Id=base.UserInfo.Group_Id,
            };
            bool flag = studentBillModule.RenewOrderModule(OrderId, guestId, orderModel,out string message);
            return flag ? ApiResult.Success() : ApiResult.Failed(message: message);
        }

        /// <summary>
        /// 获取续费班级列表
        /// </summary>
        /// <returns></returns>
        public ApiResult GetRenewClass()
        {
            var pageModel = Common.Plugin.JsonHelper.DeserializeObject<ResultPageModel>(RequestParm.Msg.ToString());
            var query = new RB_Class_ViewModel()
            {
                Group_Id = base.UserInfo.Group_Id,
                School_Id = base.ParmJObj.GetInt("School_Id"),
                ClassType = base.ParmJObj.GetInt("ClassType"),
                StartTime = base.ParmJObj.GetStringValue("StartTime"),
                EndTime = base.ParmJObj.GetStringValue("EndTime"),
                TeacherName = base.ParmJObj.GetStringValue("TeacherName"),
                CouseId = base.ParmJObj.GetInt("CouseId"),
                OldClassId = base.ParmJObj.GetInt("ClassId")
            };
            var obj = studentBillModule.GetRenewClassModule(pageModel.PageIndex, pageModel.PageSize, out long rowsCount, query);
            pageModel.Count = Convert.ToInt32(rowsCount);
            pageModel.PageData = obj;
            return ApiResult.Success(data: pageModel);
        }
    }
}
