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

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.resource.controller.admin.shiptype.vo.*;
import com.yc.ship.module.resource.dal.dataobject.shiptype.ResourceShipTypeDO;
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.resource.dal.mysql.shiptype.ResourceShipTypeMapper;

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

/**
 * 资源管理-游轮类型管理 Service 实现类
 *
 * @author lfy
 */
@Service
@Validated
public class ResourceShipTypeServiceImpl implements ResourceShipTypeService {

    @Resource
    private ResourceShipTypeMapper shipTypeMapper;

    @Override
    public Long createShipType(ResourceShipTypeSaveReqVO createReqVO) {
        // 插入
        ResourceShipTypeDO shipType = BeanUtils.toBean(createReqVO, ResourceShipTypeDO.class);
        shipTypeMapper.insert(shipType);
        // 返回
        return shipType.getId();
    }

    @Override
    public void updateShipType(ResourceShipTypeSaveReqVO updateReqVO) {
        // 校验存在
        validateShipTypeExists(updateReqVO.getId());
        // 更新
        ResourceShipTypeDO updateObj = BeanUtils.toBean(updateReqVO, ResourceShipTypeDO.class);
        shipTypeMapper.updateById(updateObj);
    }

    @Override
    public void deleteShipType(Long id) {
        // 校验存在
        validateShipTypeExists(id);
        // 删除
        shipTypeMapper.deleteById(id);
    }

    private void validateShipTypeExists(Long id) {
        if (shipTypeMapper.selectById(id) == null) {
            throw exception(SHIP_TYPE_NOT_EXISTS);
        }
    }

    @Override
    public ResourceShipTypeDO getShipType(Long id) {
        return shipTypeMapper.selectById(id);
    }

    @Override
    public PageResult<ResourceShipTypeDO> getShipTypePage(ResourceShipTypePageReqVO pageReqVO) {
        return shipTypeMapper.selectPage(pageReqVO);
    }

    @Override
    public List<ResourceShipTypeDO> getListSimple() {
        return shipTypeMapper.selectList();
    }

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

}