using System.Reflection;
using JNPF.Common.Core.Manager;
using JNPF.Common.Enums;
using JNPF.Common.Extension;
using JNPF.Common.Filter;
using JNPF.Common.Security;
using JNPF.DependencyInjection;
using JNPF.DynamicApiController;
using JNPF.Extend.Entitys;
using JNPF.Extend.Entitys.Dto.TableExample;
using JNPF.Extend.Entitys.Model;
using JNPF.FriendlyException;
using JNPF.LinqBuilder;
using JNPF.Systems.Entitys.Permission;
using JNPF.Systems.Entitys.System;
using Mapster;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using SqlSugar;
using Yitter.IdGenerator;
namespace JNPF.Extend;
///
/// 表格示例数据
/// 版 本:V3.2
/// 版 权:拓通智联科技有限公司(http://www.tuotong-tech.com)
/// 日 期:2021-06-01 .
///
[ApiDescriptionSettings(Tag = "Extend", Name = "TableExample", Order = 600)]
[Route("api/extend/[controller]")]
public class TableExampleService : IDynamicApiController, ITransient
{
private readonly ISqlSugarRepository _repository;
private readonly IUserManager _userManager;
public TableExampleService(
ISqlSugarRepository repository,
IUserManager userManager)
{
_repository = repository;
_userManager = userManager;
}
#region GET
///
/// 获取表格数据列表.
///
/// 请求参数
///
[HttpGet("")]
public async Task GetList([FromQuery] PageInputBase input)
{
return await GetPageList(string.Empty, 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 _repository.AsSugarClient().Queryable((a, b) => new JoinQueryInfos(JoinType.Left, a.Registrant == b.Id))
.WhereIF(input.keyword.IsNotEmptyOrNull(), a => a.ProjectCode.Contains(input.keyword) || a.ProjectName.Contains(input.keyword) || a.CustomerName.Contains(input.keyword)).OrderBy(a => a.RegisterDate, OrderByType.Desc)
.Select((a, b) => new TableExampleAllOutput()
{
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
}).ToListAsync();
return new { list = list };
}
///
/// 获取延伸扩展列表(行政区划).
///
///
[HttpGet("IndustryList")]
public async Task GetIndustryList([FromQuery] KeywordInput input)
{
var data = (await _repository.AsSugarClient().Queryable()
.WhereIF(input.keyword.IsNotEmptyOrNull(), x => x.EnCode.Contains(input.keyword) || x.FullName.Contains(input.keyword))
.Where(m => m.ParentId == "-1" && m.DeleteMark == null).ToListAsync()).Adapt>();
return new { list = data };
}
///
/// 获取城市信息列表(获取延伸扩展列表(行政区划))
///
///
///
[HttpGet("CityList/{id}")]
public async Task GetCityList(string id)
{
var data = (await _repository.AsSugarClient().Queryable().Where(m => m.ParentId == id && m.DeleteMark == null).ToListAsync()).Adapt>();
return new { list = data };
}
///
/// 列表(表格树形).
///
///
[HttpGet("ControlSample/TreeList")]
public async Task GetTreeList(string isTree)
{
var dicData = await _repository.AsSugarClient().Queryable((a, b) =>
new JoinQueryInfos(JoinType.Left, a.DictionaryTypeId == b.Id)).Where((a, b) => b.EnCode == "IndustryType" && a.DeleteMark == null
&& b.DeleteMark == null).Select(a => a).ToListAsync();
var data = dicData.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 _repository.GetFirstAsync(x => x.Id == id)).Adapt();
return data;
}
///
/// 获取批注
///
///
///
[HttpGet("{id}/Actions/Postil")]
public async Task GetPostil(string id)
{
var tableExampleEntity = await _repository.GetFirstAsync(x => x.Id == id);
if (tableExampleEntity == null)
throw Oops.Oh(ErrorCode.COM1007);
return new { postilJson = tableExampleEntity.PostilJson };
}
#endregion
#region POST
///
/// 删除.
///
/// 主键值
///
[HttpDelete("{id}")]
public async Task Delete(string id)
{
var entity = await _repository.GetFirstAsync(x => x.Id == id);
if (entity != null)
{
await _repository.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 _repository.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 _repository.AsSugarClient().Updateable(entity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync();
}
///
/// 更新标签.
///
/// 主键
/// 实体对象
///
[HttpPut("UpdateSign/{id}")]
public async Task UpdateSign(string id, [FromBody] TableExampleSignUpInput input)
{
var tableExampleEntity = await _repository.GetFirstAsync(x => x.Id == id);
tableExampleEntity.Sign = input.sign;
tableExampleEntity.Id = id;
tableExampleEntity.LastModifyTime = DateTime.Now;
tableExampleEntity.LastModifyUserId = _userManager.UserId;
await _repository.AsSugarClient().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 _repository.GetFirstAsync(x => x.Id == id);
var exampleEntity = BindModelValue(tableExampleEntity, entity);
exampleEntity.LastModifyTime = DateTime.Now;
exampleEntity.LastModifyUserId = _userManager.UserId;
await _repository.AsSugarClient().Updateable(tableExampleEntity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync();
}
///
/// 发送批注
///
///
///
///
[HttpPost("{id}/Postil")]
public async Task SendPostil(string id, [FromBody] TableExamplePostilSendInput input)
{
var tableExampleEntity = await _repository.GetFirstAsync(x => x.Id == id);
if (tableExampleEntity == null)
throw Oops.Oh(ErrorCode.COM1005);
var model = new PostilModel()
{
userId = string.Format("{0}/{1}", _userManager.Account, _userManager.RealName),
text = input.text,
creatorTime = DateTime.Now
};
var list = new List();
list.Add(model);
if (tableExampleEntity.PostilJson.IsNotEmptyOrNull())
{
list = list.Concat(tableExampleEntity.PostilJson.ToList()).ToList();
}
tableExampleEntity.PostilJson = JsonConvert.SerializeObject(list, new IsoDateTimeConverter { DateTimeFormat = "yyyy-MM-dd HH:mm:ss" });
tableExampleEntity.PostilCount = list.Count;
tableExampleEntity.Id = id;
tableExampleEntity.LastModifyTime = DateTime.Now;
tableExampleEntity.LastModifyUserId = _userManager.UserId;
await _repository.AsSugarClient().Updateable(tableExampleEntity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync();
}
///
/// 删除批注
///
/// 主键值
/// 请求参数
///
[HttpDelete("{id}/Postil/{index}")]
public async Task DeletePostil(string id, int index)
{
var tableExampleEntity = await _repository.GetFirstAsync(x => x.Id == id);
if (tableExampleEntity == null)
throw Oops.Oh(ErrorCode.COM1005);
var list = tableExampleEntity.PostilJson.ToList();
list.Remove(list[index]);
tableExampleEntity.PostilJson = list.ToJsonString();
tableExampleEntity.PostilCount = list.Count;
tableExampleEntity.Id = id;
tableExampleEntity.LastModifyTime = DateTime.Now;
tableExampleEntity.LastModifyUserId = _userManager.UserId;
await _repository.AsSugarClient().Updateable(tableExampleEntity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync();
}
#endregion
#region PrivateMethod
///
/// 分页列表.
///
///
///
///
private async Task GetPageList(string typeId, PageInputBase input)
{
var list = await _repository.AsSugarClient().Queryable((a, b) => new JoinQueryInfos(JoinType.Left, a.Registrant == b.Id))
.WhereIF(typeId.IsNotEmptyOrNull(), a => a.ProjectType == typeId)
.WhereIF(input.keyword.IsNotEmptyOrNull(), a => a.ProjectCode.Contains(input.keyword) || a.ProjectName.Contains(input.keyword))
.OrderBy(a => a.RegisterDate, OrderByType.Desc)
.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(),
}).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
}