| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\admin\models\worker;
- use app\models\Worker;
- use app\models\WorkerLevel;
- class WorkerLevelForm extends Model
- {
- public $id;
- public $name; //名称
- public $pic_url; //图片
- public $status; //是否显示
- public $level;
- public $profit_rate;
- public $order_money;
- public $order_count;
- public $store_id;
- public function rules()
- {
- return [
- [['id', 'sort', 'status', 'order_count', 'store_id'], 'integer'],
- [['pic_url', 'name'], 'string'],
- [['profit_rate', 'order_money', 'level'], 'number']
- ];
- }
- /*
- * 服务人员等级列表
- */
- public function workerLevelList() {
- try {
- $name = $this->name;
- $status = $this->status;
- $store_id = $this->store_id;
- $query = WorkerLevel::find()->where(['store_id' => $store_id, 'is_delete' => 0]);
- $level_query = clone $query;
- if (!is_null($status) && in_array($status, [0, 1])) {
- $query->andWhere(['status' => $status]);
- }
- if ($name) {
- $query->andWhere(['LIKE', 'name', $name]);
- }
- $query->select('id, name, pic_url, level, profit_rate, status, created_at, updated_at, profit_rate, order_money, order_count')
- ->orderBy('level ASC');
- $list = pagination_make($query);
- foreach ($list['list'] as &$item) {
- $item['status'] = (int)$item['status'];
- $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']) : '-';
- }
- $filter_level = $level_query->select('level')->column();
- $arr = range(1, 99);
- $level = array_values(array_diff($arr, $filter_level));
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => [
- 'data' => $list['list'],
- 'pageNo' => $list['pageNo'],
- 'totalCount' => $list['totalCount'],
- 'level' => $level
- ],
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- /*
- * 服务等级设置
- */
- public function workerLevelSave() {
- $t = \Yii::$app->db->beginTransaction();
- try {
- $id = $this->id;
- $name = $this->name;
- $pic_url = $this->pic_url;
- $status = (int)$this->status ?? 1;
- $level = $this->level;
- $profit_rate = $this->profit_rate ?? '0.00';
- $order_money = $this->order_money ?? '0.00';
- $order_count = $this->order_count ?? 0;
- $store_id = $this->store_id;
- //名称或类型错误
- if (!$name || !in_array($status, [0, 1]) || !$level) {
- throw new \Exception('参数错误');
- }
- $work_level_ = WorkerLevel::findOne(['level' => $level, 'is_delete' => 0, 'store_id' => $store_id]);
- if ($work_level_ && intval($work_level_->id) !== intval($id)) {
- throw new \Exception('当前等级已存在');
- }
- $work_level = WorkerLevel::findOne(['id' => $id, 'is_delete' => 0, 'store_id' => $store_id]);
- if (!$work_level) {
- $work_level = new WorkerLevel();
- $work_level->store_id = $store_id;
- $work_level->status = $status;
- }
- $work_level->level = $level;
- $work_level->name = $name;
- $work_level->pic_url = $pic_url;
- $work_level->profit_rate = $profit_rate;
- $work_level->order_money = $order_money;
- $work_level->order_count = $order_count;
- if (!$work_level->save()) {
- throw new \Exception(json_encode($work_level->errors));
- }
- $t->commit();
- return [
- 'code' => 0,
- 'msg' => '操作成功'
- ];
- } catch (\Exception $e) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- /*
- * 服务人员等级状态修改
- */
- public function workerLevelStatus() {
- $t = \Yii::$app->db->beginTransaction();
- try {
- $id = $this->id;
- $store_id = $this->store_id;
- $status = (int)$this->status;
- $work_level = WorkerLevel::findOne(['id' => $id, 'is_delete' => 0, 'store_id' => $store_id]);
- if (!$work_level) {
- throw new \Exception('数据不存在');
- }
- if (in_array($status, [2, 0])) {
- $worker = Worker::findOne(['level' => $work_level->level, 'status' => Worker::STATUS_VALID, 'store_id' => $store_id]);
- if ($worker) {
- throw new \Exception('该等级下有用户,不可操作');
- }
- }
- if ($status === 2) { //删除
- $work_level->is_delete = 1;
- } else { //修改状态
- if (!in_array($status, [0, 1])) {
- throw new \Exception('状态错误');
- }
- $work_level->status = $status;
- }
- if (!$work_level->save()) {
- throw new \Exception(json_encode($work_level->errors));
- }
- $t->commit();
- return [
- 'code' => 0,
- 'msg' => '操作成功'
- ];
- } catch (\Exception $e) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- }
|