• 使用:如A表执行完成后,需要把A表中相关字段同步到B表中

  • //保存成功后执行sql语句, 特殊情况需要用到 sqlInsert, 先执行sqlUpdate,如果失败执行sqlInsert , key如果多个使用-链接,如account-to-user

mapper配置

"saveSqlExtand": {
    "account-to-user": {
      "sortId": 1,
      "connName": "ConnBo",
      "sqlUpdate": "update bo_user_account tb_ua,bo_user tb_a set tb_a.user_pwd =tb_ua.user_pwd,tb_a.user_name =tb_ua.user_name,tb_a.nike_name =tb_ua.nike_name where tb_a.user_id = tb_ua.user_id and tb_ua.user_id=@userId",
      "sqlInsert": null
    }
  },

开启代码

            if (sqlOpSave.Data == null) sqlOpSave.Data = new Dictionary<string, object>();
            foreach (var item in sqlColumnPK)
            {
                sqlOpSave.Data[item.Field] = dicPK.ContainsKey(item.Field) ? dicPK[item.Field] : null;
                sqlOpSave.Data[item.Field + "$" + dicPK[item.Field]] = dicPK.ContainsKey(item.Field) ? dicPK[item.Field] : null;
            }
            if (sqlOpSave.Data != null && sqlOpSave.Data.Keys.Count > 0 && sqlOpSave.Result.DoResult && sqlConfig.ResponseFieldTemp != null && sqlConfig.ResponseFieldTemp.SaveSqlExtand == true && sqlConfig.SaveSqlExtand != null && sqlConfig.SaveSqlExtand.Keys.Count() > 0)
            {
                //异步执行入库和更新到其它库操作
                _saveExtendProvider.SaveExtend(pf, sqlConfig, RequestModuleType.Edit, dictionaryUrl, lstDictionary);
            }

  • responseField节点二次sql处理开启
"saveSqlExtand": true,
  • 后端实现代码(如果代码满足不了需求,可以独立实现接口,然后注入服务修改为自定义实现类)
    需要继承:ISaveExtendProvider
    Start.cs: 注入服务 services.AddSingleton<ISaveExtendProvider, SaveExtendProvider>();
using RG3.BO.Abstractions.Entity;
using RG3.BO.Abstractions.Interfaces;
using RG3.DO.Abstractions.Entity;
using RG3.PF.Abstractions.Entity;
using RG3.PF.Abstractions.Enums;
using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Threading.Tasks;
using System.Linq;
using RG3.DO.Abstractions.Interfaces;
using Microsoft.Extensions.Logging;

namespace RG3.BO.Op.Provider
{
    /// <summary>
    ///  保存成功后,把结果进行二次处理
    /// </summary>
    public class SaveExtendProvider : ISaveExtendProvider
    {
        private readonly IDbConnectionProvider _dbConnectionProvider;
        private IDbContext _db;
        private readonly ILogger<SaveExtendProvider> _logger;


        /// <summary>
        /// 注入服务
        /// </summary>
        /// <param name="mapperCollectService"></param>
        /// <param name="accessor"></param>
        public SaveExtendProvider(ILogger<SaveExtendProvider> logger, IDbConnectionProvider dbConnectionProvider, IDbContext db)
        {
            _db = db;
            _dbConnectionProvider = dbConnectionProvider;
            _logger = logger;
        }

        /// <summary>z
        /// 保存结果进行二次处理
        /// </summary>
        /// <param name="pfGloal"></param>
        /// <param name="sqlConfig"></param>
        /// <param name="lstDic"></param>
        /// <returns></returns>
        public async Task SaveExtend(PFGlobalParameter pfGloal, SqlConfig sqlConfig, RequestModuleType rmt, Dictionary<string, object> dictionaryUrl, IEnumerable<Dictionary<string, object>> lstDic)
        {
            string sql = null;
            try
            {
                var lstSSE = sqlConfig.SaveSqlExtand.Values.OrderBy(temp => temp.SortId);
                int result = 0;
                await Task.Run(() =>
                {
                    foreach (var item in lstSSE)
                    {
                        sql = item.SqlUpdate;
                        result = _db.Execute(pfGloal, item.ConnName, item.SqlUpdate, lstDic);
                        if (item.SqlInsert != null && result == 0)
                        {
                            sql = item.SqlInsert;
                            result = _db.Execute(pfGloal, item.ConnName, item.SqlInsert, lstDic);
                        }
                    }
                });
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, $"{pfGloal.OwnerId}/{pfGloal.SysId}/{pfGloal.ModuleKey}/{pfGloal.ReponseField}/【{sql}】", lstDic);
            }
        }
    }
}

文档更新时间: 2022-08-20 14:01   作者:admin