using NCC.Common.Core.Manager;
using NCC.Common.Enum;
using NCC.Common.Extension;
using NCC.Common.Filter;
using NCC.Dependency;
using NCC.DynamicApiController;
using NCC.FriendlyException;
using NCC.Order.Interfaces.BaseAmountHistory;
using Mapster;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using NCC.Order.Entitys;
using NCC.Order.Entitys.Dto.BaseAmountHistory;
using Yitter.IdGenerator;
using NCC.Common.Helper;
using NCC.JsonSerialization;
using NCC.System.Interfaces.System;
using NCC.Food.Entitys.Enum;
using NCC.Order.Interfaces.MarketingRules;
using NCC.Order.Interfaces.BaseOrder;
using Serilog;
using Antis.Pay.Core.Interface;
using NCC.System.Entitys.Permission;
using NCC.Core.Pay.WeChatPay.V2.Request;
using NCC.Core.Pay.WeChatPay.V2;
using NCC.Core.Pay.WeChatPay;
using Microsoft.Extensions.Options;
using NPOI.POIFS.Properties;
namespace NCC.Order.BaseAmountHistory
{
///
/// 金额流水记录服务
///
[ApiDescriptionSettings(Tag = "订单流水", Name = "BaseAmountHistory", Order = 200)]
[Route("api/Order/[controller]")]
public class BaseAmountHistoryService : IBaseAmountHistoryService, IDynamicApiController, ITransient
{
private readonly ISqlSugarRepository _baseAmountHistoryRepository;
private readonly IDbLinkService _dbLinkService;
private readonly IDataBaseService _dataBaseService;
private readonly SqlSugarScope _db;
private readonly IUserManager _userManager;
private readonly IWePay _wepay;
private readonly IWeChatPayClient _client;
private readonly IOptions _optionsAccessor;
///
/// 初始化一个类型的新实例
///
public BaseAmountHistoryService(
ISqlSugarRepository baseAmountHistoryRepository,
IDbLinkService dbLinkService,
IDataBaseService dataBaseService,
IUserManager userManager,
IWePay wepay,
IWeChatPayClient client,
IOptions optionsAccessor)
{
_baseAmountHistoryRepository = baseAmountHistoryRepository;
_db = _baseAmountHistoryRepository.Context;
_dbLinkService = dbLinkService;
_dataBaseService = dataBaseService;
_userManager = userManager;
_wepay = wepay;
_client = client;
_optionsAccessor = optionsAccessor;
}
///
/// 获取金额流水记录
///
/// 参数
///
[HttpGet("{id}")]
public async Task GetInfo(string id)
{
var dbLink = await _dbLinkService.GetInfo("218239598550058245");
_db.AddConnection(new ConnectionConfig()
{
ConfigId = dbLink.Id,
DbType = _dataBaseService.ToDbType(dbLink.DbType),
ConnectionString = _dataBaseService.ToConnectionString(dbLink),
InitKeyType = InitKeyType.Attribute,
IsAutoCloseConnection = true
});
_db.ChangeDatabase(dbLink.Id);
var entity = await _db.Queryable().FirstAsync(p => p.Id == id);
var output = entity.Adapt();
return output;
}
///
/// 获取金额流水记录列表
///
/// 请求参数
///
[HttpGet("")]
public async Task GetList([FromQuery] BaseAmountHistoryListQueryInput input)
{
var dbLink = await _dbLinkService.GetInfo("218239598550058245");
_db.AddConnection(new ConnectionConfig()
{
ConfigId = dbLink.Id,
DbType = _dataBaseService.ToDbType(dbLink.DbType),
ConnectionString = _dataBaseService.ToConnectionString(dbLink),
InitKeyType = InitKeyType.Attribute,
IsAutoCloseConnection = true
});
_db.ChangeDatabase(dbLink.Id);
var sidx = input.sidx == null ? "id" : input.sidx;
List queryCreatorTime = input.creatorTime != null ? input.creatorTime.Split(',').ToObeject>() : null;
DateTime? startCreatorTime = queryCreatorTime != null ? Ext.GetDateTime(queryCreatorTime.First()) : null;
DateTime? endCreatorTime = queryCreatorTime != null ? Ext.GetDateTime(queryCreatorTime.Last()) : null;
var data = await _db.Queryable()
.WhereIF(!string.IsNullOrEmpty(input.money), p => p.Money.Equals(input.money))
.WhereIF(!string.IsNullOrEmpty(input.productId), p => p.ProductId.Contains(input.productId))
.WhereIF(!string.IsNullOrEmpty(input.userId), p => p.UserId.Contains(input.userId))
.WhereIF(!string.IsNullOrEmpty(input.source), p => p.Source.Contains(input.source))
.WhereIF(!string.IsNullOrEmpty(input.status), p => p.Status.Equals(input.status))
.WhereIF(queryCreatorTime != null, p => p.CreatorTime >= new DateTime(startCreatorTime.ToDate().Year, startCreatorTime.ToDate().Month, startCreatorTime.ToDate().Day, 0, 0, 0))
.WhereIF(queryCreatorTime != null, p => p.CreatorTime <= new DateTime(endCreatorTime.ToDate().Year, endCreatorTime.ToDate().Month, endCreatorTime.ToDate().Day, 23, 59, 59))
//.WhereIF(input.myChildren != null && input.myChildren.Value, p =>
//p.Source == SqlFunc.ToString(MoneyHistoryType.分销提成.ToString())
//&& SqlFunc.Subqueryable().Where(o => o.ParentId == _userManager.UserId && o.Id == p.PayUserId).Any())
.Select(p => new BaseAmountHistoryListOutput
{
id = p.Id,
money = p.Money,
productId = p.ProductId,
userId = p.UserId,
source = p.Source,
status = p.Status,
creatorTime = p.CreatorTime,
payUserId = p.PayUserId,
orderId = p.OrderId,
orderMoney = SqlFunc.Subqueryable().Where(od => od.Id == p.OrderId).Select(o => o.Money),
payUserName = SqlFunc.Subqueryable().Where(o => o.Id == p.PayUserId).Select(o => o.RealName),
userName = SqlFunc.Subqueryable().Where(o => o.Id == p.UserId).Select(o => o.RealName)
}).MergeTable().OrderBy(sidx + " " + input.sort).ToPagedListAsync(input.currentPage, input.pageSize);
return PageResult.SqlSugarPageResult(data);
}
///
/// 新建金额流水记录
///
/// 参数
///
[HttpPost("")]
public async Task Create([FromBody] BaseAmountHistoryCrInput input)
{
var dbLink = await _dbLinkService.GetInfo("218239598550058245");
_db.AddConnection(new ConnectionConfig()
{
ConfigId = dbLink.Id,
DbType = _dataBaseService.ToDbType(dbLink.DbType),
ConnectionString = _dataBaseService.ToConnectionString(dbLink),
InitKeyType = InitKeyType.Attribute,
IsAutoCloseConnection = true
});
_db.ChangeDatabase(dbLink.Id);
var userInfo = await _userManager.GetUserInfo();
var entity = input.Adapt();
entity.Id = YitIdHelper.NextId().ToString();
entity.CreatorTime = DateTime.Now;
var isOk = await _db.Insertable(entity).IgnoreColumns(ignoreNullColumn: true).ExecuteCommandAsync();
if (!(isOk > 0)) throw NCCException.Oh(ErrorCode.COM1000);
}
///
/// 更新金额流水记录
///
/// 主键
/// 参数
///
[HttpPut("{id}")]
[NonAction]
public async Task Update(string id, [FromBody] BaseAmountHistoryUpInput input)
{
var dbLink = await _dbLinkService.GetInfo("218239598550058245");
_db.AddConnection(new ConnectionConfig()
{
ConfigId = dbLink.Id,
DbType = _dataBaseService.ToDbType(dbLink.DbType),
ConnectionString = _dataBaseService.ToConnectionString(dbLink),
InitKeyType = InitKeyType.Attribute,
IsAutoCloseConnection = true
});
_db.ChangeDatabase(dbLink.Id);
var entity = input.Adapt();
var isOk = await _db.Updateable(entity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync();
if (!(isOk > 0)) throw NCCException.Oh(ErrorCode.COM1001);
}
///
/// 删除金额流水记录
///
///
[HttpDelete("{id}")]
[NonAction]
public async Task Delete(string id)
{
var dbLink = await _dbLinkService.GetInfo("218239598550058245");
_db.AddConnection(new ConnectionConfig()
{
ConfigId = dbLink.Id,
DbType = _dataBaseService.ToDbType(dbLink.DbType),
ConnectionString = _dataBaseService.ToConnectionString(dbLink),
InitKeyType = InitKeyType.Attribute,
IsAutoCloseConnection = true
});
_db.ChangeDatabase(dbLink.Id);
var entity = await _db.Queryable().FirstAsync(p => p.Id == id);
_ = entity ?? throw NCCException.Oh(ErrorCode.COM1005);
var isOk = await _db.Deleteable().Where(d => d.Id == id).ExecuteCommandAsync();
if (!(isOk > 0)) throw NCCException.Oh(ErrorCode.COM1002);
}
///
/// 获取当前用户所有余额
///
///
[HttpGet("userAmount")]
public decimal GetUserAmount()
{
string UserId = _userManager.UserId;
return _db.Queryable()
.Where(u =>
u.UserId == UserId
&& u.Status == PriceStatus.可用.GetHashCode()
&& u.Source != MoneyHistoryType.消费.ToString()
&& u.Source != MoneyHistoryType.退款.ToString()
).Sum(u => u.Money);
}
///
/// 获取当前用户所有剩余匹配次数
///
///
[HttpGet("GetMatchOverCount")]
public int GetMatchOverCount()
{
try
{
string UserId = _userManager.UserId;
return decimal.ToInt32(_db.Queryable()
.Where(u =>
u.UserId == UserId
&& u.Status == PriceStatus.可用.GetHashCode()
&& u.Source == MoneyHistoryType.匹配次数.ToString()
).Sum(u => u.Money));
}
catch (Exception)
{
return 0;
}
}
[HttpPost("transtest")]
public async Task