using Learun.Util;
using System.Data;
using Learun.Application.TwoDevelopment.ZZDT_EC;
using System.Web.Mvc;
using System.Collections.Generic;
using System.Linq;
using Pipelines.Sockets.Unofficial.Arenas;

namespace Learun.Application.Web.Areas.ZZDT_EC.Controllers
{
    /// <summary>
    /// 版 本 PIT-ADMS V7.0.3 敏捷开发框架
    /// Copyright (c) 2013-2018 Hexagon PPM
    /// 创 建：超级管理员
    /// 日 期：2022-03-03 17:00
    /// 描 述：元件库文件表
    /// </summary>
    public class ec_library_fileController : MvcControllerBase
    {
        private ec_library_fileIBLL ec_library_fileIBLL = new ec_library_fileBLL();

        #region 视图功能

        /// <summary>
        /// 主页面
        /// <summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult Index()
        {
            return View();
        }
        /// <summary>
        /// 表单页
        /// <summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult Form()
        {
            return View();
        }

        /// <summary>
        /// 选择页面
        /// <summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult SelectIndex()
        {
            return View();
        }
        #endregion

        #region 获取数据

        /// <summary>
        /// 获取页面显示列表数据
        /// <summary>
        /// <param name="queryJson">查询参数</param>
        /// <returns></returns>
        [HttpGet]
        [AjaxOnly]
        public ActionResult GetPageList(string pagination, string queryJson)
        {
            Pagination paginationobj = pagination.ToObject<Pagination>();
            var data = ec_library_fileIBLL.GetList(queryJson, paginationobj);
            if (paginationobj == null)
            { 
                return Success(data);
            }
            else
            {
            var jsonData = new
            {
                rows = data,
                total = paginationobj?.total,
                page = paginationobj?.page,
                records = paginationobj?.records
            };
            return Success(jsonData);
            }

        }
        /// <summary>
        /// 获取表单数据
        /// <summary>
        /// <returns></returns>
        [HttpGet]
        [AjaxOnly]
        public ActionResult GetFormData(string keyValue, string ProjectId)
        {
            var ec_library_fileData = ec_library_fileIBLL.GetEntity(keyValue, ProjectId);
            var jsonData = new
            {
                ec_library_file = ec_library_fileData,
            };
            return Success(jsonData);
        }

        /// <summary>
        /// 获得某个对象下关联过的所有元件库
        /// <summary>
        /// <returns></returns>
        [HttpGet]
        [AjaxOnly]
        public ActionResult GetTreeDataByObjectType(string ProjectId,string ObjectTypeID)
        {
            var data = new ec_objecttypelibBLL().GetObjectTypeLib(ObjectTypeID, ProjectId);
            List<TreeModel> list = new List<TreeModel>();

            var catalog = new ec_library_catalogueBLL().GetList("{\"ProjectId\":\"" + ProjectId + "\"}");
            foreach (var catalogitem in catalog)
            {
                TreeModel node = new TreeModel();

                node.id = catalogitem.LibraryCatalogueID;
                node.text = catalogitem.LibraryCatalogueName;
                node.value = catalogitem.LibraryCatalogueID;
                node.showcheck = false;
                node.checkstate = 0;
                node.isexpand = false;
                node.parentId = "";
                node.NodeExtData = catalogitem;
                list.Add(node);
            }
            foreach (var item in data)
            {
                var LibFileObj = ec_library_fileIBLL.GetEntity(item.LibraryFileID, ProjectId);
                if (LibFileObj != null)
                {
                    TreeModel node = new TreeModel();

                    node.id = LibFileObj.LibraryFileID;
                    node.text = LibFileObj.LibraryFileName;
                    node.value = LibFileObj.LibraryFileID;
                    node.showcheck = false;
                    node.checkstate = 0;
                    node.isexpand = false;
                    node.parentId = LibFileObj.LibraryCatalogueID;
                    node.NodeExtData = LibFileObj;
                    list.Add(node);
                }


            }
            var res = list.ToTree();
            return Success(res);
        }

        #endregion

        #region 提交数据

        /// <summary>
        /// 删除实体数据
        /// <param name="keyValue">主键</param>
        /// <summary>
        /// <returns></returns>
        [HttpPost]
        [AjaxOnly]
        public ActionResult DeleteForm(string keyValue, string ProjectId)
        {
            ec_library_fileIBLL.DeleteEntity(keyValue, ProjectId);
            return Success("删除成功！");
        }
        /// <summary>
        /// 保存实体数据（新增、修改）
        /// <param name="keyValue">主键</param>
        /// <summary>
        /// <returns></returns>
        [HttpPost]
        [ValidateAntiForgeryToken]
        [AjaxOnly]
        public ActionResult SaveForm(string keyValue, string strEntity, string ProjectId)
        {
            ec_library_fileEntity entity = strEntity.ToObject<ec_library_fileEntity>();
            //同一目录下不允许有名称相同的元件库文件
            var data = ec_library_fileIBLL.GetList("{LibraryCatalogueID:\"" + entity.LibraryCatalogueID + "\",ProjectId:\"" + ProjectId + "\",LibraryFileNameFull:\"" + entity.LibraryFileName + "\"}").ToList();
            if (data != null && data.Count > 0)
            {
                if (!string.IsNullOrEmpty(keyValue))
                {
                    if (data.Find(x => x.LibraryFileID != keyValue) != null)
                    {
                        return Fail("同一目录下不允许有名称相同的元件库文件！");
                    }
                }
                else
                {
                    return Fail("同一目录下不允许有名称相同的元件库文件！");
                }
            }
            ec_library_fileIBLL.SaveEntity(keyValue, entity, ProjectId);
            return Success("保存成功！");
        }
        /// <summary>
        /// 批量导入数据
        /// <param name="fileId">文件主键</param>
        /// <summary>
        /// <returns></returns>
        [HttpPost]
        [AjaxOnly]
        public ActionResult MuliteImport(string fileId, string ProjectId)
        {
            ec_library_fileIBLL.MuliteImport(fileId, ProjectId);
            return Success("批量导入数据成功！");
        }
        #endregion

    }
}
