Files
number_zj/20220330_Vote/Ewide.RoadFlow/Serivce/Dictionary/DictionaryService.cs
2022-03-30 17:54:33 +08:00

691 lines
27 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 Furion.DependencyInjection;
using Furion.DynamicApiController;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using RoadFlow.Utility;
using Furion;
using RoadFlow.Data;
using Microsoft.AspNetCore.Authorization;
using Ewide.RoadFlowLite.Utility;
using Furion.FriendlyException;
using Ewide.Core;
namespace Ewide.RoadFlowLite.Serivce.Dictionary
{
/// <summary>
/// 工作流字典服务
/// </summary>
[Route("/api/roadflow/Dictionary/")]
[ApiDescriptionSettings("RoadFlow")]
public class DictionaryService : IDictionaryService, IDynamicApiController, ITransient
{
private readonly IDictionary _repo;
private readonly ILog _log;
private readonly IUserManager _userManager;
public DictionaryService(IDictionary repo ,ILog log, IUserManager userManager)
{
_repo = repo;
_log = log;
_userManager = userManager;
}
/// <summary>
/// 获取数据字典树
/// </summary>
/// <param name="args"></param>
/// <returns></returns>
[HttpPost("GetTreeJson")]
[AllowAnonymous]
public async Task<dynamic> GetTreeJson([FromBody] JObject args)
{
string rootId = (string)args["rootid"];
bool showRoot = ((string)args["showroot"]).IsNullOrWhiteSpace() || "1".Equals((string)args["showroot"]);//是否显示根节点
bool isEle = "1".Equals((string)args["isele"]);//是否是ELEELE只返回根一级返回两级
rootId = rootId.IsGuid(out Guid rid) ? rootId : _repo.GetIdByCode(rootId);
var dict = string.IsNullOrWhiteSpace(rootId) ? _repo.GetRoot() : _repo.GetOneById(rootId);
if (dict == null)
{
return "";
}
string language = RoadFlow.Utility.Tools.GetCurrentLanguage(App.HttpContext);
JArray jArray = new JArray();
JObject rootJson = new JObject
{
{ "id", dict.Id },
{ "parentId", dict.ParentId },
{ "title", _repo.GetLanguageTitle(dict, language) },
{ "open", true },
{ "current", false },
{ "ico", "icon-wallet" },
{ "code", dict.Code },
{ "value", dict.Value },
{ "note", dict.Note },
{ "other", dict.Other },
{ "status", dict.Status },
{ "slots", new JObject() { { "icon", "custom" } } }
};
JArray childsArray = new JArray();
var dictChilds = _repo.GetChilds(dict.Id);
if (!isEle)
{
foreach (var dictChild in dictChilds)
{
JObject dictJson = new JObject
{
{ "id", dictChild.Id },
{ "parentId", dictChild.ParentId },
{ "title", _repo.GetLanguageTitle(dictChild, language) },
{ "current", false },
{ "ico", "" },
{ "code", dictChild.Code },
{ "value", dictChild.Value },
{ "note", dictChild.Note },
{ "other", dictChild.Other },
{ "status", dictChild.Status },
{ "slots", new JObject() { { "icon", "custom" } } }
};
if (_repo.HasChilds(dictChild.Id))
{
dictJson.Add("open", false);
dictJson.Add("childs", new JArray());
dictJson.Add("isLeaf", false);
}
else
{
dictJson.Add("isLeaf", true);
}
childsArray.Add(dictJson);
}
}
rootJson.Add("childs", childsArray);
rootJson.Add("isLeaf", dictChilds.Count == 0);
jArray.Add(rootJson);
return showRoot ? jArray : childsArray;
}
/// <summary>
///
/// </summary>
/// <returns></returns>
[HttpGet("GetTreeChildsJson")]
public dynamic GetTreeChildsJson(string parentid)
{
string parentGuid = parentid;//(string)args["parentid"];
var dictChilds = _repo.GetChilds(parentGuid);
JArray jArray = new JArray();
foreach (var dictChild in dictChilds)
{
JObject dictJson = new JObject() {
{ "id", dictChild.Id },
{ "parentId", dictChild.ParentId },
{ "title", dictChild.Title },
{ "current", false },
{ "ico", "" },
{ "code", dictChild.Code },
{ "value", dictChild.Value },
{ "note", dictChild.Note },
{ "other", dictChild.Other },
{ "status", dictChild.Status },
{ "slots", new JObject() { { "icon", "custom" } } }
};
if (_repo.HasChilds(dictChild.Id))
{
dictJson.Add("open", false);
dictJson.Add("childs", new JArray());
dictJson.Add("isLeaf", false);
}
else
{
dictJson.Add("isLeaf", true);
}
jArray.Add(dictJson);
}
return jArray;
}
/// <summary>
/// 根据值得到标题
/// </summary>
/// <returns></returns>
[HttpPost("GetText")]
//[ApiValidate]
public dynamic GetText([FromBody] JObject args)
{
string value = (string)args["value"];
JArray jArray = new JArray();
foreach (string val in value.Split(','))
{
if (!val.IsGuid(out Guid vId))
{
continue;
}
jArray.Add(new JObject() { { "value", val }, { "title", _repo.GetTitle(val, "") } });
}
return jArray;
}
[HttpGet("hello")]
[AllowAnonymous]
public dynamic Hello()
{
string hello = "hello".DESEncrypt();
return hello+":"+hello.DESDecrypt();
}
/// <summary>
/// antd版获取字典树json
/// </summary>
/// <returns></returns>
[HttpGet("GetTreeJsonEle")]
public dynamic GetTreeJsonEle(string rootId, string showroot)
{
bool showRoot = showroot.IsNullOrEmpty() || showroot == "1";//是否显示根节点
string rootGuid = rootId.IsGuid() ? rootId : _repo.GetIdByCode(rootId.Trim());
var dict = rootGuid.IsNullOrEmpty() ? _repo.GetRoot() : _repo.GetOneById(rootGuid);
if (dict == null)
{
return new JArray();
// return RoadFlowCommon.Tools.GetReturnJsonString(jArray: new JArray());
}
string language = RoadFlow.Utility.Tools.GetCurrentLanguage(Furion.App.HttpContext);
JArray jArray = new JArray();
JObject rootJson = new JObject
{
{ "id", dict.Id },
{ "parentId", dict.ParentId },
{ "title", _repo.GetLanguageTitle(dict, language) },
{ "code", dict.Code },
{ "value", dict.Value },
{ "note", dict.Note },
{ "other", dict.Other },
{ "status", dict.Status },
};
JArray childsArray = new JArray();
var dictChilds = _repo.GetChilds(dict.Id);
if (!showRoot)
{
foreach (var dictChild in dictChilds)
{
JObject dictJson = new JObject
{
{ "id", dictChild.Id },
{ "parentId", dictChild.ParentId },
{ "title", _repo.GetLanguageTitle(dictChild, language) },
{ "code", dictChild.Code },
{ "value", dictChild.Value },
{ "note", dictChild.Note },
{ "other", dictChild.Other },
{ "status", dictChild.Status },
};
dictJson.Add("leaf", !_repo.HasChilds(dictChild.Id));
childsArray.Add(dictJson);
}
}
//rootJson.Add("childs", childsArray);
rootJson.Add("leaf", dictChilds.Count == 0);
jArray.Add(rootJson);
return showRoot ? jArray : childsArray;
//RoadFlowCommon.Tools.GetReturnJsonString(jArray: showRoot ? jArray : childsArray);
}
/// <summary>
/// antd版获取字典下级json
/// </summary>
/// <returns></returns>
[HttpGet("GetTreeChildsJsonEle")]
public dynamic GetTreeChildsJsonEle(string parentId)
{
if (!parentId.IsGuid(out Guid parentGuid))
{
return new JArray();
// return RoadFlowCommon.Tools.GetReturnJsonString(jArray: new JArray());
}
string language = RoadFlow.Utility.Tools.GetCurrentLanguage(Furion.App.HttpContext);
var dictChilds = _repo.GetChilds(parentId);
JArray jArray = new JArray();
foreach (var dictChild in dictChilds)
{
JObject dictJson = new JObject() {
{ "id", dictChild.Id },
{ "parentId", dictChild.ParentId },
{ "title", _repo.GetLanguageTitle(dictChild, language) },
{ "code", dictChild.Code },
{ "value", dictChild.Value },
{ "note", dictChild.Note },
{ "other", dictChild.Other },
{ "status", dictChild.Status },
};
dictJson.Add("leaf", !_repo.HasChilds(dictChild.Id));
jArray.Add(dictJson);
}
return jArray;
//RoadFlowCommon.Tools.GetReturnJsonString(jArray: jArray);
}
/// <summary>
/// antd版得到select选项
/// </summary>
/// <returns></returns>
[HttpPost("GetSelectOptions")]
public dynamic GetSelectOptions([FromBody] JObject args)
{
string param = args.GetJsonValue("params");
if (param.IsNullOrWhiteSpace())
{
return new JObject();
//return RoadFlowCommon.Tools.GetReturnJsonString(jObject: new JObject());
}
JArray paramArray = param.ToJArray();
if (paramArray.IsEmptyJArray())
{
return new JObject();
//return RoadFlowCommon.Tools.GetReturnJsonString(jObject: new JObject());
}
JObject returnObject = new JObject();
int i = 0;
foreach (JObject jObject in paramArray)
{
string id = jObject.Value<string>("id");//字典id或code
string key = jObject.Value<string>("key");//返回json key
string type = jObject.Value<string>("type");//类型 select(下拉选择), cascader(级联选择器)
string valueField = jObject.Value<string>("valuefield");//值字段
bool childs = jObject.Value<string>("childs").ToInt(0) == 1;//是否加载所有下级
bool isRoot = jObject.Value<string>("root").ToInt(0) == 1;//是否包含根节点
key = key.IsNullOrWhiteSpace() ? "options" + i++.ToString() : key;
if (returnObject.ContainsKey(key))
{
continue;
}
if (id.IsNullOrWhiteSpace())
{
continue;
}
if (type.IsNullOrWhiteSpace())
{
type = "select";
}
if (valueField.IsNullOrWhiteSpace())
{
valueField = "id";
}
if (!id.IsGuid(out Guid guid))
{
guid = _repo.GetIdByCode(id).ToGuid();
}
string lang = RoadFlow.Utility.Tools.GetCurrentLanguage(Furion.App.HttpContext);
if (type.EqualsIgnoreCase("select"))
{
JArray jArray = _repo.GetArrayOptionsByID(guid.ToString(), _repo.GetValueField(valueField), string.Empty, childs);
returnObject.Add(key, jArray);
}
else if (type.EqualsIgnoreCase("cascader"))
{
JArray jArray = _repo.GetAllChildsArray(guid.ToString(), _repo.GetValueField(valueField), isRoot, lang);
returnObject.Add(key, jArray);
}
}
return returnObject;
;//RoadFlowCommon.Tools.GetReturnJsonString(jObject: returnObject);
}
/// <summary>
/// 得到select下拉选项
/// </summary>
/// <returns></returns>
[HttpPost("GetOptions")]
public dynamic GetOptions([FromBody] JObject args)
{
string dictId = args.GetJsonValue("dictid");
string childs = args.GetJsonValue("childs");
string valueField = args.GetJsonValue("valuefield");
string value = args.GetJsonValue("value");
string parentValue = args.GetJsonValue("parentvalue");//上一个选择的值,联动的时候获取
string selectParent = args.GetJsonValue("selectparent");//是否可以选择上级
JArray optionsArray = new JArray();
foreach (string pValue in (parentValue.IsNullOrWhiteSpace() ? dictId : parentValue).Split(','))
{
if (!pValue.IsGuid(out Guid dictGuid))
{
continue;
}
JArray jArray = _repo.GetArrayOptionsByID(dictGuid.ToString(), _repo.GetValueField(valueField), value, "1".Equals(childs), "1".Equals(selectParent));
foreach (JObject jObject in jArray)
{
optionsArray.Add(jObject);
}
}
return optionsArray;
//RoadFlowCommon.Tools.GetReturnJsonString(jArray: optionsArray);
}
/// <summary>
/// 得到combox下拉选项
/// </summary>
/// <returns></returns>
[HttpPost("GetComboxOptions")]
public dynamic GetComboxOptions([FromBody] JObject args)
{
string dictId = args.GetJsonValue("dictid");
string childs = args.GetJsonValue("childs");
string valueField = args.GetJsonValue("valuefield");
string value = args.GetJsonValue("value");
string parentValue = args.GetJsonValue("parentvalue");//上一个选择的值,联动的时候获取
string selectParent = args.GetJsonValue("selectparent");//是否可以选择上级
JArray optionsArray = new JArray();
foreach (string pValue in (parentValue.IsNullOrWhiteSpace() ? dictId : parentValue).Split(','))
{
if (!pValue.IsGuid(out Guid dictGuid))
{
continue;
}
JArray jArray = _repo.GetArrayComboxsByID(dictGuid.ToString(), _repo.GetValueField(valueField), value, "1".Equals(childs), "1".Equals(selectParent));
foreach (JObject jObject in jArray)
{
optionsArray.Add(jObject);
}
}
return optionsArray;// RoadFlowCommon.Tools.GetReturnJsonString(jArray: optionsArray);
}
/// <summary>
/// 得到单选按钮组选项
/// </summary>
/// <returns></returns>
[HttpPost("GetRadioOrCheckboxItems")]
public dynamic GetRadioOrCheckboxItems([FromBody] JObject args)
{
string dictId = args.GetJsonValue("dictid");
string valueField = args.GetJsonValue("valuefield");
JArray jArray = _repo.GetRadioOrCheckboxItems(dictId, _repo.GetValueField(valueField));
return jArray;
//RoadFlowCommon.Tools.GetReturnJsonString(jArray: jArray);
}
/*
/// <summary>
/// 根据值得到标题
/// </summary>
/// <returns></returns>
[HttpPost("GetText")]
public dynamic GetText([FromBody] JObject args)
{
string value = args.GetJsonValue("value");
JArray jArray = new JArray();
string lang = RoadFlow.Utility.Tools.GetCurrentLanguage(Furion.App.HttpContext);
foreach (string val in value.Split(','))
{
if (!val.IsGuid(out Guid vId))
{
continue;
}
jArray.Add(new JObject() { { "value", val }, { "title", _repo.GetTitle(vId, lang) } });
}
return jArray;
//RoadFlowCommon.Tools.GetReturnJsonString(jArray: jArray);
}
*/
/// <summary>
/// 验证Code是否重复
/// </summary>
/// <returns></returns>
[HttpPost("ValidCode")]
public dynamic ValidCode([FromBody] JObject args)
{
string id = args.GetJsonValue("id");
int type = args.GetJsonValue("type").ToInt(1);//0保存 1保存为下级
string code = args.GetJsonValue("value");
if (code.IsNullOrWhiteSpace())
{
throw Oops.Oh("编码不能为空");
// return RoadFlowCommon.Tools.GetReturnJsonString(false, localizer["CodeEmpty"].Value);
}
var dictModel = _repo.GetOneById(code.Trim());
if (dictModel == null)
{
return new JObject() { { "success", true }, { "msg", string.Empty } };
//return RoadFlowCommon.Tools.GetReturnJsonString(jObject: new JObject() { { "success", true }, { "msg", string.Empty } });
}
if (dictModel.Id == id && type == 0)
{
return new JObject() { { "success", true }, { "msg", string.Empty } };
//RoadFlowCommon.Tools.GetReturnJsonString(jObject: new JObject() { { "success", true }, { "msg", string.Empty } });
}
return new JObject() { { "success", false }, { "msg", "编码重复" } };
//RoadFlowCommon.Tools.GetReturnJsonString(jObject: new JObject() { { "success", false }, { "msg", localizer["CodeDuplicate"].Value } });
}
[HttpGet("Get")]
public dynamic Get(string id)
{
if (!id.IsGuid(out Guid guid))
{
return new JObject();
//RoadFlowCommon.Tools.GetReturnJsonString(jObject: new JObject());
}
var dictModel = _repo.GetOneById(id);
if (null == dictModel)
{
return new JObject();
//RoadFlowCommon.Tools.GetReturnJsonString(jObject: new JObject());
}
return JObject.FromObject(dictModel);
//RoadFlowCommon.Tools.GetReturnJsonString(jObject: JObject.FromObject(dictModel));
}
/// <summary>
/// 得到排序的项
/// </summary>
/// <returns></returns>
[HttpGet("GetSortItems")]
public dynamic GetSortItems(string id)
{
if (!id.IsGuid(out Guid dictId))
{
throw Oops.Oh("ID错误");
//return RoadFlowCommon.Tools.GetReturnJsonString(false, localizer["IdError"].Value);
}
var dictChilds = _repo.GetChilds(id);
string lang = RoadFlow.Utility.Tools.GetCurrentLanguage(Furion.App.HttpContext);
JArray jArray = new JArray();
foreach (var dictChild in dictChilds)
{
JObject jObject = new JObject()
{
{ "id", dictChild.Id },
{ "title", _repo.GetLanguageTitle(dictChild, lang) }
};
jArray.Add(jObject);
}
return jArray;
//RoadFlowCommon.Tools.GetReturnJsonString(jArray: jArray);
}
/// <summary>
/// 保存
/// </summary>
/// <returns></returns>
[HttpPost("Save")]
public string Save(RoadFlow.Model.rf_dictionary dictModel)
{
int type = Furion.App.HttpContext.Request.Querys("type").ToInt();
string id = Furion.App.HttpContext.Request.Querys("id");
//保存为下级
if (type == 1)
{
dictModel.ParentId = dictModel.Id;
dictModel.Id = GuidExtensions.NewGuid().ToString();
dictModel.Sort = _repo.GetMaxSort(dictModel.ParentId);
}
//检查唯一代码
if (!_repo.CheckCode(dictModel.Id, dictModel.Code))
{
throw Oops.Oh("编码重复");
// return RoadFlowCommon.Tools.GetReturnJsonString(false, localizer["CodeDuplicate"].Value);
}
if (id.IsGuid(out Guid dictId) && type == 0)
{
var oldModel = _repo.GetOneById(id);
_repo.Update(dictModel);
_log.Add("修改了数据字典-" + dictModel.Title, type: LogType., oldContents: oldModel.ToString(), newContents: dictModel.ToString());
}
else
{
_repo.Add(dictModel);
_log.Add("添加了数据字典-" + dictModel.Title,_userManager.UserId ,dictModel.ToString(),LogType.);
}
return "保存成功";
//RoadFlowCommon.Tools.GetReturnJsonString(true, localizer["SaveSuccess"].Value);
}
/// <summary>
/// 保存排序
/// </summary>
/// <returns></returns>
[HttpPost("SaveSort")]
public string SaveSort([FromBody] JObject args)
{
string[] ids = args.GetJsonValue("ids").Split(',');
List<RoadFlow.Model.rf_dictionary> dictionaries = new List<RoadFlow.Model.rf_dictionary>();
for (int i = 0; i < ids.Length; i++)
{
if (!ids[i].IsGuid(out Guid dictId))
{
continue;
}
var dictModel = _repo.GetOneById(ids[i]);
if (null == dictModel)
{
continue;
}
dictModel.Sort = (i + 1) * 5;
dictionaries.Add(dictModel);
}
_repo.Update(dictionaries);
return "保存成功";
//RoadFlowCommon.Tools.GetReturnJsonString(true, localizer["SaveSuccess"].Value);
}
/// <summary>
/// 删除
/// </summary>
/// <returns></returns>
[HttpPost("Delete")]
public string Delete([FromBody] JObject args)
{
string id = args.GetJsonValue("id");
if (!id.IsGuid(out Guid dictId))
{
throw Oops.Oh("ID错误");
// return RoadFlowCommon.Tools.GetReturnJsonString(false, localizer["IdError"].Value);
}
if (id == _repo.GetRootId())
{
throw Oops.Oh("不能删除根对象");
// return RoadFlowCommon.Tools.GetReturnJsonString(false, localizer["CannotDeleteRoot"].Value);
}
var dictModel = _repo.GetOneById(id);
if (null == dictModel)
{
throw Oops.Oh("未找到字典对象");
//return RoadFlowCommon.Tools.GetReturnJsonString(false, localizer["NotFoundDict"].Value);
}
var list =_repo.GetAllChilds(id,true);
var count = _repo.DeleteBy(x=>x.Id==id);
count += _repo.DeleteBy(x => x.ParentId == id);
_log.Add("删除了数据字典-共" + count + "项",_userManager.UserId ,list.Serialize(), LogType.);
return "成功删除" + count + "条";
//RoadFlowCommon.Tools.GetReturnJsonString(true, localizer["DeleteSuccess"].Value + dictList.Count + localizer["DeleteItems"].Value);
}
/// <summary>
/// 导入数据字典
/// </summary>
/// <returns></returns>
[HttpPost("Import")]
public string Import([FromBody] JObject args)
{
var files = args.GetJsonValue("files");
if (files.IsNullOrWhiteSpace())
{
throw Oops.Oh("未选择要导入的文件");
// return RoadFlowCommon.Tools.GetReturnJsonString(false, localizer["NoSelectImportFile"].Value);
}
System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
foreach (string file in files.Split(','))
{
string filePath = RoadFlow.Utility.Config.FilePath + file.DESDecrypt();
if (!System.IO.File.Exists(filePath))
{
continue;
}
var stream = System.IO.File.OpenRead(filePath);
int count = (int)stream.Length;
byte[] b = new byte[count];
stream.Read(b, 0, count);
string json = System.Text.Encoding.UTF8.GetString(b);
string msg = _repo.Import(json, null);//localizer);
stream.Close();
stream.DisposeAsync();
try
{
System.IO.File.Delete(filePath);
}
catch (System.IO.IOException err)
{
_log.Add(err);
}
if (!"1".Equals(msg))
{
stringBuilder.Append(msg + "");
}
}
string msg1 = stringBuilder.ToString().TrimEnd('');
if (msg1.IsNullOrWhiteSpace())
return "导入成功";
else
throw Oops.Oh(msg1);
//RoadFlowCommon.Tools.GetReturnJsonString(msg1.IsNullOrWhiteSpace(), msg1.IsNullOrWhiteSpace() ? localizer["ImportSuccess"].Value : msg1);
}
}
}