valueExists((int) $param['type'], (int) $param['value'])) { return to_assign(1, '该类型下「值」已存在,请使用其它值'); } try { $param['create_time'] = time(); $insertId = self::strict(false)->field(true)->insertGetId($param); add_log('add', $insertId, $param); } catch (\Exception $e) { return to_assign(1, '操作失败,原因:' . $e->getMessage()); } return to_assign(0, '操作成功', ['return_id' => $insertId]); } public function edit($param) { if ($this->valueExists((int) $param['type'], (int) $param['value'], (int) $param['id'])) { return to_assign(1, '该类型下「值」已存在,请使用其它值'); } try { self::where('id', $param['id'])->strict(false)->field(true)->update($param); add_log('edit', $param['id'], $param); } catch (\Exception $e) { return to_assign(1, '操作失败,原因:' . $e->getMessage()); } return to_assign(0, '操作成功', ['return_id' => $param['id']]); } /** * 校验同 type 下 value 是否已被占用(编辑时排除自身) */ protected function valueExists(int $type, int $value, int $excludeId = 0): bool { $query = self::where(['type' => $type, 'value' => $value, 'delete_time' => 0]); if ($excludeId > 0) { $query->where('id', '<>', $excludeId); } return $query->count() > 0; } public function getById($id) { return self::find($id); } public function delById($id) { try { self::where('id', $id)->update(['delete_time' => time()]); add_log('delete', $id); } catch (\Exception $e) { return to_assign(1, '操作失败,原因:' . $e->getMessage()); } return to_assign(); } public static function getByType($type) { return self::where(['type' => $type, 'status' => 1, 'delete_time' => 0]) ->order('sort asc, id asc') ->select(); } }