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

import com.yc.ship.framework.ip.core.utils.AreaUtils;
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.port.vo.*;
import com.yc.ship.module.resource.dal.dataobject.port.ResourcePortDO;
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.port.ResourcePortMapper;

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 ResourcePortServiceImpl implements ResourcePortService {

    @Resource
    private ResourcePortMapper portMapper;

    @Override
    public Long createPort(ResourcePortSaveReqVO createReqVO) {
        // 插入
        ResourcePortDO port = BeanUtils.toBean(createReqVO, ResourcePortDO.class);
        port.setProvinceName(AreaUtils.format2Str(port.getProvinceId()));
        portMapper.insert(port);
        // 返回
        return port.getId();
    }

    @Override
    public void updatePort(ResourcePortSaveReqVO updateReqVO) {
        // 校验存在
        validatePortExists(updateReqVO.getId());
        // 更新
        ResourcePortDO updateObj = BeanUtils.toBean(updateReqVO, ResourcePortDO.class);
        updateObj.setProvinceName(AreaUtils.format2Str(updateObj.getProvinceId()));
        portMapper.updateById(updateObj);
    }

    @Override
    public void deletePort(Long id) {
        // 校验存在
        validatePortExists(id);
        // 删除
        portMapper.deleteById(id);
    }

    private void validatePortExists(Long id) {
        if (portMapper.selectById(id) == null) {
            throw exception(PORT_NOT_EXISTS);
        }
    }

    @Override
    public ResourcePortDO getPort(Long id) {
        return portMapper.selectById(id);
    }

    @Override
    public PageResult<ResourcePortDO> getPortPage(ResourcePortPageReqVO pageReqVO) {
        return portMapper.selectPage(pageReqVO);
    }

    @Override
    public List<ResourcePortDO> enableList() {
        return portMapper.selectEnableList();
    }

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

    @Override
    public List<ResourcePortDO> getListSimple() {
        return portMapper.selectList();
    }


}