package org.dromara.ygpassport.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 org.springframework.stereotype.Service;
import org.dromara.ygpassport.domain.bo.ExtracActivitiesBo;
import org.dromara.ygpassport.domain.vo.ExtracActivitiesVo;
import org.dromara.ygpassport.domain.ExtracActivities;
import org.dromara.ygpassport.mapper.ExtracActivitiesMapper;
import org.dromara.ygpassport.service.IExtracActivitiesService;

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

/**
 * 第二课堂活动记录Service业务层处理
 *
 * @author Lion Li
 * @date 2024-03-16
 */
@RequiredArgsConstructor
@Service
public class ExtracActivitiesServiceImpl implements IExtracActivitiesService {

    private final ExtracActivitiesMapper baseMapper;

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

    /**
     * 查询第二课堂活动记录列表
     */
    @Override
    public TableDataInfo<ExtracActivitiesVo> queryPageList(ExtracActivitiesBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ExtracActivities> lqw = buildQueryWrapper(bo);
        Page<ExtracActivitiesVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询第二课堂活动记录列表
     */
    @Override
    public List<ExtracActivitiesVo> queryList(ExtracActivitiesBo bo) {
        LambdaQueryWrapper<ExtracActivities> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    /**
     * 根据学号查询第二课堂活动记录列表
     */
    @Override
    public List<ExtracActivitiesVo> queryListByStuNo(String stuNo){
        return baseMapper.selectVoListByStuNo(stuNo);
    }

    private LambdaQueryWrapper<ExtracActivities> buildQueryWrapper(ExtracActivitiesBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ExtracActivities> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getId() != null, ExtracActivities::getId, bo.getId());
        lqw.eq(bo.getStuNo() != null, ExtracActivities::getStuNo, bo.getStuNo());
        lqw.eq(bo.getActiveTypeId() != null, ExtracActivities::getActiveTypeId, bo.getActiveTypeId());
        lqw.like(StringUtils.isNotBlank(bo.getActiveTypeName()), ExtracActivities::getActiveTypeName, bo.getActiveTypeName());
        lqw.eq(bo.getScore() != null, ExtracActivities::getScore, bo.getScore());
        lqw.eq(bo.getScoreUnitId() != null, ExtracActivities::getScoreUnitId, bo.getScoreUnitId());
        lqw.eq(bo.getReeportCardNumber() != null, ExtracActivities::getReeportCardNumber, bo.getReeportCardNumber());
        lqw.eq(bo.getTotalScore() != null, ExtracActivities::getTotalScore, bo.getTotalScore());
        lqw.eq(StringUtils.isNotBlank(bo.getRemark()), ExtracActivities::getRemark, bo.getRemark());
        return lqw;
    }

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

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

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

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