| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192 |
- <?php
- namespace app\modules\client\models\v1\integralAppreciation;
- use app\models\IntegralAppreciationCommunity;
- use app\models\IntegralAppreciationUser;
- use app\models\User;
- use app\modules\client\controllers\BaseController;
- use yii\base\Model;
- class CommunityForm extends Model
- {
- public $store_id;
- public $user;
- public $integral;
- public $mobile;
- public $type;
- public $is_self;
- public $id;
- public function rules() {
- return [
- [['mobile'], 'string'],
- [['store_id', 'integral', 'type', 'is_self', 'id'], 'integer'],
- ];
- }
- /**
- * 帖子列表
- */
- public function getList() {
- $type = $this->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()
- ];
- }
- }
- }
|