package cn.iocoder.byzs.module.ai.service.mapgame;

import cn.hutool.core.collection.CollUtil;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.byzs.module.ai.controller.admin.mapgame.vo.*;
import cn.iocoder.byzs.module.ai.dal.dataobject.mapgame.MapGameDO;
import cn.iocoder.byzs.framework.common.pojo.PageResult;
import cn.iocoder.byzs.framework.common.util.object.BeanUtils;

import cn.iocoder.byzs.module.ai.dal.mysql.mapgame.MapGameMapper;

import static cn.iocoder.byzs.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.byzs.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.byzs.module.ai.enums.ErrorCodeConstants.*;

/**
 * AI-blockly地图编程游戏 Service 实现类
 *
 * @author lyb
 */
@Service
@Validated
public class MapGameServiceImpl implements MapGameService {

    @Resource
    private MapGameMapper mapGameMapper;

    @Override
    public Integer createMapGame(MapGameSaveReqVO createReqVO) {
        // 插入
        MapGameDO mapGame = BeanUtils.toBean(createReqVO, MapGameDO.class);
        mapGameMapper.insert(mapGame);
        // 返回
        return mapGame.getId();
    }

    @Override
    public void updateMapGame(MapGameSaveReqVO updateReqVO) {
        // 校验存在
        validateMapGameExists(updateReqVO.getId());
        // 更新
        MapGameDO updateObj = BeanUtils.toBean(updateReqVO, MapGameDO.class);
        mapGameMapper.updateById(updateObj);
    }

    @Override
    public void deleteMapGame(Integer id) {
        // 校验存在
        validateMapGameExists(id);
        // 删除
        mapGameMapper.deleteById(id);
    }

    @Override
        public void deleteMapGameListByIds(List<Integer> ids) {
        // 校验存在
        validateMapGameExists(ids);
        // 删除
        mapGameMapper.deleteByIds(ids);
        }

    private void validateMapGameExists(List<Integer> ids) {
        List<MapGameDO> list = mapGameMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(MAP_GAME_NOT_EXISTS);
        }
    }

    private void validateMapGameExists(Integer id) {
        if (mapGameMapper.selectById(id) == null) {
            throw exception(MAP_GAME_NOT_EXISTS);
        }
    }

    @Override
    public MapGameDO getMapGame(Integer id) {
        return mapGameMapper.selectById(id);
    }

    @Override
    public PageResult<MapGameDO> getMapGamePage(MapGamePageReqVO pageReqVO) {
        return mapGameMapper.selectPage(pageReqVO);
    }

}