package thyyxxk.webserver.service.reports;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import thyyxxk.webserver.constants.Capacity;
import thyyxxk.webserver.dao.his.reports.DeptPeriodReportDao;
import thyyxxk.webserver.entity.ResultVo;
import thyyxxk.webserver.entity.dictionary.CodeName;
import thyyxxk.webserver.entity.reports.deptperiodreport.DeptEarn;
import thyyxxk.webserver.entity.reports.deptperiodreport.DoctorEarn;
import thyyxxk.webserver.entity.reports.deptperiodreport.PatientBill;
import thyyxxk.webserver.entity.reports.deptperiodreport.QueryDeptEarnParam;
import thyyxxk.webserver.utils.ResultVoUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;

/**
 * @author dj
 */
@Slf4j
@Service
public class DeptPeriodReportService {
    private final DeptPeriodReportDao dao;

    @Autowired
    public DeptPeriodReportService(DeptPeriodReportDao dao) {
        this.dao = dao;
    }

    public ResultVo<List<CodeName>> getAllWards() {
        return ResultVoUtil.success(dao.getAllWards());
    }

    public ResultVo<Map<String, Object>> getDeptEarn(QueryDeptEarnParam param) {
        List<DeptEarn> list = new ArrayList<>();
        param.getDepts().forEach(item -> list.addAll(dao.getSimpleDeptEarn(param.getStart(), param.getEnd(), item)));
        Map<String, Object> map = new HashMap<>(Capacity.TWO);
        AtomicReference<Double> sum = new AtomicReference<>((double) 0);
        list.forEach(item -> sum.updateAndGet(v -> (v + item.getTotalCharge())));
        map.put("list", list);
        map.put("sum", String.format("%.2f", sum.get()));
        return ResultVoUtil.success(map);
    }

    public ResultVo<Map<String, Object>> getDoctorEarn(QueryDeptEarnParam param) {
        Map<String, Object> map = new HashMap<>(Capacity.THREE);
        map.put("doctorName", dao.getDoctorName(param.getDoctorCode()));
        List<DoctorEarn> list = dao.getDoctorEarn(param);
        AtomicReference<Double> sum = new AtomicReference<>((double) 0);
        list.forEach(item -> sum.updateAndGet(v -> (v + item.getChargeFee())));
        map.put("list", list);
        map.put("sum", String.format("%.2f", sum.get()));
        return ResultVoUtil.success(map);
    }

    public ResultVo<Map<String, Object>> getPatientBill(QueryDeptEarnParam param) {
        Map<String, Object> map = new HashMap<>(Capacity.FOUR);
        map.put("doctorName", dao.getDoctorName(param.getDoctorCode()));
        map.put("patientName", dao.getPatientName(param.getInpatientNo()));
        List<PatientBill> list = dao.getPatientBill(param);
        AtomicReference<Double> sum = new AtomicReference<>((double) 0);
        list.forEach(item -> sum.updateAndGet(v -> (v + item.getChargeFee())));
        map.put("list", list);
        map.put("sum", String.format("%.2f", sum.get()));
        return ResultVoUtil.success(map);
    }
}
