161 lines
6.4 KiB
PHP
161 lines
6.4 KiB
PHP
<?php
|
|
|
|
namespace app\common\dao\store;
|
|
|
|
use app\common\dao\BaseDao;
|
|
use app\common\dao\store\consumption\StoreConsumptionUserDao;
|
|
use app\common\model\store\consumption\StoreConsumption;
|
|
use app\common\model\store\consumption\StoreConsumptionUser;
|
|
use app\common\model\store\StoreActivityOrder;
|
|
use app\common\model\store\StoreActivityUser;
|
|
use app\common\model\user\User;
|
|
|
|
class StoreActivityUserDao extends BaseDao
|
|
{
|
|
|
|
protected function getModel(): string
|
|
{
|
|
return StoreActivityUser::class;
|
|
}
|
|
|
|
/**
|
|
* 选择消费金类型
|
|
* @param int $userId
|
|
* @param int $couponId
|
|
* @param int $activityId
|
|
* @return void
|
|
* @throws \Exception
|
|
*/
|
|
public function choose(int $userId, int $couponId, int $activityId)
|
|
{
|
|
$model = StoreActivityUser::where('user_id', $userId)->where('value', $couponId)->where('activity_id', $activityId)->find();
|
|
if (empty($model)) {
|
|
$model = new StoreActivityUser();
|
|
$model->user_id = $userId;
|
|
$model->value = $couponId;
|
|
$model->activity_id = $activityId;
|
|
$model->create_time = time();
|
|
$model->status = 1;
|
|
if (!$model->save()) {
|
|
throw new \Exception('保存出错');
|
|
}
|
|
}
|
|
$validModel = StoreActivityUser::where('user_id', $userId)->where('activity_id', $activityId)->where('status', 1)->find();
|
|
if ($validModel['value'] != $couponId) {
|
|
$consumption = StoreConsumption::where('coupon_id', $validModel['value'])->find();
|
|
if ($consumption['type'] == StoreConsumption::TYPE_PULL_CONSUMPTION) {
|
|
$groupOrders = StoreActivityOrder::whereRaw('user_id=:user_id or spread_id=:spread_id', ['user_id' => $userId, 'spread_id' => $userId])
|
|
->where('activity_id', $activityId)
|
|
->where('status', StoreActivityOrder::STATUS_VALID)
|
|
->count();
|
|
if ($groupOrders > 0) {
|
|
$model->status = 0;
|
|
if (!$model->save()) {
|
|
throw new \Exception('保存出错');
|
|
}
|
|
throw new \Exception('您正在参与活动,暂时不支持切换');
|
|
}
|
|
}
|
|
$validModel->status = 0;
|
|
if (!$validModel->save()) {
|
|
throw new \Exception('保存出错');
|
|
}
|
|
$model->status = 1;
|
|
if (!$model->save()) {
|
|
throw new \Exception('保存出错');
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 获取用户选择的消费金类型
|
|
* @param int $userId
|
|
* @param int $activityId
|
|
* @return int
|
|
*/
|
|
public function getValue(int $userId, int $activityId = 1)
|
|
{
|
|
$data = StoreActivityUser::where('user_id', $userId)->where('activity_id', $activityId)->find();
|
|
return $data->value ?? 0;
|
|
}
|
|
|
|
/**
|
|
* 获取用户参与活动的状态
|
|
* @param int $userId 用户id
|
|
* @param int $activityId 活动id
|
|
* @return array
|
|
* @throws \think\db\exception\DataNotFoundException
|
|
* @throws \think\db\exception\DbException
|
|
* @throws \think\db\exception\ModelNotFoundException
|
|
*/
|
|
public function status(int $userId, int $activityId)
|
|
{
|
|
$target = 0;
|
|
$userInfo = [];
|
|
$activityUser = StoreActivityUser::where('user_id', $userId)
|
|
->where('activity_id', $activityId)
|
|
->where('status', 1)
|
|
->find()->toArray();
|
|
if (empty($activityUser)) {
|
|
return ['target' => $target, 'allow_receive' => false, 'user_info' => $userInfo];
|
|
}
|
|
$consumption = StoreConsumption::where('coupon_id', $activityUser['value'])
|
|
->where('type', StoreConsumption::TYPE_PULL_CONSUMPTION)
|
|
->find()->toArray();
|
|
$myOrder = StoreActivityOrder::where('user_id', $userId)
|
|
->where('activity_id', $activityId)
|
|
->where('status', StoreActivityOrder::STATUS_VALID)
|
|
->find()->toArray();
|
|
if (empty($myOrder)) {
|
|
return ['target' => $target, 'allow_receive' => false, 'user_info' => $userInfo];
|
|
}
|
|
$storeConsumptionUserDao = new StoreConsumptionUserDao();
|
|
$storeConsumptionUserDao->consumption = $consumption;
|
|
$scope = $storeConsumptionUserDao->getScope($myOrder['total_amount']);
|
|
$userInfo = User::where('spread_uid', $userId)->field('uid,nickname,avatar')->select()->toArray();
|
|
$orders = StoreActivityOrder::where('spread_id', $userId)
|
|
->whereIn('user_id', array_column($userInfo, 'uid'))
|
|
->where('activity_id', $activityId)
|
|
->where('is_first_order', StoreActivityOrder::IS_FIRST_ORDER)
|
|
->where('total_amount', '>=', $scope['start'])
|
|
->where('status', StoreActivityOrder::STATUS_VALID)
|
|
->select()->toArray();
|
|
$orders = reset_index($orders, 'user_id');
|
|
foreach ($userInfo as &$user) {
|
|
$user['target_amount'] = $scope['start'];
|
|
$user['is_finish'] = isset($orders[$user['uid']]) ? 1 : 0;
|
|
}
|
|
return ['target' => $scope['num'], 'allow_receive' => count($orders) >= $scope['num'], 'user_info' => $userInfo];
|
|
}
|
|
|
|
/**
|
|
* 领取消费金
|
|
* @param int $userId
|
|
* @return array
|
|
* @throws \think\db\exception\DataNotFoundException
|
|
* @throws \think\db\exception\DbException
|
|
* @throws \think\db\exception\ModelNotFoundException
|
|
*/
|
|
public function receive(int $userId)
|
|
{
|
|
$consumption = StoreConsumption::where('type', StoreConsumption::TYPE_PULL_CONSUMPTION)
|
|
->where('status', StoreConsumption::STATUS_ENABLE)
|
|
->find();
|
|
$userConsumption = StoreConsumptionUser::where('uid', $userId)
|
|
->where('coupon_id', $consumption['coupon_id'])
|
|
->where('status', StoreConsumptionUser::STATUS_UN_RECEIVE)
|
|
->find();
|
|
if (empty($userConsumption)) {
|
|
throw new \Exception('您没有可领取的红包');
|
|
}
|
|
$userConsumption->status = StoreConsumptionUser::STATUS_UNUSED;
|
|
$userConsumption->start_time = date('Y-m-d H:i:s', time() + 7 * 86400);
|
|
$userConsumption->end_time = date('Y-m-d H:i:s', time() + 7 * 86400 + 365 * 86400);
|
|
if (!$userConsumption->save()) {
|
|
throw new \Exception('领取出错,请稍后重试');
|
|
}
|
|
return ['amount' => $userConsumption['coupon_price'], 'end_time' => $userConsumption['end_time']];
|
|
}
|
|
|
|
}
|