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

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.scenic.vo.*;
import com.yc.ship.module.resource.dal.dataobject.scenic.ResourceScenicDO;
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.scenic.ResourceScenicMapper;

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 ResourceScenicServiceImpl implements ResourceScenicService {

    @Resource
    private ResourceScenicMapper scenicMapper;

    @Override
    public Long createScenic(ResourceScenicSaveReqVO createReqVO) {
        // 插入
        ResourceScenicDO scenic = BeanUtils.toBean(createReqVO, ResourceScenicDO.class);
        scenicMapper.insert(scenic);
        // 返回
        return scenic.getId();
    }

    @Override
    public void updateScenic(ResourceScenicSaveReqVO updateReqVO) {
        // 校验存在
        validateScenicExists(updateReqVO.getId());
        // 更新
        ResourceScenicDO updateObj = BeanUtils.toBean(updateReqVO, ResourceScenicDO.class);
        scenicMapper.updateById(updateObj);
    }

    @Override
    public void deleteScenic(Long id) {
        // 校验存在
        validateScenicExists(id);
        // 删除
        scenicMapper.deleteById(id);
    }

    private void validateScenicExists(Long id) {
        if (scenicMapper.selectById(id) == null) {
            throw exception(SCENIC_NOT_EXISTS);
        }
    }

    @Override
    public ResourceScenicDO getScenic(Long id) {
        return scenicMapper.selectById(id);
    }

    @Override
    public PageResult<ResourceScenicDO> getScenicPage(ResourceScenicPageReqVO pageReqVO) {
        return scenicMapper.selectPage(pageReqVO);
    }

    @Override
    public List<ResourceScenicDO> getEnableScenicList() {
        return scenicMapper.selectEnableList();
    }

    @Override
    public List<ResourceScenicDO> getList(List<Long> scenicIds) {
        if(scenicIds.isEmpty()) {
            return Collections.emptyList();
        }
        return scenicMapper.selectByIds(scenicIds);
    }

    @Override
    public List<ResourceScenicDO> getListSimple() {
        return scenicMapper.selectList();
    }

}