shop-php/app/common/dao/store/StoreActivityOrderDao.php

88 lines
3.2 KiB
PHP
Executable File

<?php
namespace app\common\dao\store;
use app\common\dao\BaseDao;
use app\common\model\store\consumption\StoreConsumptionDetail;
use app\common\model\store\consumption\StoreConsumptionUser;
use app\common\model\store\StoreActivityOrder;
class StoreActivityOrderDao extends BaseDao
{
protected function getModel(): string
{
return StoreActivityOrder::class;
}
/**
* 保存活动订单数据
* @param $groupOrder 订单
* @param int $spreadId 推荐人id
* @param int $isFirstOrder 是否首单
* @param int $activityId 活动id
* @return StoreActivityOrder
* @throws \Exception
*/
public function save($groupOrder, int $spreadId, int $isFirstOrder, int $activityId = 1)
{
$model = new StoreActivityOrder();
$model->activity_id = $activityId;
$model->user_id = $groupOrder['uid'];
$model->group_order_id = $groupOrder['group_order_id'];
$model->spread_id = $spreadId;
$model->pay_price = $groupOrder['pay_price'];
$model->is_first_order = $isFirstOrder;
$model->status = StoreActivityOrder::STATUS_VALID;
$consumptionDetail = StoreConsumptionDetail::where('group_order_id', $groupOrder['group_order_id'])
->field('group_order_id,sum(amount) amount,sum(pay_price) pay_price,coupon_user_id')
->find();
if (!empty($consumptionDetail['amount'])) {
// 当前订单使用现金抵扣红包 或 使用通用红包后实付金额不足红包金额的1.5倍,视为无效订单
$redPackType = StoreConsumptionUser::where('coupon_user_id', $consumptionDetail['coupon_user_id'])->value('type');
if ($redPackType == StoreConsumptionUser::TYPE_TWO || ($redPackType == StoreConsumptionUser::TYPE_TWO && $consumptionDetail['pay_price'] < $consumptionDetail['amount'] * 1.5)) {
$model->status = StoreActivityOrder::STATUS_INVALID;
}
}
$model->red_pack = $consumptionDetail['amount'] ?? 0;
if (!$model->save()) {
throw new \Exception('活动订单保存失败');
}
return $model;
}
/**
* 活动订单撤销
* @param int $groupOrderId
* @return void
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function repeal(int $groupOrderId)
{
$model = StoreActivityOrder::where('group_order_id', $groupOrderId)->find();
if ($model) {
$model->status = StoreActivityOrder::STATUS_INVALID;
if (!$model->save()) {
throw new \Exception('活动订单保存失败');
}
}
}
/**
* 活动订单批量撤销
* @param array $groupOrderIds
* @return void
* @throws \Exception
*/
public function batchRepeal(array $groupOrderIds)
{
$count = StoreActivityOrder::whereIn('group_order_id', $groupOrderIds)->update(['status' => StoreActivityOrder::STATUS_INVALID]);
if ($count < count($groupOrderIds)) {
throw new \Exception('活动订单批量作废失败');
}
}
}