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

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.system.controller.admin.reservation.vo.AppointmentPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.reservation.vo.AppointmentRespVO;
import cn.iocoder.yudao.module.system.controller.admin.reservation.vo.AppointmentSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.reservation.VisitAppointmentDO;
import cn.iocoder.yudao.module.system.dal.dataobject.reservation.VisitRegistrationDO;
import cn.iocoder.yudao.module.system.dal.mysql.reservation.VisitAppointmentMapper;
import cn.iocoder.yudao.module.system.dal.mysql.reservation.VisitRegistrationMapper;
import cn.iocoder.yudao.module.system.service.biz.BuildService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;




/**
 * 长者来访预约 Service 实现类
 *
 * @author 系统管理员
 */
@Service
@Validated
public class VisitAppointmentServiceImpl implements VisitAppointmentService {

    @Resource
    private VisitAppointmentMapper visitAppointmentMapper;
    @Resource
    private VisitRegistrationMapper visitRegistrationMapper;
    @Resource
    private BuildService buildService;


    @Override
    public Long createAppointment(AppointmentSaveReqVO createReqVO) {
        VisitAppointmentDO visitAppointmentDO = new VisitAppointmentDO();
        BeanUtils.copyProperties(createReqVO, visitAppointmentDO);
        visitAppointmentMapper.insert(visitAppointmentDO);
        return visitAppointmentDO.getId();
    }

    @Override
    public void updateAppointment(AppointmentSaveReqVO updateReqVO) {
        VisitAppointmentDO dbAppointment = visitAppointmentMapper.selectById(updateReqVO.getId());
        BeanUtils.copyProperties(updateReqVO,dbAppointment);
        visitAppointmentMapper.updateById(dbAppointment);
    }

    @Override
    public void closeAppointment(Long id) {
        VisitAppointmentDO visitAppointmentDO = visitAppointmentMapper.selectById(id);
        visitAppointmentDO.setStatus(2); //关闭状态
        visitAppointmentMapper.updateById(visitAppointmentDO);
    }

    @Override
    public AppointmentRespVO getAppointment(Long id) {
        AppointmentRespVO respVO = new AppointmentRespVO();
        VisitAppointmentDO visitAppointmentDO = visitAppointmentMapper.selectById(id);
        BeanUtils.copyProperties(visitAppointmentDO,respVO);
        String fullBedName = buildService.getFullBedNameByBedId(respVO.getBedId());
        respVO.setFullBedName(fullBedName);

        //todo 查询来访记录
        VisitRegistrationDO registrationDO = visitRegistrationMapper.selectOne(new LambdaQueryWrapperX<VisitRegistrationDO>()
                .eq(VisitRegistrationDO::getAppointmentId, id));
        respVO.setVisitRegistration(registrationDO);
        return respVO;
    }

    @Override
    public PageResult<VisitAppointmentDO> getAppointmentPage(AppointmentPageReqVO pageReqVO) {
        return visitAppointmentMapper.selectPage(pageReqVO);
    }
}