WorkerGoodsCateForm.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\admin\models\worker;
  8. use app\models\WorkerGoodsCat;
  9. class WorkerGoodsCateForm extends Model
  10. {
  11. public $id;
  12. public $name; //名称
  13. public $pic_url; //图片
  14. public $sort; //排序
  15. public $is_show; //是否显示
  16. public $store_id;
  17. public $no_page;
  18. public function rules()
  19. {
  20. return [
  21. [['sort', 'is_show', 'store_id', 'no_page'], 'integer'],
  22. [['id', 'pic_url', 'name'], 'string']
  23. ];
  24. }
  25. /*
  26. * 服务商品分类列表
  27. */
  28. public function workerGoodsCateList() {
  29. try {
  30. $name = $this->name;
  31. $is_show = $this->is_show;
  32. $store_id = $this->store_id;
  33. $query = WorkerGoodsCat::find()->where(['store_id' => $store_id, 'is_delete' => 0]);
  34. if (!is_null($is_show) && in_array($is_show, [0, 1])) {
  35. $query->andWhere(['is_show' => $is_show]);
  36. }
  37. if ($name) {
  38. $query->andWhere(['LIKE', 'name', $name]);
  39. }
  40. $query->select('id, name, pic_url, is_show, created_at, updated_at, sort')->orderBy('sort desc');
  41. if ($this->no_page) {
  42. $list = [
  43. 'list' => $query->asArray()->all(),
  44. 'pageNo' => 0,
  45. 'totalCount' => 0,
  46. ];
  47. } else {
  48. $list = pagination_make($query);
  49. }
  50. foreach ($list['list'] as &$item) {
  51. $item['is_show'] = (int)$item['is_show'];
  52. $item['created_at'] = date('Y-m-d H:i:s', $item['created_at']);
  53. $item['updated_at'] = $item['updated_at'] > 0 ? date('Y-m-d H:i:s', $item['updated_at']) : '-';
  54. }
  55. return [
  56. 'code' => 0,
  57. 'msg' => 'success',
  58. 'data' => [
  59. 'data' => $list['list'],
  60. 'pageNo' => $list['pageNo'],
  61. 'totalCount' => $list['totalCount'],
  62. ],
  63. ];
  64. } catch (\Exception $e) {
  65. return [
  66. 'code' => 1,
  67. 'msg' => $e->getMessage()
  68. ];
  69. }
  70. }
  71. /*
  72. * 服务商品分类设置
  73. */
  74. public function workerGoodsCateSave() {
  75. $t = \Yii::$app->db->beginTransaction();
  76. try {
  77. $id = $this->id;
  78. $name = $this->name;
  79. $pic_url = $this->pic_url;
  80. $sort = $this->sort;
  81. $is_show = $this->is_show;
  82. $store_id = $this->store_id;
  83. //名称或类型错误
  84. if (!$name || !in_array($is_show, [0, 1])) {
  85. throw new \Exception('参数错误');
  86. }
  87. $work_cat = WorkerGoodsCat::findOne(['id' => $id, 'is_delete' => 0, 'store_id' => $store_id]);
  88. if (!$work_cat) {
  89. $work_cat = new WorkerGoodsCat();
  90. $work_cat->store_id = $store_id;
  91. }
  92. $work_cat->name = $name;
  93. $work_cat->pic_url = $pic_url;
  94. $work_cat->sort = $sort;
  95. $work_cat->is_show = $is_show;
  96. if (!$work_cat->save()) {
  97. throw new \Exception(json_encode($work_cat->errors));
  98. }
  99. $t->commit();
  100. return [
  101. 'code' => 0,
  102. 'msg' => '操作成功'
  103. ];
  104. } catch (\Exception $e) {
  105. $t->rollBack();
  106. return [
  107. 'code' => 1,
  108. 'msg' => $e->getMessage()
  109. ];
  110. }
  111. }
  112. /*
  113. * 服务商品分类状态修改
  114. */
  115. public function workerGoodsCateStatus()
  116. {
  117. try {
  118. $id = \explode(',', $this->id);
  119. $store_id = $this->store_id;
  120. $is_show = (int)$this->is_show;
  121. $work_cat = WorkerGoodsCat::find()->where(['id' => $id, 'is_delete' => 0, 'store_id' => $store_id])->all();
  122. if (count($work_cat) == 0) {
  123. throw new \Exception('数据不存在');
  124. }
  125. foreach ($work_cat as $item) {
  126. if ($is_show === 2) { //删除
  127. $item->is_delete = 1;
  128. } else { //修改状态
  129. if (!in_array($is_show, [0, 1])) {
  130. throw new \Exception('状态错误');
  131. }
  132. $item->is_show = $is_show;
  133. }
  134. if (!$item->save()) {
  135. throw new \Exception(json_encode($item->errors));
  136. }
  137. }
  138. return [
  139. 'code' => 0,
  140. 'msg' => '操作成功'
  141. ];
  142. } catch (\Exception $e) {
  143. return [
  144. 'code' => 1,
  145. 'msg' => $e->getMessage()
  146. ];
  147. }
  148. }
  149. }