CommunityForm.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. namespace app\modules\client\models\v1\integralAppreciation;
  3. use app\models\IntegralAppreciationCommunity;
  4. use app\models\IntegralAppreciationUser;
  5. use app\models\User;
  6. use app\modules\client\controllers\BaseController;
  7. use yii\base\Model;
  8. class CommunityForm extends Model
  9. {
  10. public $store_id;
  11. public $user;
  12. public $integral;
  13. public $mobile;
  14. public $type;
  15. public $is_self;
  16. public $id;
  17. public function rules() {
  18. return [
  19. [['mobile'], 'string'],
  20. [['store_id', 'integral', 'type', 'is_self', 'id'], 'integer'],
  21. ];
  22. }
  23. /**
  24. * 帖子列表
  25. */
  26. public function getList() {
  27. $type = $this->type;
  28. $is_self = intval($this->is_self);
  29. $store_id = $this->store_id;
  30. $user = $this->user;
  31. $query = IntegralAppreciationCommunity::find()->alias('c')
  32. ->leftJoin(['u' => User::tableName()], 'c.user_id = u.id')
  33. ->where([
  34. 'c.store_id' => $store_id,
  35. 'c.is_delete' => 0
  36. ]);
  37. if (!$is_self) {
  38. $query->andWhere(['c.status' => IntegralAppreciationCommunity::STATUS_APPLY]);
  39. } else {
  40. $query->andWhere(['c.user_id' => $user->id]);
  41. }
  42. if (!in_array($type, [IntegralAppreciationCommunity::TYPE_BUY, IntegralAppreciationCommunity::TYPE_SELL])) {
  43. return [
  44. 'code' => 1,
  45. 'msg' => '类型错误'
  46. ];
  47. }
  48. $query->andWhere(['c.type' => $type]);
  49. $query->orderBy('c.id DESC')->select('c.id, c.type, c.integral, c.mobile, c.status,
  50. c.created_at, u.nickname, u.avatar_url avatar');
  51. $pagination = pagination_make($query);
  52. foreach ($pagination['list'] as &$item) {
  53. $item['type'] = intval($item['type']);
  54. $item['created_at'] = date('Y-m-d H:i:s', $item['created_at']);
  55. $item['status'] = intval($item['status']);
  56. $item['status_text'] = IntegralAppreciationCommunity::$statusArray[$item['status']];
  57. }
  58. return [
  59. 'code' => 0,
  60. 'msg' => 'success',
  61. 'data' => $pagination
  62. ];
  63. }
  64. /**
  65. * 添加帖子
  66. */
  67. public function save() {
  68. try {
  69. $store_id = $this->store_id;
  70. $type = $this->type;
  71. $integral = $this->integral;
  72. $user = $this->user;
  73. $mobile = $this->mobile;
  74. $id = $this->id;
  75. if (!in_array($type, [IntegralAppreciationCommunity::TYPE_BUY, IntegralAppreciationCommunity::TYPE_SELL])) {
  76. throw new \Exception('类型错误');
  77. }
  78. if (!$user) {
  79. throw new \Exception('用户不存在');
  80. }
  81. if (!$integral) {
  82. throw new \Exception('积分不能为空');
  83. }
  84. if (!$mobile) {
  85. throw new \Exception('手机号不能为空');
  86. }
  87. // $communityIntegral = IntegralAppreciationCommunity::find()->where([
  88. // 'user_id' => $user->id,
  89. // 'status' => [IntegralAppreciationCommunity::STATUS_NO_APPLY, IntegralAppreciationCommunity::STATUS_APPLY],
  90. // 'type' => IntegralAppreciationCommunity::TYPE_SELL
  91. // ])->sum('integral') ?: 0;
  92. //判断已经发帖的积分以及当前发帖的积分是否大于用户积分
  93. $integralAppreciationUser = IntegralAppreciationUser::findOne(['user_id' => $user->id]);
  94. if (intval($type) === IntegralAppreciationCommunity::TYPE_SELL && $integralAppreciationUser->integral < $integral) {
  95. throw new \Exception('积分不足');
  96. }
  97. if ($id) {
  98. $form = IntegralAppreciationCommunity::findOne(['id' => $id, 'is_delete' => 0]);
  99. if (!$form) {
  100. throw new \Exception('帖子不存在');
  101. }
  102. } else {
  103. $form = new IntegralAppreciationCommunity();
  104. }
  105. $form->store_id = $store_id;
  106. $form->user_id = $user->id;
  107. $form->type = $type;
  108. $form->integral = $integral;
  109. $form->mobile = $mobile;
  110. $form->status = IntegralAppreciationCommunity::STATUS_NO_APPLY;
  111. if (!$form->save()) {
  112. throw new \Exception(implode(';', $form->getFirstErrors()));
  113. }
  114. return [
  115. 'code' => 0,
  116. 'msg' => '添加成功'
  117. ];
  118. } catch (\Exception $e) {
  119. return [
  120. 'code' => 1,
  121. 'msg' => $e->getMessage()
  122. ];
  123. }
  124. }
  125. /**
  126. * 删除帖子
  127. */
  128. public function del() {
  129. try {
  130. $id = $this->id;
  131. $user = $this->user;
  132. $form = IntegralAppreciationCommunity::findOne(['id' => $id, 'is_delete' => 0, 'user_id' => $user->id]);
  133. if (!$form) {
  134. throw new \Exception('帖子不存在');
  135. }
  136. $form->is_delete = 1;
  137. if (!$form->save()) {
  138. throw new \Exception(implode(';', $form->getFirstErrors()));
  139. }
  140. return [
  141. 'code' => 0,
  142. 'msg' => '删除成功'
  143. ];
  144. } catch (\Exception $e) {
  145. return [
  146. 'code' => 1,
  147. 'msg' => $e->getMessage()
  148. ];
  149. }
  150. }
  151. }