From 1d9ec93e1210b81977033fa2f0ef7c4b57d39338 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E4=B8=8D=E5=81=9A=E7=A0=81=E5=86=9C?= <599854767@qq.com>
Date: Fri, 6 May 2022 22:12:51 +0800
Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E5=A4=9A=E8=AF=AD=E8=A8=80?=
=?UTF-8?q?=E7=AE=A1=E7=90=86?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../System/CommonLangController.cs | 239 ++++++++++++++++++
ZR.Model/System/CommonLang.cs | 59 +++++
ZR.Model/System/Dto/CommonLangDto.cs | 35 +++
ZR.Model/System/Vo/LangVo.cs | 13 +
ZR.Repository/System/CommonLangRepository.cs | 20 ++
ZR.Service/System/CommonLangService.cs | 92 +++++++
.../ISystemService/ICommonLangService.cs | 21 ++
7 files changed, 479 insertions(+)
create mode 100644 ZR.Admin.WebApi/Controllers/System/CommonLangController.cs
create mode 100644 ZR.Model/System/CommonLang.cs
create mode 100644 ZR.Model/System/Dto/CommonLangDto.cs
create mode 100644 ZR.Model/System/Vo/LangVo.cs
create mode 100644 ZR.Repository/System/CommonLangRepository.cs
create mode 100644 ZR.Service/System/CommonLangService.cs
create mode 100644 ZR.Service/System/ISystemService/ICommonLangService.cs
diff --git a/ZR.Admin.WebApi/Controllers/System/CommonLangController.cs b/ZR.Admin.WebApi/Controllers/System/CommonLangController.cs
new file mode 100644
index 0000000..77d82b7
--- /dev/null
+++ b/ZR.Admin.WebApi/Controllers/System/CommonLangController.cs
@@ -0,0 +1,239 @@
+using Infrastructure;
+using Infrastructure.Attribute;
+using Infrastructure.Enums;
+using Infrastructure.Model;
+using Mapster;
+using Microsoft.AspNetCore.Authorization;
+using Microsoft.AspNetCore.Mvc;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using ZR.Admin.WebApi.Extensions;
+using ZR.Admin.WebApi.Filters;
+using ZR.Common;
+using ZR.Model;
+using ZR.Model.Dto;
+using ZR.Model.Models;
+using ZR.Service.System.ISystemService;
+
+namespace ZR.Admin.WebApi.Controllers
+{
+ ///
+ /// 多语言配置Controller
+ ///
+ /// @author zr
+ /// @date 2022-05-06
+ ///
+ [Verify]
+ [Route("system/CommonLang")]
+ public class CommonLangController : BaseController
+ {
+ ///
+ /// 多语言配置接口
+ ///
+ private readonly ICommonLangService _CommonLangService;
+
+ public CommonLangController(ICommonLangService CommonLangService)
+ {
+ _CommonLangService = CommonLangService;
+ }
+
+ ///
+ /// 查询多语言配置列表
+ ///
+ ///
+ ///
+ [HttpGet("list")]
+ [ActionPermissionFilter(Permission = "system:lang:list")]
+ public IActionResult QueryCommonLang([FromQuery] CommonLangQueryDto parm)
+ {
+ if (parm.ShowMode == 2)
+ {
+ PagedInfo pagedInfo = new();
+ pagedInfo.Result = _CommonLangService.GetListToPivot(parm);
+
+ return SUCCESS(pagedInfo);
+ }
+
+ return SUCCESS(_CommonLangService.GetList(parm));
+ }
+
+ ///
+ /// 查询多语言配置列表
+ ///
+ ///
+ [HttpGet("list/{lang}")]
+ [AllowAnonymous]
+ public IActionResult QueryCommonLangs()
+ {
+ var msgList = _CommonLangService.GetLangList(new CommonLangQueryDto() { LangCode = "zh-cn" });
+ var msgListEn = _CommonLangService.GetLangList(new CommonLangQueryDto() { LangCode = "en" });
+ var msgListTw = _CommonLangService.GetLangList(new CommonLangQueryDto() { LangCode = "zh-tw" });
+ Dictionary dic = new();
+ Dictionary dicEn = new();
+ Dictionary dicTw = new();
+ SetLang(msgList, dic);
+ SetLang(msgListEn, dicEn);
+ SetLang(msgListTw, dicTw);
+ return SUCCESS(new
+ {
+ en = dicEn,
+ cn = dic,
+ tw = dicTw
+ });
+ }
+
+ private static void SetLang(List msgList, Dictionary dic)
+ {
+ foreach (var item in msgList)
+ {
+ if (!dic.ContainsKey(item.LangKey))
+ {
+ dic.Add(item.LangKey, item.LangName);
+ }
+ }
+ }
+
+ ///
+ /// 查询多语言配置详情
+ ///
+ ///
+ ///
+ [HttpGet("{Id}")]
+ [ActionPermissionFilter(Permission = "system:lang:query")]
+ public IActionResult GetCommonLang(long Id)
+ {
+ var response = _CommonLangService.GetFirst(x => x.Id == Id);
+ var modal = response.Adapt();
+ if (modal != null)
+ {
+ var list = _CommonLangService.GetList(f => f.LangKey == modal.LangKey);
+ modal.LangName = list.Find(f => f.LangCode == "zh-cn")?.LangName;
+ modal.LangNameEn = list.Find(f => f.LangCode == "en")?.LangName;
+ modal.LangNameTw = list.Find(f => f.LangCode == "zh-tw")?.LangName;
+ }
+ return SUCCESS(modal);
+ }
+
+ ///
+ /// 查询多语言配置详情
+ ///
+ ///
+ ///
+ [HttpGet("key/{langKey}")]
+ [ActionPermissionFilter(Permission = "system:lang:query")]
+ public IActionResult GetCommonLangByKey(string langKey)
+ {
+ var response = _CommonLangService.GetList(x => x.LangKey == langKey);
+
+ return SUCCESS(response);
+ }
+ ///
+ /// 添加多语言配置
+ ///
+ ///
+ [HttpPost]
+ [ActionPermissionFilter(Permission = "system:lang:add")]
+ [Log(Title = "多语言配置", BusinessType = BusinessType.INSERT)]
+ public IActionResult AddCommonLang([FromBody] CommonLangDto parm)
+ {
+ if (parm == null)
+ {
+ throw new CustomException("请求参数错误");
+ }
+
+ //从 Dto 映射到 实体
+ var modal = parm.Adapt().ToCreate(HttpContext);
+ var list = _CommonLangService.GetList(f => f.LangKey == modal.LangKey);
+ modal.Addtime = DateTime.Now;
+ modal.LangCode = "zh-cn";
+ modal.LangName = parm.LangName;
+ if (!list.Any(f => f.LangCode == modal.LangCode))
+ {
+ _CommonLangService.InsertReturnSnowflakeId(modal);
+ }
+ modal.LangCode = "zh-tw";
+ modal.LangName = parm.LangNameTw;
+ if (!list.Any(f => f.LangCode == modal.LangCode))
+ {
+ _CommonLangService.InsertReturnSnowflakeId(modal);
+ }
+ modal.LangCode = "en";
+ modal.LangName = parm.LangNameEn;
+ if (!list.Any(f => f.LangCode == modal.LangCode))
+ {
+ _CommonLangService.InsertReturnSnowflakeId(modal);
+ }
+ return ToResponse(1);
+ }
+
+ ///
+ /// 更新多语言配置
+ ///
+ ///
+ [HttpPut]
+ [ActionPermissionFilter(Permission = "system:lang:edit")]
+ [Log(Title = "多语言配置", BusinessType = BusinessType.UPDATE)]
+ public IActionResult UpdateCommonLang([FromBody] CommonLangDto parm)
+ {
+ if (parm == null)
+ {
+ throw new CustomException("请求实体不能为空");
+ }
+ //从 Dto 映射到 实体
+ var modal = parm.Adapt().ToUpdate(HttpContext);
+ var list = _CommonLangService.GetList(f => f.LangKey == modal.LangKey);
+
+ _CommonLangService.Update(w => w.LangKey == modal.LangKey && w.LangCode == "zh-cn", it => new CommonLang()
+ {
+ LangName = modal.LangName,
+ });
+
+ _CommonLangService.Update(w => w.LangKey == modal.LangKey && w.LangCode == "zh-tw", it => new CommonLang()
+ {
+ LangName = parm.LangNameTw,
+ });
+
+ _CommonLangService.Update(w => w.LangKey == modal.LangKey && w.LangCode == "en", it => new CommonLang()
+ {
+ LangName = parm.LangNameEn,
+ });
+
+ return ToResponse(1);
+ }
+
+ ///
+ /// 删除多语言配置
+ ///
+ ///
+ [HttpDelete("{ids}")]
+ [ActionPermissionFilter(Permission = "system:lang:delete")]
+ [Log(Title = "多语言配置", BusinessType = BusinessType.DELETE)]
+ public IActionResult DeleteCommonLang(string ids)
+ {
+ long[] idsArr = Tools.SpitLongArrary(ids);
+ if (idsArr.Length <= 0) { return ToResponse(ApiResult.Error($"删除失败Id 不能为空")); }
+
+ var response = _CommonLangService.Delete(idsArr);
+
+ return ToResponse(response);
+ }
+
+ ///
+ /// 导出多语言配置
+ ///
+ ///
+ [Log(Title = "多语言配置", BusinessType = BusinessType.EXPORT, IsSaveResponseData = false)]
+ [HttpGet("export")]
+ [ActionPermissionFilter(Permission = "system:lang:export")]
+ public IActionResult Export([FromQuery] CommonLangQueryDto parm)
+ {
+ parm.PageSize = 10000;
+ var list = _CommonLangService.GetList(parm).Result;
+
+ string sFileName = ExportExcel(list, "CommonLang", "多语言配置");
+ return SUCCESS(new { path = "/export/" + sFileName, fileName = sFileName });
+ }
+
+ }
+}
\ No newline at end of file
diff --git a/ZR.Model/System/CommonLang.cs b/ZR.Model/System/CommonLang.cs
new file mode 100644
index 0000000..b48c740
--- /dev/null
+++ b/ZR.Model/System/CommonLang.cs
@@ -0,0 +1,59 @@
+using System;
+using System.Collections.Generic;
+using SqlSugar;
+using OfficeOpenXml.Attributes;
+using Newtonsoft.Json;
+
+namespace ZR.Model.Models
+{
+ ///
+ /// 多语言配置,数据实体对象
+ ///
+ /// @author zr
+ /// @date 2022-05-06
+ ///
+ [Tenant("0")]
+ [SugarTable("sys_common_lang")]
+ public class CommonLang
+ {
+ ///
+ /// 描述 : id
+ /// 空值 : false
+ ///
+ [JsonConverter(typeof(ValueToStringConverter))]
+ [EpplusTableColumn(Header = "id")]
+ [SugarColumn(IsPrimaryKey = true)]
+ public long Id { get; set; }
+
+ ///
+ /// 描述 : 语言code
+ /// 空值 : false
+ ///
+ [EpplusTableColumn(Header = "语言code")]
+ [SugarColumn(ColumnName = "lang_code")]
+ public string LangCode { get; set; }
+
+ ///
+ /// 描述 : 语言key
+ /// 空值 : true
+ ///
+ [EpplusTableColumn(Header = "语言key")]
+ [SugarColumn(ColumnName = "lang_key")]
+ public string LangKey { get; set; }
+
+ ///
+ /// 描述 : 名称
+ /// 空值 : false
+ ///
+ [EpplusTableColumn(Header = "名称")]
+ [SugarColumn(ColumnName = "lang_name")]
+ public string LangName { get; set; }
+
+ ///
+ /// 描述 : 添加时间
+ /// 空值 : true
+ ///
+ [EpplusTableColumn(Header = "添加时间", NumberFormat = "yyyy-MM-dd HH:mm:ss")]
+ public DateTime? Addtime { get; set; }
+ }
+}
\ No newline at end of file
diff --git a/ZR.Model/System/Dto/CommonLangDto.cs b/ZR.Model/System/Dto/CommonLangDto.cs
new file mode 100644
index 0000000..1f56d7b
--- /dev/null
+++ b/ZR.Model/System/Dto/CommonLangDto.cs
@@ -0,0 +1,35 @@
+using System;
+using System.Collections.Generic;
+using System.ComponentModel.DataAnnotations;
+
+namespace ZR.Model.Dto
+{
+ ///
+ /// 多语言配置输入对象
+ ///
+ public class CommonLangDto
+ {
+ [Required(ErrorMessage = "id不能为空")]
+ public long Id { get; set; }
+ //[Required(ErrorMessage = "语言code不能为空")]
+ public string LangCode { get; set; }
+ public string LangKey { get; set; }
+ [Required(ErrorMessage = "名称不能为空")]
+ public string LangName { get; set; }
+ public string LangNameEn { get; set; }
+ public string LangNameTw { get; set; }
+ //public List Langs { get; set; }
+ }
+
+ ///
+ /// 多语言配置查询对象
+ ///
+ public class CommonLangQueryDto : PagerInfo
+ {
+ public string LangCode { get; set; }
+ public string LangKey { get; set; }
+ public DateTime? BeginAddtime { get; set; }
+ public DateTime? EndAddtime { get; set; }
+ public int ShowMode { get; set; }
+ }
+}
diff --git a/ZR.Model/System/Vo/LangVo.cs b/ZR.Model/System/Vo/LangVo.cs
new file mode 100644
index 0000000..802d842
--- /dev/null
+++ b/ZR.Model/System/Vo/LangVo.cs
@@ -0,0 +1,13 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace ZR.Model.System.Vo
+{
+ public class LangVo
+ {
+
+ }
+}
diff --git a/ZR.Repository/System/CommonLangRepository.cs b/ZR.Repository/System/CommonLangRepository.cs
new file mode 100644
index 0000000..9ef5eb1
--- /dev/null
+++ b/ZR.Repository/System/CommonLangRepository.cs
@@ -0,0 +1,20 @@
+using System;
+using Infrastructure.Attribute;
+using ZR.Repository.System;
+using ZR.Model.Models;
+
+namespace ZR.Repository
+{
+ ///
+ /// 多语言配置仓储
+ ///
+ /// @author zr
+ /// @date 2022-05-06
+ ///
+ [AppService(ServiceLifetime = LifeTime.Transient)]
+ public class CommonLangRepository : BaseRepository
+ {
+ #region 业务逻辑代码
+ #endregion
+ }
+}
\ No newline at end of file
diff --git a/ZR.Service/System/CommonLangService.cs b/ZR.Service/System/CommonLangService.cs
new file mode 100644
index 0000000..818442d
--- /dev/null
+++ b/ZR.Service/System/CommonLangService.cs
@@ -0,0 +1,92 @@
+using Infrastructure.Attribute;
+using SqlSugar;
+using System.Collections.Generic;
+using System.Linq;
+using ZR.Model;
+using ZR.Model.Dto;
+using ZR.Model.Models;
+using ZR.Repository;
+using ZR.Service.System.ISystemService;
+
+namespace ZR.Service.System
+{
+ ///
+ /// 多语言配置Service业务层处理
+ ///
+ /// @author zr
+ /// @date 2022-05-06
+ ///
+ [AppService(ServiceType = typeof(ICommonLangService), ServiceLifetime = LifeTime.Transient)]
+ public class CommonLangService : BaseService, ICommonLangService
+ {
+ private readonly CommonLangRepository _CommonLangrepository;
+ public CommonLangService(CommonLangRepository repository)
+ {
+ _CommonLangrepository = repository;
+ }
+
+ #region 业务逻辑代码
+
+ ///
+ /// 查询多语言配置列表
+ ///
+ ///
+ ///
+ public PagedInfo GetList(CommonLangQueryDto parm)
+ {
+ //开始拼装查询条件
+ var predicate = Expressionable.Create();
+
+ //搜索条件查询语法参考Sqlsugar
+ predicate = predicate.AndIF(!string.IsNullOrEmpty(parm.LangCode), it => it.LangCode == parm.LangCode);
+ predicate = predicate.AndIF(!string.IsNullOrEmpty(parm.LangKey), it => it.LangKey.Contains(parm.LangKey));
+ //predicate = predicate.AndIF(parm.BeginAddtime == null, it => it.Addtime >= DateTime.Now.AddDays(-1));
+ predicate = predicate.AndIF(parm.BeginAddtime != null, it => it.Addtime >= parm.BeginAddtime && it.Addtime <= parm.EndAddtime);
+ var response = _CommonLangrepository
+ .Queryable()
+ .Where(predicate.ToExpression())
+ .ToPage(parm);
+ return response;
+ }
+
+ ///
+ /// 行转列
+ ///
+ ///
+ ///
+ public dynamic GetListToPivot(CommonLangQueryDto parm)
+ {
+ //开始拼装查询条件
+ var predicate = Expressionable.Create();
+
+ //搜索条件查询语法参考Sqlsugar
+ predicate = predicate.AndIF(!string.IsNullOrEmpty(parm.LangCode), it => it.LangCode == parm.LangCode);
+ predicate = predicate.AndIF(!string.IsNullOrEmpty(parm.LangKey), it => it.LangKey.Contains(parm.LangKey));
+ //predicate = predicate.AndIF(parm.BeginAddtime == null, it => it.Addtime >= DateTime.Now.AddDays(-1));
+ predicate = predicate.AndIF(parm.BeginAddtime != null, it => it.Addtime >= parm.BeginAddtime && it.Addtime <= parm.EndAddtime);
+ var response = _CommonLangrepository
+ .Queryable()
+ .Where(predicate.ToExpression())
+ .ToPivotList(it => it.LangCode, it => it.LangKey, it => it.Max(f => f.LangName));
+ return response;
+ }
+
+ public List GetLangList(CommonLangQueryDto parm)
+ {
+ //开始拼装查询条件
+ var predicate = Expressionable.Create();
+
+ //搜索条件查询语法参考Sqlsugar
+ predicate = predicate.AndIF(!string.IsNullOrEmpty(parm.LangCode), it => it.LangCode == parm.LangCode);
+ //predicate = predicate.AndIF(!string.IsNullOrEmpty(parm.LangKey), it => it.LangKey.Contains(parm.LangKey));
+ //predicate = predicate.AndIF(parm.BeginAddtime == null, it => it.Addtime >= DateTime.Now.AddDays(-1));
+ //predicate = predicate.AndIF(parm.BeginAddtime != null, it => it.Addtime >= parm.BeginAddtime && it.Addtime <= parm.EndAddtime);
+ var response = _CommonLangrepository
+ .Queryable()
+ .Where(predicate.ToExpression())
+ .ToList();
+ return response;
+ }
+ #endregion
+ }
+}
\ No newline at end of file
diff --git a/ZR.Service/System/ISystemService/ICommonLangService.cs b/ZR.Service/System/ISystemService/ICommonLangService.cs
new file mode 100644
index 0000000..67c6351
--- /dev/null
+++ b/ZR.Service/System/ISystemService/ICommonLangService.cs
@@ -0,0 +1,21 @@
+using System;
+using ZR.Model;
+using ZR.Model.Dto;
+using ZR.Model.Models;
+using System.Collections.Generic;
+
+namespace ZR.Service.System.ISystemService
+{
+ ///
+ /// 多语言配置service接口
+ ///
+ /// @author zr
+ /// @date 2022-05-06
+ ///
+ public interface ICommonLangService : IBaseService
+ {
+ PagedInfo GetList(CommonLangQueryDto parm);
+ List GetLangList(CommonLangQueryDto parm);
+ dynamic GetListToPivot(CommonLangQueryDto parm);
+ }
+}