package thyyxxk.webserver.service.zhuyuanyisheng;

import cn.hutool.core.collection.ListUtil;
import org.springframework.stereotype.Service;
import thyyxxk.webserver.config.exception.ExceptionEnum;
import thyyxxk.webserver.dao.his.inpatient.casefrontsheet.CaseFrontSheetDao;
import thyyxxk.webserver.dao.his.zhuyuanyisheng.Level3DoctorSetupDao;
import thyyxxk.webserver.entity.ResultVo;
import thyyxxk.webserver.entity.inpatient.patient.Overview;
import thyyxxk.webserver.utils.ResultVoUtil;

import java.util.List;

@Service
public class Level3DoctorSetupService {

    private final Level3DoctorSetupDao dao;

    private final CaseFrontSheetDao caseFrontSheetDao;

    public Level3DoctorSetupService(Level3DoctorSetupDao dao, CaseFrontSheetDao caseFrontSheetDao) {
        this.dao = dao;
        this.caseFrontSheetDao = caseFrontSheetDao;
    }

    public ResultVo<String> updateDoctor(List<Overview> userInfoSelects) {
        List<List<Overview>> list = ListUtil.split(userInfoSelects, 50);

        for (List<Overview> item : list) {
            dao.updateDoctor(item);
            for (Overview overview : item) {
                try {
                    if (caseFrontSheetDao.isBatjBa2Exist(overview.getInpatientNo(), overview.getAdmissTimes()) == 0) {
                        caseFrontSheetDao.createBatjBa2(overview.getInpatientNo(), overview.getAdmissTimes());
                    }
                } catch (Exception ignored) {
                }
            }
            dao.updateZkys(item);
        }

        return ResultVoUtil.success(ExceptionEnum.SUCCESS_AND_EL_MESSAGE, "设置成功。");
    }


}
