type; $is_self = intval($this->is_self); $store_id = $this->store_id; $user = $this->user; $query = IntegralAppreciationCommunity::find()->alias('c') ->leftJoin(['u' => User::tableName()], 'c.user_id = u.id') ->where([ 'c.store_id' => $store_id, 'c.is_delete' => 0 ]); if (!$is_self) { $query->andWhere(['c.status' => IntegralAppreciationCommunity::STATUS_APPLY]); } else { $query->andWhere(['c.user_id' => $user->id]); } if (!in_array($type, [IntegralAppreciationCommunity::TYPE_BUY, IntegralAppreciationCommunity::TYPE_SELL])) { return [ 'code' => 1, 'msg' => '类型错误' ]; } $query->andWhere(['c.type' => $type]); $query->orderBy('c.id DESC')->select('c.id, c.type, c.integral, c.mobile, c.status, c.created_at, u.nickname, u.avatar_url avatar'); $pagination = pagination_make($query); foreach ($pagination['list'] as &$item) { $item['type'] = intval($item['type']); $item['created_at'] = date('Y-m-d H:i:s', $item['created_at']); $item['status'] = intval($item['status']); $item['status_text'] = IntegralAppreciationCommunity::$statusArray[$item['status']]; } return [ 'code' => 0, 'msg' => 'success', 'data' => $pagination ]; } /** * 添加帖子 */ public function save() { try { $store_id = $this->store_id; $type = $this->type; $integral = $this->integral; $user = $this->user; $mobile = $this->mobile; $id = $this->id; if (!in_array($type, [IntegralAppreciationCommunity::TYPE_BUY, IntegralAppreciationCommunity::TYPE_SELL])) { throw new \Exception('类型错误'); } if (!$user) { throw new \Exception('用户不存在'); } if (!$integral) { throw new \Exception('积分不能为空'); } if (!$mobile) { throw new \Exception('手机号不能为空'); } // $communityIntegral = IntegralAppreciationCommunity::find()->where([ // 'user_id' => $user->id, // 'status' => [IntegralAppreciationCommunity::STATUS_NO_APPLY, IntegralAppreciationCommunity::STATUS_APPLY], // 'type' => IntegralAppreciationCommunity::TYPE_SELL // ])->sum('integral') ?: 0; //判断已经发帖的积分以及当前发帖的积分是否大于用户积分 $integralAppreciationUser = IntegralAppreciationUser::findOne(['user_id' => $user->id]); if (intval($type) === IntegralAppreciationCommunity::TYPE_SELL && $integralAppreciationUser->integral < $integral) { throw new \Exception('积分不足'); } if ($id) { $form = IntegralAppreciationCommunity::findOne(['id' => $id, 'is_delete' => 0]); if (!$form) { throw new \Exception('帖子不存在'); } } else { $form = new IntegralAppreciationCommunity(); } $form->store_id = $store_id; $form->user_id = $user->id; $form->type = $type; $form->integral = $integral; $form->mobile = $mobile; $form->status = IntegralAppreciationCommunity::STATUS_NO_APPLY; if (!$form->save()) { throw new \Exception(implode(';', $form->getFirstErrors())); } return [ 'code' => 0, 'msg' => '添加成功' ]; } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } /** * 删除帖子 */ public function del() { try { $id = $this->id; $user = $this->user; $form = IntegralAppreciationCommunity::findOne(['id' => $id, 'is_delete' => 0, 'user_id' => $user->id]); if (!$form) { throw new \Exception('帖子不存在'); } $form->is_delete = 1; if (!$form->save()) { throw new \Exception(implode(';', $form->getFirstErrors())); } return [ 'code' => 0, 'msg' => '删除成功' ]; } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } }