package com.openhis.web.basicmanage.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.core.common.core.domain.R;
import com.core.common.utils.SecurityUtils;
import com.openhis.administration.domain.Invoice;
import com.openhis.administration.service.IInvoiceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * 发票管理控制器
 *
 * @author system
 * @date 2025-02-20
 */
@RestController
@RequestMapping("/basicmanage/invoice")
public class InvoiceController {

    @Autowired
    private IInvoiceService invoiceService;

    /**
     * 分页查询发票列表（带用户角色权限过滤）
     *
     * @param pageNo   页码
     * @param pageSize 每页条数
     * @param request  请求对象
     * @return 发票列表
     */
    @GetMapping("/page")
    public R<?> selectInvoicePage(
            @RequestParam(defaultValue = "1") Integer pageNo,
            @RequestParam(defaultValue = "10") Integer pageSize,
            HttpServletRequest request) {
        
        // 获取当前用户ID
        Long userId = SecurityUtils.getUserId();
        
        // 判断当前用户是否为管理员
        boolean isAdmin = SecurityUtils.isAdmin(userId);
        
        // 分页查询发票列表
        Page<Invoice> page = new Page<>(pageNo, pageSize);
        return R.ok(invoiceService.selectInvoicePage(page, isAdmin, userId));
    }
}