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

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.dock.vo.*;
import com.yc.ship.module.resource.dal.dataobject.dock.ResourceDockDO;
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.dock.ResourceDockMapper;

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 ResourceDockServiceImpl implements ResourceDockService {

    @Resource
    private ResourceDockMapper dockMapper;

    @Override
    public Long createDock(ResourceDockSaveReqVO createReqVO) {
        // 插入
        ResourceDockDO dock = BeanUtils.toBean(createReqVO, ResourceDockDO.class);
        dockMapper.insert(dock);
        // 返回
        return dock.getId();
    }

    @Override
    public void updateDock(ResourceDockSaveReqVO updateReqVO) {
        // 校验存在
        validateDockExists(updateReqVO.getId());
        // 更新
        ResourceDockDO updateObj = BeanUtils.toBean(updateReqVO, ResourceDockDO.class);
        dockMapper.updateById(updateObj);
    }

    @Override
    public void deleteDock(Long id) {
        // 校验存在
        validateDockExists(id);
        // 删除
        dockMapper.deleteById(id);
    }

    private void validateDockExists(Long id) {
        if (dockMapper.selectById(id) == null) {
            throw exception(DOCK_NOT_EXISTS);
        }
    }

    @Override
    public ResourceDockDO getDock(Long id) {
        return dockMapper.selectById(id);
    }

    @Override
    public PageResult<ResourceDockDO> getDockPage(ResourceDockPageReqVO pageReqVO) {
        return dockMapper.selectPage(pageReqVO);
    }

    @Override
    public List<ResourceDockDO> getEnableList() {
        return dockMapper.selectEnableList();
    }

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

    @Override
    public List<ResourceDockDO> getListSimple() {
        return dockMapper.selectList();
    }

}