using APT.Infrastructure.Core;
using APT.MS.Domain.Entities.BS;
using APT.MS.Domain.Enums;
using APT.Utility;
using APT.WebApi.Models;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;

namespace APT.BS.WebApi.Controllers.Api
{
    [Route("api/BS/BSCheckType")]
    public partial class BSCheckTypeController : AuthorizeApiController<T_BS_CHECK_TYPE>
    {
        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        [HttpPost, Route("FullUpdate")]
        public JsonActionResult<bool> FullUpdate([FromBody] T_BS_CHECK_TYPE entity)
        {
            return SafeExecute<bool>(() =>
            {
                List<T_BS_CHECK_TYPE_LEVEL> listCheckTypeLevelAll = entity.Nav_ListCheckTypeLevel.ToList();
                List<T_BS_CHECK_TYPE_LEVEL> listCheckTypeLevel = listCheckTypeLevelAll.FindAll(e => !e.IS_DELETED);
                if ((listCheckTypeLevel == null || listCheckTypeLevel.Count < 1) && (listCheckTypeLevelAll == null || listCheckTypeLevelAll.Count < 1))
                {
                    throw new Exception("请新增检查层级!");
                }
                var ListMineType = entity.Nav_ListMineType;
                entity.Nav_ListMineType = null;
                if (ListMineType == null)
                {
                    throw new Exception("请选择生产单元!");
                }

                if (listCheckTypeLevel.Count > 1)
                {
                    T_BS_CHECK_TYPE_LEVEL modelTemp = null;
                    for (int i = 0; i < listCheckTypeLevel.Count; i++)
                    {
                        modelTemp = GetEntity<T_BS_CHECK_TYPE_LEVEL>(e => e.CHECK_TYPE_ID == listCheckTypeLevel[i].CHECK_TYPE_ID && e.ID != listCheckTypeLevel[i].ID && e.CODE == listCheckTypeLevel[i].CODE && !e.IS_DELETED, new string[] { "Nav_CheckType" });
                        if (modelTemp != null)
                        {
                            throw new Exception("行【" + (i + 1) + "】编号在系统中【" + modelTemp.Nav_CheckType.NAME + "】有重复不能重复!");
                        }
                        for (int j = i + 1; j < listCheckTypeLevel.Count; j++)
                        {
                            if (listCheckTypeLevel[i].CheckLevel_ENUMS_ID == listCheckTypeLevel[j].CheckLevel_ENUMS_ID)
                            {
                                throw new Exception("检查层级第【" + (i + 1) + "】项与第【" + (j + 1) + "】项不能重复!");
                            }
                        }
                    }
                }
                foreach (var item in ListMineType)
                {
                    item.Nav_Enums = null;
                }
                this.UnifiedCommit(() =>
                {
                    if (entity != null)
                        UpdateEntityNoCommit(entity);
                    if (listCheckTypeLevelAll.Any())
                        BantchSaveEntityNoCommit(listCheckTypeLevelAll);
                    if (ListMineType != null && ListMineType.Any())
                        BantchSaveEntityNoCommit(ListMineType);
                });
                return true;
            });
        }



        /// <summary>
        /// 排序分页查询数据
        /// </summary>
        /// <param name="pageFilter">分页过滤实体</param>
        /// <returns></returns>
        [HttpPost, Route("OrderPaged")]
        public PagedActionResult<T_BS_CHECK_TYPE> OrderPaged([FromBody] KeywordPageFilter pageFilter)
        {
            return SafeGetPagedData(delegate (PagedActionResult<T_BS_CHECK_TYPE> result)
            {
                Expression<Func<T_BS_CHECK_TYPE, bool>> expression = null;
                string MineType = APT.Infrastructure.Api.AppContext.CurrentSession.MineType;
                if (!string.IsNullOrEmpty(MineType))
                {
                    List<int> listiMineType = new List<int>();//全公司
                    if (MineType.Contains(","))
                    {
                        listiMineType.Add(0);
                        List<string> listMineType = new List<string>();
                        listMineType = MineType.Split(",", StringSplitOptions.RemoveEmptyEntries).ToList();
                        listMineType.ForEach(i => listiMineType.Add(int.Parse(i)));
                    }
                    else
                    {
                        listiMineType.Add(int.Parse(MineType));
                        listiMineType.Add(0);
                    }

                    //自己所在的 和全公司的
                    var listType = GetEntities<T_BS_CHECK_TYPE_MINETYPE>(e => listiMineType.Contains(e.Nav_Enums.VALUE), null, "Nav_Enums");
                    if (listType != null && listType.Any())
                    {
                        List<Guid> listCheckTypeID = listType.Where(e => e.CHECK_TYPE_ID.HasValue).Select(e => e.CHECK_TYPE_ID.Value).Distinct().ToList();
                        expression = e => listCheckTypeID.Contains(e.ID);
                    }
                }


                PagedActionResult<T_BS_CHECK_TYPE> orderPageEntities = GetOrderPageEntities<T_BS_CHECK_TYPE>(expression, pageFilter, null);
                result.Data = orderPageEntities.Data;
                result.TotalCount = orderPageEntities.TotalCount;
            });
        }
    }
}
