/*
 * Copyright ©2023 CJB-CNIT Team. All rights reserved
 */
package com.openhis.web.reportmanage.appservice.impl;


import javax.servlet.http.HttpServletRequest;
import com.openhis.common.enums.*;
import com.openhis.web.reportmanage.appservice.IDrugDosageSettlementAppService;
import com.openhis.web.reportmanage.dto.DrugDosageSettlementPageDto;
import com.openhis.web.reportmanage.dto.DrugDosageSettlementSearchParam;
import com.openhis.web.reportmanage.mapper.DrugDosageSettlementMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.core.common.core.domain.R;
import com.openhis.common.utils.HisQueryUtils;

/**
 * 药品用量结算 impl
 *
 * @author yuxj
 * @date 2025-09-09
 */
@Service
public class DrugDosageSettlementAppServiceImpl implements IDrugDosageSettlementAppService {

    @Autowired
    private DrugDosageSettlementMapper drugDosageSettlementMapper;

    /**
     * 药品用量明细列表
     *
     * @param drugDosageSettlementSearchParam 药品用量明细查询条件
     * @param pageNo 当前页码
     * @param pageSize 查询条数
     * @param searchKey 模糊查询关键字
     * @param request 请求数据
     * @return 药品用量明细
     */
    @Override
    public R<?> getPage(DrugDosageSettlementSearchParam drugDosageSettlementSearchParam, Integer pageNo, Integer pageSize,
        String searchKey, HttpServletRequest request) {

        // 数据初始化，不使用eq条件拼接
        // 项目类型
        Integer categoryCode = drugDosageSettlementSearchParam.getCategoryCode();
        drugDosageSettlementSearchParam.setCategoryCode(null);
        // 抗生素flg
        Integer antibioticFlg = drugDosageSettlementSearchParam.getAntibioticFlg();
        drugDosageSettlementSearchParam.setAntibioticFlg(null);
//        // 开始时间
//        String dispenseDateSTime = drugDosageSettlementSearchParam.getDispenseDateSTime();
//        drugDosageSettlementSearchParam.setDispenseDateSTime(null);
//        // 结束时间
//        String dispenseDateETime = drugDosageSettlementSearchParam.getDispenseDateETime();
//        drugDosageSettlementSearchParam.setDispenseDateETime(null);

        // 构建查询条件
        QueryWrapper<DrugDosageSettlementSearchParam> queryWrapper = HisQueryUtils.buildQueryWrapper(
            drugDosageSettlementSearchParam, searchKey, null, request);

        // 项目类型为中成药（1）、西药（2）、外购药品（3）、中草药（4）时查药品表
        if (categoryCode != null) {
                queryWrapper.eq("category_code", categoryCode.toString());
        }

        // 查询商品调拨明细分页列表
        Page<DrugDosageSettlementPageDto> drugDosageReportPage =
            drugDosageSettlementMapper.selectDrugDosageReportPage(new Page<>(pageNo, pageSize), queryWrapper,
                DispenseStatus.COMPLETED.getValue(), RequestStatus.COMPLETED.getValue(),antibioticFlg);

        return R.ok(drugDosageReportPage);
    }
}
