using Edu.AOP.CustomerAttribute;
using Edu.Cache.User;
using Edu.Common.Enum.Course;
using Edu.Model.Entity.EduTask;
using Edu.Model.ViewModel.Course;
using Edu.Model.ViewModel.EduTask;
using Edu.Module.Course;
using Edu.Repository.Course;
using Edu.Repository.EduTask;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Edu.Module.EduTask
{
    /// <summary>
    /// 订单转班、分拆处理类
    /// </summary>
    public class OrderChangeModule
    {
        /// <summary>
        /// 订单转班、分拆仓储层对象
        /// </summary>
        private readonly RB_Order_ChangeRepository order_ChangeRepository = new RB_Order_ChangeRepository();

        /// <summary>
        /// 教务单据仓储层对象
        /// </summary>
        private readonly RB_Education_ReceiptRepository education_ReceiptRepository = new RB_Education_ReceiptRepository();

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

        /// <summary>
        /// 班级仓储层对象
        /// </summary>
        private readonly RB_ClassRepository classRepository = new RB_ClassRepository();

        /// <summary>
        /// 课程仓储层对象
        /// </summary>
        private readonly RB_CourseRepository courseRepository = new RB_CourseRepository();

        /// <summary>
        /// 订单处理类对象
        /// </summary>
        private readonly OrderModule orderModule = new OrderModule();

        /// <summary>
        /// 课程签到记录表
        /// </summary>
        private readonly RB_Class_CheckRepository classCheckRepository = new RB_Class_CheckRepository();

        /// <summary>
        /// 订单学员仓储层对象
        /// </summary>
        private readonly RB_Order_GuestRepository guestRepository = new RB_Order_GuestRepository();

        /// <summary>
        /// 新增订单转班、分拆
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [TransactionCallHandler]
        public virtual bool SetOrderChangeModule(RB_Order_Change_ViewModel model, out string message)
        {
            message = "";
            bool flag = false;
            var newId = order_ChangeRepository.Insert(model);
            model.Id = newId;
            flag = newId > 0;
            if (flag)
            {
                var educationReceipt = new RB_Education_Receipt()
                {
                    Id = 0,
                    Title = "订单转班申请",
                    ReceiptType = Common.Enum.Finance.ReceiptTypeEnum.OrderTransClass,
                    RelationId = newId,
                    OrderId = model.SourceOrderId,
                    ClassId = model.NewClassId,
                    Group_Id = model.Group_Id,
                    School_Id = model.School_Id,
                    CreateBy = model.CreateBy,
                    CreateTime = model.CreateTime,
                    UpdateBy = model.UpdateBy,
                    UpdateTime = model.UpdateTime,
                    VerifyStatus = Common.Enum.EduTask.EduTaskRrocessStatus.NotAudit
                };
                flag = education_ReceiptRepository.SetEducationReceiptRepository(educationReceipt, out message);
            }
            return flag;
        }

        /// <summary>
        /// 订单转班处理类方法
        /// </summary>
        /// <returns></returns>
        public bool UpdateOrderChangeModule(RB_Education_Receipt receiptModel)
        {
            bool flag = false;
            if (receiptModel.RelationId > 0)
            {
                var orderChangeModel = order_ChangeRepository.GetEntity<RB_Order_Change_ViewModel>(receiptModel.RelationId);
                if (orderChangeModel != null && orderChangeModel.Id > 0)
                {
                    var oldOrderModel = orderRepository.GetEntity(orderChangeModel.SourceOrderId);
                    var newClassModel = classRepository.GetEntity(orderChangeModel.NewClassId);
                    var newCourseModel = courseRepository.GetEntity(orderChangeModel.NewCourseId);
                    var userInfo = UserReidsCache.GetUserLoginInfo(oldOrderModel.EnterID);
                    var guestClassHours = classCheckRepository.GetGuestFinishMinutesRepository(orderChangeModel.OrderGuestId.ToString())?.FirstOrDefault();
                    var guestModel = guestRepository.GetEntity(orderChangeModel.OrderGuestId);

                    //计算课时单价
                    //老订单应收
                    decimal oldPreferPrice = oldOrderModel.PreferPrice / guestModel.TotalHours * guestClassHours.FinishClassHours;
                    //新订单应收
                    decimal newPreferPrice = oldOrderModel.PreferPrice - oldPreferPrice;
                    //老订单实收
                    decimal oldIncome = 0;
                     
                    var orderModel = new RB_Order_ViewModel()
                    {
                        OrderId = 0,
                        ClassId = orderChangeModel.NewClassId,//班级编号
                        GuestNum = 1,
                        OrderSource = oldOrderModel.OrderSource,
                        OrderType = oldOrderModel.OrderType,
                        Class_Price = newCourseModel.SellPrice,//单价
                        Unit_Price = newCourseModel.SellPrice,//成交单价
                        PreferPrice = newPreferPrice,//应收，
                        Income = 0,
                        HelpEnterId = 0,
                        GeneralOccupation = "",
                        EduOccupation = "",
                        SaleRemark = orderChangeModel.Remarks,//销售备注
                        SourceId = 0,
                        IsLessPrice = 0,
                        LessPrice = 0,
                        OrderNature = oldOrderModel.OrderNature,
                        OldPreferPrice = newPreferPrice,//应收，
                        CourseId = 0,
                        StartClassHours = 0,
                        IsChaBan = 0,
                        UpOrderId = 0,
                        VisitorReserveId = 0,
                        IsRenewOrder = 1,
                    };
                    //orderModel.RenewOrderId = OrderId;
                    orderModel.Group_Id = userInfo.Group_Id;
                    orderModel.School_Id = userInfo.School_Id;
                    orderModel.Dept_Id = userInfo.DeptId;
                    orderModel.OrderForm = OrderFormEnum.Computer;
                    orderModel.EnterID = userInfo.Id;
                    orderModel.OrderState = OrderStateEnum.Normal;
                    orderModel.TradeWay = TradeWayEnum.OnLine;
                    orderModel.CreateBy = userInfo.Id;
                    orderModel.CreateTime = DateTime.Now;
                    orderModel.UpdateBy = userInfo.Id;
                    orderModel.UpdateTime = DateTime.Now;
                    orderModel.OldGuestId = orderChangeModel.OrderGuestId;
                    flag = orderModule.SetClassOrderModule(orderModel, userInfo, false, out string message);
                }
            }
            return flag;
        }
    }
}
