using NCC.Common.Core.Manager; using NCC.Common.Enum; using NCC.Common.Extension; using NCC.Common.Filter; using NCC.Common.Helper; using NCC.Common.Util; using NCC.Dependency; using NCC.DynamicApiController; using NCC.Extend.Entitys.common_extend; using NCC.Extend.Entitys.Dto.TableExample; using NCC.Extend.Entitys.Model; using NCC.FriendlyException; using NCC.LinqBuilder; using NCC.System.Entitys.Permission; using NCC.System.Interfaces.Permission; using NCC.System.Interfaces.System; using Mapster; using Microsoft.AspNetCore.Mvc; using SqlSugar; using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Threading.Tasks; using Yitter.IdGenerator; namespace NCC.Extend { ///     /// 表格示例数据     /// 版 本:V1.20.15     /// 版 权:Wesley(https://www.NCCsoft.com)     /// 作 者:NCC开发平台组     /// 日 期:2022-03-16     /// [ApiDescriptionSettings(Tag = "Extend", Name = "TableExample", Order = 600)] [Route("api/extend/[controller]")] public class TableExampleService : IDynamicApiController, ITransient { private readonly ISqlSugarRepository _tableExampleRepository; private readonly SqlSugarScope db; private readonly IUsersService _usersService; private readonly IProvinceService _provinceService; private readonly IDictionaryDataService _dictionaryDataService; private readonly IUserManager _userManager; /// /// /// /// /// /// /// /// public TableExampleService(ISqlSugarRepository tableExampleRepository, IUsersService usersService, IProvinceService provinceService, IDictionaryDataService dictionaryDataService,IUserManager userManager) { _tableExampleRepository = tableExampleRepository; db = tableExampleRepository.Context; _usersService = usersService; _provinceService = provinceService; _dictionaryDataService = dictionaryDataService; _userManager = userManager; } #region GET ///  /// 获取表格数据列表  /// /// 请求参数 /// [HttpGet("")] public async Task GetList([FromQuery] PageInputBase input) { return await GetPageList("",input); } /// /// 列表(树形表格) /// /// /// /// [HttpGet("ControlSample/{typeId}")] public async Task GetList(string typeId, [FromQuery] PageInputBase input) { return await GetPageList(typeId, input); }         ///         /// 列表         ///         ///         [HttpGet("All")] public async Task GetListAll([FromQuery] KeywordInput input) { var list = await db.Queryable((a, b) => new JoinQueryInfos(JoinType.Left, a.Registrant == b.Id)).Select((a, b) => new TableExampleEntity() { Id = a.Id, InteractionDate = a.InteractionDate, ProjectCode = a.ProjectCode, ProjectName = a.ProjectName, Principal = a.Principal, JackStands = a.JackStands, ProjectType = a.ProjectType, ProjectPhase = a.ProjectPhase, CustomerName = a.CustomerName, CostAmount = a.CostAmount, TunesAmount = a.TunesAmount, ProjectedIncome = a.ProjectedIncome, RegisterDate = a.RegisterDate, Registrant = SqlFunc.MergeString(b.RealName, "/", b.Account), Description = a.Description, Sign = a.Sign, PostilJson = a.Sign, PostilCount = a.PostilCount, EnabledMark = a.EnabledMark, SortCode = a.SortCode, LastModifyTime = a.LastModifyTime, LastModifyUserId = a.LastModifyUserId }).WhereIF(input.keyword.IsNotEmptyOrNull(), a => a.ProjectCode.Contains(input.keyword) || a.ProjectName.Contains(input.keyword) || a.CustomerName.Contains(input.keyword)).MergeTable().ToListAsync(); var output = list.Adapt>().OrderByDescending(x=>x.registerDate); return new { list = output }; } /// /// 获取延伸扩展列表(行政区划) /// /// [HttpGet("IndustryList")] public async Task GetIndustryList([FromQuery] KeywordInput input) { var data = (await _provinceService.GetList("-1")).Adapt>(); if (input.keyword.IsNotEmptyOrNull()) { data = data.FindAll(x => x.enCode.Contains(input.keyword) || x.fullName.Contains(input.keyword)); } return new { list = data }; } /// /// 获取城市信息列表(获取延伸扩展列表(行政区划)) /// /// /// [HttpGet("CityList/{id}")] public async Task GetCityList(string id) { var data = (await _provinceService.GetList(id)).Adapt>(); return new { list = data }; } /// /// 列表(表格树形) /// /// [HttpGet("ControlSample/TreeList")] public async Task GetTreeList(string isTree) { var data = (await _dictionaryDataService.GetList("IndustryType")).FindAll(x => x.EnabledMark == 1); var treeList = data.Select(x => new TableExampleTreeListOutput() { id = x.Id, parentId = x.ParentId, text = x.FullName, loaded = true, expanded = true, ht = x.Adapt>() }).ToList(); var output = isTree.IsNotEmptyOrNull() && "1".Equals(isTree) ? treeList.ToTree() : treeList; return new { list = output }; } /// /// 信息 /// /// 主键值 /// [HttpGet, Route("{id}")] public async Task GetInfo(string id) { var data = (await _tableExampleRepository.FirstOrDefaultAsync(x=>x.Id==id)).Adapt(); return data; } /// /// 获取批注 /// /// /// [HttpGet("{id}/Actions/Postil")] public async Task GetPostil(string id) { var tableExampleEntity = await _tableExampleRepository.FirstOrDefaultAsync(x => x.Id == id); if (tableExampleEntity == null) throw NCCException.Oh(ErrorCode.COM1007); return new { postilJson = tableExampleEntity.PostilJson }; } #endregion #region POST /// /// 删除  /// /// 主键值  /// [HttpDelete("{id}")] public async Task Delete(string id) { var entity = await _tableExampleRepository.FirstOrDefaultAsync(x => x.Id == id); if (entity != null) { await _tableExampleRepository.DeleteAsync(entity); } }         ///         /// 创建         ///         /// 实体对象         ///         [HttpPost("")] public async Task Create([FromBody] TableExampleCrInput input) { var entity = input.Adapt(); entity.Id = YitIdHelper.NextId().ToString(); entity.RegisterDate = DateTime.Now; entity.Registrant = _userManager.UserId; entity.CostAmount = entity.CostAmount == null ? 0 : entity.CostAmount; entity.TunesAmount = entity.TunesAmount == null ? 0 : entity.TunesAmount; entity.ProjectedIncome = entity.ProjectedIncome == null ? 0 : entity.ProjectedIncome; entity.Sign = "0000000"; await _tableExampleRepository.InsertAsync(entity); }         ///         /// 编辑         /// /// 主键         /// 实体对象         ///         [HttpPut("{id}")] public async Task Update(string id, [FromBody] TableExampleUpInput input) { var entity = input.Adapt(); entity.Id = id; entity.LastModifyTime = DateTime.Now; entity.LastModifyUserId = _userManager.UserId; entity.CostAmount = entity.CostAmount == null ? 0 : entity.CostAmount; entity.TunesAmount = entity.TunesAmount == null ? 0 : entity.TunesAmount; entity.ProjectedIncome = entity.ProjectedIncome == null ? 0 : entity.ProjectedIncome; await _tableExampleRepository.Context.Updateable(entity).IgnoreColumns(ignoreAllNullColumns:true).ExecuteCommandAsync(); } /// /// 更新标签 /// /// 主键         /// 实体对象 /// [HttpPut("UpdateSign/{id}")] public async Task UpdateSign(string id, [FromBody] TableExampleSignUpInput input) { var tableExampleEntity = await _tableExampleRepository.FirstOrDefaultAsync(x => x.Id == id); tableExampleEntity.Sign = input.sign; tableExampleEntity.Id = id; tableExampleEntity.LastModifyTime = DateTime.Now; tableExampleEntity.LastModifyUserId = _userManager.UserId; await _tableExampleRepository.Context.Updateable(tableExampleEntity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync(); } /// /// 行编辑 /// /// /// ///         [HttpPut("{id}/Actions/RowsEdit")] public async Task RowEditing(string id, [FromBody] TableExampleRowUpInput input) { var entity = input.Adapt(); entity.Id = id; entity.CostAmount = entity.CostAmount == null ? 0 : entity.CostAmount; entity.TunesAmount = entity.TunesAmount == null ? 0 : entity.TunesAmount; entity.ProjectedIncome = entity.ProjectedIncome == null ? 0 : entity.ProjectedIncome; var tableExampleEntity = await _tableExampleRepository.FirstOrDefaultAsync(x => x.Id == id); var exampleEntity = BindModelValue(tableExampleEntity, entity); exampleEntity.LastModifyTime = DateTime.Now; exampleEntity.LastModifyUserId = _userManager.UserId; await _tableExampleRepository.Context.Updateable(tableExampleEntity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync(); } /// /// 发送批注 /// /// /// /// [HttpPost("{id}/Postil")] public async Task SendPostil(string id, [FromBody] TableExamplePostilSendInput input) { var tableExampleEntity = await _tableExampleRepository.FirstOrDefaultAsync(x => x.Id == id); if (tableExampleEntity == null) throw NCCException.Oh(ErrorCode.COM1005); var model = new PostilModel() { userId =await _usersService.GetUserName(_userManager.UserId), text = input.text, creatorTime = DateTime.Now }; var list = new List(); list.Add(model); if (tableExampleEntity.PostilJson.IsNotEmptyOrNull()) { list = list.Concat(tableExampleEntity.PostilJson.ToList()).ToList(); } var postilJson = list.ToJson(); tableExampleEntity.PostilJson = postilJson; tableExampleEntity.PostilCount = list.Count; tableExampleEntity.Id = id; tableExampleEntity.LastModifyTime = DateTime.Now; tableExampleEntity.LastModifyUserId = _userManager.UserId; await _tableExampleRepository.Context.Updateable(tableExampleEntity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync(); } /// /// 删除批注 /// /// 主键值 /// 请求参数 /// [HttpDelete("{id}/Postil/{index}")] public async Task DeletePostil(string id, int index) { var tableExampleEntity = await _tableExampleRepository.FirstOrDefaultAsync(x => x.Id == id); if (tableExampleEntity == null) throw NCCException.Oh(ErrorCode.COM1005); var list = tableExampleEntity.PostilJson.ToList(); list.Remove(list[index]); tableExampleEntity.PostilJson = list.ToJson(); tableExampleEntity.PostilCount = list.Count; tableExampleEntity.Id = id; tableExampleEntity.LastModifyTime = DateTime.Now; tableExampleEntity.LastModifyUserId = _userManager.UserId; await _tableExampleRepository.Context.Updateable(tableExampleEntity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync(); } #endregion #region PrivateMethod /// /// 分页列表 /// /// /// /// private async Task GetPageList(string typeId, PageInputBase input) { var queryWhere = LinqExpression.And(); var queryParam = input.queryJson.ToObject(); if (typeId.IsNotEmptyOrNull()) { queryWhere = queryWhere.And(a => a.projectType == typeId); } //关键字(项目编码、项目名称、客户名称) if (input.keyword.IsNotEmptyOrNull()) { var keyword = input.keyword; queryWhere = queryWhere.And(a => a.projectCode.Contains(input.keyword) || a.projectName.Contains(input.keyword)); } //标签查询 if (queryParam["sign"].IsNotEmptyOrNull()) { var index = 0; var arraySign = queryParam["sign"].ToString().Split(','); foreach (var item in arraySign) { queryWhere = index == 0 ? queryWhere.And(a => a.sign.Contains(item)) : queryWhere.Or(a => a.sign.Contains(item)); index++; } } var list = await db.Queryable((a, b) => new JoinQueryInfos(JoinType.Left, a.Registrant == b.Id)).Select((a, b) => new TableExampleListOutput() { id=a.Id, interactionDate=a.InteractionDate, projectCode=a.ProjectCode, projectName=a.ProjectName, principal=a.Principal, jackStands=a.JackStands, projectType=a.ProjectType, projectPhase=a.ProjectPhase, customerName=a.CustomerName, costAmount=a.CostAmount, tunesAmount=a.TunesAmount, projectedIncome=a.ProjectedIncome, registerDate=a.RegisterDate, registrant= SqlFunc.MergeString(b.RealName,"/",b.Account), description = a.Description, sign=a.Sign, postilJson=a.Sign, postilCount=a.PostilCount.ToString(), }).MergeTable().Where(queryWhere).OrderBy(a => a.registerDate,OrderByType.Desc).ToPagedListAsync(input.currentPage, input.pageSize); return PageResult.SqlSugarPageResult(list); } /// /// 将实体2的值动态赋值给实体1(名称一样的属性进行赋值) /// /// 实体1 /// 实体2 /// 赋值后的model1 private T1 BindModelValue(T1 entity1, T2 entity2) where T1 : class where T2 : class { Type t1 = entity1.GetType(); Type t2 = entity2.GetType(); PropertyInfo[] property2 = t2.GetProperties(); //排除字段 List exclude = new List() { "F_Id", "F_Registrant", "F_RegisterDate", "F_SortCode", "F_Sign", "F_PostilJson", "F_PostilCount" }; foreach (PropertyInfo p in property2) { if (exclude.Contains(p.Name)) { continue; } t1.GetProperty(p.Name)?.SetValue(entity1, p.GetValue(entity2, null)); } return entity1; } #endregion } }