package org.dromara.system.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.dromara.system.domain.bo.GameEventProjectBo;
import org.dromara.system.domain.vo.GameEventProjectVo;
import org.dromara.system.domain.GameEventProject;
import org.dromara.system.mapper.GameEventProjectMapper;
import org.dromara.system.service.IGameEventProjectService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 赛事项目Service业务层处理
 *
 * @author zlt
 * @date 2025-07-30
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class GameEventProjectServiceImpl implements IGameEventProjectService {

    private final GameEventProjectMapper baseMapper;

    /**
     * 查询赛事项目
     *
     * @param projectId 主键
     * @return 赛事项目
     */
    @Override
    public GameEventProjectVo queryById(Long projectId){
        return baseMapper.selectVoById(projectId);
    }

    /**
     * 分页查询赛事项目列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 赛事项目分页列表
     */
    @Override
    public TableDataInfo<GameEventProjectVo> queryPageList(GameEventProjectBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<GameEventProject> lqw = buildQueryWrapper(bo);
        Page<GameEventProjectVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的赛事项目列表
     *
     * @param bo 查询条件
     * @return 赛事项目列表
     */
    @Override
    public List<GameEventProjectVo> queryList(GameEventProjectBo bo) {
        LambdaQueryWrapper<GameEventProject> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<GameEventProject> buildQueryWrapper(GameEventProjectBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<GameEventProject> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(GameEventProject::getProjectId);
        lqw.eq(bo.getEventId() != null, GameEventProject::getEventId, bo.getEventId());
        lqw.like(StringUtils.isNotBlank(bo.getProjectName()), GameEventProject::getProjectName, bo.getProjectName());
        lqw.eq(StringUtils.isNotBlank(bo.getProjectType()), GameEventProject::getProjectType, bo.getProjectType());
        lqw.eq(StringUtils.isNotBlank(bo.getGroupType()), GameEventProject::getGroupType, bo.getGroupType());
        lqw.eq(bo.getStartTime() != null, GameEventProject::getStartTime, bo.getStartTime());
        lqw.eq(StringUtils.isNotBlank(bo.getOrderType()), GameEventProject::getOrderType, bo.getOrderType());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), GameEventProject::getStatus, bo.getStatus());
        return lqw;
    }

    /**
     * 新增赛事项目
     *
     * @param bo 赛事项目
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(GameEventProjectBo bo) {
        GameEventProject add = MapstructUtils.convert(bo, GameEventProject.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setProjectId(add.getProjectId());
        }
        return flag;
    }

    /**
     * 修改赛事项目
     *
     * @param bo 赛事项目
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(GameEventProjectBo bo) {
        GameEventProject update = MapstructUtils.convert(bo, GameEventProject.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(GameEventProject entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除赛事项目信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
