WorkerCateForm.php 5.0 KB

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