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() ]; } } }