using APT.BaseData.Domain.Entities.FM;
using APT.Infrastructure.Core;
using APT.MS.Domain.Entities.SK;
using APT.MS.Domain.Enums.SK;
using APT.Utility;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Linq;

namespace APT.SK.WebApi.Controllers.Api
{
    /// <summary>
    ///  检查类型
    /// </summary>
    [Route("api/SK/SKCheckType")]
    public partial class CheckTypeController : AuthorizeApiController<T_SK_CHECK_TYPE>
    {
        /// <summary>
        /// 更新或新增数据
        /// </summary>
        /// <param name="entity">对象实体</param>
        /// <returns></returns>
        [HttpPost, Route("FullUpdate")]
        public JsonActionResult<bool> FullUpdate([FromBody] T_SK_CHECK_TYPE entity)
        {
            return SafeExecute<bool>(() =>
            {
                var olds = this.GetEntity<T_SK_CHECK_TYPE>(t => !t.IS_DELETED && t.PRODUCTION_UNIT_ID == entity.PRODUCTION_UNIT_ID && t.NAME == entity.NAME && t.ID != entity.ID);
                if (olds != null)
                {
                    throw new Exception("该生产单元检查类型已存在，请先修改");
                }
                var details = entity.Nav_CheckCycle;
                entity.Nav_CheckCycle = null;
                if (details != null && details.Any())
                {
                    foreach (var item in details)
                    {
                        item.ORG_ID = entity.ORG_ID;
                        item.CHECK_TYPE_ID = entity.ID;
                    }
                }

                this.UnifiedCommit(() =>
                {
                    if (entity != null)
                        UpdateEntityNoCommit(entity);
                    if (details != null && details.Any())
                        BantchSaveEntityNoCommit(details);
                });
                return true;
            });
        }
        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="pageFilter"></param>
        /// <returns></returns>
        [HttpPost, Route("OrderPagedNew")]
        public PagedActionResult<T_SK_CHECK_TYPE> OrderPagedNew([FromBody] KeywordPageFilter pageFilter)
        {
            var result = new PagedActionResult<T_SK_CHECK_TYPE>();
            if (!string.IsNullOrEmpty(pageFilter.Keyword.ToString()))
            {
                FilterRule order2 = new FilterRule();
                order2.Field = "PRODUCTION_UNIT_ID";
                order2.Value = Guid.Parse(pageFilter.Keyword.ToString());
                pageFilter.FilterGroup.Rules.Add(order2);
            }
            result = GetOrderPageEntities<T_SK_CHECK_TYPE>(null, pageFilter);
            return result;
        }
    }
}
