添加用户参与红包活动状态,添加领取红包

This commit is contained in:
luofei 2024-01-19 10:58:56 +08:00
parent d268290840
commit 164ab1e89c
5 changed files with 137 additions and 7 deletions

View File

@ -1178,5 +1178,20 @@ if (!function_exists('Financial_Operations')) {
}
}
if (!function_exists('reset_index')) {
/**
* 重置数组索引
* @param array $data
* @param string $index
* @return array
*/
function reset_index(array $data, string $index)
{
$return = [];
foreach ($data as $item) {
$return[$item[$index]] = $item;
}
return $return;
}
}

View File

@ -3,9 +3,12 @@
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
{
@ -76,4 +79,81 @@ class StoreActivityUserDao extends BaseDao
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, 'userInfo' => $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, 'userInfo' => $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['is_finish'] = isset($orders[$user['uid']]) ? 1 : 0;
}
return ['target' => $scope['num'], 'allow_receive' => count($orders) >= $scope['num'], 'userInfo' => $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']];
}
}

View File

@ -71,7 +71,7 @@ class StoreConsumptionUserDao extends BaseDao
}
}
} elseif ($this->consumption['type'] == StoreConsumption::TYPE_OWNER_CONSUMPTION) {
$this->send($scope['rate'], $groupOrder['uid'], $groupOrder['group_order_id'], $groupOrder['pay_price']);
$this->send($scope['rate'], $groupOrder['uid'], $groupOrder['group_order_id'], $groupOrder['pay_price'], StoreConsumptionUser::STATUS_UNUSED);
$storeActivityOrderDao->repeal($groupOrder['group_order_id']);
}
Db::commit();
@ -125,14 +125,15 @@ class StoreConsumptionUserDao extends BaseDao
/**
* 发放消费金
* @param float $rate
* @param int $userId
* @param string $groupOrderIds
* @param float $amount
* @param float $rate 红包发放比例
* @param int $userId 用户id
* @param string $groupOrderIds 订单id集合
* @param float $amount 订单金额
* @param float $status 红包领取状态
* @return void
* @throws \Exception
*/
public function send(float $rate, int $userId, string $groupOrderIds, float $amount)
public function send(float $rate, int $userId, string $groupOrderIds, float $amount, $status = -2)
{
$model = new StoreConsumptionUser();
$model->coupon_id = $this->consumption['coupon_id'];
@ -145,6 +146,7 @@ class StoreConsumptionUserDao extends BaseDao
$model->start_time = date('Y-m-d H:i:s', time() + 7 * 86400);
$model->end_time = date('Y-m-d H:i:s', time() + 7 * 86400 + 365 * 86400);
$model->type = 'send';
$model->status = $status;
if (!$model->save()) {
throw new \Exception('发放失败');
}

View File

@ -7,6 +7,8 @@ use app\common\model\BaseModel;
class StoreConsumptionUser extends BaseModel
{
const STATUS_UN_RECEIVE = -2; //未领取
const STATUS_REPEAL = -1; //已作废
const STATUS_UNUSED = 0; //未使用
const STATUS_USED = 1; //已使用
const STATUS_OVERDUE = 2; //过期的

View File

@ -38,4 +38,35 @@ class StoreActivity extends BaseController
return app('json')->success('提交成功');
}
/**
* 获取用户参与活动的状态
* @param StoreActivityUserDao $dao
* @return mixed
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function status(StoreActivityUserDao $dao)
{
$userId = $this->request->uid();
$activityId = $this->request->post('activity_id', 1);
$result = $dao->status($userId, $activityId);
return app('json')->status('success', '获取成功', $result);
}
/**
* 领取消费金
* @param StoreActivityUserDao $dao
* @return mixed
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function receive(StoreActivityUserDao $dao)
{
$userId = $this->request->uid();
$result = $dao->receive($userId);
return app('json')->status('success', '领取成功', $result);
}
}