|
@@ -3,7 +3,12 @@ namespace app\api\logic;
|
|
|
|
|
|
|
|
use think\Exception;
|
|
use think\Exception;
|
|
|
use think\facade\Db;
|
|
use think\facade\Db;
|
|
|
|
|
+use app\common\enum\RefundEnum;
|
|
|
use app\common\logic\BaseLogic;
|
|
use app\common\logic\BaseLogic;
|
|
|
|
|
+use app\common\logic\RefundLogic;
|
|
|
|
|
+use app\common\model\equity\UserEquity;
|
|
|
|
|
+use app\common\model\refund\RefundRecord;
|
|
|
|
|
+use app\common\model\equity\UserEquityLog;
|
|
|
use app\common\model\service_area\ServiceArea;
|
|
use app\common\model\service_area\ServiceArea;
|
|
|
use app\common\model\group_activity\GroupOrder;
|
|
use app\common\model\group_activity\GroupOrder;
|
|
|
use app\common\model\group_activity\GroupActivity;
|
|
use app\common\model\group_activity\GroupActivity;
|
|
@@ -42,11 +47,15 @@ class GroupActivityLogic extends BaseLogic
|
|
|
$detail = GroupOrder::where(['sn'=>$sn])->findOrEmpty()->toArray();
|
|
$detail = GroupOrder::where(['sn'=>$sn])->findOrEmpty()->toArray();
|
|
|
if(!empty($detail)){
|
|
if(!empty($detail)){
|
|
|
|
|
|
|
|
- $detail['activity'] = GroupActivity::with('goods')->where(['id'=>$detail['group_activity_id']])->findOrEmpty()->toArray();
|
|
|
|
|
|
|
+ $detail['activity'] = GroupActivity::with('goods')->where(['id'=>$detail['group_activity_id']])->visible([
|
|
|
|
|
+ 'id','title','image','start_time','end_time','type','equity_id',
|
|
|
|
|
+ 'participant_num','origin_price','price','form_time_limit'
|
|
|
|
|
+ ])->findOrEmpty()->toArray();
|
|
|
$detail['users'] = GroupUserOrder::alias('a')
|
|
$detail['users'] = GroupUserOrder::alias('a')
|
|
|
->leftJoin('user b','a.user_id=b.id')
|
|
->leftJoin('user b','a.user_id=b.id')
|
|
|
->where(['a.sn'=>$sn,'a.status'=>1])
|
|
->where(['a.sn'=>$sn,'a.status'=>1])
|
|
|
- ->visible(['a.id','a.user_id','a.status','a.create_time','b.avatar','b.nickname'])
|
|
|
|
|
|
|
+ ->field(['a.id','a.user_id','a.status','a.create_time','b.avatar','b.nickname'])
|
|
|
|
|
+ ->order('a.create_time','asc')
|
|
|
->select()
|
|
->select()
|
|
|
->toArray();
|
|
->toArray();
|
|
|
}
|
|
}
|
|
@@ -110,7 +119,7 @@ class GroupActivityLogic extends BaseLogic
|
|
|
'origin_price' => $activity['origin_price'],
|
|
'origin_price' => $activity['origin_price'],
|
|
|
'price' => $order_amount,
|
|
'price' => $order_amount,
|
|
|
'create_time' => time(),
|
|
'create_time' => time(),
|
|
|
- 'end_time' => time() + $activity['form_time_limit'] * 60,
|
|
|
|
|
|
|
+ 'end_time' => time() + $activity['form_time_limit'] * 60 * 60,
|
|
|
];
|
|
];
|
|
|
$group_order = GroupOrder::create($data);
|
|
$group_order = GroupOrder::create($data);
|
|
|
} else {
|
|
} else {
|
|
@@ -119,16 +128,18 @@ class GroupActivityLogic extends BaseLogic
|
|
|
if (empty($group_order)) {
|
|
if (empty($group_order)) {
|
|
|
throw new Exception('拼团订单不存在!'); //拼团活动不存在
|
|
throw new Exception('拼团订单不存在!'); //拼团活动不存在
|
|
|
}
|
|
}
|
|
|
- if ($group_order['status'] != 0) {
|
|
|
|
|
- throw new Exception('拼团活动已结束!'); //拼团活动已结束
|
|
|
|
|
|
|
+ if ($group_order['num'] >= 100) {
|
|
|
|
|
+ throw new Exception('拼团人数已满!'); //拼团人数已满
|
|
|
|
|
+ }
|
|
|
|
|
+ if ($group_order['status'] == 1 ) {
|
|
|
|
|
+ throw new Exception('订单已支付!');
|
|
|
|
|
+ }
|
|
|
|
|
+ if ($group_order['status'] >= 1 ) {
|
|
|
|
|
+ throw new Exception('订单已取消!');
|
|
|
}
|
|
}
|
|
|
if (strtotime($group_order['end_time']) < time()) {
|
|
if (strtotime($group_order['end_time']) < time()) {
|
|
|
throw new Exception('拼团活动已结束!'); //拼团活动已结束
|
|
throw new Exception('拼团活动已结束!'); //拼团活动已结束
|
|
|
}
|
|
}
|
|
|
- if ($group_order['num'] >= 100) {
|
|
|
|
|
- throw new Exception('拼团人数已满!'); //拼团人数已满
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
$order_amount = $group_order['price'];
|
|
$order_amount = $group_order['price'];
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -148,6 +159,7 @@ class GroupActivityLogic extends BaseLogic
|
|
|
'area_name' => $areas['area_name']??'',
|
|
'area_name' => $areas['area_name']??'',
|
|
|
'service_area_id' => $areas['id']??0,
|
|
'service_area_id' => $areas['id']??0,
|
|
|
'order_amount' => $order_amount,
|
|
'order_amount' => $order_amount,
|
|
|
|
|
+ 'order_terminal' => $params['terminal'],
|
|
|
];
|
|
];
|
|
|
$group_user_order = GroupUserOrder::create($data);
|
|
$group_user_order = GroupUserOrder::create($data);
|
|
|
|
|
|
|
@@ -176,21 +188,22 @@ class GroupActivityLogic extends BaseLogic
|
|
|
$detail = GroupUserOrder::where([
|
|
$detail = GroupUserOrder::where([
|
|
|
'user_id' => $params['user_id'],
|
|
'user_id' => $params['user_id'],
|
|
|
'sn'=>$params['sn']
|
|
'sn'=>$params['sn']
|
|
|
- ])->file('id,status')->findOrEmpty()->toArray();
|
|
|
|
|
|
|
+ ])->field('id,status')->findOrEmpty()->toArray();
|
|
|
if(empty($detail)){
|
|
if(empty($detail)){
|
|
|
throw new Exception('订单不存在');
|
|
throw new Exception('订单不存在');
|
|
|
}
|
|
}
|
|
|
if($detail['status'] == 1){
|
|
if($detail['status'] == 1){
|
|
|
- throw new Exception('已支付订单不能取消');
|
|
|
|
|
|
|
+ throw new Exception('已支付订单不支持取消');
|
|
|
}
|
|
}
|
|
|
if($detail['status'] != 0){
|
|
if($detail['status'] != 0){
|
|
|
- throw new Exception('当前订单不能取消');
|
|
|
|
|
|
|
+ throw new Exception('当前订单不支持取消');
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
//将用户订单状态更新为已取消
|
|
//将用户订单状态更新为已取消
|
|
|
GroupUserOrder::where('id',$detail['id'])->update(['status' => 4, 'pay_status' => 2]);
|
|
GroupUserOrder::where('id',$detail['id'])->update(['status' => 4, 'pay_status' => 2]);
|
|
|
|
|
|
|
|
Db::commit();
|
|
Db::commit();
|
|
|
|
|
+ return true;
|
|
|
}
|
|
}
|
|
|
catch (\Exception $e) {
|
|
catch (\Exception $e) {
|
|
|
Db::rollback();
|
|
Db::rollback();
|
|
@@ -198,4 +211,81 @@ class GroupActivityLogic extends BaseLogic
|
|
|
return false;
|
|
return false;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 订单退款
|
|
|
|
|
+ * @param $params
|
|
|
|
|
+ * @return false|void
|
|
|
|
|
+ */
|
|
|
|
|
+ public static function refundOrder($params)
|
|
|
|
|
+ {
|
|
|
|
|
+ Db::startTrans();
|
|
|
|
|
+ try {
|
|
|
|
|
+ $order = GroupUserOrder::where([
|
|
|
|
|
+ 'user_id' => $params['user_id'],
|
|
|
|
|
+ 'sn'=>$params['sn']
|
|
|
|
|
+ ])->field('id,sn,status,order_amount,pay_status,pay_way,user_equity_id,user_id')->findOrEmpty()->toArray();
|
|
|
|
|
+ if(empty($order)){
|
|
|
|
|
+ throw new Exception('订单不存在');
|
|
|
|
|
+ }
|
|
|
|
|
+ if($order['status'] != 1 || $order['pay_status'] != 1){
|
|
|
|
|
+ throw new Exception('当前订单不支持退款');
|
|
|
|
|
+ }
|
|
|
|
|
+ if ($order['user_equity_id']) {
|
|
|
|
|
+ //判断权益卡是否已使用
|
|
|
|
|
+ $used = UserEquityLog::where(['user_equity_id' => $order['user_equity_id'],'user_id' => $params['user_id']])->findOrEmpty();
|
|
|
|
|
+ if ($used) {
|
|
|
|
|
+ throw new Exception('当前权益卡已使用,不支持退款');
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ //将用户订单状态更新为申请退款
|
|
|
|
|
+ GroupUserOrder::where('id',$order['id'])->update(['status' => 2,'refund_status' => 1]);
|
|
|
|
|
+
|
|
|
|
|
+ // 生成退款记录
|
|
|
|
|
+ $recordSn = generate_sn(RefundRecord::class, 'sn');
|
|
|
|
|
+ $record = RefundRecord::create([
|
|
|
|
|
+ 'sn' => $recordSn,
|
|
|
|
|
+ 'user_id' => $order['user_id'],
|
|
|
|
|
+ 'order_id' => $order['id'],
|
|
|
|
|
+ 'order_sn' => $order['sn'],
|
|
|
|
|
+ 'order_type' => RefundEnum::ORDER_TYPE_GROUP,
|
|
|
|
|
+ 'order_amount' => $order['order_amount'],
|
|
|
|
|
+ 'refund_amount' => $order['order_amount'],
|
|
|
|
|
+ 'refund_type' => RefundEnum::TYPE_ADMIN,
|
|
|
|
|
+ 'transaction_id' => $order['transaction_id'] ?? '',
|
|
|
|
|
+ 'refund_way' => RefundEnum::getRefundWayByPayWay($order['pay_way']),
|
|
|
|
|
+ ]);
|
|
|
|
|
+
|
|
|
|
|
+ // 退款
|
|
|
|
|
+ RefundLogic::refund($order, $record['id'], $order['order_amount'], 1);
|
|
|
|
|
+
|
|
|
|
|
+ Db::commit();
|
|
|
|
|
+ return true;
|
|
|
|
|
+ }
|
|
|
|
|
+ catch (\Exception $e) {
|
|
|
|
|
+ Db::rollback();
|
|
|
|
|
+ self::setError($e->getMessage());
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public static function deleteOrder($params):bool
|
|
|
|
|
+ {
|
|
|
|
|
+ try{
|
|
|
|
|
+ $order = GroupUserOrder::where([
|
|
|
|
|
+ 'status' => 0,
|
|
|
|
|
+ 'user_id' => $params['worker_iuser_idd'],
|
|
|
|
|
+ 'sn' => $params['sn']
|
|
|
|
|
+ ])->findOrEmpty();
|
|
|
|
|
+ if($order->isEmpty()){
|
|
|
|
|
+ throw new \Exception('订单不存在');
|
|
|
|
|
+ }
|
|
|
|
|
+ $order->delete();
|
|
|
|
|
+ return true;
|
|
|
|
|
+ } catch(\Exception $e){
|
|
|
|
|
+ self::setError($e->getMessage());
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|