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.GameScoreBo;
import org.dromara.system.domain.vo.GameScoreVo;
import org.dromara.system.domain.GameScore;
import org.dromara.system.mapper.GameScoreMapper;
import org.dromara.system.service.IGameScoreService;

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

/**
 * 成绩Service业务层处理
 *
 * @author zlt
 * @date 2025-07-30
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class GameScoreServiceImpl implements IGameScoreService {

    private final GameScoreMapper baseMapper;

    /**
     * 查询成绩
     *
     * @param scoreId 主键
     * @return 成绩
     */
    @Override
    public GameScoreVo queryById(Long scoreId){
        return baseMapper.selectVoById(scoreId);
    }

    /**
     * 分页查询成绩列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 成绩分页列表
     */
    @Override
    public TableDataInfo<GameScoreVo> queryPageList(GameScoreBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<GameScore> lqw = buildQueryWrapper(bo);
        Page<GameScoreVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的成绩列表
     *
     * @param bo 查询条件
     * @return 成绩列表
     */
    @Override
    public List<GameScoreVo> queryList(GameScoreBo bo) {
        LambdaQueryWrapper<GameScore> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<GameScore> buildQueryWrapper(GameScoreBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<GameScore> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(GameScore::getScoreId);
        lqw.eq(bo.getEventId() != null, GameScore::getEventId, bo.getEventId());
        lqw.eq(bo.getProjectId() != null, GameScore::getProjectId, bo.getProjectId());
        lqw.eq(bo.getAthleteId() != null, GameScore::getAthleteId, bo.getAthleteId());
        lqw.eq(bo.getTeamId() != null, GameScore::getTeamId, bo.getTeamId());
        lqw.eq(StringUtils.isNotBlank(bo.getStatusFlag()), GameScore::getStatusFlag, bo.getStatusFlag());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), GameScore::getStatus, bo.getStatus());
        return lqw;
    }

    /**
     * 新增成绩
     *
     * @param bo 成绩
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(GameScoreBo bo) {
        GameScore add = MapstructUtils.convert(bo, GameScore.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setScoreId(add.getScoreId());
        }
        return flag;
    }

    /**
     * 修改成绩
     *
     * @param bo 成绩
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(GameScoreBo bo) {
        GameScore update = MapstructUtils.convert(bo, GameScore.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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