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.ScholarshipNewMapper;
import org.springframework.stereotype.Service;
import org.dromara.ygpassport.domain.bo.ScholarshipBo;
import org.dromara.ygpassport.domain.vo.ScholarshipVo;
import org.dromara.ygpassport.domain.Scholarship;
import org.dromara.ygpassport.mapper.ScholarshipMapper;
import org.dromara.ygpassport.service.IScholarshipService;

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

/**
 * 奖学金获评记录Service业务层处理
 *
 * @author Lion Li
 * @date 2024-01-11
 */
@RequiredArgsConstructor
@Service
public class ScholarshipServiceImpl extends MPJBaseServiceImpl<ScholarshipNewMapper, Scholarship> implements IScholarshipService {

    private final ScholarshipMapper baseMapper;

    private final ScholarshipNewMapper newMapper;

    /**
     * 查询奖学金获评记录
     */
    @Override
    public ScholarshipVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询奖学金获评记录列表
     */
    @Override
    public TableDataInfo<ScholarshipVo> queryPageList(ScholarshipBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<Scholarship> lqw = buildQueryWrapper(bo);
        Page<ScholarshipVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询奖学金获评记录列表
     */
    @Override
    public List<ScholarshipVo> queryList(ScholarshipBo bo) {
        LambdaQueryWrapper<Scholarship> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<Scholarship> buildQueryWrapper(ScholarshipBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<Scholarship> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getStuNo()), Scholarship::getStuNo, bo.getStuNo());
        lqw.eq(bo.getGrade() != null, Scholarship::getGrade, bo.getGrade());
        lqw.eq(bo.getSemester() != null, Scholarship::getSemester, bo.getSemester());
        lqw.eq(StringUtils.isNotBlank(bo.getExperienceFilePath()), Scholarship::getExperienceFilePath, bo.getExperienceFilePath());
        return lqw;
    }

    /**
     * 新增奖学金获评记录
     */
    @Override
    public Boolean insertByBo(ScholarshipBo bo) {
        Scholarship add = MapstructUtils.convert(bo, Scholarship.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改奖学金获评记录
     */
    @Override
    public Boolean updateByBo(ScholarshipBo bo) {
        Scholarship update = MapstructUtils.convert(bo, Scholarship.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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

    @Override
    public List<ScholarshipVo> queryListByStuNo(String stuNo) {
        return baseMapper.selectVoListByStuNo(stuNo);
    }
}
