ThinkAdmin/app/data/service/OrderService.php
2020-09-10 10:17:09 +08:00

73 lines
3.3 KiB
PHP

<?php
namespace app\data\service;
use think\admin\Service;
/**
* 订单数据服务
* Class OrderService
* @package app\data\service
*/
class OrderService extends Service
{
/**
* 创建申请售后单
* @param string $orderNo
* @param array $data [type,refund_content,refund_images]
* @param array $rules [[goods_id,goods_spec,refund_number]]
* @return boolean
* @throws \think\Exception
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function refund(string $orderNo, array $data, array $rules = [])
{
[$all, $map] = [[], ['order_no' => $orderNo]];
$order = $this->app->db->name('StoreOrder')->where($map)->find();
$olist = $this->app->db->name('StoreOrderList')->where($map)->select()->toArray();
$rlist = $this->app->db->name('StoreOrderRefund')->where($map)->whereIn('refund_status', [1, 2, 3])->select()->toArray();
$discountRate = $order['pay_price'] / $order['price_discount'];
if (count($olist) > 0) foreach ($olist as &$vo) {
$vo['discount_unit_amount'] = $vo['discount_amount'] / $vo['number_goods'] * $discountRate;
if (count($rlist) > 0) foreach ($rlist as $rule) {
if ($vo['goods_id'] === $rule['goods_id'] && $vo['goods_spec'] === $rule['goods_spec']) {
$vo['number_goods'] -= $rule['refund_number'];
}
}
}
// dump($olist);
$data['group_no'] = CodeExtend::uniqidDate(18, "G");
if (count($olist) > 0 && count($rules) > 0) foreach ($olist as &$vo) {
foreach ($rules as $rule) if ($vo['goods_id'] === $rule['goods_id'] && $vo['goods_spec'] === $rule['goods_spec']) {
if ($vo['number_goods'] - $rule['refund_number'] < 0) {
throw new \think\Exception("订单商品数量异常!");
}
$data['mid'] = $vo['mid'];
$data['order_no'] = $orderNo;
$data['goods_id'] = $vo['goods_id'];
$data['goods_sku'] = $vo['goods_sku'];
$data['goods_title'] = $vo['goods_title'];
$data['goods_logo'] = $vo['goods_logo'];
$data['goods_spec'] = $vo['goods_spec'];
$data['number_goods'] = $vo['number_goods'];
$data['goods_price_total'] = $vo['goods_price_total'];
$data['goods_price_market'] = $vo['goods_price_market'];
$data['refund_no'] = CodeExtend::uniqidDate(18, 'R');
$data['refund_rate'] = $discountRate;
$data['refund_number'] = $rule['refund_number'];
$data['refund_amount'] = $rule['refund_number'] * $vo['discount_unit_amount'];
$data['refund_status'] = 1;
$data['discount_amount'] = $vo['discount_amount'];
// 支付金额处理
if ($order['pay_price'] < $data['refund_amount']) {
$data['refund_amount'] = $order['pay_price'];
}
$all[] = $data;
}
}
if (empty($all)) throw new \think\Exception("没有需要处理的商品!");
return $this->app->db->name('StoreOrderRefund')->strict(false)->insertAll($all) !== false;
}
}