package com.yc.ship.module.product.service.spuship;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import com.yc.ship.module.product.controller.admin.spuship.vo.*;
import com.yc.ship.module.product.dal.dataobject.spuship.SpuShipDO;
import com.yc.ship.framework.common.pojo.PageResult;
import com.yc.ship.framework.common.pojo.PageParam;
import com.yc.ship.framework.common.util.object.BeanUtils;

import com.yc.ship.module.product.dal.mysql.spuship.SpuShipMapper;

import static com.yc.ship.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.yc.ship.module.product.enums.ErrorCodeConstants.*;

/**
 * 产品游船 Service 实现类
 *
 * @author lsq
 */
@Service
@Validated
public class SpuShipServiceImpl implements SpuShipService {

    @Resource
    private SpuShipMapper spuShipMapper;

    @Override
    public Long createSpuShip(SpuShipSaveReqVO createReqVO) {
        // 插入
        SpuShipDO spuShip = BeanUtils.toBean(createReqVO, SpuShipDO.class);
        spuShipMapper.insert(spuShip);
        // 返回
        return spuShip.getId();
    }

    @Override
    public void updateSpuShip(SpuShipSaveReqVO updateReqVO) {
        // 校验存在
        validateSpuShipExists(updateReqVO.getId());
        // 更新
        SpuShipDO updateObj = BeanUtils.toBean(updateReqVO, SpuShipDO.class);
        spuShipMapper.updateById(updateObj);
    }

    @Override
    public void deleteSpuShip(Long id) {
        // 校验存在
        validateSpuShipExists(id);
        // 删除
        spuShipMapper.deleteById(id);
    }

    private void validateSpuShipExists(Long id) {
        if (spuShipMapper.selectById(id) == null) {
            throw exception(500,"不存在");
        }
    }

    @Override
    public SpuShipDO getSpuShip(Long id) {
        return spuShipMapper.selectById(id);
    }

    @Override
    public PageResult<SpuShipDO> getSpuShipPage(SpuShipPageReqVO pageReqVO) {
        return spuShipMapper.selectPage(pageReqVO);
    }

}