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

namespace APT.SC.WebApi.Controllers.Api.OG
{
    [Route("api/OG/OGAgency")]
    public class OGAgencyController : AuthorizeTreeApiController<T_OG_AGENCY>
    {
        /// <summary>
        /// 查询包含上级部门级本部门
        /// </summary>
        /// <param name="pageFilter"></param>
        /// <returns></returns>
        [HttpPost, Route("OrderPagedOther")]
        public PagedActionResult<T_OG_AGENCY> OrderPagedOther([FromBody]KeywordPageFilter pageFilter)
        {
            List<T_OG_AGENCY> list = GetEntities<T_OG_AGENCY>(t => t.ORG_ID == pageFilter.OrgId, new BaseFilter()).ToList();
            var departmentA = new T_OG_AGENCY { ID = new Guid("00000000-0000-0000-0000-000000000000"), ORG_ID = pageFilter.OrgId, NAME = "通知部门" };
            var departmentB = new T_OG_AGENCY { ID = new Guid("99999999-9999-9999-9999-999999999999"), ORG_ID = pageFilter.OrgId, NAME = "上级部门" };
            list.Add(departmentA);
            list.Add(departmentB);
            return WitOrderPaged(null, pageFilter);
        }

        /// <summary>
        /// 查询公司级/部门级部门及车间级部门,除班组级部门
        /// </summary>
        /// <param name="pageFilter"></param>
        /// <returns></returns>
        [HttpPost, Route("OrderPagedWithOutClassDep")]
        public JsonActionResult<List<T_OG_AGENCY>> OrderPagedWithOutClassDep([FromBody]KeywordPageFilter pageFilter)
        {
            var list = GetEntities<T_OG_AGENCY>(t => t.LEVEL == FMDepartmentType.公司 || t.LEVEL == FMDepartmentType.部门 ||  t.LEVEL == FMDepartmentType.车间, new BaseFilter()).ToList();

            JsonActionResult<List<T_OG_AGENCY>> result = new JsonActionResult<List<T_OG_AGENCY>>();
            result.Data = list.OrderBy(t => t.LEVEL).ToList();
            result.IsSuccessful = true;
            result.TotalCount = list.Count();
            return result;
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost, Route("Update")]
        public JsonActionResult<bool> Update([FromBody]T_OG_AGENCY entity)
        {
            return SafeExecute(() =>
            {
                if (entity.PARENT_ID != null)
                {
                    var parent = GetEntity<T_OG_AGENCY>(entity.PARENT_ID.ToString());
                    if (parent != null)
                    {
                        parent.IS_LEAF = false;
                        UpdateEntity(parent);
                    }
                }
                var users = entity.Nav_Users;
                entity.Nav_Users = null;
                if (users != null)
                {
                    users.ForEach(t =>
                    {
                        t.AGENCY_ID = entity.ID;
                        t.ORG_ID = entity.ORG_ID;
                        t.Nav_User = null;
                    });
                }
                UnifiedCommit(() =>
                {
                    UpdateEntityNoCommit(entity);
                    if (users != null)
                        BantchSaveEntityNoCommit(users);
                });
                return true;
            });
        }
        /// <summary>
        /// 更新（含人员）
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost, Route("FullUpdate")]
        public JsonActionResult<bool> FullUpdate([FromBody]T_OG_AGENCY entity)
        {
            T_OG_AGENCY parent = new T_OG_AGENCY();
            if (entity.PARENT_ID != null)
            {
                parent = GetEntity<T_OG_AGENCY>(entity.PARENT_ID.ToString());
            }

            var users = entity.Nav_Users;
            entity.Nav_Users = null;
            if (users != null)
            {
                users.ForEach(t =>
                {
                    t.AGENCY_ID = entity.ID;
                    t.ORG_ID = entity.ORG_ID;
                    t.Nav_User = null;
                });
            }

            WitCommitTransaction(() =>
            {
                UpdateEntity(entity);
                if (parent != null)
                {
                    parent.IS_LEAF = false;
                    UpdateEntity(parent);
                }
                if (users != null)
                    BantchSaveEntity(users);
            });

            return SafeExecute<bool>(() =>
            {
                return true;
            });
        }
    }
}
