435 lines
16 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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
{
/// <summary>
/// 版 本 PIT-ADMS V7.0.3 敏捷开发框架
/// Copyright (c) 2013-2018 Hexagon PPM
/// 创 建:超级管理员
/// 日 期2022-02-24 11:10
/// 描 述lookup
/// </summary>
public class ec_LookupTableController : MvcControllerBase
{
ec_REFLOOKUPTABLEBLL bll = new ec_REFLOOKUPTABLEBLL();
#region
/// <summary>
/// 主页面
/// <summary>
/// <returns></returns>
[HttpGet]
public ActionResult LookupIndex()
{
return View();
}
/// <summary>
/// 表单页
/// <summary>
/// <returns></returns>
[HttpGet]
public ActionResult Form()
{
return View();
}
/// <summary>
/// 表单页
/// <summary>
/// <returns></returns>
[HttpGet]
public ActionResult Structure()
{
return View();
}
/// <summary>
/// lookup 复用,选择仪表类型
/// </summary>
/// <returns></returns>
[HttpGet]
public ActionResult SelectObjectType()
{
return View();
}
/// <summary>
/// 表单页
/// <summary>
/// <returns></returns>
[HttpGet]
public ActionResult DataDefinition()
{
return View();
}
/// <summary>
/// 表单页
/// <summary>
/// <returns></returns>
[HttpGet]
public ActionResult FormStructure()
{
return View();
}
/// <summary>
/// 某个类型下的lookup
/// <param name="objectTypeId">对象类型id</param>
/// <param name="ProjectId">项目id</param>
/// <summary>
/// <returns></returns>
[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);
}
/// <summary>
/// 某个类型下的lookup
/// <param name="objectTypeId">对象类型id</param>
/// <param name="ProjectId">项目id</param>
/// <summary>
/// <returns></returns>
[HttpGet]
[AjaxOnly]
public ActionResult GetStructure(string spid, string ProjectId)
{
var data = bll.GetLookupTableStructure(ProjectId, spid);
return Success(data);
}
/// <summary>
/// 可以lookup复用出去的仪表类型
/// </summary>
/// <param name="ProjectId"></param>
/// <param name="ObjectTypeID"></param>
/// <returns></returns>
[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<string>();
//1.过滤掉自己
var type = list.FirstOrDefault(x => x.ObjectTypeID == ObjectTypeID);
list.Remove(type);
//2.过滤出属性比自己至少多的类型
var lookupTbName = ProjectSugar.TableName<EC_REFLOOKUPTABLE>(ProjectId);
var lookupAttTbName = ProjectSugar.TableName<ec_REFLOOKUPTABLEAttr>(ProjectId);
var objectTypePTbName = ProjectSugar.TableName<ec_objecttypepEntity>(ProjectId);
var curTypeLookupPs = SqlSugarHelper.Db.Queryable<ec_REFLOOKUPTABLEAttr>().AS(lookupAttTbName)
.InnerJoin<EC_REFLOOKUPTABLE>((a, b) => a.lookupTableID == b.SP_ID).AS<EC_REFLOOKUPTABLE>(lookupTbName)
.Where((a, b) => b.ObjectTypeId == ObjectTypeID).ToList();
var curTypeLookupPId = curTypeLookupPs.Select(x => x.PropertyID).Distinct().ToList();
var allProp = SqlSugarHelper.Db.Queryable<ec_objecttypepEntity>().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<string>();
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<TreeModel> treeList = new List<TreeModel>();
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);
}
/// <summary>
/// 某个类型下的lookup
/// <param name="objectTypeId">对象类型id</param>
/// <param name="ProjectId">项目id</param>
/// <summary>
/// <returns></returns>
[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);
}
/// <summary>
///
/// </summary>
/// <param name="ProjectId"></param>
/// <param name="lookupTableId"></param>
/// <returns></returns>
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
/// <summary>
///
/// </summary>
/// <param name="typeId"></param>
/// <param name="ProjectId"></param>
/// <returns></returns>
[HttpPost]
[AjaxOnly]
public ActionResult Copy2(string typeId, string ProjectId, string objectTypeIDs)
{
var typesWillCopy = objectTypeIDs.Split(',');
var lookupTb = ProjectSugar.TableName<EC_REFLOOKUPTABLE>(ProjectId);
var lookupDefTb = ProjectSugar.TableName<ec_REFLOOKUPTABLEAttr>(ProjectId);
var lookupDataTb = ProjectSugar.TableName<ec_REFLOOKUPTABLEData>(ProjectId);
var lookups = SqlSugarHelper.Db.Queryable<EC_REFLOOKUPTABLE>().AS(lookupTb).ToList();
var source = lookups.Where(x => x.ObjectTypeId == typeId).ToList();
var sourceDef = SqlSugarHelper.Db.Queryable<ec_REFLOOKUPTABLEAttr>().AS(lookupDefTb)
.Where(x => source.Select(y=>y.SP_ID).Contains(x.lookupTableID)).ToList();
var sourceData = SqlSugarHelper.Db.Queryable<ec_REFLOOKUPTABLEData>().AS(lookupDataTb)
.Where(x => sourceDef.Select(y => y.SP_ID).Contains(x.lookupTableAttrId)).ToList();
var newLookups = new List<EC_REFLOOKUPTABLE>();
var newLookupDefs = new List<ec_REFLOOKUPTABLEAttr>();
var newLookupData = new List<ec_REFLOOKUPTABLEData>();
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<string, string>();
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已复制。");
}
/// <summary>
/// 保存实体数据(新增、修改)
/// <param name="keyValue">主键</param>
/// <summary>
/// <returns></returns>
[HttpPost]
[ValidateAntiForgeryToken]
[AjaxOnly]
public ActionResult SaveAttr(string ProjectId, string strEntity)
{
try
{
var entity = strEntity.ToObject<ec_REFLOOKUPTABLEAttr>();
if (string.IsNullOrEmpty(entity.SP_ID))
{
bll.AddAttr(ProjectId, entity);
}
else
{
bll.ModifyAttr(ProjectId, entity);
}
return Success("修改成功");
}
catch (Exception e)
{
return Fail(e.Message);
}
}
/// <summary>
/// 保存
/// </summary>
/// <param name="ProjectId"></param>
/// <param name="lookupTableId"></param>
/// <param name="strEntity"></param>
/// <returns></returns>
[HttpPost]
[AjaxOnly]
public ActionResult SaveData(string ProjectId, string lookupTableId, string strEntity)
{
List<Dictionary<string, string>> tableData = JsonConvert.DeserializeObject<List<Dictionary<string, string>>>(strEntity);
bll.ModifyData(ProjectId, lookupTableId, tableData);
return Success("");
}
/// <summary>
/// 保存实体数据(新增、修改)
/// <param name="keyValue">主键</param>
/// <summary>
/// <returns></returns>
[HttpPost]
[ValidateAntiForgeryToken]
[AjaxOnly]
public ActionResult SaveForm(string objectTypeId, string ProjectId, string strEntity)
{
var entity = strEntity.ToObject<EC_REFLOOKUPTABLE>();
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("更新成功");
}
}
/// <summary>
/// 删除实体数据
/// <param name="keyValue">主键</param>
/// <summary>
/// <returns></returns>
[HttpPost]
[AjaxOnly]
public ActionResult DeleteForm(string keyValue, string ProjectId)
{
bll.DeleteEntity(keyValue, ProjectId);
return Success("删除成功!");
}
/// <summary>
/// 删除实体数据
/// <param name="keyValue">主键</param>
/// <summary>
/// <returns></returns>
[HttpPost]
[AjaxOnly]
public ActionResult DeleteAttr(string keyValue, string ProjectId)
{
bll.DeleteEntity(keyValue, ProjectId);
return Success("删除成功!");
}
/// <summary>
/// 删除实体数据
/// <param name="keyValue">主键</param>
/// <summary>
/// <returns></returns>
[HttpPost]
[AjaxOnly]
public ActionResult DeleteData(string ProjectId, string lookupTableId, string strEntity)
{
Dictionary<string, string> tableData = JsonConvert.DeserializeObject<Dictionary<string, string>>(strEntity);
bll.DeleteData(lookupTableId, tableData, ProjectId);
return Success("删除成功!");
}
#endregion
}
}