package top.continew.admin.business.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import top.continew.admin.business.mapper.ScoreMapper;
import top.continew.admin.business.model.entity.ScoreDO;
import top.continew.admin.business.model.query.ScoreQuery;
import top.continew.admin.business.model.req.ScoreAddReq;
import top.continew.admin.business.model.req.ScoreEditReq;
import top.continew.admin.business.model.resp.ScoreResp;
import top.continew.admin.business.service.ScoreService;
import top.continew.starter.data.mp.util.QueryWrapperHelper;
import top.continew.starter.extension.crud.model.query.PageQuery;
import top.continew.starter.extension.crud.model.resp.PageResp;
import java.util.Collections;
import java.util.List;

/**
 * business业务实现
 *
 * @author jiay
 * @since 2025/06/26 10:24
 */
@Service
@RequiredArgsConstructor
public class ScoreServiceImpl extends ServiceImpl<ScoreMapper, ScoreDO> implements ScoreService {

    private final ScoreMapper scoreMapper;

    @Override
    public List<ScoreResp> getScoreList(ScoreQuery query) {
        QueryWrapper queryWrapper = QueryWrapperHelper.build(query);
        List list = this.list(queryWrapper);

        if (CollectionUtil.isNotEmpty(list)) {
            return BeanUtil.copyToList(list, ScoreResp.class);
        }
        return Collections.emptyList();
    }

    @Override
    public boolean addScore(ScoreAddReq req) {
        //插入记录到数据库
        ScoreDO entity = BeanUtil.copyProperties(req, ScoreDO.class);
        return scoreMapper.insert(entity) > 0;

    }

    @Override
    public boolean editScore(ScoreEditReq req) {
        ScoreDO ScoreDO = BeanUtil.copyProperties(req, ScoreDO.class);
        return scoreMapper.updateById(ScoreDO) > 0;

    }

    @Override
    public boolean deleteScore(Long id) {
        if (id != null) {
            return scoreMapper.deleteById(id) > 0;
        }
        return false;
    }

    @Override
    public ScoreResp getScore(Long id) {
        ScoreDO ScoreDO = scoreMapper.selectById(id);
        ScoreResp ScoreResp = new ScoreResp();
        if (ScoreDO != null) {
            ScoreResp = BeanUtil.copyProperties(ScoreDO, ScoreResp.class);
        }
        return ScoreResp;
    }

    @Override
    public PageResp<ScoreResp> getScorePage(ScoreQuery query, PageQuery pageQuery) {
        QueryWrapper queryWrapper = QueryWrapperHelper.build(query);
        Page page = scoreMapper.selectPage(new Page<>(pageQuery.getPage(), pageQuery.getSize()), queryWrapper);
        return PageResp.build(page);
    }
}