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.framework.tenant.core.context.TenantContextHolder;
import cn.iocoder.yudao.module.system.controller.admin.reservation.vo.ReservationBedPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.reservation.vo.ReservationBedRespVO;
import cn.iocoder.yudao.module.system.controller.admin.reservation.vo.ReservationBedSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.reservation.ReservationBedDO;
import cn.iocoder.yudao.module.system.dal.mysql.biz.BuildBedMapper;
import cn.iocoder.yudao.module.system.dal.mysql.reservation.ReservationBedMapper;
import cn.iocoder.yudao.module.system.service.biz.BuildService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;

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


/**
 * 长者预约床位 Service 实现类
 *
 * @author 系统管理员
 */
@Service
@Validated
public class ReservationBedServiceImpl implements ReservationBedService {

    @Resource
    private ReservationBedMapper reservationBedMapper;
    @Resource
    private BuildBedMapper buildBedMapper;
    @Resource
    private BuildService buildService;

    @Override
    public void createReservationBed(ReservationBedSaveReqVO createReqVO) {
        ReservationBedDO existBed = reservationBedMapper.selectOne(new LambdaQueryWrapperX<ReservationBedDO>()
                .eq(ReservationBedDO::getIdNumber, createReqVO.getIdNumber())
                .eq(ReservationBedDO::getTenantId, TenantContextHolder.getTenantId()));
        if (existBed != null) {
            throw exceptionCustomMsg(COMMON_ERROR,"该身份证长者已预约床位");
        }
        ReservationBedDO reservationBedDO = new ReservationBedDO();
        BeanUtils.copyProperties(createReqVO, reservationBedDO);
        reservationBedDO.setPurposeType("预约床位");
        reservationBedMapper.insert(reservationBedDO);
    }

    @Override
    public ReservationBedRespVO getReservationBed(Long id) {
        ReservationBedDO reservationBedDO = reservationBedMapper.selectById(id);
        ReservationBedRespVO respVO = new ReservationBedRespVO();
        BeanUtils.copyProperties(reservationBedDO, respVO);
        String fullBedName = buildService.getFullBedNameByBedId(reservationBedDO.getBedId());
        respVO.setFullBedName(fullBedName);
        return respVO;
    }

    @Override
    public PageResult<ReservationBedDO> getReservationBedPage(ReservationBedPageReqVO pageReqVO) {
        return reservationBedMapper.selectPage(pageReqVO);
    }

    @Override
    public void updateReservationBed(ReservationBedSaveReqVO updateReqVO) {
        if (updateReqVO.getStatus() == 2) {
            throw exceptionCustomMsg(COMMON_ERROR,"预约已缴费无法修改");
        }
        ReservationBedDO reservationBedDO = reservationBedMapper.selectById(updateReqVO.getId());
        boolean isChangeBed = !reservationBedDO.getBedId().equals(updateReqVO.getBedId());
        BeanUtils.copyProperties(updateReqVO, reservationBedDO);
        if(isChangeBed){
            reservationBedDO.setStatus(1);
            reservationBedDO.setPayStatus(0);
        }
        reservationBedMapper.updateById(reservationBedDO);
    }

    @Override
    public void closeReservationBed(Long id) {
        ReservationBedDO reservationBedDO = reservationBedMapper.selectById(id);
        reservationBedDO.setStatus(3);
        reservationBedMapper.updateById(reservationBedDO);
    }
}