using NCC.Common.Core.Manager;
using NCC.Common.Enum;
using NCC.Common.Extension;
using NCC.Common.Filter;
using NCC.Dependency;
using NCC.DynamicApiController;
using NCC.FriendlyException;
using NCC.Education.Interfaces.TbMbtCharacter;
using Mapster;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using NCC.Education.Entitys;
using NCC.Education.Entitys.Dto.TbMbtCharacter;
using Yitter.IdGenerator;
using NCC.Common.Helper;
using NCC.JsonSerialization;
using NCC.Common.Model.NPOI;
using NCC.Common.Configuration;
using NCC.DataEncryption;
using NCC.ClayObject;
using NCC.System.Interfaces.System;
using NCC.Blind.Entitys.Dto.TbAreaLineNode;
using NCC.Blind.Entitys;
using static NCC.Education.TbArea.TbAreaService;
using System.ComponentModel.DataAnnotations;
using NCC.Blind.Entitys.Dto.TbMbtLineNode;
namespace NCC.Education.TbMbtCharacter
{
///
/// MBT人格服务
///
[ApiDescriptionSettings(Tag = "MBT人格服务",Name = "TbMbtCharacter", Order = 200)]
[Route("api/Blind/[controller]")]
public class TbMbtCharacterService : ITbMbtCharacterService, IDynamicApiController, ITransient
{
private readonly ISqlSugarRepository _tbMbtCharacterRepository;
private readonly IDbLinkService _dbLinkService;
private readonly IDataBaseService _dataBaseService;
private readonly SqlSugarScope _db;
private readonly IUserManager _userManager;
///
/// 初始化一个类型的新实例
///
public TbMbtCharacterService(
ISqlSugarRepository tbMbtCharacterRepository,
IDbLinkService dbLinkService,
IDataBaseService dataBaseService,
IUserManager userManager)
{
_tbMbtCharacterRepository = tbMbtCharacterRepository;
_db = _tbMbtCharacterRepository.Context;
_dbLinkService = dbLinkService;
_dataBaseService = dataBaseService;
_userManager = userManager;
}
///
/// 根据MBT人格ID查询MBT人格线路节点入参
///
public class MbtLineNodeOptions : PageInputBase
{
///
/// MBT人格ID
///
[Required(ErrorMessage = "MBT人格不能为空")]
public string mbtId { get; set; }
}
///
/// 根据MBTI人格查询人格线路节点
///
///
///
[HttpPost("GetMbtLineNodeByAreaId")]
public async Task GetMbtLineNodeByAreaId(MbtLineNodeOptions input)
{
List areaLineIds = new List();
var areaLineList = _db.Queryable().Where(o => o.MbtId == input.mbtId).ToList();
foreach (var item in areaLineList)
{
areaLineIds.Add(item.Id);
}
var sidx = input.sidx == null ? "id" : input.sidx;
var data = await _db.Queryable()
.WhereIF(!string.IsNullOrEmpty(input.mbtId), p => areaLineIds.Contains(p.MbtLineId))
.Select(it => new TbMbtLineNodeListOutput
{
id = it.Id,
mbtLineId = it.MbtLineId,
title = it.Title,
//description = it.Description,
remark = it.Remark,
creatorTime = it.CreatorTime,
lastModifyTime = it.LastModifyTime,
banner = it.Banner,
sorts = it.Sorts,
}).MergeTable().Mapper(p =>
{
p.mbtLine = _db.Queryable().Where(o => o.Id == p.mbtLineId).First();
}).OrderBy(sidx + " " + input.sort).ToPagedListAsync(input.currentPage, input.pageSize);
return PageResult.SqlSugarPageResult(data);
}
///
/// 获取MBT人格
///
/// 参数
///
[HttpGet("{id}")]
public async Task GetInfo(string id)
{
var dbLink = await _dbLinkService.GetInfo("218239598550058245");
_db.AddConnection(new ConnectionConfig()
{
ConfigId = dbLink.Id,
DbType = _dataBaseService.ToDbType(dbLink.DbType),
ConnectionString = _dataBaseService.ToConnectionString(dbLink),
InitKeyType = InitKeyType.Attribute,
IsAutoCloseConnection = true
});
_db.ChangeDatabase(dbLink.Id);
var entity = await _db.Queryable().FirstAsync(p => p.Id == id);
var output = entity.Adapt();
return output;
}
///
/// 获取MBT人格列表
///
/// 请求参数
///
[HttpGet("")]
public async Task GetList([FromQuery] TbMbtCharacterListQueryInput input)
{
var dbLink = await _dbLinkService.GetInfo("218239598550058245");
_db.AddConnection(new ConnectionConfig()
{
ConfigId = dbLink.Id,
DbType = _dataBaseService.ToDbType(dbLink.DbType),
ConnectionString = _dataBaseService.ToConnectionString(dbLink),
InitKeyType = InitKeyType.Attribute,
IsAutoCloseConnection = true
});
_db.ChangeDatabase(dbLink.Id);
var sidx = input.sidx == null ? "id" : input.sidx;
var data = await _db.Queryable()
.WhereIF(!string.IsNullOrEmpty(input.name), p => p.Name.Contains(input.name))
.Select(it=> new TbMbtCharacterListOutput
{
id = it.Id,
name=it.Name,
creatorTime=it.CreatorTime,
lastModifyTime=it.LastModifyTime,
}).MergeTable().OrderBy(sidx+" "+input.sort).ToPagedListAsync(input.currentPage, input.pageSize);
return PageResult.SqlSugarPageResult(data);
}
///
/// 新建MBT人格
///
/// 参数
///
[HttpPost("")]
public async Task Create([FromBody] TbMbtCharacterCrInput input)
{
var dbLink = await _dbLinkService.GetInfo("218239598550058245");
_db.AddConnection(new ConnectionConfig()
{
ConfigId = dbLink.Id,
DbType = _dataBaseService.ToDbType(dbLink.DbType),
ConnectionString = _dataBaseService.ToConnectionString(dbLink),
InitKeyType = InitKeyType.Attribute,
IsAutoCloseConnection = true
});
_db.ChangeDatabase(dbLink.Id);
var userInfo = await _userManager.GetUserInfo();
var entity = input.Adapt();
entity.Id = YitIdHelper.NextId().ToString();
entity.CreatorTime = DateTime.Now;
var isOk = await _db.Insertable(entity).IgnoreColumns(ignoreNullColumn: true).ExecuteCommandAsync();
if (!(isOk > 0)) throw NCCException.Oh(ErrorCode.COM1000);
}
///
/// 获取MBT人格无分页列表
///
/// 请求参数
///
[NonAction]
public async Task GetNoPagingList([FromQuery] TbMbtCharacterListQueryInput input)
{
var dbLink = await _dbLinkService.GetInfo("218239598550058245");
_db.AddConnection(new ConnectionConfig()
{
ConfigId = dbLink.Id,
DbType = _dataBaseService.ToDbType(dbLink.DbType),
ConnectionString = _dataBaseService.ToConnectionString(dbLink),
InitKeyType = InitKeyType.Attribute,
IsAutoCloseConnection = true
});
_db.ChangeDatabase(dbLink.Id);
var sidx = input.sidx == null ? "id" : input.sidx;
var data = await _db.Queryable()
.WhereIF(!string.IsNullOrEmpty(input.name), p => p.Name.Contains(input.name))
.Select(it=> new TbMbtCharacterListOutput
{
id = it.Id,
name=it.Name,
creatorTime=it.CreatorTime,
lastModifyTime=it.LastModifyTime,
}).MergeTable().OrderBy(sidx+" "+input.sort).ToListAsync();
return data;
}
///
/// 导出MBT人格
///
/// 请求参数
///
[HttpGet("Actions/Export")]
public async Task Export([FromQuery] TbMbtCharacterListQueryInput input)
{
var userInfo = await _userManager.GetUserInfo();
var exportData = new List();
if (input.dataType == 0)
{
var data = Clay.Object(await this.GetList(input));
exportData = data.Solidify>().list;
}
else
{
exportData = await this.GetNoPagingList(input);
}
List paramList = "[{\"value\":\"名称\",\"field\":\"name\"},{\"value\":\"创建时间\",\"field\":\"creatorTime\"},{\"value\":\"修改时间\",\"field\":\"lastModifyTime\"},]".ToList();
ExcelConfig excelconfig = new ExcelConfig();
excelconfig.FileName = "MBT人格.xls";
excelconfig.HeadFont = "微软雅黑";
excelconfig.HeadPoint = 10;
excelconfig.IsAllSizeColumn = true;
excelconfig.ColumnModel = new List();
List selectKeyList = input.selectKey.Split(',').ToList();
foreach (var item in selectKeyList)
{
var isExist = paramList.Find(p => p.field == item);
if (isExist != null)
{
excelconfig.ColumnModel.Add(new ExcelColumnModel() { Column = isExist.field, ExcelColumn = isExist.value });
}
}
var addPath = FileVariable.TemporaryFilePath + excelconfig.FileName;
ExcelExportHelper.Export(exportData, excelconfig, addPath);
var fileName = _userManager.UserId + "|" + addPath + "|xls";
var output = new
{
name = excelconfig.FileName,
url = "/api/File/Download?encryption=" + DESCEncryption.Encrypt(fileName, "NCC")
};
return output;
}
///
/// 批量删除MBT人格
///
/// 主键数组
///
[HttpPost("batchRemove")]
public async Task BatchRemove([FromBody] List ids)
{
var dbLink = await _dbLinkService.GetInfo("218239598550058245");
_db.AddConnection(new ConnectionConfig()
{
ConfigId = dbLink.Id,
DbType = _dataBaseService.ToDbType(dbLink.DbType),
ConnectionString = _dataBaseService.ToConnectionString(dbLink),
InitKeyType = InitKeyType.Attribute,
IsAutoCloseConnection = true
});
_db.ChangeDatabase(dbLink.Id);
var entitys = await _db.Queryable().In(it => it.Id, ids).ToListAsync();
if (entitys.Count > 0)
{
try
{
//开启事务
_db.BeginTran();
//批量删除MBT人格
await _db.Deleteable().In(d => d.Id,ids).ExecuteCommandAsync();
//关闭事务
_db.CommitTran();
}
catch (Exception)
{
//回滚事务
_db.RollbackTran();
throw NCCException.Oh(ErrorCode.COM1002);
}
}
}
///
/// 更新MBT人格
///
/// 主键
/// 参数
///
[HttpPut("{id}")]
public async Task Update(string id, [FromBody] TbMbtCharacterUpInput input)
{
var dbLink = await _dbLinkService.GetInfo("218239598550058245");
_db.AddConnection(new ConnectionConfig()
{
ConfigId = dbLink.Id,
DbType = _dataBaseService.ToDbType(dbLink.DbType),
ConnectionString = _dataBaseService.ToConnectionString(dbLink),
InitKeyType = InitKeyType.Attribute,
IsAutoCloseConnection = true
});
_db.ChangeDatabase(dbLink.Id);
var entity = input.Adapt();
entity.LastModifyTime = DateTime.Now;
var isOk = await _db.Updateable(entity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync();
if (!(isOk > 0)) throw NCCException.Oh(ErrorCode.COM1001);
}
///
/// 删除MBT人格
///
///
[HttpDelete("{id}")]
public async Task Delete(string id)
{
var dbLink = await _dbLinkService.GetInfo("218239598550058245");
_db.AddConnection(new ConnectionConfig()
{
ConfigId = dbLink.Id,
DbType = _dataBaseService.ToDbType(dbLink.DbType),
ConnectionString = _dataBaseService.ToConnectionString(dbLink),
InitKeyType = InitKeyType.Attribute,
IsAutoCloseConnection = true
});
_db.ChangeDatabase(dbLink.Id);
var entity = await _db.Queryable().FirstAsync(p => p.Id == id);
_ = entity ?? throw NCCException.Oh(ErrorCode.COM1005);
var isOk = await _db.Deleteable().Where(d => d.Id == id).ExecuteCommandAsync();
if (!(isOk > 0)) throw NCCException.Oh(ErrorCode.COM1002);
}
}
}