where('del_tag', 0) ->order('order asc') ->select(); return $this->buildSuccess([ 'list' => $list ]); } catch (\Exception $e) { return $this->buildFailed('获取失败:' . $e->getMessage()); } } /** * 新增分公司 */ public function add() { $data = input('post.'); // 验证必填字段 if (empty($data['full_name']) || empty($data['short_name'])) { return $this->buildFailed('分公司名称和简称不能为空'); } try { $data['create_uid'] = $this->getUserId(); // 获取当前登录用户ID $data['update_time'] = date('Y-m-d H-i-s'); $data['create_time'] = date('Y-m-d H-i-s'); $data['del_tag'] = 0; // 如果未设置order,则设置为当前最大order + 1 if (empty($data['order'])) { $maxOrder = Db::name('branchcom_config')->max('order'); $data['order'] = $maxOrder ? $maxOrder + 1 : 1; } $id = Db::name('branchcom_config')->insertGetId($data); return $this->buildSuccess([ 'id' => $id ], '添加成功'); } catch (\Exception $e) { return $this->buildFailed('添加失败:' . $e->getMessage()); } } /** * 更新分公司信息 */ public function update() { $data = input('post.'); // 验证必填字段 if (empty($data['id'])) { return $this->buildFailed('缺少ID参数'); } if (empty($data['full_name']) || empty($data['short_name'])) { return $this->buildFailed('分公司名称和简称不能为空'); } try { $data['update_time'] = date('Y-m-d H-i-s'); Db::name('branchcom_config') ->where('id', $data['id']) ->update($data); return $this->buildSuccess([], '更新成功'); } catch (\Exception $e) { return $this->buildFailed('更新失败:' . $e->getMessage()); } } /** * 删除分公司(逻辑删除) */ public function delete() { $id = input('post.id'); if (empty($id)) { return $this->buildFailed('缺少ID参数'); } try { $data = [ 'del_tag' => 2, 'create_uid' => $this->getUserId(), // 记录删除人ID 'update_time' => date('Y-m-d H-i-s') ]; Db::name('branchcom_config') ->where('id', $id) ->update($data); return $this->buildSuccess([], '删除成功'); } catch (\Exception $e) { return $this->buildFailed('删除失败:' . $e->getMessage()); } } }