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
{
///
/// 网站账号维护服务
///
[ApiDescriptionSettings(Name = "DjyWebsiteAccountConfig", Order = 1)]
public class DjyWebsiteAccountConfigService : IDjyWebsiteAccountConfigService, IDynamicApiController, ITransient
{
private readonly SqlSugarRepository _rep;
private readonly SqlSugarRepository _sysUserRep; // 用户表仓储
private readonly ILogger _logger;
public DjyWebsiteAccountConfigService(SqlSugarRepository rep, SqlSugarRepository sysUserRep, ILogger logger)
{
_rep = rep;
_sysUserRep = sysUserRep;
_logger = logger;
}
///
/// 分页查询网站账号维护
///
///
///
[HttpGet("/DjyWebsiteAccountConfig/page")]
public async Task 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();
}
///
/// 增加网站账号维护(同一用户、同类型账号不允许重复)
///
///
///
[HttpPost("/DjyWebsiteAccountConfig/add")]
public async Task 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();
entity.IsTenant = false;
await _rep.InsertAsync(entity);
return entity.Id;
}
///
/// 更新网站账号维护
///
///
///
[HttpPost("/DjyWebsiteAccountConfig/edit")]
public async Task 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();
entity.IsTenant = false;
await _rep.AsUpdateable(entity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync();
return entity.Id;
}
///
/// 删除网站账号维护
///
///
///
[HttpPost("/DjyWebsiteAccountConfig/delete")]
public async Task Delete(GetDjyWebsiteAccountConfigInput input)
{
var entity = await _rep.FirstOrDefaultAsync(u => u.Id == input.Id);
await _rep.DeleteAsync(entity);
}
#region 公司网站账号维护
///
/// 分页查询网站账号维护(租户公司)
///
///
///
[HttpGet("/DjyWebsiteAccountConfig/PageTenant")]
public async Task 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();
}
///
/// 增加网站账号维护(同一租户、同类型账号不允许重复)
///
///
///
[HttpPost("/DjyWebsiteAccountConfig/AddTenant")]
public async Task 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();
entity.IsTenant = true;
await _rep.InsertAsync(entity);
return entity.Id;
}
///
/// 更新网站账号维护(租户公司)
///
///
///
[HttpPost("/DjyWebsiteAccountConfig/EditTenant")]
public async Task 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();
entity.IsTenant = true;
await _rep.AsUpdateable(entity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync();
return entity.Id;
}
///
/// 删除网站账号维护(租户公司)
///
///
///
[HttpPost("/DjyWebsiteAccountConfig/DeleteTenant")]
public async Task DeleteTenant(GetDjyWebsiteAccountConfigInput input)
{
var entity = await _rep.FirstOrDefaultAsync(u => u.Id == input.Id);
await _rep.DeleteAsync(entity);
}
///
/// 获取接口Key 个人或公司
///
///
///
///
public async Task GetAccountConfig(string TypeCode, long UserId)
{
if (string.IsNullOrWhiteSpace(TypeCode) || UserId == 0)
{
throw Oops.Bah("请传入正确参数!");
}
DjyWebsiteAccountConfig accountConfig = new DjyWebsiteAccountConfig();
accountConfig = await _rep.AsQueryable().Filter(null,true).FirstAsync(x => x.TypeCode == TypeCode && x.CreatedUserId == UserId && x.IsTenant == false);
if (accountConfig == null)
{
accountConfig = await _rep.AsQueryable().Filter(null, true).InnerJoin((d, t) => d.TenantId == t.TenantId).Where((d, t) => d.TypeCode == TypeCode && t.Id == UserId && d.IsTenant == true).FirstAsync();
}
return accountConfig;
}
/////
///// 获取个人或公司网站账户配置
/////
///// 账户类型代码
///// 用户ID
///// 租户ID
///// 返回账户配置
//private async Task GetAccountConfig(string typeCode, long userId, long tendId)
//{
// DjyWebsiteAccountConfig accountConfig = new DjyWebsiteAccountConfig();
// accountConfig = await _djyWebsiteAccountConfigRepository.EntityContext.CopyNew().Queryable()
// .FirstAsync(x => x.TypeCode == typeCode && x.CreatedUserId == userId);
// if (accountConfig == null)
// {
// accountConfig = await _djyWebsiteAccountConfigRepository.EntityContext.CopyNew().Queryable()
// .FirstAsync(x => x.TypeCode == typeCode && x.TenantId == tendId && x.IsTenant == true);
// }
// return accountConfig;
//}
#endregion
///
/// 获取接口Key 个人或公司
///
/// 账户类型代码
/// 用户ID
/// 租户ID
///
public async Task GetAccountConfigByTenantId(string typeCode, long userId, long tenantId)
{
if (string.IsNullOrWhiteSpace(typeCode) || userId == 0 || tenantId == 0)
{
throw Oops.Bah("请传入正确参数!");
}
DjyWebsiteAccountConfig accountConfig = new DjyWebsiteAccountConfig();
accountConfig = await _rep.AsQueryable().Filter(null,true).FirstAsync(x => x.TypeCode == typeCode && x.TenantId == tenantId && x.CreatedUserId == userId && x.IsTenant == false);
if (accountConfig == null)
{
accountConfig = await _rep.AsQueryable().Filter(null, true).FirstAsync(x => x.TypeCode == typeCode && x.TenantId == tenantId && x.IsTenant == true);
}
return accountConfig;
}
}
}