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

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.system.controller.admin.reservation.vo.RegistrationPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.reservation.vo.RegistrationRespVO;
import cn.iocoder.yudao.module.system.controller.admin.reservation.vo.RegistrationSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.ElderlySyntheticAbilityDO;
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 com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;

import java.util.List;


/**
 * 来访登记 Service 实现类
 *
 * @author 系统管理员
 */
@Service
@Validated
public class VisitRegistrationServiceImpl implements VisitRegistrationService {

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


    @Override
    public Long createRegistration(RegistrationSaveReqVO createReqVO) {
        VisitRegistrationDO registrationDO = new VisitRegistrationDO();
        BeanUtils.copyProperties(createReqVO, registrationDO);
        registrationMapper.insert(registrationDO);

        if (createReqVO.getAppointmentId() != null) {
            //修改预约状态
            VisitAppointmentDO visitAppointmentDO = visitAppointmentMapper.selectById(createReqVO.getAppointmentId());
            visitAppointmentDO.setStatus(1);  //已接待
            visitAppointmentMapper.updateById(visitAppointmentDO);
        }

//        VisitAppointmentDO visitAppointmentDO = visitAppointmentMapper.selectOne(new LambdaQueryWrapper<VisitAppointmentDO>()
//                .eq(VisitAppointmentDO::getAppointedBy, createReqVO.getVisitorName()));
//
//        if (visitAppointmentDO != null) {
//            registrationDO.setAppointmentId(visitAppointmentDO.getId());
//            registrationMapper.updateById(registrationDO);
//
//            //修改预约状态
//            visitAppointmentDO.setStatus(1);  //已接待
//            visitAppointmentMapper.updateById(visitAppointmentDO);
//        }
        return registrationDO.getId();
    }

    @Override
    public void updateRegistration(RegistrationSaveReqVO updateReqVO) {
        VisitRegistrationDO registrationDO = registrationMapper.selectById(updateReqVO.getId());
        BeanUtils.copyProperties(updateReqVO, registrationDO);
        registrationMapper.updateById(registrationDO);
    }

    @Override
    public RegistrationRespVO getRegistration(Long id) {
        RegistrationRespVO respVO = new RegistrationRespVO();
        VisitRegistrationDO registrationDO = registrationMapper.selectById(id);
        BeanUtils.copyProperties(registrationDO, respVO);
        return respVO;
    }

    @Override
    public PageResult<RegistrationRespVO> getRegistrationPage(RegistrationPageReqVO pageReqVO) {
        Page<RegistrationRespVO> page = new Page<>(pageReqVO.getPageNo(), pageReqVO.getPageSize());
        List<RegistrationRespVO> list = registrationMapper.getRegistrationPage(page, pageReqVO);
        for (RegistrationRespVO respVO : list) {
            String fullBedName = buildService.getFullBedNameByBedId(respVO.getBedId());
            respVO.setFullBedName(fullBedName);
        }
        return new PageResult<>(list, page.getTotal());
    }
}