LqMallConfigService.cs 3.9 KB
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
{
    /// <summary>
    /// 会员商城-支付配置(管理端)
    /// </summary>
    [ApiDescriptionSettings(Tag = "会员商城-配置", Name = "LqMallConfig", Order = 305)]
    [Route("api/Extend/LqMallConfig")]
    public class LqMallConfigService : IDynamicApiController, ITransient
    {
        private readonly IUserManager _userManager;
        private readonly ILogger<LqMallConfigService> _logger;
        private readonly ISqlSugarClient _db;
        private readonly IBusinessOperationLogService _log;

        /// <summary>
        /// 构造函数
        /// </summary>
        public LqMallConfigService(
            IUserManager userManager,
            ILogger<LqMallConfigService> logger,
            ISqlSugarClient db,
            IBusinessOperationLogService log)
        {
            _userManager = userManager;
            _logger = logger;
            _db = db;
            _log = log;
        }

        /// <summary>
        /// 获取商城支付配置
        /// </summary>
        /// <returns>支付配置</returns>
        [HttpGet("GetPayConfig")]
        public async Task<dynamic> 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
            };
        }

        /// <summary>
        /// 保存商城支付配置(逐项 upsert)
        /// </summary>
        /// <param name="input">支付配置</param>
        [HttpPost("SavePayConfig")]
        public async Task SavePayConfigAsync([FromBody] MallPayConfigInput input)
        {
            if (input == null) throw NCCException.Oh("参数不能为空");

            var items = new Dictionary<string, string>
            {
                { 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<SysConfigEntity>()
                    .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);
        }
    }
}