using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Edu.Cache.User;
using Edu.Common;
using Edu.Common.API;
using Edu.Common.Enum.Finance;
using Edu.Common.Enum.WeChat;
using Edu.Common.Plugin;
using Edu.Model.ViewModel.WeChat;
using Edu.Module.QYWeChat;
using Edu.Module.User;
using Edu.ThirdCore.QYWinXin;
using Edu.WebApi.Filter;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json.Linq;

namespace Edu.WebApi.Controllers.QYWeChat
{
    /// <summary>
    /// 投稿管理接口
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiExceptionFilter]
    [ApiController]
    [EnableCors("AllowCors")]
    public class QYContributeController : BaseController
    {
        /// <summary>
        /// 投稿处理对象
        /// </summary>
        private readonly QYContributeModule contributeModule = new QYContributeModule();


        #region 投稿配置

        /// <summary>
        /// 获取投稿配置信息
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetContributeConfigInfo()
        {
            var userInfo = base.UserInfo;
            JObject parms = JObject.Parse(base.RequestParm.Msg.ToString());
            int Type = parms.GetInt("Type", 0);//平台类型
            if (Type <= 0)
            {
                return ApiResult.ParamIsNull();
            }

            var model = contributeModule.GetContributeConfigInfo(userInfo.Group_Id, Type);
            return ApiResult.Success("", new
            {
                model?.Id,
                model?.Type,
                model?.Name,
                model?.AppId,
                model?.AppSecret,
                model?.Enable
            });
        }

        /// <summary>
        /// 设置投稿配置
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult SetContributeConfigInfo()
        {
            var userInfo = base.UserInfo;
            RB_Contribute_Config_ViewModel demodel = JsonHelper.DeserializeObject<RB_Contribute_Config_ViewModel>(base.RequestParm.Msg.ToString());
            if (string.IsNullOrEmpty(demodel.Name))
            {
                return ApiResult.ParamIsNull("请传递对象名称");
            }
            if (string.IsNullOrEmpty(demodel.AppId))
            {
                return ApiResult.ParamIsNull("请传递对象appid");
            }
            if (string.IsNullOrEmpty(demodel.AppSecret))
            {
                return ApiResult.ParamIsNull("请传递对象秘钥");
            }

            demodel.Group_Id = userInfo.Group_Id;
            demodel.CreateBy = userInfo.Id;
            demodel.CreateTime = DateTime.Now;
            demodel.UpdateBy = userInfo.Id;
            demodel.UpdateTime = DateTime.Now;

            string msg = contributeModule.SetContributeConfigInfo(demodel);
            if (msg == "")
            {
                return ApiResult.Success();
            }
            else
            {
                return ApiResult.Failed(msg);
            }
        }

        /// <summary>
        /// 获取枚举列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetContributeConfigTypeEnumList()
        {
            var list = EnumHelper.EnumToList(typeof(ContributeConfigTypeEnum));
            return ApiResult.Success("", list.OrderBy(x => x.Id).Select(x => new
            {
                x.Name,
                x.Id
            }));
        }

        #endregion

        #region 素材管理

        /// <summary>
        /// 获取素材分组分页列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetContributeMediumGroupPageList()
        {
            var userInfo = base.UserInfo;
            var pageModel = JsonHelper.DeserializeObject<ResultPageModel>(base.RequestParm.Msg.ToString());
            var demodel = JsonHelper.DeserializeObject<RB_Contribute_MediumGroup_ViewModel>(base.RequestParm.Msg.ToString());
            demodel.Group_Id = userInfo.Group_Id;

            var list = contributeModule.GetContributeMediumGroupPageList(pageModel.PageIndex, pageModel.PageSize, out long count, demodel);
            pageModel.Count = Convert.ToInt32(count);
            pageModel.PageData = list.Select(x => new
            {
                x.Id,
                x.Name,
                x.Sort,
                x.UpdateBy,
                UpdateByName = UserReidsCache.GetUserLoginInfo(x.UpdateBy)?.AccountName ?? "",
                UpdateTime = x.UpdateTime.ToString("yyyy-MM-dd HH:mm")
            });
            return ApiResult.Success("", pageModel);
        }

        /// <summary>
        /// 获取素材分组列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetContributeMediumGroupList()
        {
            var userInfo = base.UserInfo;
            var demodel = JsonHelper.DeserializeObject<RB_Contribute_MediumGroup_ViewModel>(base.RequestParm.Msg.ToString());
            demodel.Group_Id = userInfo.Group_Id;

            var list = contributeModule.GetContributeMediumGroupList(demodel);
            return ApiResult.Success("", list.Select(x => new
            {
                x.Id,
                x.Name,
                x.Sort,
            }));
        }

        /// <summary>
        /// 新增修改素材分组
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult SetContributeMediumGroup()
        {
            var userInfo = base.UserInfo;
            var demodel = JsonHelper.DeserializeObject<RB_Contribute_MediumGroup_ViewModel>(base.RequestParm.Msg.ToString());
            if (string.IsNullOrEmpty(demodel.Name))
            {
                return ApiResult.ParamIsNull("请传递分组名称");
            }

            demodel.Group_Id = userInfo.Group_Id;
            demodel.Status = 0;
            demodel.CreateBy = userInfo.Id;
            demodel.CreateTime = DateTime.Now;
            demodel.UpdateBy = userInfo.Id;
            demodel.UpdateTime = DateTime.Now;
            string msg = contributeModule.SetContributeMediumGroup(demodel);
            if (msg == "")
            {
                return ApiResult.Success();
            }
            else
            {
                return ApiResult.Failed();
            }
        }

        /// <summary>
        /// 删除素材分组
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult DelContributeMediumGroup()
        {
            var userInfo = base.UserInfo;
            JObject parms = JObject.Parse(base.RequestParm.Msg.ToString());
            int MediumId = parms.GetInt("MediumId", 0);
            if (MediumId <= 0)
            {
                return ApiResult.ParamIsNull();
            }

            string msg = contributeModule.DelContributeMediumGroup(MediumId, userInfo);
            if (msg == "")
            {
                return ApiResult.Success();
            }
            else
            {
                return ApiResult.Failed(msg);
            }
        }


        #region 素材

        /// <summary>
        /// 获取素材分页列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetContributeMediumPageList()
        {
            var userInfo = base.UserInfo;
            ResultPageModel pmodel = JsonHelper.DeserializeObject<ResultPageModel>(base.RequestParm.Msg.ToString());
            RB_Contribute_Medium_ViewModel demodel = JsonHelper.DeserializeObject<RB_Contribute_Medium_ViewModel>(base.RequestParm.Msg.ToString());
            demodel.Group_Id = userInfo.Group_Id;

            var list = contributeModule.GetContributeMediumPageList(pmodel.PageIndex, pmodel.PageSize, out long count, demodel);
            pmodel.Count = Convert.ToInt32(count);
            pmodel.PageData = list.Select(x => new
            {
                x.Id,
                x.MediaGroupId,
                x.MediaGroupName,
                x.Media_Id,
                x.ImageUrl,
                x.MediaName,
                x.Description,
                x.Type,
                TypeName = x.Type.ToName(),
                x.UpdateBy,
                UpdateByName = Cache.User.UserReidsCache.GetUserLoginInfo(x.UpdateBy)?.AccountName ?? "",
                UpdateTime = x.UpdateTime.ToString("yyyy-MM-dd HH:mm"),
                x.VideoImage,
                x.VideoWXImage,
                x.Minutes,
                x.Path,
                x.VideoWXPath,
                x.PlatformType
            });
            return ApiResult.Success("", pmodel);
        }

        /// <summary>
        /// 新增修改素材
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult SetContributeMediumInfo()
        {
            var userInfo = base.UserInfo;
            RB_Contribute_Medium_ViewModel demodel = JsonHelper.DeserializeObject<RB_Contribute_Medium_ViewModel>(base.RequestParm.Msg.ToString());
            if (demodel.Id <= 0)
            {
                if (demodel.MediaGroupId <= 0)
                {
                    return ApiResult.ParamIsNull("请选择分组");
                }
                if (demodel.PlatformType <= 0)
                {
                    return ApiResult.ParamIsNull("请选择所属平台");
                }
                if (string.IsNullOrEmpty(demodel.MediaName))
                {
                    return ApiResult.ParamIsNull("请传递标题");
                }
                if (string.IsNullOrEmpty(demodel.Path))
                {
                    return ApiResult.ParamIsNull("请传递文件路径");
                }
                if (demodel.Type == ContributeMediumTypeEnum.Video)
                {
                    return ApiResult.ParamIsNull("视频暂无法上传");
                }
                if (demodel.Type != ContributeMediumTypeEnum.Image)
                {
                    if (string.IsNullOrEmpty(demodel.Minutes))
                    {
                        return ApiResult.ParamIsNull("请传递时长");
                    }
                }
            }
            else
            {
                //修改  图片 音频只能修改标题   视频可修改封面图 + 介绍 + 标题
                if (string.IsNullOrEmpty(demodel.MediaName))
                {
                    return ApiResult.ParamIsNull("请传递标题");
                }
                if (demodel.Type == ContributeMediumTypeEnum.Video)
                {
                    if (string.IsNullOrEmpty(demodel.VideoWXImage))
                    {
                        return ApiResult.ParamIsNull("请传递视频封面图");
                    }
                }
            }
            demodel.Group_Id = userInfo.Group_Id;
            demodel.Status = 0;
            demodel.CreateBy = userInfo.Id;
            demodel.CreateTime = DateTime.Now;
            demodel.UpdateBy = userInfo.Id;
            demodel.UpdateTime = DateTime.Now;

            string msg = contributeModule.SetContributeMediumInfo(demodel);
            if (msg == "")
            {
                return ApiResult.Success();
            }
            else
            {
                return ApiResult.Failed(msg);
            }
        }

        /// <summary>
        /// 获取启用的素材平台列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetContributePlatfromList()
        {
            var userInfo = base.UserInfo;

            var list = contributeModule.GetContributePlatfromList(userInfo);
            return ApiResult.Success("", list);
        }

        /// <summary>
        /// 素材移动分组
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult SetContributeMediumMoveGroup()
        {
            var userInfo = base.UserInfo;
            JObject parms = JObject.Parse(base.RequestParm.Msg.ToString());
            string MediumIds = parms.GetStringValue("MediumIds");// 素材Ids  英文逗号分隔
            int MediumGroupId = parms.GetInt("MediumGroupId", 0);//新分组的ID
            if (string.IsNullOrEmpty(MediumIds))
            {
                return ApiResult.ParamIsNull("请传递素材ID");
            }
            if (MediumGroupId <= 0)
            {
                return ApiResult.ParamIsNull("请传递素材分组ID");
            }

            string msg = contributeModule.SetContributeMediumMoveGroup(MediumIds, MediumGroupId, userInfo);
            if (msg == "")
            {
                return ApiResult.Success();
            }
            else
            {
                return ApiResult.Failed(msg);
            }
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult DelContributeMediumBatch()
        {
            var userInfo = base.UserInfo;
            JObject parms = JObject.Parse(base.RequestParm.Msg.ToString());
            string MediumIds = parms.GetStringValue("MediumIds");// 素材Ids  英文逗号分隔
            if (string.IsNullOrEmpty(MediumIds))
            {
                return ApiResult.ParamIsNull("请传递素材ID");
            }

            string msg = contributeModule.DelContributeMediumBatch(MediumIds, userInfo);
            if (msg == "")
            {
                return ApiResult.Success();
            }
            else
            {
                return ApiResult.Failed(msg);
            }
        }

        /// <summary>
        /// 获取枚举列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetContributeMediumTypeEnumList()
        {
            var list = EnumHelper.EnumToList(typeof(ContributeMediumTypeEnum));
            return ApiResult.Success("", list.OrderBy(x => x.Id).Select(x => new
            {
                x.Name,
                x.Id
            }));
        }

        /// <summary>
        /// 上传临时文件-图文素材 图片 至公众号
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult SetContributeImageText()
        {
            var userInfo = base.UserInfo;
            JObject parms = JObject.Parse(base.RequestParm.Msg.ToString());
            string filePath = parms.GetStringValue("filePath");//临时文件路径
            if (string.IsNullOrEmpty(filePath))
            {
                return ApiResult.Failed("请传递文件路径");
            }

            string RMsg = contributeModule.UploadImageTextGetUrl(filePath, userInfo.Group_Id, out bool flag);
            if (flag)
            {
                return ApiResult.Success("", RMsg);
            }
            else
            {
                return ApiResult.Failed(RMsg);
            }
        }

        #endregion

        #endregion

        #region 小程序信息

        /// <summary>
        /// 获取小程序分页列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetContributeAppletPageList()
        {
            var userInfo = base.UserInfo;
            var pageModel = JsonHelper.DeserializeObject<ResultPageModel>(base.RequestParm.Msg.ToString());
            var demodel = JsonHelper.DeserializeObject<RB_Contribute_Applet_ViewModel>(base.RequestParm.Msg.ToString());
            demodel.Group_Id = userInfo.Group_Id;

            var list = contributeModule.GetContributeAppletPageList(pageModel.PageIndex, pageModel.PageSize, out long count, demodel);
            pageModel.Count = Convert.ToInt32(count);
            pageModel.PageData = list.Select(x => new
            {
                x.Id,
                x.Name,
                x.AppID,
                x.Image,
                x.Page,
                x.UpdateBy,
                UpdateByName = UserReidsCache.GetUserLoginInfo(x.UpdateBy)?.AccountName ?? "",
                UpdateTime = x.UpdateTime.ToString("yyyy-MM-dd HH:mm")
            });
            return ApiResult.Success("", pageModel);
        }

        /// <summary>
        /// 获取小程序列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetContributeAppletList()
        {
            var userInfo = base.UserInfo;
            var demodel = JsonHelper.DeserializeObject<RB_Contribute_Applet_ViewModel>(base.RequestParm.Msg.ToString());
            demodel.Group_Id = userInfo.Group_Id;

            var list = contributeModule.GetContributeAppletList(demodel);
            return ApiResult.Success("", list.Select(x => new
            {
                x.Id,
                x.Name,
                x.AppID,
                x.Image,
                x.Page
            }));
        }

        /// <summary>
        /// 新增修改小程序信息
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult SetContributeApplet()
        {
            var userInfo = base.UserInfo;
            var demodel = JsonHelper.DeserializeObject<RB_Contribute_Applet_ViewModel>(base.RequestParm.Msg.ToString());
            if (string.IsNullOrEmpty(demodel.Name))
            {
                return ApiResult.ParamIsNull("请传递小程序名称");
            }
            if (string.IsNullOrEmpty(demodel.AppID))
            {
                return ApiResult.ParamIsNull("请传递小程序appid");
            }
            if (string.IsNullOrEmpty(demodel.Image))
            {
                return ApiResult.ParamIsNull("请传递小程序图标");
            }
            if (string.IsNullOrEmpty(demodel.Page))
            {
                return ApiResult.ParamIsNull("请传递小程序默认路径");
            }

            demodel.Group_Id = userInfo.Group_Id;
            demodel.Status = 0;
            demodel.CreateBy = userInfo.Id;
            demodel.CreateTime = DateTime.Now;
            demodel.UpdateBy = userInfo.Id;
            demodel.UpdateTime = DateTime.Now;
            string msg = contributeModule.SetContributeApplet(demodel);
            if (msg == "")
            {
                return ApiResult.Success();
            }
            else
            {
                return ApiResult.Failed();
            }
        }

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

            string msg = contributeModule.DelContributeApplet(AppletId, userInfo);
            if (msg == "")
            {
                return ApiResult.Success();
            }
            else
            {
                return ApiResult.Failed(msg);
            }
        }

        #endregion

        #region 投稿管理
        /// <summary>
        /// 获取我的投稿分页列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetMyContributeInfoPageList()
        {
            var userInfo = base.UserInfo;
            ResultPageModel pmodel = JsonHelper.DeserializeObject<ResultPageModel>(base.RequestParm.Msg.ToString());
            RB_Contribute_Info_ViewModel demodel = JsonHelper.DeserializeObject<RB_Contribute_Info_ViewModel>(base.RequestParm.Msg.ToString());
            demodel.Group_Id = userInfo.Group_Id;
            demodel.CreateBy = userInfo.Id;

            var list = contributeModule.GetContributeInfoPageList(pmodel.PageIndex, pmodel.PageSize, out long count, demodel);
            pmodel.Count = count;
            pmodel.PageData = list.Select(x => new
            {
                x.Id,
                x.Type,
                TypeName = x.Type.ToName(),
                x.MediaState,
                x.CoverImageId,
                x.CoverImagePath,
                x.Title,
                x.Author,
                x.Description,
                x.VideoMediumId,
                x.VideoMediumPath,
                x.VideoCoverImage,
                ImageList = x.ImageList.Select(y => new
                {
                    y.Id,
                    y.ImageUrl
                }),
                x.AuditState,
                AuditStateName = x.AuditState.ToName(),
                x.IsSendCommission,
                x.CommissionMoney,
                x.UpdateBy,
                UpdateByName = Cache.User.UserReidsCache.GetUserLoginInfo(x.UpdateBy)?.AccountName ?? "",
                UpdateTime = x.UpdateTime.ToString("yyyy-MM-dd HH:mm"),
                x.RemunerationState
            });
            return ApiResult.Success("", pmodel);
        }

        /// <summary>
        /// 获取投稿详情
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetContributeInfo()
        {
            var userInfo = base.UserInfo;
            JObject prams = JObject.Parse(base.RequestParm.Msg.ToString());
            int ContributeId = prams.GetInt("ContributeId", 0);//投稿ID
            if (ContributeId <= 0)
            {
                return ApiResult.ParamIsNull();
            }

            var model = contributeModule.GetContributeInfo(ContributeId, userInfo.Group_Id);
            if (model == null || model.Status == 1) { return ApiResult.Failed("投稿不存在"); }
            return ApiResult.Success("", new
            {
                model.Id,
                model.Type,
                TypeName = model.Type.ToName(),
                model.MediaState,
                model.CoverImageId,
                model.CoverImagePath,
                model.Title,
                model.Author,
                model.Description,
                model.VideoMediumId,
                model.VideoMediumPath,
                model.VideoCoverImage,
                ImageList = model.ImageList.Select(y => new
                {
                    y.Id,
                    y.ImageUrl
                }),
                model.Content,
                model.AuditState,
                AuditStateName = model.AuditState.ToName(),
                model.UpdateBy,
                model.RemunerationState
            });
        }

        /// <summary>
        /// 新增修改投稿
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult SetContributeInfo()
        {
            var userInfo = base.UserInfo;
            RB_Contribute_Info_ViewModel demodel = JsonHelper.DeserializeObject<RB_Contribute_Info_ViewModel>(base.RequestParm.Msg.ToString());
            if (demodel.AuditState != ContributeInfoStateEnum.Auditing && demodel.AuditState != ContributeInfoStateEnum.Temporary)
            {
                return ApiResult.ParamIsNull("状态不正确");
            }
            if (demodel.Type == ContributeInfoTypeEnum.Video)
            {
                return ApiResult.ParamIsNull("视频暂无法发布");
            }
            switch (demodel.Type)
            {
                case ContributeInfoTypeEnum.ImgText:
                    if (string.IsNullOrEmpty(demodel.Title))
                    {
                        return ApiResult.ParamIsNull("请输入标题");
                    }
                    if (string.IsNullOrEmpty(demodel.Description))
                    {
                        return ApiResult.ParamIsNull("请输入摘要");
                    }
                    if (demodel.CoverImageId <= 0)
                    {
                        return ApiResult.ParamIsNull("请选择封面图");
                    }
                    break;
                case ContributeInfoTypeEnum.Audio:
                    if (string.IsNullOrEmpty(demodel.Title))
                    {
                        return ApiResult.ParamIsNull("请输入标题");
                    }
                    if (demodel.VideoMediumId <= 0)
                    {
                        return ApiResult.ParamIsNull("请选择音频");
                    }
                    break;
                case ContributeInfoTypeEnum.Image:
                    if (demodel.ImageList == null || !demodel.ImageList.Any())
                    {
                        return ApiResult.ParamIsNull("请选择图片列表");
                    }
                    demodel.ImageMediumIds = string.Join(",", demodel.ImageList.Select(x => x.Id));
                    break;
                case ContributeInfoTypeEnum.Text:
                    if (string.IsNullOrEmpty(demodel.Description))
                    {
                        return ApiResult.ParamIsNull("请输入文本内容");
                    }
                    break;
            }

            demodel.Group_Id = userInfo.Group_Id;
            demodel.School_Id = userInfo.School_Id;
            demodel.Status = 0;
            demodel.CreateBy = userInfo.Id;
            demodel.CreateTime = DateTime.Now;
            demodel.UpdateBy = userInfo.Id;
            demodel.UpdateTime = DateTime.Now;

            string msg = contributeModule.SetContributeInfo(demodel);
            if (msg == "")
            {
                return ApiResult.Success();
            }
            else
            {
                return ApiResult.Failed(msg);
            }
        }

        /// <summary>
        /// 设置投稿状态
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult SetContributeInfoState()
        {
            var userInfo = base.UserInfo;
            JObject parms = JObject.Parse(base.RequestParm.Msg.ToString());
            int ContributeId = parms.GetInt("ContributeId", 0);//投稿ID
            int Type = parms.GetInt("Type", 1);//类型  1提交审核  2删除
            if (ContributeId <= 0)
            {
                return ApiResult.ParamIsNull("请传递投稿ID");
            }

            string msg = contributeModule.SetContributeInfoState(ContributeId, Type, userInfo);
            if (msg == "")
            {
                return ApiResult.Success();
            }
            else
            {
                return ApiResult.Failed(msg);
            }
        }

        /// <summary>
        /// 设置投稿发放
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult SetRemunerationState()
        {
            var userInfo = base.UserInfo;
            JObject parms = JObject.Parse(base.RequestParm.Msg.ToString());
            int ContributeId = parms.GetInt("ContributeId", 0);//投稿ID
            int RemunerationState = parms.GetInt("RemunerationState", 1);//稿酬状态   1发放  2不发放
            if (ContributeId <= 0)
            {
                return ApiResult.ParamIsNull("请传递投稿ID");
            }

            string msg = contributeModule.SetRemunerationState(ContributeId, RemunerationState, userInfo);
            if (msg == "")
            {
                return ApiResult.Success();
            }
            else
            {
                return ApiResult.Failed(msg);
            }
        }

        /// <summary>
        /// 获取枚举列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetContributeInfoStateEnumList()
        {
            var list = EnumHelper.EnumToList(typeof(ContributeInfoStateEnum));
            return ApiResult.Success("", list.OrderBy(x => x.Id).Select(x => new
            {
                x.Name,
                x.Id
            }));
        }

        /// <summary>
        /// 获取枚举列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetContributeInfoTypeEnumList()
        {
            var list = EnumHelper.EnumToList(typeof(ContributeInfoTypeEnum));
            return ApiResult.Success("", list.OrderBy(x => x.Id).Select(x => new
            {
                x.Name,
                x.Id
            }));
        }
        #endregion

        #region 发布管理

        /// <summary>
        /// 获取投稿发布分页列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetContributeInfoAuditedPageList()
        {
            var userInfo = base.UserInfo;
            ResultPageModel pmodel = JsonHelper.DeserializeObject<ResultPageModel>(base.RequestParm.Msg.ToString());
            RB_Contribute_Info_ViewModel demodel = JsonHelper.DeserializeObject<RB_Contribute_Info_ViewModel>(base.RequestParm.Msg.ToString());
            demodel.Group_Id = userInfo.Group_Id;
            demodel.AuditState = ContributeInfoStateEnum.Audited;//只查询审核通过的

            var list = contributeModule.GetContributeInfoPageList(pmodel.PageIndex, pmodel.PageSize, out long count, demodel);
            pmodel.Count = count;
            pmodel.PageData = list.Select(x => new
            {
                x.Id,
                x.Type,
                TypeName = x.Type.ToName(),
                x.MediaState,
                x.CoverImageId,
                x.CoverImagePath,
                x.Title,
                x.Author,
                x.Description,
                x.VideoMediumId,
                x.VideoMediumPath,
                x.VideoCoverImage,
                ImageList = x.ImageList.Select(y => new
                {
                    y.Id,
                    y.ImageUrl
                }),
                x.AuditState,
                AuditStateName = x.AuditState.ToName(),
                x.UpdateBy,
                UpdateByName = Cache.User.UserReidsCache.GetUserLoginInfo(x.UpdateBy)?.AccountName ?? "",
                UpdateTime = x.UpdateTime.ToString("yyyy-MM-dd HH:mm"),
                x.RemunerationState
            });
            return ApiResult.Success("", pmodel);
        }

        /// <summary>
        /// 管理端修改投稿
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult SetAdminContributeInfo()
        {
            var userInfo = base.UserInfo;
            RB_Contribute_Info_ViewModel demodel = JsonHelper.DeserializeObject<RB_Contribute_Info_ViewModel>(base.RequestParm.Msg.ToString());
            switch (demodel.Type)
            {
                case ContributeInfoTypeEnum.ImgText:
                    if (string.IsNullOrEmpty(demodel.Title))
                    {
                        return ApiResult.ParamIsNull("请输入标题");
                    }
                    if (string.IsNullOrEmpty(demodel.Description))
                    {
                        return ApiResult.ParamIsNull("请输入摘要");
                    }
                    if (demodel.CoverImageId <= 0)
                    {
                        return ApiResult.ParamIsNull("请选择封面图");
                    }
                    break;
                case ContributeInfoTypeEnum.Video:
                    if (string.IsNullOrEmpty(demodel.Title))
                    {
                        return ApiResult.ParamIsNull("请输入标题");
                    }
                    if (demodel.VideoMediumId <= 0)
                    {
                        return ApiResult.ParamIsNull("请选择视频");
                    }
                    break;
                case ContributeInfoTypeEnum.Audio:
                    if (string.IsNullOrEmpty(demodel.Title))
                    {
                        return ApiResult.ParamIsNull("请输入标题");
                    }
                    if (demodel.VideoMediumId <= 0)
                    {
                        return ApiResult.ParamIsNull("请选择音频");
                    }
                    break;
                case ContributeInfoTypeEnum.Image:
                    if (demodel.ImageList == null || !demodel.ImageList.Any())
                    {
                        return ApiResult.ParamIsNull("请选择图片列表");
                    }
                    demodel.ImageMediumIds = string.Join(",", demodel.ImageList.Select(x => x.Id));
                    break;
                case ContributeInfoTypeEnum.Text:
                    if (string.IsNullOrEmpty(demodel.Description))
                    {
                        return ApiResult.ParamIsNull("请输入文本内容");
                    }
                    break;
            }

            if (demodel.IsPreview > 0)
            {
                if (demodel.IsPreview == 1)
                {
                    if (string.IsNullOrEmpty(demodel.TagId))
                    {
                        return ApiResult.ParamIsNull("请传递微信账号");
                    }
                }
                if (demodel.PlatformId <= 0)
                {
                    return ApiResult.ParamIsNull("请选择平台");
                }
            }

            demodel.Group_Id = userInfo.Group_Id;
            demodel.School_Id = userInfo.School_Id;
            demodel.Status = 0;
            demodel.CreateBy = userInfo.Id;
            demodel.CreateTime = DateTime.Now;
            demodel.UpdateBy = userInfo.Id;
            demodel.UpdateTime = DateTime.Now;

            string msg = contributeModule.SetAdminContributeInfo(demodel, userInfo);
            if (msg == "")
            {
                return ApiResult.Success();
            }
            else
            {
                return ApiResult.Failed(msg);
            }
        }

        /// <summary>
        /// 发布投稿至平台
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult SetContributeInfoPublish()
        {
            var userInfo = base.UserInfo;
            JObject parms = JObject.Parse(base.RequestParm.Msg.ToString());
            int ContributeId = parms.GetInt("ContributeId", 0);//投稿ID
            int PlatformId = parms.GetInt("PlatformId", 0);//平台ID
            int IsPreview = parms.GetInt("IsPreview", 1);// 1预览  2发布
            string TagId = parms.GetStringValue("TagId");//预览微信号   发布标签ID

            if (ContributeId <= 0)
            {
                return ApiResult.ParamIsNull("请传递投稿");
            }
            if (PlatformId <= 0)
            {
                return ApiResult.ParamIsNull("请传递平台");
            }
            if (IsPreview != 0 && IsPreview != 1 && IsPreview != 2)
            {
                return ApiResult.ParamIsNull("请传递类型");
            }

            string msg = contributeModule.SetContributeInfoPublish(ContributeId, PlatformId, IsPreview, TagId, userInfo);
            if (msg == "")
            {
                return ApiResult.Success();
            }
            else
            {
                return ApiResult.Failed(msg);
            }
        }

        /// <summary>
        /// 获取微信公众号用户标签
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetGZHUserTagList()
        {
            var userInfo = base.UserInfo;

            var list = contributeModule.GetGZHUserTagList(userInfo, out bool flag);
            if (flag)
            {
                return ApiResult.Success("", list);
            }
            else
            {
                return ApiResult.Failed(list.ToString());
            }
        }

        /// <summary>
        /// 获取投稿发布记录列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ApiResult GetContributeInfoPublishLogList()
        {
            var userInfo = base.UserInfo;
            JObject parms = JObject.Parse(base.RequestParm.Msg.ToString());
            int ContributeId = parms.GetInt("ContributeId", 0);//投稿ID
            if (ContributeId <= 0)
            {
                return ApiResult.ParamIsNull();
            }

            var list = contributeModule.GetContributeInfoPublishLogList(ContributeId, userInfo);
            return ApiResult.Success("", list.Select(x => new
            {
                x.Id,
                x.PlatfromId,
                PlatfromName = x.PlatfromId.ToName(),
                x.Media_Id,
                x.Content,
                x.Msg_Id,
                x.UpdateBy,
                UpdateByName = Cache.User.UserReidsCache.GetUserLoginInfo(x.UpdateBy)?.AccountName ?? "",
                UpdateTime = x.UpdateTime.ToString("yyyy-MM-dd HH:mm")
            }));
        }

        #endregion

        #region 测试接口

        [HttpPost]
        public ApiResult TestUplad()
        {
            string msg = contributeModule.Test();
            return ApiResult.Success(msg);
        }

        #endregion
    }
}

