CommunityForm.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. namespace app\modules\admin\models\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_name;
  12. public $type;
  13. public $mobile;
  14. public $start_time;
  15. public $end_time;
  16. public $status;
  17. public $ids;
  18. public function rules() {
  19. return [
  20. [['mobile', 'user_name', 'start_time', 'end_time', 'ids'], 'string'],
  21. [['store_id', 'type', 'status'], 'integer'],
  22. ];
  23. }
  24. /**
  25. * 帖子列表
  26. */
  27. public function getList() {
  28. $type = $this->type;
  29. $mobile = $this->mobile;
  30. $status = $this->status;
  31. $store_id = $this->store_id;
  32. $start_time = $this->start_time;
  33. $end_time = $this->end_time;
  34. $user_name = $this->user_name;
  35. $query = IntegralAppreciationCommunity::find()->alias('c')
  36. ->leftJoin(['u' => User::tableName()], 'c.user_id = u.id')
  37. ->where(['c.store_id' => $store_id, 'c.is_delete' => 0]);
  38. if ($mobile) {
  39. $query->andWhere(['LIKE', 'c.mobile', $mobile]);
  40. }
  41. if ($user_name) {
  42. $query->andWhere(['LIKE', 'u.nickname', $user_name]);
  43. }
  44. if ($start_time) {
  45. $start_time = strtotime($start_time);
  46. $query->andWhere(['>=', 'c.created_at', $start_time]);
  47. }
  48. if ($end_time) {
  49. $end_time = strtotime($end_time);
  50. $query->andWhere(['<=', 'c.created_at', $end_time]);
  51. }
  52. if (isset($type) && in_array($type, [IntegralAppreciationCommunity::TYPE_BUY, IntegralAppreciationCommunity::TYPE_SELL])) {
  53. $query->andWhere(['c.type' => $type]);
  54. }
  55. if (isset($status) && in_array($status, [IntegralAppreciationCommunity::STATUS_NO_APPLY, IntegralAppreciationCommunity::STATUS_APPLY, IntegralAppreciationCommunity::STATUS_REFUSE])) {
  56. $query->andWhere(['c.status' => $status]);
  57. }
  58. $query->orderBy('c.id DESC')->select('c.id, c.type, c.integral, c.mobile,
  59. c.created_at, u.nickname, u.avatar_url avatar, u.binding user_mobile, c.status');
  60. $pagination = pagination_make($query);
  61. foreach ($pagination['list'] as &$item) {
  62. $item['type'] = intval($item['type']);
  63. $item['status'] = intval($item['status']);
  64. $item['created_at'] = date('Y-m-d H:i:s', $item['created_at']);
  65. $item['type_text'] = IntegralAppreciationCommunity::$typeArray[$item['type']];
  66. $item['status_text'] = IntegralAppreciationCommunity::$statusArray[$item['status']];
  67. }
  68. return [
  69. 'code' => 0,
  70. 'msg' => 'success',
  71. 'data' => $pagination
  72. ];
  73. }
  74. public function setStatus() {
  75. try {
  76. $ids = $this->ids;
  77. $status = $this->status;
  78. $ids = explode(',', $ids);
  79. if (!$ids) {
  80. throw new \Exception('请选择要删除的项');
  81. }
  82. if (!in_array($status, [IntegralAppreciationCommunity::STATUS_APPLY, IntegralAppreciationCommunity::STATUS_REFUSE])) {
  83. throw new \Exception('状态错误');
  84. }
  85. foreach ($ids as $id) {
  86. $community = IntegralAppreciationCommunity::findOne($id);
  87. if (intval($community->status) !== IntegralAppreciationCommunity::STATUS_NO_APPLY) {
  88. throw new \Exception('状态已经被修改');
  89. }
  90. $community->status = $status;
  91. if (!$community->save()) {
  92. throw new \Exception(implode(',', $community->getFirstErrors()));
  93. }
  94. }
  95. return [
  96. 'code' => 0,
  97. 'msg' => '操作成功'
  98. ];
  99. } catch (\Exception $e) {
  100. return [
  101. 'code' => 1,
  102. 'msg' => $e->getMessage()
  103. ];
  104. }
  105. }
  106. public function del() {
  107. try {
  108. $ids = $this->ids;
  109. $ids = explode(',', $ids);
  110. if (!$ids) {
  111. throw new \Exception('请选择要删除的项');
  112. }
  113. foreach ($ids as $id) {
  114. $goods = IntegralAppreciationCommunity::findOne($id);
  115. $goods->is_delete = 1;
  116. if (!$goods->save()) {
  117. throw new \Exception(implode(',', $goods->getFirstErrors()));
  118. }
  119. }
  120. return [
  121. 'code' => 0,
  122. 'msg' => '删除成功'
  123. ];
  124. } catch (\Exception $e) {
  125. return [
  126. 'code' => 1,
  127. 'msg' => $e->getMessage()
  128. ];
  129. }
  130. }
  131. }