using APT.BaseData.Domain.Entities.PF;
using APT.BaseData.Domain.IServices.Platform;
using APT.Infrastructure.Core;
using APT.BaseData.Domain.ApiModel.EX;
using APT.BaseData.Domain.IServices.FM;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Linq;

using APT.Utility;
using APT.BaseData.Domain.ApiModel.EX;
using APT.BaseData.Domain.Enums;
using System.Dynamic;
using APT.Infrastructure.Api;

namespace APT.PF.WebApiControllers.Api.PF
{
    //[APT.Infrastructure.Api.RootOrg]
    /// <summary>
    /// 版本管理
    /// </summary>
    [Route("api/PF/VersionManage")]
    public class VersionManageController : AuthorizeApiController<T_PF_VERSION_MANAGE>
    {
        IPFLastVersionService LastVersionService { get; set; }
        /// <summary>
        /// 构造函数实现接口
        /// </summary>
        /// <param name="lastVersionService"></param>
        public VersionManageController(IPFLastVersionService lastVersionService)
        {
            LastVersionService = lastVersionService;
        }

        /// <summary>
        /// 保存并发送通知
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        [HttpPost, Route("FullUpdateToNotifice")]
        public JsonActionResult<bool> FullUpdateToNotifice([FromBody] T_PF_VERSION_MANAGE entity)
        {
            return SafeExecute<bool>(() =>
            {
                this.UpdateEntity(entity);

                //发送通知
                try
                {
                    SendNotificationModel model = new SendNotificationModel();
                    model.UserId = APT.Infrastructure.Api.AppContext.CurrentSession.UserID;
                    model.OrgId = entity.ORG_ID;
                    model.CheckOrgType = (int)FMNotificationOrgTypeEnum.本组织及下级组织;
                    model.ShowInfoType = (int)FMNotificationShowTypeEnum.弹窗;
                    model.NotificatoinType = (int)FMNotificationTypeEnum.普通通知;
                    model.Title = "系统版本升级更新提醒";
                    var UserName = APT.Infrastructure.Api.AppContext.CurrentSession.UserName;
                    model.Message = UserName + "发布了新的系统版本,更新内容：" + entity.CONTENT + ",最新版本为" + entity.VERSION + ",请知悉！";
                    List<WebSocketClientInfo> clientInfos = null;
                    if (model.UserId != null)
                        clientInfos = WebSocketServiceHelper.ClintInfos.Where(t => t.UserId == model.UserId).ToList();
                    var notificatoinService = APT.Infrastructure.Api.ServiceLocator.Instance.GetService<IFMNotificatoinService>();
                    notificatoinService.SendNotification(model, clientInfos);
                }
                catch { }
                return true;
            });

        }

        /// <summary>
        /// 获取上个版本
        /// </summary>
        /// <param name="filter"></param>
        /// <returns></returns>
        [HttpPost, Route("GetLastVersion")]
        public JsonActionResult<string> GetLastVersion([FromBody] KeywordFilter filter)
        {
            return SafeExecute(() =>
            {
                return LastVersionService.GetLastVersion(filter);
            });
        }

        /// <summary>
        /// 获取当前版本
        /// </summary>
        /// <param name="filter"></param>
        /// <returns></returns>
        [HttpPost, Route("GetCurrVer")]
        public JsonActionResult<dynamic> GetCurrVer([FromBody] KeywordFilter filter)
        {
            return SafeExecute(() =>
            {
                var version = this.GetEntity<T_PF_VERSION_MANAGE>(null,
                    new BaseFilter(filter.OrgId) { Sort = "CREATE_TIME", Order = DbOrder.DESC });
                dynamic obj = new ExpandoObject();
                obj.Version = version?.VERSION_NO;
                obj.Version_ZH = version?.VERSION_ZH;
                return obj;
            });
        }
    }
}
