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

import javax.servlet.http.HttpServletRequest;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.core.common.utils.bean.BeanUtils;
import com.openhis.common.enums.*;
import com.openhis.common.enums.ybenums.YbMedChrgItmType;
import com.openhis.common.utils.HisQueryUtils;
import com.openhis.web.reportmanage.dto.AnnualReportStatisticsPageDto;
import com.openhis.web.reportmanage.dto.DailyReportStatisticsPageDto;
import com.openhis.web.reportmanage.dto.DrugDosageSettlementSearchParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.core.common.core.domain.R;
import com.openhis.web.reportmanage.appservice.IReportStatisticsAppService;
import com.openhis.web.reportmanage.mapper.ReportStatisticsMapper;

/**
 * 科室收入统计 controller
 *
 * @author yuxj
 * @date 2025-09-23
 */
@Service
public class ReportStatisticsAppServiceImpl implements IReportStatisticsAppService {

    @Autowired
    private ReportStatisticsMapper reportStatisticsMapper;

    /**
     * 月报明细列表
     *
     * @param startTime 开始日期
     * @param endTime 结束日期
     * @return
     */
    @Override
    public R<?> getDailyReport(String startTime, String endTime) {

        DailyReportStatisticsPageDto dailyReport = new DailyReportStatisticsPageDto();
        // -------------------------- 1. 卫生技术人员统计 --------------------------
        // todo 中医执业（助理）医师数 没查询 人员没有添加时间条件，查询的都是当前的状态
        DailyReportStatisticsPageDto.StaffStatDTO staffStat =
            reportStatisticsMapper.getStaffStat(PractitionerRoles.DOCTOR.getCode(), PractitionerRoles.NURSE.getCode());
        dailyReport.setStaffStat(staffStat);
        // -------------------------- 2. 床位与床日统计 --------------------------
        // TODO 实际开放总床日数、实际占用总床日数、出院者占用总床日数 没查询 床位没有添加时间条件，查询的都是当前的状态
        DailyReportStatisticsPageDto.BedStatDTO bedStat =
            reportStatisticsMapper.getBedStat(LocationForm.BED.getValue());
        dailyReport.setBedStat(bedStat);
        // -------------------------- 3. 收入统计（单位：千元） --------------------------
        // todo 疫苗收入给0 、结算差额给0 时间筛选用的update_time，不确定对不对
        DailyReportStatisticsPageDto.RevenueStatDTO revenueStat = this.getRevenueStat(startTime,endTime);
        dailyReport.setRevenueStat(revenueStat);
        // -------------------------- 4. 费用统计（单位：千元） --------------------------
        // todo 费用的查询没写
        // DailyReportStatisticsPageDto.ExpenseStatDTO expenseStat=reportStatisticsMapper
        // dailyReport.setExpenseStat(expenseStat);
        // -------------------------- 5. 诊疗与服务量统计 --------------------------
        // todo 只查了门诊人次数,系统里应该不存在急诊
        DailyReportStatisticsPageDto.TreatmentStatDTO treatmentStat =
            reportStatisticsMapper.getTreatmentStat(EncounterClass.AMB.getValue(), startTime, endTime);
        dailyReport.setTreatmentStat(treatmentStat);
        // -------------------------- 6. 死亡人数统计 --------------------------
        // todo 死亡人数没写
        // DailyReportStatisticsPageDto.DeathStatDTO deathStat=reportStatisticsMapper
        // dailyReport.setDeathStat(deathStat);
        // -------------------------- 7. 健康检查统计 --------------------------
        // todo 健康检查统计的查询没写,给0了
        // Integer healthCheckCount=reportStatisticsMapper
        dailyReport.setHealthCheckCount(0);

        return R.ok(dailyReport);
    }

    /**
     * 年报明细列表
     *
     * @param startTime 开始日期
     * @param endTime 结束日期
     * @return
     */
    @Override
    public R<?> getAnnualRreport(String startTime, String endTime) {
        AnnualReportStatisticsPageDto annualReportStatistics = new AnnualReportStatisticsPageDto();
        // 6.1 总收入相关字段
        // 6.1.2 事业收入
        // 6.1.2.1 医疗收入
        //todo 只写了 总收入相关字段>事业收入>医疗收入
        AnnualReportStatisticsPageDto.MedicalIncomeDTO medicalIncome =
            new AnnualReportStatisticsPageDto.MedicalIncomeDTO();
        DailyReportStatisticsPageDto.RevenueStatDTO revenueStat = this.getRevenueStat(startTime,endTime);
        BeanUtils.copyProperties(revenueStat, medicalIncome);
        annualReportStatistics.getTotalIncome().getUndertakingIncome().setMedicalIncome(medicalIncome);
        // 6.2 总费用相关字段
        // todo 费用的查询没写
        // 8 诊疗人次数相关字段
        // todo 人次的查询没写


        return R.ok(annualReportStatistics);
    }

    /**
     * 医疗收入
     *
     * @param startTime 开始日期
     * @param endTime 结束日期
     * @return
     */
    public DailyReportStatisticsPageDto.RevenueStatDTO getRevenueStat(String startTime, String endTime) {

        DailyReportStatisticsPageDto.RevenueStatDTO revenueStat =
            reportStatisticsMapper.getRevenueStat(ChargeItemStatus.BILLED.getValue(), EncounterClass.IMP.getValue(),
                EncounterClass.AMB.getValue(), YbMedChrgItmType.REGISTRATION_FEE.getValue(),
                YbMedChrgItmType.DIAGNOSTIC_FEE.getValue(), YbMedChrgItmType.CHECK_FEE.getValue(),
                YbMedChrgItmType.DIAGNOSTIC_TEST_FEE.getValue(), YbMedChrgItmType.MEDICAL_EXPENSE_FEE.getValue(),
                YbMedChrgItmType.OPERATION_FEE.getValue(), YbMedChrgItmType.SANITARY_MATERIALS_FEE.getValue(),
                YbMedChrgItmType.WEST_MEDICINE.getValue(), YbMedChrgItmType.CHINESE_MEDICINE_FEE.getValue(),
                YbMedChrgItmType.CHINESE_MEDICINE_SLICES_FEE.getValue(), YbMedChrgItmType.OTHER_FEE.getValue(),
                YbMedChrgItmType.BED_FEE.getValue(), YbMedChrgItmType.NURSING_FEE.getValue(), startTime, endTime);
        // ----总收入 = 医疗收入 = 门急诊收入 + 住院收入 + 结算差额
        // ----药品收入合计 = 西药收入 + 中成药收入 + 中药饮片收入
        // 药品收入合计
        revenueStat.setOutpatientTotalDrugRevenue(
            revenueStat.getOutpatientTotalWesternDrugRevenue().add(revenueStat.getOutpatientChinesePatentDrugRevenue())
                .add(revenueStat.getOutpatientChineseHerbRevenue()));
        // 门急诊收入合计
        revenueStat.setTotalOutpatientRevenue(revenueStat.getOutpatientRegistrationRevenue()
            .add(revenueStat.getOutpatientConsultationRevenue()).add(revenueStat.getOutpatientInspectionRevenue())
            .add(revenueStat.getOutpatientLabTestRevenue()).add(revenueStat.getOutpatientTreatmentRevenue())
            .add(revenueStat.getOutpatientSurgeryRevenue()).add(revenueStat.getOutpatientMedicalMaterialRevenue())
            .add(revenueStat.getOutpatientTotalDrugRevenue()).add(revenueStat.getOtherOutpatientRevenue()));
        // 药品收入合计
        revenueStat.setInpatientTotalDrugRevenue(revenueStat.getInpatientTotalWesternDrugRevenue()
            .add(revenueStat.getInpatientChinesePatentDrugRevenue()).add(revenueStat.getInpatientChineseHerbRevenue()));
        // 住院收入合计
        revenueStat.setTotalInpatientRevenue(
            revenueStat.getInpatientBedRevenue().add(revenueStat.getInpatientConsultationRevenue())
                .add(revenueStat.getInpatientInspectionRevenue()).add(revenueStat.getInpatientLabTestRevenue())
                .add(revenueStat.getInpatientTreatmentRevenue()).add(revenueStat.getInpatientSurgeryRevenue())
                .add(revenueStat.getInpatientNursingRevenue()).add(revenueStat.getInpatientMedicalMaterialRevenue())
                .add(revenueStat.getInpatientTotalDrugRevenue()).add(revenueStat.getOtherInpatientRevenue()));
        // 医疗收入合计
        revenueStat.setTotalMedicalRevenue(
            revenueStat.getTotalOutpatientRevenue().add(revenueStat.getTotalInpatientRevenue()));
        // 总收入
        revenueStat.setTotalRevenue(revenueStat.getTotalMedicalRevenue());
        return revenueStat;
    }

}
