package com.yc.ship.module.resource.service.ship;

import com.yc.ship.framework.common.pojo.PageResult;
import com.yc.ship.framework.common.util.object.BeanUtils;
import com.yc.ship.module.resource.controller.admin.ship.vo.ResourceShipPageReqVO;
import com.yc.ship.module.resource.controller.admin.ship.vo.ResourceShipSaveReqVO;
import com.yc.ship.module.resource.dal.dataobject.ship.ResourceShipDO;
import com.yc.ship.module.resource.dal.mysql.ship.ResourceShipMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

import static com.yc.ship.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.yc.ship.module.resource.enums.ErrorCodeConstants.SHIP_NOT_EXISTS;

/**
 * 资源管理-游轮管理 Service 实现类
 *
 * @author fycloud
 */
@Service
@Validated
public class ResourceShipServiceImpl implements ResourceShipService {

    @Resource
    private ResourceShipMapper resourceShipMapper;

    @Override
    public Long createShip(ResourceShipSaveReqVO createReqVO) {
        // 插入
        ResourceShipDO ship = BeanUtils.toBean(createReqVO, ResourceShipDO.class);
        resourceShipMapper.insert(ship);
        // 返回
        return ship.getId();
    }

    @Override
    public void updateShip(ResourceShipSaveReqVO updateReqVO) {
        // 校验存在
        validateShipExists(updateReqVO.getId());
        // 更新
        ResourceShipDO updateObj = BeanUtils.toBean(updateReqVO, ResourceShipDO.class);
        resourceShipMapper.updateById(updateObj);
    }

    @Override
    public void deleteShip(Long id) {
        // 校验存在
        validateShipExists(id);
        // 删除
        resourceShipMapper.deleteById(id);
    }

    private void validateShipExists(Long id) {
        if (resourceShipMapper.selectById(id) == null) {
            throw exception(SHIP_NOT_EXISTS);
        }
    }

    @Override
    public ResourceShipDO getShip(Long id) {
        return resourceShipMapper.selectById(id);
    }

    @Override
    public PageResult<ResourceShipDO> getShipPage(ResourceShipPageReqVO pageReqVO) {
        return resourceShipMapper.selectPage(pageReqVO);
    }

    @Override
    public List<ResourceShipDO> getListSimple() {
        return resourceShipMapper.selectList();
    }

    @Override
    public List<ResourceShipDO> getList(List<Long> ids) {
        if(ids.isEmpty()) {
            return Collections.emptyList();
        }
        return resourceShipMapper.selectByIds(ids);
    }

}