using Infrastructure;
using Infrastructure.Attribute;
using Infrastructure.Enums;
using Infrastructure.Model;
using Mapster;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using ARW.Admin.WebApi.Extensions;
using ARW.Admin.WebApi.Filters;
using ARW.Common;
using ARW.Model.Dto.Business.OrderManage.OrderRefunds;
using ARW.Service.Business.IBusinessService.OrderManage.OrderRefunds;
using ARW.Admin.WebApi.Controllers;
using ARW.Model.Models.Business.OrderManage.OrderRefunds;
using ARW.Model.Vo.Business.OrderManage.OrderRefunds;
using Microsoft.AspNetCore.Authorization;
using ARW.Admin.WebApi.Framework;
using ARW.Service.Business.IBusinessService.OrderManage.Orders;
using Geocoding;
using ARW.Service.Business.IBusinessService.ShopManager.Shops;
using ARW.Service.Business.IBusinessService.GoodsManager.GoodsSpecs.GoodsSkus;
using ARW.Service.Business.IBusinessService.OrderManage.OrderGoodss;
using ARW.Service.Business.IBusinessService.Payments;
using Infrastructure.WeChat.TenPay;
using System.Net.Http;
using ARW.Service.Business.IBusinessService.GoodsManager.Goodss;
using Senparc.CO2NET.HttpUtility;
using ARW.Model.Models.Business.Payments;
namespace ARW.WebApi.Controllers.Business.OrderManage.OrderRefunds
{
///
/// 售后单记录表控制器
///
/// @author lwh
/// @date 2023-08-29
///
[Verify]
[Route("business/[controller]")]
public class OrderRefundController : BaseController
{
private readonly IOrderRefundService _OrderRefundService;
private readonly IShopService _ShopService;
private readonly IPaymentService _PaymentService;
private readonly SenparcHttpClient _httpClient;
private readonly IGoodsService _GoodsService;
private readonly IGoodsSkuService _GoodsSkuService;
private readonly IOrderService _OrderService;
private readonly IOrderGoodsService _OrderGoodsService;
///
/// 依赖注入
///
/// 售后单记录表服务
public OrderRefundController(IOrderRefundService OrderRefundService, IShopService shopService, IPaymentService paymentService, SenparcHttpClient httpClient, IGoodsService goodsService, IGoodsSkuService goodsSkuService, IOrderService orderService, IOrderGoodsService orderGoodsService)
{
_OrderRefundService = OrderRefundService;
_ShopService = shopService;
_PaymentService = paymentService;
_httpClient = httpClient;
_GoodsService = goodsService;
_GoodsSkuService = goodsSkuService;
_OrderService = orderService;
_OrderGoodsService = orderGoodsService;
}
///
/// 获取售后单记录表列表
///
/// 查询参数
///
[HttpGet("getOrderRefundList")]
[ActionPermissionFilter(Permission = "business:orderrefund:list")]
public async Task GetOrderRefundList([FromQuery] OrderRefundQueryDto parm)
{
var user = JwtUtil.GetLoginUser(App.HttpContext);
if (user.UserId != 1)
{
var shop = await _ShopService.GetFirstAsync(s => s.ShopUserId == user.UserId);
if (shop == null) throw new Exception("当前用户没有店铺");
parm.ShopGuid = shop.ShopGuid;
}
var res = await _OrderRefundService.GetOrderRefundList(parm);
return SUCCESS(res);
}
///
/// 获取售后单详情
///
/// 查询参数
///
[HttpGet("getOrderRefundDetails")]
public async Task GetOrderDetails([FromQuery] OrderRefundDetailsDto parm)
{
if (parm == null) throw new CustomException("参数错误!");
var res = await _OrderRefundService.GetOrderRefundDetails(parm);
if (res != "[]")
{
var data = res.FromJSON();
return SUCCESS(data);
}
else
{
return SUCCESS(res);
}
}
///
/// 审核售后订单
///
///
///
[HttpPost("auditOrderRefund")]
[Log(Title = "审核售后订单", BusinessType = BusinessType.ADDORUPDATE)]
public async Task AuditOrderRefund([FromBody] OrderRefundAuditDto parm)
{
if (parm == null) { throw new CustomException("请求参数错误"); }
if (parm.IsAgree == true)
{
var orderRefund = await _OrderRefundService.GetFirstAsync(s => s.OrderRefundGuid == parm.OrderRefundGuid);
var order = await _OrderService.GetFirstAsync(s => s.OrderGuid == orderRefund.OrderGuid);
var payment = await _PaymentService.GetFirstAsync(s => s.PaymentGuid == order.PaymentGuid);
if (orderRefund.RefundType == 2)
{
Pay pay = new Pay(_httpClient);
var transactionId = payment.PaymentWeixinNumber;
string paymentRefundNumber = "";
if (!string.IsNullOrEmpty(payment.PaymentRefundNumber))
{
paymentRefundNumber = payment.PaymentRefundNumber;
}
var totalFee = payment.PaymentMoney * 100;
var canleRes = await pay.Refund(transactionId, totalFee, paymentRefundNumber, "商品退款");
if (canleRes.ResultCode.Success == false)
throw new CustomException("订单退款失败!");
else
{
await _OrderRefundService.UpdateAsync(f => new OrderRefund
{
RefundActualMoney = totalFee,
}, f => f.OrderRefundGuid == orderRefund.OrderRefundGuid);
await _PaymentService.UpdateAsync(f => new Payment
{
PaymentRefundNumber = canleRes.out_refund_no,
}, f => f.PaymentNumber == canleRes.out_trade_no);
var orderGoodsList = await _OrderGoodsService.GetListAsync(s => s.OrderGuid == orderRefund.OrderGuid);
// 库存加回来
foreach (var orderGood in orderGoodsList)
{
var goods = await _GoodsService.GetFirstAsync(s => s.GoodsGuid == orderGood.GoodsGuid);
var shop = await _ShopService.GetFirstAsync(s => s.ShopGuid == goods.ShopGuid);
goods.GoodsSalesActual += orderGood.GoodsTotalNum;
await _GoodsService.UpdateAsync(goods);
shop.ShopSalesOrderCount += orderGood.GoodsTotalNum;
await _ShopService.UpdateAsync(shop);
if (goods.GoodsDeductStockType == 2)
{
if (orderGood.GoodsSkuId != 0)
{
var sku = await _GoodsSkuService.GetFirstAsync(s => s.GoodsSkuId == orderGood.GoodsSkuId);
sku.GoodsSkuStockNum += orderGood.GoodsTotalNum;
goods.GoodsTotalInventory += orderGood.GoodsTotalNum;
await _GoodsSkuService.UpdateAsync(sku);
await _GoodsService.UpdateAsync(goods);
}
else
{
goods.GoodsTotalInventory += orderGood.GoodsTotalNum;
await _GoodsService.UpdateAsync(goods);
}
}
}
}
}
}
var res = await _OrderRefundService.AuditOrderRefund(parm);
return SUCCESS(res);
}
///
/// 商家确认收货
///
///
///
[HttpPost("confirmsReceipt")]
[Log(Title = "商家确认收货", BusinessType = BusinessType.ADDORUPDATE)]
public async Task ConfirmsReceipt([FromBody] OrderRefundAuditDto parm)
{
if (parm == null) { throw new CustomException("请求参数错误"); }
var orderRefund = await _OrderRefundService.GetFirstAsync(s => s.OrderRefundGuid == parm.OrderRefundGuid);
var order = await _OrderService.GetFirstAsync(s => s.OrderGuid == orderRefund.OrderGuid);
var payment = await _PaymentService.GetFirstAsync(s => s.PaymentGuid == order.PaymentGuid);
Pay pay = new Pay(_httpClient);
var transactionId = payment.PaymentWeixinNumber;
string paymentRefundNumber = "";
if (!string.IsNullOrEmpty(payment.PaymentRefundNumber))
{
paymentRefundNumber = payment.PaymentRefundNumber;
}
var totalFee = payment.PaymentMoney * 100;
var canleRes = await pay.Refund(transactionId, totalFee, paymentRefundNumber, "商品退款");
if (canleRes.ResultCode.Success == false)
throw new CustomException("订单退款失败!");
else
{
await _OrderRefundService.UpdateAsync(f => new OrderRefund
{
RefundActualMoney = totalFee,
}, f => f.OrderRefundGuid == orderRefund.OrderRefundGuid);
var respones = await _PaymentService.UpdateAsync(f => new Payment
{
PaymentRefundNumber = canleRes.out_refund_no,
}, f => f.PaymentNumber == canleRes.out_trade_no);
var orderGoodsList = await _OrderGoodsService.GetListAsync(s => s.OrderGuid == orderRefund.OrderGuid);
// 库存加回来
foreach (var orderGood in orderGoodsList)
{
var goods = await _GoodsService.GetFirstAsync(s => s.GoodsGuid == orderGood.GoodsGuid);
var shop = await _ShopService.GetFirstAsync(s => s.ShopGuid == goods.ShopGuid);
goods.GoodsSalesActual += orderGood.GoodsTotalNum;
await _GoodsService.UpdateAsync(goods);
shop.ShopSalesOrderCount += orderGood.GoodsTotalNum;
await _ShopService.UpdateAsync(shop);
if (goods.GoodsDeductStockType == 2)
{
if (orderGood.GoodsSkuId != 0)
{
var sku = await _GoodsSkuService.GetFirstAsync(s => s.GoodsSkuId == orderGood.GoodsSkuId);
sku.GoodsSkuStockNum += orderGood.GoodsTotalNum;
goods.GoodsTotalInventory += orderGood.GoodsTotalNum;
await _GoodsSkuService.UpdateAsync(sku);
await _GoodsService.UpdateAsync(goods);
}
else
{
goods.GoodsTotalInventory += orderGood.GoodsTotalNum;
await _GoodsService.UpdateAsync(goods);
}
}
}
}
var res = await _OrderRefundService.ConfirmsReceipt(parm);
return SUCCESS(res);
}
///
/// 删除售后单记录表
///
///
[HttpDelete("{ids}")]
[ActionPermissionFilter(Permission = "business:orderrefund:delete")]
[Log(Title = "售后单记录表删除", BusinessType = BusinessType.DELETE)]
public IActionResult Delete(string ids)
{
long[] idsArr = Tools.SpitLongArrary(ids);
if (idsArr.Length <= 0) { return ToResponse(ApiResult.Error($"删除失败Id 不能为空")); }
var response = _OrderRefundService.Delete(idsArr);
return SUCCESS("删除成功!");
}
///
/// 导出售后单记录表
///
///
[Log(Title = "售后单记录表导出", BusinessType = BusinessType.EXPORT, IsSaveResponseData = false)]
[HttpGet("exportOrderRefund")]
[ActionPermissionFilter(Permission = "business:orderrefund:export")]
public async Task ExportExcel([FromQuery] OrderRefundQueryDto parm)
{
parm.PageSize = 10000;
var list = await _OrderRefundService.GetOrderRefundList(parm);
var data = list.Result;
// 选中数据
if (!string.IsNullOrEmpty(parm.ids))
{
int[] idsArr = Tools.SpitIntArrary(parm.ids);
var selectDataList = new List();
foreach (var item in idsArr)
{
var select_data = data.Where(s => s.OrderRefundId == item).First();
selectDataList.Add(select_data);
}
data = selectDataList;
}
// 导出数据处理
var handleData = await _OrderRefundService.HandleExportData(data);
string sFileName = ExportExcel(handleData, "OrderRefund", "售后单记录表列表");
return SUCCESS(new { path = "/export/" + sFileName, fileName = sFileName });
}
}
}