| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\admin\models\worker;
- use app\models\Worker;
- use app\models\WorkerCat;
- use app\models\WorkerCatExt;
- class WorkerCateForm extends Model
- {
- public $id;
- public $name; //名称
- public $pic_url; //图片
- public $sort; //排序
- public $is_show; //是否显示
- public $store_id;
- public function rules()
- {
- return [
- [['sort', 'is_show', 'store_id'], 'integer'],
- [['id', 'pic_url', 'name'], 'string']
- ];
- }
- /*
- * 服务分类列表
- */
- public function workerCateList() {
- try {
- $name = $this->name;
- $is_show = $this->is_show;
- $store_id = $this->store_id;
- $query = WorkerCat::find()->where(['store_id' => $store_id, 'is_delete' => 0]);
- if (!is_null($is_show) && in_array($is_show, [0, 1])) {
- $query->andWhere(['is_show' => $is_show]);
- }
- if ($name) {
- $query->andWhere(['LIKE', 'name', $name]);
- }
- $query->select('id, name, pic_url, is_show, created_at, updated_at, sort')->orderBy('sort desc');
- $list = pagination_make($query);
- foreach ($list['list'] as &$item) {
- $item['is_show'] = (int)$item['is_show'];
- $item['created_at'] = date('Y-m-d H:i:s', $item['created_at']);
- $item['updated_at'] = $item['updated_at'] > 0 ? date('Y-m-d H:i:s', $item['updated_at']) : '-';
- }
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => [
- 'data' => $list['list'],
- 'pageNo' => $list['pageNo'],
- 'totalCount' => $list['totalCount'],
- ],
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- /*
- * 服务分类设置
- */
- public function workerCateSave() {
- $t = \Yii::$app->db->beginTransaction();
- try {
- $id = $this->id;
- $name = $this->name;
- $pic_url = $this->pic_url;
- $sort = $this->sort;
- $is_show = $this->is_show;
- $store_id = $this->store_id;
- //名称或类型错误
- if (!$name || !in_array($is_show, [0, 1])) {
- throw new \Exception('参数错误');
- }
- $work_cat = WorkerCat::findOne(['id' => $id, 'is_delete' => 0, 'store_id' => $store_id]);
- if (!$work_cat) {
- $work_cat = new WorkerCat();
- $work_cat->store_id = $store_id;
- }
- $work_cat->name = $name;
- $work_cat->pic_url = $pic_url;
- $work_cat->sort = $sort;
- $work_cat->is_show = $is_show;
- if (!$work_cat->save()) {
- throw new \Exception(json_encode($work_cat->errors));
- }
- $t->commit();
- return [
- 'code' => 0,
- 'msg' => '操作成功'
- ];
- } catch (\Exception $e) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- /*
- * 服务分类状态修改
- */
- public function workerCateStatus() {
- try {
- $id = \explode(',', $this->id);
- $store_id = $this->store_id;
- $is_show = (int)$this->is_show;
- $work_cat = WorkerCat::find()->where(['id' => $id, 'is_delete' => 0, 'store_id' => $store_id])->all();
- if (count($work_cat) == 0) {
- throw new \Exception('数据不存在');
- }
- foreach ($work_cat as $item) {
- if ($is_show === 2) { //删除
- $worker = WorkerCatExt::findOne(['cat_id' => $item->id, 'is_delete' => 0]);
- if ($worker) {
- throw new \Exception('该分类下有用户,不可删除');
- }
- $item->is_delete = 1;
- } else { //修改状态
- if (!in_array($is_show, [0, 1])) {
- throw new \Exception('状态错误');
- }
- $item->is_show = $is_show;
- }
- if (!$item->save()) {
- throw new \Exception(json_encode($item->errors));
- }
- }
-
- return [
- 'code' => 0,
- 'msg' => '操作成功'
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- }
|