using DocumentFormat.OpenXml.Drawing.Spreadsheet; using DocumentFormat.OpenXml.Spreadsheet; using Learun.Application.Base.SystemModule; using Learun.Application.Organization; using Learun.Application.TwoDevelopment.ZZDT_EC; using Learun.Application.TwoDevelopment.ZZDT_EC.ec_lookup; using Learun.Util; using Learun.Util.SqlSugar; using Newtonsoft.Json; using NPOI.HSSF.UserModel; using NPOI.SS.UserModel; using NPOI.SS.Util; using NPOI.XSSF.UserModel; using Org.BouncyCastle.Asn1.Ocsp; using Pipelines.Sockets.Unofficial.Arenas; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Web.Mvc; using System.Windows.Interop; namespace Learun.Application.Web.Areas.ZZDT_EC.Controllers { /// /// 版 本 PIT-ADMS V7.0.3 敏捷开发框架 /// Copyright (c) 2013-2018 Hexagon PPM /// 创 建:超级管理员 /// 日 期:2022-02-24 11:10 /// 描 述:lookup /// public class ec_LookupTableController : MvcControllerBase { ec_REFLOOKUPTABLEBLL bll = new ec_REFLOOKUPTABLEBLL(); #region 获取数据 /// /// 主页面 /// /// [HttpGet] public ActionResult LookupIndex() { return View(); } /// /// 表单页 /// /// [HttpGet] public ActionResult Form() { return View(); } /// /// 表单页 /// /// [HttpGet] public ActionResult Structure() { return View(); } /// /// lookup 复用,选择仪表类型 /// /// [HttpGet] public ActionResult SelectObjectType() { return View(); } /// /// 表单页 /// /// [HttpGet] public ActionResult DataDefinition() { return View(); } /// /// 表单页 /// /// [HttpGet] public ActionResult FormStructure() { return View(); } /// /// 某个类型下的lookup /// 对象类型id /// 项目id /// /// [HttpGet] [AjaxOnly] public ActionResult GetList(string objectTypeId, string ProjectId, string filter = "") { var data = bll.GetList(ProjectId); data = data.FindAll(x => x.ObjectTypeId == objectTypeId); if (!string.IsNullOrEmpty(filter)) { data = data.FindAll(x => x.Name.Contains(filter)); } return Success(data); } /// /// 某个类型下的lookup /// 对象类型id /// 项目id /// /// [HttpGet] [AjaxOnly] public ActionResult GetStructure(string spid, string ProjectId) { var data = bll.GetLookupTableStructure(ProjectId, spid); return Success(data); } /// /// 可以lookup复用出去的仪表类型 /// /// /// /// [HttpGet] [AjaxOnly] public ActionResult AvailableObjTypeTree(string ProjectId, string ObjectTypeID) { string queryJson = "{\"ProjectId\":\"" + ProjectId + "\"}"; var list = new ec_objecttypeBLL().GetList(queryJson).ToList(); var validTypes = new List(); //1.过滤掉自己 var type = list.FirstOrDefault(x => x.ObjectTypeID == ObjectTypeID); list.Remove(type); //2.过滤出属性比自己至少多的类型 var lookupTbName = ProjectSugar.TableName(ProjectId); var lookupAttTbName = ProjectSugar.TableName(ProjectId); var objectTypePTbName = ProjectSugar.TableName(ProjectId); var curTypeLookupPs = SqlSugarHelper.Db.Queryable().AS(lookupAttTbName) .InnerJoin((a, b) => a.lookupTableID == b.SP_ID).AS(lookupTbName) .Where((a, b) => b.ObjectTypeId == ObjectTypeID).ToList(); var curTypeLookupPId = curTypeLookupPs.Select(x => x.PropertyID).Distinct().ToList(); var allProp = SqlSugarHelper.Db.Queryable().AS(objectTypePTbName).ToList(); var matchProps = allProp.Where(x => curTypeLookupPId.Contains(x.PropertyID)).ToList(); var matchTypeIds = matchProps.Select(x => x.ObjectTypeID).Distinct().Where(x => x != ObjectTypeID).ToList(); foreach (var typeid in matchTypeIds) { var count = matchProps.Count(x => x.ObjectTypeID == typeid); if (count < curTypeLookupPId.Count) { } else { validTypes.Add(typeid); } } var validFolders = new List(); foreach (var id in validTypes) { var fullPath = list.FirstOrDefault(x => x.ObjectTypeID == id)?.FullPath; validFolders.AddRange(fullPath.Split(',')); } list = list.Where(x => validTypes.Contains(x.ObjectTypeID) || validFolders.Contains(x.ObjectTypeID)).ToList();//想办法保留目录,否则tree结构没了 List treeList = new List(); foreach (var item in list) { TreeModel node = new TreeModel(); node.id = item.ObjectTypeID; node.text = item.ObjectTypeName; node.value = item.ObjectTypeID; node.showcheck = true; node.checkstate = 0; node.isexpand = false; node.parentId = item.UpObjectTypeID; node.NodeExtData = item; treeList.Add(node); } var res = treeList.ToTree(); return Success(res); } /// /// 某个类型下的lookup /// 对象类型id /// 项目id /// /// [HttpGet] [AjaxOnly] public ActionResult GetAttrData(string PropertyID, string ProjectId, string lookuptableid) { var data = bll.GetLookupTableStructure(ProjectId, lookuptableid); var res = data.FirstOrDefault(x => x.PropertyID == PropertyID); return Success(res); } /// /// /// /// /// /// public ActionResult GetDataRecord(string ProjectId, string lookupTableId) { var res = bll.GetLookupDataRecord(ProjectId, lookupTableId).OrderBy(x => x.recordNumber).ToList(); var structure = bll.GetLookupTableStructure(ProjectId, lookupTableId); var res2 = bll.TransformData(res, structure); return Success(res2); } #endregion #region 修改 /// /// /// /// /// /// [HttpPost] [AjaxOnly] public ActionResult Copy2(string typeId, string ProjectId, string objectTypeIDs) { var typesWillCopy = objectTypeIDs.Split(','); var lookupTb = ProjectSugar.TableName(ProjectId); var lookupDefTb = ProjectSugar.TableName(ProjectId); var lookupDataTb = ProjectSugar.TableName(ProjectId); var lookups = SqlSugarHelper.Db.Queryable().AS(lookupTb).ToList(); var source = lookups.Where(x => x.ObjectTypeId == typeId).ToList(); var sourceDef = SqlSugarHelper.Db.Queryable().AS(lookupDefTb) .Where(x => source.Select(y=>y.SP_ID).Contains(x.lookupTableID)).ToList(); var sourceData = SqlSugarHelper.Db.Queryable().AS(lookupDataTb) .Where(x => sourceDef.Select(y => y.SP_ID).Contains(x.lookupTableAttrId)).ToList(); var newLookups = new List(); var newLookupDefs = new List(); var newLookupData = new List(); foreach (var item in typesWillCopy) { if (lookups.Any(x=>x.ObjectTypeId == item)) { continue;//原本就有,就不复制了 } #region copy foreach (var lookup in source) { var newLookup = new EC_REFLOOKUPTABLE() { SP_ID = Guid.NewGuid().ToString(), DefaultFlag = lookup.DefaultFlag, Name = lookup.Name, ObjectTypeId = item }; newLookups.Add(newLookup); //var attDic = new Dictionary(); foreach (var def in sourceDef.Where(x => x.lookupTableID == lookup.SP_ID)) { var newattrId = Guid.NewGuid().ToString(); var newDef = new ec_REFLOOKUPTABLEAttr() { SP_ID = newattrId, lookupTableID = newLookup.SP_ID, PropertyID = def.PropertyID, PropertyName = def.PropertyName , inOutFlag = def.inOutFlag, orderId = def.orderId, overwriteFlag = def.overwriteFlag }; newLookupDefs.Add(newDef); //attDic.Add(def.SP_ID, newDef.SP_ID); foreach (var data in sourceData.Where(x => x.lookupTableAttrId == def.SP_ID)) { var newData = new ec_REFLOOKUPTABLEData() { SP_ID = Guid.NewGuid().ToString(), lookupValue = data.lookupValue, recordNumber = data.recordNumber, lookupTableAttrId = newattrId, }; newLookupData.Add(newData); } } } #endregion } SqlSugarHelper.Db.Insertable(newLookups).AS(lookupTb).ExecuteCommand(); SqlSugarHelper.Db.Insertable(newLookupDefs).AS(lookupDefTb).ExecuteCommand(); SqlSugarHelper.Db.Insertable(newLookupData).AS(lookupDataTb).ExecuteCommand(); return Success("lookup已复制。"); } /// /// 保存实体数据(新增、修改) /// 主键 /// /// [HttpPost] [ValidateAntiForgeryToken] [AjaxOnly] public ActionResult SaveAttr(string ProjectId, string strEntity) { try { var entity = strEntity.ToObject(); if (string.IsNullOrEmpty(entity.SP_ID)) { bll.AddAttr(ProjectId, entity); } else { bll.ModifyAttr(ProjectId, entity); } return Success("修改成功"); } catch (Exception e) { return Fail(e.Message); } } /// /// 保存 /// /// /// /// /// [HttpPost] [AjaxOnly] public ActionResult SaveData(string ProjectId, string lookupTableId, string strEntity) { List> tableData = JsonConvert.DeserializeObject>>(strEntity); bll.ModifyData(ProjectId, lookupTableId, tableData); return Success(""); } /// /// 保存实体数据(新增、修改) /// 主键 /// /// [HttpPost] [ValidateAntiForgeryToken] [AjaxOnly] public ActionResult SaveForm(string objectTypeId, string ProjectId, string strEntity) { var entity = strEntity.ToObject(); entity.ObjectTypeId = objectTypeId; var exist = bll.GetList(ProjectId).FindAll(x => x.ObjectTypeId == objectTypeId); if (exist.Any(x => x.Name.ToUpper() == entity.Name.ToUpper() && x.SP_ID != entity.SP_ID)) { return Success("不能有重名的!"); } if (string.IsNullOrEmpty(entity.SP_ID)) { bll.AddLookupTable(ProjectId, entity); return Success("插入成功"); } else { bll.ModifyLookupTable(ProjectId, entity); return Success("更新成功"); } } /// /// 删除实体数据 /// 主键 /// /// [HttpPost] [AjaxOnly] public ActionResult DeleteForm(string keyValue, string ProjectId) { bll.DeleteEntity(keyValue, ProjectId); return Success("删除成功!"); } /// /// 删除实体数据 /// 主键 /// /// [HttpPost] [AjaxOnly] public ActionResult DeleteAttr(string keyValue, string ProjectId) { bll.DeleteEntity(keyValue, ProjectId); return Success("删除成功!"); } /// /// 删除实体数据 /// 主键 /// /// [HttpPost] [AjaxOnly] public ActionResult DeleteData(string ProjectId, string lookupTableId, string strEntity) { Dictionary tableData = JsonConvert.DeserializeObject>(strEntity); bll.DeleteData(lookupTableId, tableData, ProjectId); return Success("删除成功!"); } #endregion } }