package cn.iocoder.yudao.module.member.service.spacecapsule;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.member.controller.admin.spacecapsule.vo.SpaceCapsulePageReqVO;
import cn.iocoder.yudao.module.member.controller.admin.spacecapsule.vo.SpaceCapsuleSaveReqVO;
import cn.iocoder.yudao.module.member.dal.dataobject.spacecapsule.SpaceCapsuleDO;
import cn.iocoder.yudao.module.member.dal.mysql.spacecapsule.SpaceCapsuleMapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.member.enums.ErrorCodeConstants.SPACE_CAPSULE_NOT_EXISTS;

/**
 * 太空舱 Service 实现类
 *
 * @author 管理员
 */
@Service
@Validated
public class SpaceCapsuleServiceImpl implements SpaceCapsuleService {

    @Resource
    private SpaceCapsuleMapper spaceCapsuleMapper;

    @Override
    public Long createSpaceCapsule(SpaceCapsuleSaveReqVO createReqVO) {
        // 插入
        SpaceCapsuleDO spaceCapsule = BeanUtils.toBean(createReqVO, SpaceCapsuleDO.class);
        spaceCapsuleMapper.insert(spaceCapsule);
        // 返回
        return spaceCapsule.getId();
    }

    @Override
    public void updateSpaceCapsule(SpaceCapsuleSaveReqVO updateReqVO) {
        // 校验存在
        validateSpaceCapsuleExists(updateReqVO.getId());
        // 更新
        SpaceCapsuleDO updateObj = BeanUtils.toBean(updateReqVO, SpaceCapsuleDO.class);
        spaceCapsuleMapper.updateById(updateObj);
    }

    @Override
    public void updateSpaceCapsule(SpaceCapsuleDO spaceCapsuleDO) {
        // 校验存在
        validateSpaceCapsuleExists(spaceCapsuleDO.getId());
        spaceCapsuleMapper.updateById(spaceCapsuleDO);
    }

    @Override
    public void deleteSpaceCapsule(Long id) {
        // 校验存在
        validateSpaceCapsuleExists(id);
        // 删除
        spaceCapsuleMapper.deleteById(id);
    }

    private void validateSpaceCapsuleExists(Long id) {
        if (spaceCapsuleMapper.selectById(id) == null) {
            throw exception(SPACE_CAPSULE_NOT_EXISTS);
        }
    }

    @Override
    public SpaceCapsuleDO getSpaceCapsule(Long id) {
        return spaceCapsuleMapper.selectById(id);
    }

    @Override
    public PageResult<SpaceCapsuleDO> getSpaceCapsulePage(SpaceCapsulePageReqVO pageReqVO) {
        return spaceCapsuleMapper.selectPage(pageReqVO);
    }

    @Override
    public List<SpaceCapsuleDO> getListByCarteenId(Long carteenId) {
        return spaceCapsuleMapper.selectList(new LambdaQueryWrapperX<SpaceCapsuleDO>().eq(SpaceCapsuleDO::getCarteenId,carteenId));
    }

    @Override
    public SpaceCapsuleDO getPayNum(String payNum) {
        return spaceCapsuleMapper.selectOne(new LambdaQueryWrapperX<SpaceCapsuleDO>()
                .eq(SpaceCapsuleDO::getPayNum,payNum).last(" limit 1"));
    }

}