using Edu.Common.API;
using Edu.Common.Plugin;
using Edu.Model.ViewModel.Customer;
using Edu.Model.ViewModel.Grade;
using Edu.Model.ViewModel.Sell;
using Edu.Module.Course;
using Edu.Module.Customer;
using Edu.Module.System;
using Edu.WebApi.Filter;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Edu.WebApi.Controllers.B2BApp
{
    /// <summary>
    /// 甲鹤同业小程序相关接口
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiExceptionFilter]
    [ApiController]
    [EnableCors("AllowCors")]
    public class B2BAppController : BaseController
    {
        /// <summary>
        /// 订单处理类对象
        /// </summary>
        private readonly OrderModule orderModule = new OrderModule();

        /// <summary>
        /// 班级处理类对象
        /// </summary>
        private readonly ClassModule classModule = new ClassModule();


        /// <summary>
        /// 客户返佣和幸福存折处理类对象
        /// </summary>
        private readonly CustomerBalanceDetailModule customerBalanceDetailModule = new CustomerBalanceDetailModule();

        private readonly AppHomePageModule appHomePageModule = new AppHomePageModule();


        /// <summary>
        /// 同行获取学员订单分页列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult CustomerGetStudentOrderPage()
        {
            var pageModel = JsonHelper.DeserializeObject<ResultPageModel>(RequestParm.Msg.ToString());
            var customer = base.AppletCustomerInfo;
            var query = new RB_Order_ViewModel()
            {
                Group_Id=customer.GroupId,
                CustomerId=customer.CustomerId,
                GuestName=base.ParmJObj.GetStringValue("GuestName"),
                Q_StudentId=base.ParmJObj.GetInt("Q_StudentId"),
            };
            var list = orderModule.GetMyOrderPageList(pageModel.PageIndex, pageModel.PageSize, out long rowsCount, query);
            List<RB_Class_Time_ViewModel> timeList = new List<RB_Class_Time_ViewModel>();
            List<RB_Customer_BalanceDetail_ViewModel> balanceList = new List<RB_Customer_BalanceDetail_ViewModel>();
            if (list != null && list.Count > 0)
            {
                string classIds = string.Join(",", list.Select(qitem => qitem.ClassId));
                timeList= orderModule.GetClassOpenTimeListModule(classIds);
                string orderIds = string.Join(",", list.Select(qitem => qitem.OrderId));
                balanceList= customerBalanceDetailModule.GetCustomerBalanceDetailListModule(new RB_Customer_BalanceDetail_ViewModel()
                {
                    QOrderIds = orderIds
                });
            }
            List<object> result = new List<object>();
            foreach (var item in list)
            {
                var timeModel = timeList.Where(qitem => qitem.ClassId == item.ClassId)?.FirstOrDefault();
                var tempBalanceList = balanceList?.Where(qitem => qitem.OrderId == item.OrderId)?.ToList();
                var gModel = item?.GuestList?.FirstOrDefault();
                result.Add(new
                {
                    item.CourseId,
                    item.CourseName,
                    item.CourseImg,
                    item.ClassId,
                    item.ClassName,
                    item.ClassNo,
                    OpenTime= timeModel?.NewPlanDateTime??"",
                    item.ClassSchoolId,
                    item.SchoolName,
                    item.OrderState,
                    OrderStateName = item.OrderState.ToName(),
                    CreateTimeStr = Common.ConvertHelper.FormatTime(item.CreateTime),
                    item.GuestNum,
                    GuestList = item.GuestList.Select(qitem => new { qitem.GuestName,qitem.TotalHours,qitem.CompleteHours }),
                    item.Income,
                    item.ClassStatus,
                    ClassStatusName = item.ClassStatus.ToName(),
                    RebateMoney = tempBalanceList?.Where(qitem=>qitem.RebateType==1)?.Sum(qitem=>qitem.Money)??0,
                    DepositbookMoney = tempBalanceList?.Where(qitem => qitem.RebateType == 2)?.Sum(qitem => qitem.Money) ?? 0,
                    item.B2BBackground,
                    item.B2BIcon,
                    item.OrderId,
                    TotalHours=gModel?.TotalHours??0,
                    CompleteHours=gModel?.CompleteHours??0,
                    item.CreateType,
                    CreateTypeName= item.CreateType.ToName(),
                    item.StuSourceIdName,
                });
            }
            pageModel.PageData = result;
            pageModel.Count = rowsCount;
            return ApiResult.Success(data: pageModel);
        }


        /// <summary>
        /// 同行获取学员上课计划
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult CustomerGetStudentPlan()
        {
            var customer = base.AppletCustomerInfo;
            int StuId = base.ParmJObj.GetInt("StuId");
            //StuId = 0;
            var obj = classModule.AppCustomerGetClassPlan(StuId);
            return ApiResult.Success(data: obj);
        }

        #region 获取客户阶段

        public Object GetPeersCustomerStageCount()
        {
            var result = appHomePageModule.GetPeersCustomerStageCount(base.AppletCustomerInfo.GroupId, base.AppletCustomerInfo.CustomerId);
            return ApiResult.Success(data: result,message:"OK");
        }

        #endregion
    }
}
