package thyyxxk.webserver.service.dashboard;

import com.alibaba.fastjson.JSONObject;
import lombok.Data;
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);
        transformed(list);
        return ResultVoUtil.success(list);
    }

    public List<InpatientBrief> selectInpatientBriefsV22(String deptCode) {
        List<InpatientBrief> list = dao.selectInpatientBriefV2(deptCode);
        transformed(list);
        return list;
    }

    private void transformed(List<InpatientBrief> list) {
        list.forEach(itm -> {
            itm.setIndays(DateUtil.daysBetween(new Date(), itm.getAdmissDate()));
            itm.setMedTypeName(MedType.getName(itm.getMedType()));
            itm.setAge(DateUtil.age(itm.getBirthDate(), itm.getAdmissDate()));
            if (StringUtil.isBlank(itm.getSickLevelOrderName())) {
                itm.setSickLevel(0);
            } else {
                if (itm.getSickLevelOrderName().equals("病重")) {
                    itm.setSickLevel(1);
                } else if (itm.getSickLevelOrderName().equals("病危")) {
                    itm.setSickLevel(2);
                }
            }
            String n = itm.getNursingLevel();
            if (StringUtil.notBlank(n) && n.length() > 4) {
                itm.setNursingLevel(n.substring(0, 4));
            }
        });
    }

    public ResultVo<JSONObject> selectInpatientBriefsV2(String deptCode) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("data", selectInpatientBriefsV22(deptCode));
        jsonObject.put("bedCount", dao.bedCount(deptCode));
        List<String[]> list = new ArrayList<>();
        list.add(new String[]{"病危", dao.criticallyIllCount(deptCode).toString()});
        list.add(new String[]{"病重", dao.seriouslyIllCount(deptCode).toString()});
        list.add(new String[]{"一级护理", dao.primaryCare(deptCode).toString()});
        list.add(new String[]{"二级护理", dao.secondaryCare(deptCode).toString()});
        list.add(new String[]{"入院人数", dao.admissCount(deptCode).toString()});
        list.add(new String[]{"出院人数", dao.dischargeCount(deptCode).toString()});
        list.add(new String[]{"转出人数", dao.outCount(deptCode).toString()});
        list.add(new String[]{"转入人数", dao.inCount(deptCode).toString()});
        jsonObject.put("huli", list);
        return ResultVoUtil.success(jsonObject);
    }

}
