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

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.BuildBedVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.elderlybloodsugarrecord.ElderlyBloodSugarRecordPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.elderlybloodsugarrecord.ElderlyBloodSugarRecordRespVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.elderlybloodsugarrecord.ElderlyBloodSugarRecordSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.ElderlyBloodSugarRecordDO;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.ElderlyInfoDO;
import cn.iocoder.yudao.module.system.dal.mysql.biz.BuildBedMapper;
import cn.iocoder.yudao.module.system.dal.mysql.biz.ElderlyBloodSugarRecordMapper;
import cn.iocoder.yudao.module.system.dal.mysql.biz.ElderlyInfoMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;

import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.COMMON_NOT_FOUND;

@Service
@Validated
public class ElderlyBloodSugarRecordServiceImpl implements ElderlyBloodSugarRecordService {

    @Resource
    private ElderlyBloodSugarRecordMapper elderlyBloodSugarRecordMapper;

    @Autowired
    private ElderlyInfoMapper elderlyInfoMapper;

    @Autowired
    private BuildService buildService;

    @Override
    public Long createElderlyBloodSugarRecord(ElderlyBloodSugarRecordSaveReqVO createReqVO) {
        ElderlyBloodSugarRecordDO record = BeanUtils.toBean(createReqVO, ElderlyBloodSugarRecordDO.class);
        elderlyBloodSugarRecordMapper.insert(record);
        return record.getId();
    }

    @Override
    public void updateElderlyBloodSugarRecord(ElderlyBloodSugarRecordSaveReqVO updateReqVO) {
        validateElderlyBloodSugarRecordExists(updateReqVO.getId());
        ElderlyBloodSugarRecordDO updateObj = BeanUtils.toBean(updateReqVO, ElderlyBloodSugarRecordDO.class);
        elderlyBloodSugarRecordMapper.updateById(updateObj);
    }

    @Override
    public void deleteElderlyBloodSugarRecord(Long id) {
        validateElderlyBloodSugarRecordExists(id);
        elderlyBloodSugarRecordMapper.deleteById(id);
    }

    @Override
    public ElderlyBloodSugarRecordRespVO getElderlyBloodSugarRecord(Long id) {
        ElderlyBloodSugarRecordDO record = elderlyBloodSugarRecordMapper.selectById(id);
        if (record == null) {
            throw exception(COMMON_NOT_FOUND);
        }
        ElderlyBloodSugarRecordRespVO recordRespVO = BeanUtils.toBean(record, ElderlyBloodSugarRecordRespVO.class);
        ElderlyInfoDO elderlyInfoDO = elderlyInfoMapper.selectById(record.getElderId());
        recordRespVO.setElderName(elderlyInfoDO.getElderName());
        recordRespVO.setElderSex(elderlyInfoDO.getElderSex());
        BuildBedVO bedInfo = buildService.getBedInfoById(elderlyInfoDO.getBedId());
        recordRespVO.setBedName(bedInfo.getBuildBedName());
        recordRespVO.setRoomName(bedInfo.getBuildRoomName());
        return recordRespVO;
    }

    @Override
    public PageResult<ElderlyBloodSugarRecordRespVO> getElderlyBloodSugarRecordPage(ElderlyBloodSugarRecordPageReqVO pageReqVO) {
        Page<ElderlyBloodSugarRecordDO> page = new Page<>(pageReqVO.getPageNo(), pageReqVO.getPageSize());
        List<ElderlyBloodSugarRecordRespVO> list = elderlyBloodSugarRecordMapper.selectPageWithElderInfo(page, pageReqVO);
        PageResult<ElderlyBloodSugarRecordRespVO> pageResult = new PageResult<>();
        pageResult.setList(list);
        pageResult.setTotal(page.getTotal());
        return pageResult;
    }

    private void validateElderlyBloodSugarRecordExists(Long id) {
        if (id == null || elderlyBloodSugarRecordMapper.selectById(id) == null) {
            throw exception(COMMON_NOT_FOUND);
        }
    }

    @Override
    public List<ElderlyBloodSugarRecordRespVO> getElderlyBloodSugarRecordList(ElderlyBloodSugarRecordPageReqVO pageReqVO) {
        return elderlyBloodSugarRecordMapper.selectListWithElderInfo(pageReqVO);
    }
}

