You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

219 lines
9.0 KiB
C#

using Myshipping.Core;
using Furion.DependencyInjection;
using Furion.DynamicApiController;
using Mapster;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using System.Linq;
using System.Threading.Tasks;
using Myshipping.Core.Entity;
using Microsoft.Extensions.Logging;
using Furion.FriendlyException;
using System;
namespace Myshipping.Core.Service
{
/// <summary>
/// 网站账号维护服务
/// </summary>
[ApiDescriptionSettings(Name = "DjyWebsiteAccountConfig", Order = 1)]
public class DjyWebsiteAccountConfigService : IDjyWebsiteAccountConfigService, IDynamicApiController, ITransient
{
private readonly SqlSugarRepository<DjyWebsiteAccountConfig> _rep;
private readonly SqlSugarRepository<SysUser> _sysUserRep; // 用户表仓储
private readonly ILogger<DjyWebsiteAccountConfig> _logger;
public DjyWebsiteAccountConfigService(SqlSugarRepository<DjyWebsiteAccountConfig> rep, SqlSugarRepository<SysUser> sysUserRep, ILogger<DjyWebsiteAccountConfig> logger)
{
_rep = rep;
_sysUserRep = sysUserRep;
_logger = logger;
}
/// <summary>
/// 分页查询网站账号维护
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpGet("/DjyWebsiteAccountConfig/page")]
public async Task<dynamic> Page([FromQuery] QueryDjyWebsiteAccountConfigInput input)
{
var entities = await _rep.AsQueryable()
.Where(m => !m.IsTenant)
.Where(m => m.CreatedUserId == UserManager.UserId)
.WhereIF(!string.IsNullOrWhiteSpace(input.TypeCode), u => u.TypeCode == input.TypeCode)
.WhereIF(!string.IsNullOrWhiteSpace(input.Account), u => u.Account.Contains(input.Account.Trim()))
.ToPagedListAsync(input.PageNo, input.PageSize);
return entities.XnPagedResult();
}
/// <summary>
/// 增加网站账号维护(同一用户、同类型账号不允许重复)
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost("/DjyWebsiteAccountConfig/add")]
public async Task<long> Add(AddDjyWebsiteAccountConfigInput input)
{
var cc = _rep.Count(x => x.TypeCode == input.TypeCode && x.CreatedUserId == UserManager.UserId && !x.IsTenant);
if (cc > 0)
{
throw Oops.Bah("已存在同类型账号");
}
var entity = input.Adapt<DjyWebsiteAccountConfig>();
entity.IsTenant = false;
await _rep.InsertAsync(entity);
return entity.Id;
}
/// <summary>
/// 更新网站账号维护
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost("/DjyWebsiteAccountConfig/edit")]
public async Task<long> Update(UpdateDjyWebsiteAccountConfigInput input)
{
var cc = _rep.Count(x => x.TypeCode == input.TypeCode && x.CreatedUserId == UserManager.UserId && !x.IsTenant && x.Id != input.Id);
if (cc > 0)
{
throw Oops.Bah("已存在同类型账号");
}
var entity = input.Adapt<DjyWebsiteAccountConfig>();
entity.IsTenant = false;
await _rep.AsUpdateable(entity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync();
return entity.Id;
}
/// <summary>
/// 删除网站账号维护
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost("/DjyWebsiteAccountConfig/delete")]
public async Task Delete(GetDjyWebsiteAccountConfigInput input)
{
var entity = await _rep.FirstOrDefaultAsync(u => u.Id == input.Id);
await _rep.DeleteAsync(entity);
}
#region 公司网站账号维护
/// <summary>
/// 分页查询网站账号维护(租户公司)
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpGet("/DjyWebsiteAccountConfig/PageTenant")]
public async Task<dynamic> PageTenant([FromQuery] QueryDjyWebsiteAccountConfigInput input)
{
var entities = await _rep.AsQueryable().Filter(null, true)
.Where(m => m.TenantId == UserManager.TENANT_ID)
.Where(m => m.IsTenant)
.Where(m => !m.IsDeleted)
.WhereIF(!string.IsNullOrWhiteSpace(input.TypeCode), u => u.TypeCode == input.TypeCode)
.WhereIF(!string.IsNullOrWhiteSpace(input.Account), u => u.Account.Contains(input.Account.Trim()))
.ToPagedListAsync(input.PageNo, input.PageSize);
return entities.XnPagedResult();
}
/// <summary>
/// 增加网站账号维护(同一租户、同类型账号不允许重复)
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost("/DjyWebsiteAccountConfig/AddTenant")]
public async Task<long> AddTenant(AddDjyWebsiteAccountConfigInput input)
{
var tenantId = UserManager.TENANT_ID;
var cc = _rep.Count(x => x.TypeCode == input.TypeCode && x.TenantId == tenantId && x.IsTenant);
if (cc > 0)
{
throw Oops.Bah("已存在同类型账号");
}
var entity = input.Adapt<DjyWebsiteAccountConfig>();
entity.IsTenant = true;
await _rep.InsertAsync(entity);
return entity.Id;
}
/// <summary>
/// 更新网站账号维护(租户公司)
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost("/DjyWebsiteAccountConfig/EditTenant")]
public async Task<long> UpdateTenant(UpdateDjyWebsiteAccountConfigInput input)
{
var tenantId = UserManager.TENANT_ID;
var cc = _rep.Count(x => x.TypeCode == input.TypeCode && x.TenantId == tenantId && x.IsTenant && x.Id != input.Id);
if (cc > 0)
{
throw Oops.Bah("已存在同类型账号");
}
var entity = input.Adapt<DjyWebsiteAccountConfig>();
entity.IsTenant = true;
await _rep.AsUpdateable(entity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync();
return entity.Id;
}
/// <summary>
/// 删除网站账号维护(租户公司)
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost("/DjyWebsiteAccountConfig/DeleteTenant")]
public async Task DeleteTenant(GetDjyWebsiteAccountConfigInput input)
{
var entity = await _rep.FirstOrDefaultAsync(u => u.Id == input.Id);
await _rep.DeleteAsync(entity);
}
/// <summary>
/// 获取接口Key 个人或公司
/// </summary>
/// <param name="TypeCode"></param>
/// <param name="UserId"></param>
/// <returns></returns>
public async Task<DjyWebsiteAccountConfig> GetAccountConfig(string TypeCode, long UserId)
{
if (string.IsNullOrWhiteSpace(TypeCode) || UserId == 0)
{
throw Oops.Bah("请传入正确参数!");
}
DjyWebsiteAccountConfig accountConfig = new DjyWebsiteAccountConfig();
accountConfig = await _rep.FirstOrDefaultAsync(x => x.TypeCode == TypeCode && x.CreatedUserId == UserId);
if (accountConfig == null)
{
accountConfig = await _rep.AsQueryable().InnerJoin<SysUser>((d, t) => d.TenantId == t.TenantId).Where((d, t) => d.TypeCode == TypeCode && t.Id == UserId && d.IsTenant == true).FirstAsync();
}
return accountConfig;
}
///// <summary>
///// 获取个人或公司网站账户配置
///// </summary>
///// <param name="typeCode">账户类型代码</param>
///// <param name="userId">用户ID</param>
///// <param name="tendId">租户ID</param>
///// <returns>返回账户配置</returns>
//private async Task<DjyWebsiteAccountConfig> GetAccountConfig(string typeCode, long userId, long tendId)
//{
// DjyWebsiteAccountConfig accountConfig = new DjyWebsiteAccountConfig();
// accountConfig = await _djyWebsiteAccountConfigRepository.EntityContext.CopyNew().Queryable<DjyWebsiteAccountConfig>()
// .FirstAsync(x => x.TypeCode == typeCode && x.CreatedUserId == userId);
// if (accountConfig == null)
// {
// accountConfig = await _djyWebsiteAccountConfigRepository.EntityContext.CopyNew().Queryable<DjyWebsiteAccountConfig>()
// .FirstAsync(x => x.TypeCode == typeCode && x.TenantId == tendId && x.IsTenant == true);
// }
// return accountConfig;
//}
#endregion
}
}