using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; using NCC.ClayObject; using NCC.Common.Configuration; using NCC.Common.Core.Manager; using NCC.Common.Filter; using NCC.Common.Helper; using NCC.Common.Model.NPOI; using NCC.DataEncryption; using NCC.Dependency; using NCC.DynamicApiController; using NCC.Extend.Entitys.Dto.LqCardTransferLog; using NCC.Extend.Entitys.Enum; using NCC.Extend.Entitys.lq_card_transfer_log; using NCC.Extend.Entitys.lq_hytk_hytk; using NCC.Extend.Entitys.lq_khxx; using NCC.Extend.Interfaces; using NCC.Extend.Interfaces.BusinessOperationLog; using NCC.FriendlyException; using NCC.JsonSerialization; using NCC.System.Entitys.Permission; using SqlSugar; using Yitter.IdGenerator; namespace NCC.Extend.LqCardTransferLog { /// /// 转卡日志服务 /// [ApiDescriptionSettings(Tag = "绿纤转卡日志服务", Name = "LqCardTransferLog", Order = 200)] [Route("api/Extend/[controller]")] public class LqCardTransferLogService : IDynamicApiController, ILqCardTransferLogService, ITransient { private readonly ISqlSugarClient _db; private readonly IUserManager _userManager; private readonly ILogger _logger; private readonly IBusinessOperationLogService _businessOperationLogService; private const string BizLogModuleTransferCard = "会员转卡"; public LqCardTransferLogService( ISqlSugarClient db, IUserManager userManager, ILogger logger, IBusinessOperationLogService businessOperationLogService) { _db = db; _userManager = userManager; _logger = logger; _businessOperationLogService = businessOperationLogService; } #region 添加转卡日志 /// /// 添加转卡日志 /// /// 转卡日志创建输入参数 /// 创建结果 [HttpPost("add")] public async Task AddTransferLog([FromBody] LqCardTransferLogCrInput input) { try { if (input == null) { throw NCCException.Oh("输入参数不能为空"); } var entity = new LqCardTransferLogEntity { Id = YitIdHelper.NextId().ToString(), RefundCardId = input.refundCardId, BillingCardId = input.billingCardId, TransferFromMemberId = input.transferFromMemberId, TransferToMemberId = input.transferToMemberId, TransferAmount = input.transferAmount, TransferTime = input.transferTime, OperatorId = input.operatorId, Remarks = input.remarks ?? "", CreateTime = DateTime.Now, IsEffective = 1 }; var result = await _db.Insertable(entity).ExecuteCommandAsync(); await TryWriteBizLogAsync( "新增转卡", "lq_card_transfer_log", entity.Id, $"新增转卡记录 {entity.Id},转出 {input.transferFromMemberId} → 转入 {input.transferToMemberId},金额 {input.transferAmount}", Newtonsoft.Json.JsonConvert.SerializeObject(new { after = new { entity.Id, entity.RefundCardId, entity.BillingCardId, entity.TransferFromMemberId, entity.TransferToMemberId, entity.TransferAmount, entity.TransferTime, entity.Remarks } })); return new { Success = true, Message = "转卡日志创建成功", Data = new { Id = entity.Id } }; } catch (Exception ex) { throw NCCException.Oh($"创建转卡日志失败: {ex.Message}"); } } #endregion #region 获取转卡日志列表 /// /// 获取转卡日志列表 /// /// 查询输入参数 /// 转卡日志列表 [HttpGet("")] public async Task GetList([FromQuery] LqCardTransferLogListQueryInput input) { try { var result = await BuildTransferLogQuery(input) .OrderBy(x => x.transferTime, OrderByType.Desc) .ToPagedListAsync(input.currentPage, input.pageSize); return PageResult.SqlSugarPageResult(result); } catch (Exception ex) { throw NCCException.Oh($"获取转卡日志列表失败: {ex.Message}"); } } /// /// 获取转卡日志无分页列表(导出用) /// [NonAction] public async Task> GetNoPagingList([FromQuery] LqCardTransferLogListQueryInput input) { return await BuildTransferLogQuery(input) .OrderBy(x => x.transferTime, OrderByType.Desc) .ToListAsync(); } /// /// 导出转卡记录(固定列,忽略 selectKey) /// /// /// GET /api/Extend/LqCardTransferLog/Actions/Export?dataType=1 /// dataType:0 当前页,1 全部。 /// /// 与列表一致的筛选参数 /// Excel 下载信息(name、url) [HttpGet("Actions/Export")] public async Task Export([FromQuery] LqCardTransferLogListQueryInput input) { var exportData = new List(); if (input.dataType == 0) { var data = Clay.Object(await GetList(input)); exportData = data.Solidify>().list; } else { exportData = await GetNoPagingList(input); } foreach (var row in exportData) { row.isEffectiveText = row.isEffective == StatusEnum.有效.GetHashCode() ? "正常" : "作废"; } List paramList = "[{\"value\":\"转卡ID\",\"field\":\"id\"},{\"value\":\"转出会员名称\",\"field\":\"transferFromMemberName\"},{\"value\":\"转入会员名称\",\"field\":\"transferToMemberName\"},{\"value\":\"转卡金额\",\"field\":\"transferAmount\"},{\"value\":\"转卡时间\",\"field\":\"transferTime\"},{\"value\":\"操作员\",\"field\":\"operatorName\"},{\"value\":\"退卡单号\",\"field\":\"refundCardId\"},{\"value\":\"开单单号\",\"field\":\"billingCardId\"},{\"value\":\"是否有效\",\"field\":\"isEffectiveText\"},{\"value\":\"备注\",\"field\":\"remarks\"}]" .ToList(); var excelconfig = new ExcelConfig { FileName = "会员转卡.xls", HeadFont = "微软雅黑", HeadPoint = 10, IsAllSizeColumn = true, ColumnModel = new List() }; foreach (var param in paramList) { excelconfig.ColumnModel.Add(new ExcelColumnModel { Column = param.field, ExcelColumn = param.value }); } var addPath = FileVariable.TemporaryFilePath + excelconfig.FileName; ExcelExportHelper.Export(exportData, excelconfig, addPath); var fileName = _userManager.UserId + "|" + addPath + "|xls"; await TryWriteBizLogAsync( "导出转卡记录", "lq_card_transfer_log", null, $"导出转卡记录,共 {exportData.Count} 条", new { filters = new { input.transferFromMemberId, input.transferToMemberId, input.operatorId, input.storeId, input.isEffective, input.dataType }, rowCount = exportData.Count, fileName = excelconfig.FileName }.Serialize()); return new { name = excelconfig.FileName, url = "/api/File/Download?encryption=" + DESCEncryption.Encrypt(fileName, "NCC") }; } #endregion private ISugarQueryable BuildTransferLogQuery(LqCardTransferLogListQueryInput input) { return _db.Queryable() .WhereIF(input.isEffective != 0, x => x.IsEffective == input.isEffective) .WhereIF(!string.IsNullOrEmpty(input.transferFromMemberId), x => x.TransferFromMemberId == input.transferFromMemberId) .WhereIF(!string.IsNullOrEmpty(input.transferToMemberId), x => x.TransferToMemberId == input.transferToMemberId) .WhereIF(!string.IsNullOrEmpty(input.operatorId), x => x.OperatorId == input.operatorId) .WhereIF(input.transferStartTime.HasValue, x => x.TransferTime >= input.transferStartTime.Value) .WhereIF(input.transferEndTime.HasValue, x => x.TransferTime <= input.transferEndTime.Value) .WhereIF(input.minTransferAmount.HasValue, x => x.TransferAmount >= input.minTransferAmount.Value) .WhereIF(input.maxTransferAmount.HasValue, x => x.TransferAmount <= input.maxTransferAmount.Value) .WhereIF(!string.IsNullOrEmpty(input.keyword), x => x.Id.Contains(input.keyword) || x.RefundCardId.Contains(input.keyword) || x.BillingCardId.Contains(input.keyword) || x.Remarks.Contains(input.keyword) || SqlFunc.Subqueryable() .Where(m => m.Id == x.TransferFromMemberId && m.Khmc.Contains(input.keyword)).Any() || SqlFunc.Subqueryable() .Where(m => m.Id == x.TransferToMemberId && m.Khmc.Contains(input.keyword)).Any()) .WhereIF(!string.IsNullOrEmpty(input.storeId), x => SqlFunc.Subqueryable() .Where(r => r.Id == x.RefundCardId && r.Md == input.storeId).Any()) .Select(x => new LqCardTransferLogListOutput { id = x.Id, refundCardId = x.RefundCardId, billingCardId = x.BillingCardId, transferFromMemberId = x.TransferFromMemberId, transferFromMemberName = SqlFunc.Subqueryable() .Where(m => m.Id == x.TransferFromMemberId).Select(m => m.Khmc), transferToMemberId = x.TransferToMemberId, transferToMemberName = SqlFunc.Subqueryable() .Where(m => m.Id == x.TransferToMemberId).Select(m => m.Khmc), transferAmount = x.TransferAmount, transferTime = x.TransferTime, operatorId = x.OperatorId, operatorName = SqlFunc.Subqueryable() .Where(u => u.Id == x.OperatorId).Select(u => u.RealName), remarks = x.Remarks, createTime = x.CreateTime, updateTime = x.UpdateTime, isEffective = x.IsEffective }); } private async Task TryWriteBizLogAsync( string actionCode, string bizType, string bizId, string summary, string detailJson) { try { await _businessOperationLogService.WriteAsync( BizLogModuleTransferCard, actionCode, bizType, bizId, summary, detailJson); } catch (Exception ex) { _logger.LogWarning(ex, "写入会员转卡业务操作日志失败 action={Action}", actionCode); } } } }