package com.openhis.administration.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.core.common.utils.SecurityUtils;
import com.openhis.administration.domain.Invoice;
import com.openhis.administration.domain.Supplier;
import com.openhis.administration.mapper.InvoiceMapper;
import com.openhis.administration.service.IInvoiceService;
import com.openhis.common.enums.SupplyStatus;
import com.openhis.workflow.domain.SupplyRequest;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 发票管理Service业务层处理
 *
 * @author system
 * @date 2025-02-20
 */
@Service
public class InvoiceServiceImpl extends ServiceImpl<InvoiceMapper, Invoice> implements IInvoiceService {
    /**
     * 新增发票
     *  
     * @param invoice 发票实体
     * @return
     */
    @Override
    public Long addInvoice(Invoice invoice){
       // 根据编码判断发票是否存在
       List<Invoice> invoices =
           baseMapper.selectList(new LambdaQueryWrapper<Invoice>().eq(Invoice::getBusNo, invoice.getBusNo()));
       if (invoices.size() > 0) {
           return null;
       }
       // 新增发票
       int insert = baseMapper.insert(invoice);
       if (insert != 1) {
           return null;
       }

       return invoice.getId();
   }

    /**
     * 分页查询发票列表（带用户角色权限过滤）
     *
     * @param page 分页参数
     * @param isAdmin 是否管理员
     * @param userId 当前用户ID
     * @return 发票列表
     */
    @Override
    public IPage<Invoice> selectInvoicePage(Page<Invoice> page, boolean isAdmin, Long userId) {
        LambdaQueryWrapper<Invoice> queryWrapper = new LambdaQueryWrapper<>();
        
        // 如果不是管理员，只查询当前用户创建的发票
        if (!isAdmin) {
            queryWrapper.eq(Invoice::getInvoicingStaffId, userId);
        }
        
        // 按创建时间降序排序
        queryWrapper.orderByDesc(Invoice::getCreateTime);
        
        return baseMapper.selectPage(page, queryWrapper);
    }
}