using APT.BaseData.Domain.ApiModel.Platform;
using APT.BaseData.Domain.Entities.OP;
using APT.Infrastructure.Core;
using APT.Utility;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Linq;

namespace CMS.OP.WebApi.Controllers.Api
{
    [Route("api/OP/OPBillingRule")]
    public partial class BillingRuleController : AuthorizeApiController<T_OP_BILLING_RULE>
    {
        /// <summary>
        /// 更新或新增数据
        /// </summary>
        /// <param name="entity">对象实体</param>
        /// <returns></returns>
        [HttpPost, Route("FullUpdate")]
        public JsonActionResult<ResultModel> FullUpdate([FromBody] T_OP_BILLING_RULE entity)
        {
            return SafeExecute(() =>
            {
                if (entity.Nav_Versions == null || entity.Nav_Versions.Count != 1)
                    return new ResultModel { IsSucceed = false, Msg = "计费规则版本必须且只能写一个" };
                var version = entity.Nav_Versions.FirstOrDefault();
                entity.Nav_Versions = null;
                version.RULE_ID = entity.ID;
                this.UnifiedCommit(() =>
                {
                    AddEntityNoCommit(entity);
                    AddEntityNoCommit(version);
                });
                entity.VERSION_ID = version.ID;
                UnifiedCommit(() =>
                {
                    UpdateEntityNoCommit(entity);
                });

                return new ResultModel { IsSucceed = true };
            });
        }

        /// <summary>
        /// 获得当前版本
        /// </summary>
        /// <param name="filter">过滤实体</param>
        /// <returns></returns>
        [HttpPost, Route("GetVersion")]
        public JsonActionResult<T_OP_BILLING_RULE_VERSION> GetVersion([FromBody] KeywordFilter filter)
        {
            return SafeExecute(() =>
            {
                var ruleId = Guid.Parse(filter.FilterGroup.Rules.FirstOrDefault().Value?.ToString());
                var version = GetEntity<T_OP_BILLING_RULE>(x => x.ID == ruleId, "Nav_Version")?.Nav_Version;
                return version;
            });
        }


        /// <summary>
        /// 升级版本
        /// </summary>
        /// <param name="entity">对象实体</param>
        /// <returns></returns>
        [HttpPost, Route("UpdateVersion")]
        public JsonActionResult<bool> UpdateVersion([FromBody] T_OP_BILLING_RULE_VERSION entity)
        {
            return SafeExecute(() =>
            {
                entity.ID = Guid.NewGuid();
                UnifiedCommit(() =>
                {
                    AddEntityNoCommit(entity);
                });
                var rule = this.GetEntity<T_OP_BILLING_RULE>(x => x.ID == entity.RULE_ID);
                rule.VERSION_ID = entity.ID;
                UnifiedCommit(() =>
                {
                    UpdateEntityNoCommit(rule);
                });

                return true;
            });
        }

        /// <summary>
        /// 根据主键删除数据
        /// </summary>
        /// <param name="id">主键ID</param>
        /// <returns></returns>
        [HttpGet, Route("FullDelete")]
        public JsonActionResult<bool> FullDelete(string id)
        {
            return SafeExecute(() =>
            {
                var ruleId = Guid.Parse(id);
                var rule = GetEntity<T_OP_BILLING_RULE>(id);
                rule.VERSION_ID = null;
                var version = GetEntities<T_OP_BILLING_RULE_VERSION>(x => x.RULE_ID == ruleId, new BaseFilter());
                var ids = version.Select(x => x.ID)?.ToList();
                UpdateEntity(rule);
                if (ids != null)
                    BantchDeleteEntity<T_OP_BILLING_RULE_VERSION>(ids);
                DeleteEntity<T_OP_BILLING_RULE>(id);
                return true;
            });
        }

    }
}
