using Kivii.Linq;
using Kivii.Web;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Kivii.Domain.Contents.Transforms
{
    #region  CRUDQ
    /// <summary>
    /// 没有设置权限,方便Job中调用接口
    /// </summary>
    //[RequiresAnyRole(MemberRoles.Everyone)]
    public class RedirectCreate : RestfulExecution<Redirect>
    {
        public Redirect Item { get; set; }
        public List<Redirect> Items { get; set; }
        public override object OnExecution(IRequest req, IResponse res)
        {
            (Item == null && Items.IsNullOrEmpty()).ThrowIfTrue("Need Item Or Items!");
            if (Items.IsNullOrEmpty()) Items = new List<Redirect>();
            if (Item != null) Items.Add(Item);

            var conn = KiviiContext.GetOpenedDbConnection<Redirect>();
            var rtns = new RestfulCreateResponse<Redirect>();
            rtns.Results = new List<Redirect>();

            foreach (var item in Items)
            {
                item.ThrowIfNull("请传入要创建的内容!");
                if (item.Kvid == Guid.Empty) item.Kvid = Guid.NewGuid();
                if (item.InternalCode.IsNullOrEmpty())
                {
                    if (item.Type.IsNullOrEmpty()) throw new Exception("InternalCode和Type不能同时为空!");
                    item.InternalCode = $"{item.Type}/{item.Kvid.ToString().Substring(0, 8)}";
                }
                item.Title.ThrowIfNullOrEmpty("重定向标题不能为空!");
                item.OperateTime = DateTime.Now;
                conn.Insert(item);
                rtns.Results.Add(item);
            }
            return rtns;
        }
    }

    [RequiresAnyRole(MemberRoles.Administrator, MemberRoles.Manager, MemberRoles.Configuration)]
    public class RedirectUpdate : RestfulUpdate<Redirect>
    { }
    
    public class RedirectQuery : RestfulQuery<Redirect>
    { }
    
    public class RedirectRead : RestfulRead<Redirect>
    { }
    #endregion

    [Api(Description = "业务关联")]
    [RequiresAnyRole(MemberRoles.Administrator, MemberRoles.Manager, MemberRoles.Configuration)]
    public class RedirectBiz : RestfulExecution<Redirect>
    {
        public List<Guid> Kvids { get; set; }

        public Guid BizKvid { get; set; }

        public string BizType { get; set; }

        public string BizId { get; set; }

        public override object OnExecution(IRequest req, IResponse res)
        {
            Kvids.ThrowIfNull("缺少Kvid");
            BizKvid.ThrowIfEmpty("缺少关联对象BizKvid");
            BizType.ThrowIfNullOrEmpty("缺少关联对象实体类名BizType");
            Type entityType = Text.AssemblyUtils.FindType(BizType);
            entityType.ThrowIfNull($"不正确的实体类名:{BizType}");
            var conn = KiviiContext.GetOpenedDbConnection<Redirect>();
            var redirects = conn.SelectByIds<Redirect>(Kvids);
            var rtns = new RestfulUpdateResponse<Redirect>();
            rtns.Results = new List<Redirect>();
            foreach (var redirect in redirects)
            {
                redirect.ThrowIfNull("未找到对应数据,无法进行关联!");
                redirect.BizId = BizId;
                redirect.AddOnlyProperties(o => o.BizId);
                redirect.BizType = BizType;
                redirect.AddOnlyProperties(o => o.BizType);
                redirect.BizKvid = BizKvid;
                redirect.AddOnlyProperties(o => o.BizKvid);
                conn.UpdateOnly(redirect);
                rtns.Results.Add(redirect);
            }
            return rtns;
        }
    }
}
