package thyyxxk.webserver.service.dashboard;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import thyyxxk.webserver.constants.sidicts.MedType;
import thyyxxk.webserver.dao.his.dashboard.DashboardDao;
import thyyxxk.webserver.entity.ResultVo;
import thyyxxk.webserver.entity.dashboard.InpatientBrief;
import thyyxxk.webserver.utils.DateUtil;
import thyyxxk.webserver.utils.ResultVoUtil;
import thyyxxk.webserver.utils.StringUtil;

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

@Service
public class DashboardService {
    private final DashboardDao dao;

    @Autowired
    public DashboardService(DashboardDao dao) {
        this.dao = dao;
    }

    public ResultVo<List<InpatientBrief>> selectInpatientBriefs(String deptCode) {
        List<InpatientBrief> list = dao.selectInpatientBrief(deptCode);
        if (null == list) {
            list = new ArrayList<>();
        }
        if (!list.isEmpty()) {
            list.forEach(itm -> {
                itm.setIndays(DateUtil.daysBetween(new Date(), itm.getAdmissDate()));
                itm.setMedTypeName(MedType.getName(itm.getMedType()));
                if (StringUtil.isBlank(itm.getSickLevelOrderName())) {
                    itm.setSickLevel(0);
                } else {
                    if (itm.getSickLevelOrderName().equals("病重")) {
                        itm.setSickLevel(1);
                    } else if (itm.getSickLevelOrderName().equals("病危")) {
                        itm.setSickLevel(2);
                    }
                }
            });
        }
        return ResultVoUtil.success(list);
    }
}
