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.InfantFeeReportDao;
import thyyxxk.webserver.entity.ResultVo;
import thyyxxk.webserver.entity.reports.infantfee.GetInfantFeeParam;
import thyyxxk.webserver.entity.reports.infantfee.InfantFee;
import thyyxxk.webserver.utils.ResultVoUtil;

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

/**
 * @author dj
 */
@Slf4j
@Service
public class InfantFeeReportService {
    private final InfantFeeReportDao dao;

    @Autowired
    public InfantFeeReportService(InfantFeeReportDao dao) {
        this.dao = dao;
    }

    public ResultVo<Map<String, Object>> getInfantFee(GetInfantFeeParam param) {
        List<InfantFee> list = dao.getInfantCharge(param);
        Map<String, Object> map = new HashMap<>(Capacity.TWO);
        AtomicReference<Double> sum = new AtomicReference<>((double) 0);
        list.forEach(item -> sum.updateAndGet(v -> (v + item.getCharge())));
        map.put("list", list);
        map.put("sum", String.format("%.2f", sum.get()));
        return ResultVoUtil.success(map);
    }
}
