using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Mall.AOP;
using Mall.Common.API;
using Mall.Common.Plugin;
using Mall.Model.Extend.Product;
using Mall.Model.Extend.User;
using Mall.Module.Product;
using Mall.Module.User;
using Mall.WebApi.Filter;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json.Linq;

namespace Mall.WebApi.Controllers.Product
{
    [Route("api/[controller]/[action]")]
    [ApiExceptionFilter]
    [ApiController]
    [EnableCors("AllowCors")]
    public class GroupBuyController : BaseController
    {
        /// <summary>
        /// 拼团配置处理类对象
        /// </summary>
        private readonly Module.User.GroupBuyConfigModule groupBuyConfigModule = new Module.User.GroupBuyConfigModule();



        /// <summary>
        /// 拼团活动
        /// </summary>
        private readonly Module.Product.GroupBuyActivityModule groupBuyConfigActivityModule = AOPHelper.CreateAOPObject<Module.Product.GroupBuyActivityModule>();

        #region 拼团配置设置
        /// <summary>
        /// 添加修改拼团配置
        /// </summary>
        /// <returns></returns>
        public ApiResult SetGroupBuyConfig()
        {
            var extModel = Common.Plugin.JsonHelper.DeserializeObject<RB_GroupBuyConfig_Extend>(RequestParm.msg.ToString());
            extModel.MallBaseId = RequestParm.MallBaseId;
            extModel.TenantId = RequestParm.TenantId;
            if (extModel.Payment_TypeList != null && extModel.Payment_TypeList.Count > 0)
            {
                extModel.Payment_Type = Common.Plugin.JsonHelper.Serialize(extModel.Payment_TypeList);
            }
            if (extModel.Send_TypeList != null && extModel.Send_TypeList.Count > 0)
            {
                extModel.Send_Type = Common.Plugin.JsonHelper.Serialize(extModel.Send_TypeList);
            }
            if (extModel.MagicDataList != null && extModel.MagicDataList.Count > 0)
            { 
               extModel.MagicJson = Common.Plugin.JsonHelper.Serialize(extModel.MagicDataList);
            }
            if (extModel.BannerList != null && extModel.BannerList.Count > 0)
            {
                extModel.BannerJson = Common.Plugin.JsonHelper.Serialize(extModel.BannerList);
            }
            var flag = groupBuyConfigModule.SetGroupBuyConfigModule(extModel);
            return flag ? ApiResult.Success() : ApiResult.Failed();
        }

        /// <summary>
        /// 添加修改拼团消息配置
        /// </summary>
        /// <returns></returns>
        public ApiResult SetGroupBuyConfigMessage()
        {
            var extModel = Common.Plugin.JsonHelper.DeserializeObject<RB_GroupBuyConfig_Extend>(RequestParm.msg.ToString());
            extModel.MallBaseId = RequestParm.MallBaseId;
            extModel.TenantId = RequestParm.TenantId;
            var flag = groupBuyConfigModule.SetGroupBuyConfigMessageModule(extModel);
            return flag ? ApiResult.Success() : ApiResult.Failed();
        }

        /// <summary>
        /// 获取拼团配置实体
        /// </summary>
        /// <returns></returns>
        public ApiResult GetGroupBuyConfig()
        {
            var query = Common.Plugin.JsonHelper.DeserializeObject<RB_GroupBuyConfig_Extend>(RequestParm.msg.ToString());
            query.MallBaseId = RequestParm.MallBaseId;
            query.TenantId = RequestParm.TenantId;
            var extModel = groupBuyConfigModule.GetGroupBuyConfigModule(query);
            return ApiResult.Success(data: extModel);
        }
        #endregion

        /// <summary>
        /// 获取拼团活动列表
        /// </summary>
        /// <returns></returns>
        public ApiResult GetGroupbuyActivityPageList()
        {
            ResultPageModel pageModel  =Common.Plugin.JsonHelper.DeserializeObject<ResultPageModel>(RequestParm.msg.ToString());
            var query = Common.Plugin.JsonHelper.DeserializeObject<RB_Groupbuy_Activity_Extend>(RequestParm.msg.ToString());
            query.MallBaseId = RequestParm.MallBaseId;
            query.TenantId = RequestParm.TenantId;
            var list = groupBuyConfigActivityModule.GetGroupbuyActivityPageListModule(pageModel.pageIndex, pageModel.pageSize, out long rowsCount, query); ;
            pageModel.pageData = list;
            pageModel.count = Convert.ToInt32(rowsCount);
            return ApiResult.Success(data: pageModel);
        }

        /// <summary>
        /// 获取拼团活动实体类
        /// </summary>
        /// <returns></returns>
        public ApiResult GetGroupbuyActivity()
        {
            var query = Common.Plugin.JsonHelper.DeserializeObject<RB_Groupbuy_Activity_Extend>(RequestParm.msg.ToString());
            query.MallBaseId = RequestParm.MallBaseId;
            query.TenantId = RequestParm.TenantId;
            var extModel = groupBuyConfigActivityModule.GetGroupbuyActivityEntityModule(query,isGetSpec:true);
            return ApiResult.Success(data: extModel);
        }

        /// <summary>
        /// 新增修改拼团活动
        /// </summary>
        /// <returns></returns>
        public ApiResult SetGroupbuyActivity()
        {
            var extModel = Common.Plugin.JsonHelper.DeserializeObject<RB_Groupbuy_Activity_Extend>(RequestParm.msg.ToString());
            extModel.MallBaseId = RequestParm.MallBaseId;
            extModel.TenantId = RequestParm.TenantId;
            if (extModel.ServiceList != null && extModel.ServiceList.Count > 0)
            {
                extModel.GoodsService = Common.Plugin.JsonHelper.Serialize(extModel.ServiceList);
            }
            bool flag = groupBuyConfigActivityModule.SetGroupbuyActivityModule(extModel);
            return flag ? ApiResult.Success() : ApiResult.Failed();
        }

        /// <summary>
        /// 删除规格
        /// </summary>
        /// <returns></returns>
        public ApiResult DeleteSpec()
        {
            JObject parms = JObject.Parse(RequestParm.msg.ToString());
            var Id = parms.GetInt("Id");
            bool flag = groupBuyConfigActivityModule.RemoveGroupBuySpecificationModule(Id);
            return flag ? ApiResult.Success() : ApiResult.Failed();
        }

        /// <summary>
        /// 删除规格值
        /// </summary>
        /// <returns></returns>
        public ApiResult DeleteSpecValue()
        {
            JObject parms = JObject.Parse(RequestParm.msg.ToString());
            var Id = parms.GetInt("Id");
            bool flag = groupBuyConfigActivityModule.RemoveGroupBuySpecificationValueModule(Id);
            return flag ? ApiResult.Success() : ApiResult.Failed();
        }
    }
}