model = new Salary(); } /** * 获取员工工资分页列表 * @param array $where * @param int $staffId 员工ID * @return array */ public function getPage(array $where = [], int $staffId = 0) { if (empty($staffId)) { throw new ApiException('员工信息不存在'); } $field = 's.*, p.name as staff_name, c.campus_name as campus_name'; $search_model = $this->model ->alias('s') ->join('school_personnel p', 's.staff_id = p.id', 'left') ->leftJoin('school_campus_person_role cpr', 'p.id = cpr.person_id') ->leftJoin('school_campus c', 'cpr.campus_id = c.id') ->field($field) ->where('s.staff_id', $staffId) // 只能查看自己的工资 ->group('s.id') // 添加分组避免重复数据 ->order('s.created_at desc'); // 筛选条件 - 按月份筛选 if (!empty($where['salary_month'])) { $search_model->where('s.salary_month', 'like', $where['salary_month'] . '%'); } return $this->pageQuery($search_model); } /** * 获取员工工资详情 * @param int $id 工资记录ID * @param int $staffId 员工ID * @return array */ public function getInfo(int $id, int $staffId = 0) { if (empty($staffId)) { throw new ApiException('员工信息不存在'); } $info = $this->model ->alias('s') ->join('school_personnel p', 's.staff_id = p.id', 'left') ->leftJoin('school_campus_person_role cpr', 'p.id = cpr.person_id') ->leftJoin('school_campus c', 'cpr.campus_id = c.id') ->field('s.*, p.name as staff_name, c.campus_name as campus_name') ->where('s.id', $id) ->where('s.staff_id', $staffId) // 只能查看自己的工资 ->group('s.id') // 添加分组避免重复数据 ->findOrEmpty() ->toArray(); if (empty($info)) { throw new ApiException('工资条不存在或无权限查看'); } return $info; } }