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.Extend.Interfaces.UavRefundApplication;
using Mapster;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using NCC.Extend.Entitys;
using NCC.Extend.Entitys.Dto.UavRefundApplication;
using Yitter.IdGenerator;
using NCC.Common.Helper;
using NCC.JsonSerialization;
using NCC.Extend.Entitys.Enums;
namespace NCC.Extend.UavRefundApplication
{
///
/// 退款申请表服务
///
[ApiDescriptionSettings(Tag = "退款申请表服务", Name = "UavRefundApplication", Order = 200)]
[Route("api/Extend/[controller]")]
public class UavRefundApplicationService : IUavRefundApplicationService, IDynamicApiController, ITransient
{
private readonly ISqlSugarRepository _uavRefundApplicationRepository;
private readonly SqlSugarScope _db;
private readonly IUserManager _userManager;
///
/// 初始化一个类型的新实例
///
public UavRefundApplicationService(
ISqlSugarRepository uavRefundApplicationRepository,
IUserManager userManager)
{
_uavRefundApplicationRepository = uavRefundApplicationRepository;
_db = _uavRefundApplicationRepository.Context;
_userManager = userManager;
}
#region 获取退款申请表
///
/// 获取退款申请表
///
/// 参数
///
[HttpGet("{id}")]
public async Task GetInfo(string id)
{
var entity = await _db.Queryable().FirstAsync(p => p.Id == id);
var output = entity.Adapt();
return output;
}
#endregion
#region 获取退款申请表列表
///
/// 获取退款申请表列表
///
/// 请求参数
///
[HttpGet("")]
public async Task GetList([FromQuery] UavRefundApplicationListQueryInput input)
{
var sidx = input.sidx == null ? "id" : input.sidx;
var data = await _db.Queryable()
.WhereIF(!string.IsNullOrEmpty(input.orderNo), p => p.OrderNo.Contains(input.orderNo))
.WhereIF(!string.IsNullOrEmpty(input.userId), p => p.UserId.Contains(input.userId))
.WhereIF(!string.IsNullOrEmpty(input.userPhone), p => p.UserPhone.Contains(input.userPhone))
.Select(it => new UavRefundApplicationListOutput
{
id = it.Id,
applyTime = it.ApplyTime,
orderNo = it.OrderNo,
applyAmount = it.ApplyAmount,
userId = it.UserId,
userPhone = it.UserPhone,
refundStatus = it.RefundStatus,
refundReason = it.RefundReason,
remark = it.Remark,
handleTime = it.HandleTime,
handleUser = it.HandleUser,
handleResult = it.HandleResult
}).MergeTable().OrderBy(sidx + " " + input.sort).ToPagedListAsync(input.currentPage, input.pageSize);
return PageResult.SqlSugarPageResult(data);
}
#endregion
#region 新建退款申请表
///
/// 新建退款申请表
///
/// 参数
///
[HttpPost("")]
public async Task Create([FromBody] UavRefundApplicationCrInput input)
{
var userInfo = await _userManager.GetUserInfo();
var entity = input.Adapt();
entity.Id = YitIdHelper.NextId().ToString();
var isOk = await _db.Insertable(entity).IgnoreColumns(ignoreNullColumn: true).ExecuteCommandAsync();
if (!(isOk > 0)) throw NCCException.Oh(ErrorCode.COM1000);
}
#endregion
#region 更新退款申请表
///
/// 更新退款申请表
///
/// 主键
/// 参数
///
[HttpPut("{id}")]
public async Task Update(string id, [FromBody] UavRefundApplicationUpInput input)
{
var entity = input.Adapt();
var isOk = await _db.Updateable(entity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync();
if (!(isOk > 0)) throw NCCException.Oh(ErrorCode.COM1001);
}
#endregion
#region 删除退款申请表
///
/// 删除退款申请表
///
///
[HttpDelete("{id}")]
public async Task Delete(string 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);
}
#endregion
#region 支付宝小程序用户申请退款
///
/// 支付宝小程序用户申请退款
///
/// 退款申请参数
///
[HttpPost("ApplyRefund")]
public async Task ApplyRefund([FromBody] UavRefundApplicationCrInputByUser input)
{
try
{
//判断是否申请过退款
var refundApplication = await _db.Queryable().Where(p => p.OrderNo == input.OrderNo).FirstAsync();
if (refundApplication != null)
{
if (refundApplication.RefundStatus == RefundStatusEnum.待处理.GetHashCode() || refundApplication.RefundStatus == RefundStatusEnum.已同意.GetHashCode() || refundApplication.RefundStatus == RefundStatusEnum.已退款.GetHashCode())
{
return new { code = 500, message = "该订单已申请过退款,请勿重复申请" };
}
}
// 查询订单信息
var orderInfo = await _db.Queryable().FirstAsync(x => x.OrderNo == input.OrderNo);
if (orderInfo == null)
{
return new { code = 500, message = "订单不存在" };
}
// 验证订单状态
if (orderInfo.Status != UavOrderStatusEnum.已完成.GetHashCode()) // 已完成状态
{
return new { code = 500, message = "只有已完成的订单才能申请退款" };
}
// 验证申请人是否为订单所有者
if (orderInfo.UserAccount != _userManager.UserId)
{
return new { code = 500, message = "无权操作此订单" };
}
// 验证退款金额
if (input.ApplyAmount > orderInfo.ActualAmount)
{
return new { code = 500, message = "退款金额不能大于实际支付金额" };
}
// 创建退款申请
var entity = new UavRefundApplicationEntity
{
Id = YitIdHelper.NextId().ToString(),
OrderNo = input.OrderNo,
UserId = orderInfo.UserAccount,
UserPhone = orderInfo.UserPhone,
ApplyAmount = input.ApplyAmount,
ApplyTime = DateTime.Now,
AgentId = orderInfo.AgentId,
RefundReason = input.RefundReason,
RefundStatus = RefundStatusEnum.待处理.GetHashCode(), // 待处理
Remark = input.Remark
};
// 开启事务
var result = await _db.UseTranAsync(async () =>
{
// 插入退款申请记录
await _db.Insertable(entity).ExecuteCommandAsync();
});
if (!result.IsSuccess)
{
return new { code = 500, message = "申请退款失败,请重试" };
}
return new { code = 200, message = "退款申请提交成功", data = entity.Id };
}
catch (Exception ex)
{
return new { code = 500, message = $"申请退款失败:{ex.Message}" };
}
}
#endregion
#region 获取当前登录用户申请退款列表
///
/// 获取当前登录用户申请退款列表
///
/// 请求参数
///
[HttpGet("GetUserRefundList")]
public async Task GetUserRefundList([FromQuery] UavRefundApplicationListQueryInput input)
{
var sidx = input.sidx == null ? "id" : input.sidx;
var data = await _db.Queryable()
.WhereIF(!string.IsNullOrEmpty(input.orderNo), p => p.OrderNo.Contains(input.orderNo))
.Where(p => p.UserId == _userManager.UserId)
.Select(it => new UavRefundApplicationListOutput
{
id = it.Id,
applyTime = it.ApplyTime,
orderNo = it.OrderNo,
applyAmount = it.ApplyAmount,
userId = it.UserId,
userPhone = it.UserPhone,
refundStatus = it.RefundStatus,
refundReason = it.RefundReason,
remark = it.Remark,
handleTime = it.HandleTime,
handleUser = it.HandleUser,
handleResult = it.HandleResult
}).MergeTable().OrderBy(sidx + " " + input.sort).ToPagedListAsync(input.currentPage, input.pageSize);
return PageResult.SqlSugarPageResult(data);
}
#endregion
#region 获取当前代理商的退款申请列表
///
///获取当前代理商的退款申请列表
///
///
///
[HttpPost("GetAgentRefundList")]
public async Task GetAgentRefundList([FromBody] UavRefundApplicationListQueryInput input)
{
var sidx = input.sidx == null ? "id" : input.sidx;
var data = await _db.Queryable()
.WhereIF(!string.IsNullOrEmpty(input.orderNo), p => p.OrderNo.Contains(input.orderNo))
.Where(p => p.AgentId == _userManager.UserId)
.Select(it => new UavRefundApplicationListOutput
{
id = it.Id,
applyTime = it.ApplyTime,
orderNo = it.OrderNo,
applyAmount = it.ApplyAmount,
userId = it.UserId,
userPhone = it.UserPhone,
refundStatus = it.RefundStatus,
refundReason = it.RefundReason,
remark = it.Remark,
handleTime = it.HandleTime,
handleUser = it.HandleUser,
handleResult = it.HandleResult
}).MergeTable().OrderBy(sidx + " " + input.sort).ToPagedListAsync(input.currentPage, input.pageSize);
return PageResult.SqlSugarPageResult(data);
}
#endregion
#region 拒绝用户退款
///
/// 拒绝用户退款
///
///
///
[HttpPost("RejectRefund")]
public async Task RejectRefund([FromBody] UavRefundApplicationUpInput input)
{
var info = _db.Queryable().Where(x => x.Id == input.id).First();
if (info != null)
{
info.RefundStatus = RefundStatusEnum.已拒绝.GetHashCode();
info.HandleResult = input.handleResult;
info.HandleTime = DateTime.Now;
info.HandleUser = _userManager.UserId;
var isOk = await _db.Updateable(info).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync();
if (!(isOk > 0)) throw NCCException.Oh(ErrorCode.COM1001);
return "退款申请已拒绝";
}
else
{
return "无此退款申请信息";
}
}
#endregion
}
}