using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using NCC.Common.Core.Manager;
using NCC.Dependency;
using NCC.DynamicApiController;
using NCC.Extend.Entitys.Dto.Mall;
using NCC.Extend.Helpers;
using NCC.Extend.Interfaces.BusinessOperationLog;
using NCC.FriendlyException;
using NCC.System.Entitys.System;
using SqlSugar;
using Yitter.IdGenerator;
namespace NCC.Extend
{
///
/// 会员商城-支付配置(管理端)
///
[ApiDescriptionSettings(Tag = "会员商城-配置", Name = "LqMallConfig", Order = 305)]
[Route("api/Extend/LqMallConfig")]
public class LqMallConfigService : IDynamicApiController, ITransient
{
private readonly IUserManager _userManager;
private readonly ILogger _logger;
private readonly ISqlSugarClient _db;
private readonly IBusinessOperationLogService _log;
///
/// 构造函数
///
public LqMallConfigService(
IUserManager userManager,
ILogger logger,
ISqlSugarClient db,
IBusinessOperationLogService log)
{
_userManager = userManager;
_logger = logger;
_db = db;
_log = log;
}
///
/// 获取商城支付配置
///
/// 支付配置
[HttpGet("GetPayConfig")]
public async Task GetPayConfigAsync()
{
var config = await MallPayConfigHelper.GetAsync(_db);
return new
{
mchId = config.MchId,
apiV3Key = config.ApiV3Key,
appId = config.AppId,
appSecret = config.AppSecret,
notifyUrl = config.NotifyUrl,
payEnabled = config.PayEnabledRaw
};
}
///
/// 保存商城支付配置(逐项 upsert)
///
/// 支付配置
[HttpPost("SavePayConfig")]
public async Task SavePayConfigAsync([FromBody] MallPayConfigInput input)
{
if (input == null) throw NCCException.Oh("参数不能为空");
var items = new Dictionary
{
{ MallPayConfigHelper.KeyMchId, input.MchId },
{ MallPayConfigHelper.KeyApiV3Key, input.ApiV3Key },
{ MallPayConfigHelper.KeyAppId, input.AppId },
{ MallPayConfigHelper.KeyAppSecret, input.AppSecret },
{ MallPayConfigHelper.KeyNotifyUrl, input.NotifyUrl },
{ MallPayConfigHelper.KeyPayEnabled, input.PayEnabled }
};
foreach (var kv in items)
{
var existing = await _db.Queryable()
.Where(x => x.Category == MallPayConfigHelper.Category && x.Key == kv.Key)
.FirstAsync();
if (existing == null)
{
await _db.Insertable(new SysConfigEntity
{
Id = YitIdHelper.NextId().ToString(),
Category = MallPayConfigHelper.Category,
Key = kv.Key,
Name = kv.Key,
Value = kv.Value
}).ExecuteCommandAsync();
}
else
{
existing.Value = kv.Value;
await _db.Updateable(existing).UpdateColumns(x => new { x.Value }).ExecuteCommandAsync();
}
}
await _log.WriteAsync("会员商城", "保存支付配置", "mall_config", MallPayConfigHelper.Category,
"保存商城微信支付配置", null);
}
}
}