From 164ab1e89ca9073b2d538205faa49b9d9ad87f8c Mon Sep 17 00:00:00 2001 From: luofei <604446095@qq.com> Date: Fri, 19 Jan 2024 10:58:56 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E7=94=A8=E6=88=B7=E5=8F=82?= =?UTF-8?q?=E4=B8=8E=E7=BA=A2=E5=8C=85=E6=B4=BB=E5=8A=A8=E7=8A=B6=E6=80=81?= =?UTF-8?q?=EF=BC=8C=E6=B7=BB=E5=8A=A0=E9=A2=86=E5=8F=96=E7=BA=A2=E5=8C=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/common.php | 17 +++- app/common/dao/store/StoreActivityUserDao.php | 80 +++++++++++++++++++ .../consumption/StoreConsumptionUserDao.php | 14 ++-- .../consumption/StoreConsumptionUser.php | 2 + app/controller/api/store/StoreActivity.php | 31 +++++++ 5 files changed, 137 insertions(+), 7 deletions(-) diff --git a/app/common.php b/app/common.php index e324f660..bcabe892 100644 --- a/app/common.php +++ b/app/common.php @@ -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; + } +} diff --git a/app/common/dao/store/StoreActivityUserDao.php b/app/common/dao/store/StoreActivityUserDao.php index cdcbd8bb..0c803590 100644 --- a/app/common/dao/store/StoreActivityUserDao.php +++ b/app/common/dao/store/StoreActivityUserDao.php @@ -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']]; + } + } diff --git a/app/common/dao/store/consumption/StoreConsumptionUserDao.php b/app/common/dao/store/consumption/StoreConsumptionUserDao.php index 13e17e7b..be5fc724 100644 --- a/app/common/dao/store/consumption/StoreConsumptionUserDao.php +++ b/app/common/dao/store/consumption/StoreConsumptionUserDao.php @@ -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('发放失败'); } diff --git a/app/common/model/store/consumption/StoreConsumptionUser.php b/app/common/model/store/consumption/StoreConsumptionUser.php index acf62e7d..92002ddd 100644 --- a/app/common/model/store/consumption/StoreConsumptionUser.php +++ b/app/common/model/store/consumption/StoreConsumptionUser.php @@ -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; //过期的 diff --git a/app/controller/api/store/StoreActivity.php b/app/controller/api/store/StoreActivity.php index 4987e7dd..e7ff5665 100644 --- a/app/controller/api/store/StoreActivity.php +++ b/app/controller/api/store/StoreActivity.php @@ -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); + } + } \ No newline at end of file