package org.dromara.ygpassport.service.impl;

import com.github.yulichang.base.MPJBaseServiceImpl;
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 org.dromara.ygpassport.mapper.ExactiveItemsNewMapper;
import org.springframework.stereotype.Service;
import org.dromara.ygpassport.domain.bo.ExactiveItemsBo;
import org.dromara.ygpassport.domain.vo.ExactiveItemsVo;
import org.dromara.ygpassport.domain.ExactiveItems;
import org.dromara.ygpassport.mapper.ExactiveItemsMapper;
import org.dromara.ygpassport.service.IExactiveItemsService;

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

/**
 * 第二课堂活动明细Service业务层处理
 *
 * @author Lion Li
 * @date 2024-04-24
 */
@RequiredArgsConstructor
@Service
public class ExactiveItemsServiceImpl extends MPJBaseServiceImpl<ExactiveItemsNewMapper, ExactiveItems> implements IExactiveItemsService {

    private final ExactiveItemsMapper baseMapper;

    /**
     * 查询第二课堂活动明细
     */
    @Override
    public ExactiveItemsVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询第二课堂活动明细列表
     */
    @Override
    public TableDataInfo<ExactiveItemsVo> queryPageList(ExactiveItemsBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ExactiveItems> lqw = buildQueryWrapper(bo);
        Page<ExactiveItemsVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询第二课堂活动明细列表
     */
    @Override
    public List<ExactiveItemsVo> queryList(ExactiveItemsBo bo) {
        LambdaQueryWrapper<ExactiveItems> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ExactiveItems> buildQueryWrapper(ExactiveItemsBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ExactiveItems> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getStuNo()), ExactiveItems::getStuNo, bo.getStuNo());
        lqw.eq(bo.getLevelId() != null, ExactiveItems::getLevelId, bo.getLevelId());
        lqw.eq(StringUtils.isNotBlank(bo.getContent()), ExactiveItems::getContent, bo.getContent());
        lqw.eq(bo.getActiveTypeId() != null, ExactiveItems::getActiveTypeId, bo.getActiveTypeId());
        lqw.eq(bo.getScore() != null, ExactiveItems::getScore, bo.getScore());
        lqw.eq(bo.getScoreUnitId() != null, ExactiveItems::getScoreUnitId, bo.getScoreUnitId());
        lqw.eq(bo.getActiveDate() != null, ExactiveItems::getActiveDate, bo.getActiveDate());
        return lqw;
    }

    /**
     * 新增第二课堂活动明细
     */
    @Override
    public Boolean insertByBo(ExactiveItemsBo bo) {
        ExactiveItems add = MapstructUtils.convert(bo, ExactiveItems.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改第二课堂活动明细
     */
    @Override
    public Boolean updateByBo(ExactiveItemsBo bo) {
        ExactiveItems update = MapstructUtils.convert(bo, ExactiveItems.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

    /**
     * 批量删除第二课堂活动明细
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
