package cn.iocoder.yudao.module.system.service.nursing;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.system.controller.admin.nursing.vo.ElderlySpecialNursingLogListReqVO;
import cn.iocoder.yudao.module.system.controller.admin.nursing.vo.ElderlySpecialNursingLogPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.nursing.vo.ElderlySpecialNursingLogRespVO;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.ElderlyInfoDO;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.ElderlySpecialNursingLogDO;
import cn.iocoder.yudao.module.system.dal.mysql.biz.ElderlyInfoMapper;
import cn.iocoder.yudao.module.system.dal.mysql.biz.ElderlySpecialNursingLogMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

@Service
@Validated
public class ElderlySpecialNursingLogServiceImpl implements ElderlySpecialNursingLogService {

    @Resource
    private ElderlySpecialNursingLogMapper elderlySpecialNursingLogMapper;
    @Resource
    private ElderlyInfoMapper elderlyInfoMapper;

    @Override
    public PageResult<ElderlySpecialNursingLogRespVO> getPage(ElderlySpecialNursingLogPageReqVO pageReqVO) {

        Page<ElderlySpecialNursingLogRespVO> page = new Page<>(pageReqVO.getPageNo(), pageReqVO.getPageSize());
        List<ElderlySpecialNursingLogRespVO> records = elderlySpecialNursingLogMapper
                .selectPageWithElderInfo(page, pageReqVO);
        page.setRecords(records);
        return new PageResult<>(records, page.getTotal());
    }

    @Override
    public List<ElderlySpecialNursingLogRespVO> getListByElderIdAndFinishTime(ElderlySpecialNursingLogListReqVO listReqVO) {
        LocalDateTime begin = toBegin(listReqVO.getFinishTime());
        LocalDateTime endExclusive = toEndExclusive(listReqVO.getFinishTime());

        LambdaQueryWrapperX<ElderlySpecialNursingLogDO> wrapper = new LambdaQueryWrapperX<ElderlySpecialNursingLogDO>()
                .eq(ElderlySpecialNursingLogDO::getElderId, listReqVO.getElderId())
                .eqIfPresent(ElderlySpecialNursingLogDO::getTenantId, listReqVO.getTenantId())
                .geIfPresent(ElderlySpecialNursingLogDO::getFinishTime, begin)
                .ltIfPresent(ElderlySpecialNursingLogDO::getFinishTime, endExclusive)
                .orderByDesc(ElderlySpecialNursingLogDO::getFinishTime)
                .orderByDesc(ElderlySpecialNursingLogDO::getId);
        List<ElderlySpecialNursingLogDO> list = elderlySpecialNursingLogMapper.selectList(wrapper);
        if (list.isEmpty()) {
            return Collections.emptyList();
        }
        List<ElderlySpecialNursingLogRespVO> respList = BeanUtils.toBean(list, ElderlySpecialNursingLogRespVO.class);
        enrichElderInfo(respList);
        return respList;
    }

    private void enrichElderInfo(List<ElderlySpecialNursingLogRespVO> respList) {
        Set<Long> elderIds = respList.stream()
                .map(ElderlySpecialNursingLogRespVO::getElderId)
                .filter(Objects::nonNull)
                .collect(Collectors.toSet());
        if (elderIds.isEmpty()) {
            return;
        }
        Map<Long, ElderlyInfoDO> elderMap = elderlyInfoMapper.selectBatchIds(elderIds).stream()
                .collect(Collectors.toMap(ElderlyInfoDO::getId, e -> e, (a, b) -> a));
        for (ElderlySpecialNursingLogRespVO vo : respList) {
            ElderlyInfoDO elder = elderMap.get(vo.getElderId());
            if (elder == null) {
                continue;
            }
            vo.setElderName(elder.getElderName());
            vo.setBedName(elder.getBedName());
            vo.setNurseLevelName(elder.getNurseLevelName());
        }
    }

    private static LocalDateTime toBegin(LocalDate[] range) {
        if (range == null || range.length == 0 || range[0] == null) {
            return null;
        }
        return range[0].atStartOfDay();
    }

    private static LocalDateTime toEndExclusive(LocalDate[] range) {
        if (range == null || range.length < 2 || range[1] == null) {
            return null;
        }
        return range[1].plusDays(1).atStartOfDay();
    }
}
