using APT.BaseData.Domain.Entities;
using APT.Infrastructure.Core;
using APT.MS.Domain.Entities.HM;
using APT.MS.Domain.Entities.SK;
using APT.MS.Domain.Enums;
using APT.Utility;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;

namespace APT.SK.WebApi.Controllers.Api
{
    /// <summary>
    ///  辨识区域
    /// </summary>
    [Route("api/SK/SKRiskArea")]
    public partial class RiskAreaController : AuthorizeTreeApiController<T_SK_RISK_AREA>
    {
        /// <summary>
        /// 更新或新增数据
        /// </summary>
        /// <param name="entity">对象实体</param>
        /// <returns></returns>
        [HttpPost, Route("FullUpdate")]
        public JsonActionResult<bool> FullUpdate([FromBody] T_SK_RISK_AREA entity)
        {
            //return WitUpdate(entity);
            return SafeExecute(() =>
            {
                var videos = entity.Nav_Videos;
                entity.Nav_Videos = null;
                if (videos.Count > 0)
                {
                    foreach (var item in videos)
                    {
                        item.Nav_Viedo = null;
                        item.Nav_Area = null;
                    }
                }
                this.UnifiedCommit(() =>
                {
                    if (videos != null && videos.Count > 0)
                    {
                        BantchSaveEntityNoCommit(videos);
                    }
                    TreeUpdateEntity(entity);
                });
                return true;
            });
        }

        /// <summary>
        /// 区域最小单元
        /// </summary>
        /// <param name="pageFilter"></param>
        /// <returns></returns>
        [HttpPost, Route("GetAreaList")]
        public PagedActionResult<T_SK_RISK_AREA> GetAreaList([FromBody] KeywordPageFilter pageFilter)
        {
            return SafeGetPagedData<T_SK_RISK_AREA>((result) =>
            {
                var orgId = APT.Infrastructure.Api.AppContext.CurrentSession.OrgId.ToString();
                List<Guid> ids = new List<Guid>();
                Expression<Func<T_SK_RISK_AREA, bool>> expression = e => !e.IS_DELETED;
                var areas = this.GetEntities<T_SK_RISK_AREA>(expression, new BaseFilter(pageFilter.OrgId)).ToList();
                if (areas != null & areas.Any())
                {
                    var parentIds = areas.Where(m => m.PARENT_ID != null).Select(t => t.PARENT_ID).ToList();
                    foreach (var item in areas)
                    {
                        if (!parentIds.Contains(item.ID))
                        {
                            ids.Add(item.ID);
                        }
                    }
                }
                Expression<Func<T_SK_RISK_AREA, bool>> expressionArea = e => !e.IS_DELETED;
                if (ids != null && ids.Any())
                {
                    expressionArea = expressionArea.And(t => ids.Contains(t.ID));
                }
                var info = this.GetOrderPageEntities<T_SK_RISK_AREA>(expressionArea, pageFilter, null);
                result.TotalCount = info.TotalCount;
                result.Data = info.Data;
                result.IsSuccessful = info.IsSuccessful;
            });
        }
        /// <summary>
        /// 根据主键删除数据
        /// </summary>
        /// <param name="id">主键ID</param>
        /// <returns></returns>
        [HttpGet, Route("SKDelete")]
        public JsonActionResult<bool> SKDelete(string ids)
        {
            return SafeExecute(() =>
            {
                if (string.IsNullOrEmpty(ids))
                    this.ThrowError("060010");
                var area = this.GetEntity<T_SK_RISK_AREA>(ids);
                var areas = new List<T_SK_RISK_AREA>();
                if (area != null)
                {
                    area.IS_DELETED = true;
                    areas.Add(area);
                }
                UnifiedCommit(() =>
                {
                    if (area != null)
                        this.BantchUpdateEntity_noneBase(areas);
                   
                });
                return true;
            });
        }
    }
}