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.ForeignLanguageSkillNewMapper;
import org.springframework.stereotype.Service;
import org.dromara.ygpassport.domain.bo.ForeignLanguageSkillBo;
import org.dromara.ygpassport.domain.vo.ForeignLanguageSkillVo;
import org.dromara.ygpassport.domain.ForeignLanguageSkill;
import org.dromara.ygpassport.mapper.ForeignLanguageSkillMapper;
import org.dromara.ygpassport.service.IForeignLanguageSkillService;

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

/**
 * 外语能力水平Service业务层处理
 *
 * @author Lion Li
 * @date 2024-01-11
 */
@RequiredArgsConstructor
@Service
public class ForeignLanguageSkillServiceImpl extends MPJBaseServiceImpl<ForeignLanguageSkillNewMapper, ForeignLanguageSkill> implements IForeignLanguageSkillService {

    private final ForeignLanguageSkillMapper baseMapper;

    private final ForeignLanguageSkillNewMapper newMapper;

    /**
     * 查询外语能力水平
     */
    @Override
    public ForeignLanguageSkillVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询外语能力水平列表
     */
    @Override
    public TableDataInfo<ForeignLanguageSkillVo> queryPageList(ForeignLanguageSkillBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ForeignLanguageSkill> lqw = buildQueryWrapper(bo);
        Page<ForeignLanguageSkillVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 根据学号查询外语能力水平列表
     */
    @Override
    public List<ForeignLanguageSkill> queryListByStuNo(String stuNo){
        return baseMapper.selectListByStuNo(stuNo);
    }

    /**
     * 查询外语能力水平列表
     */
    @Override
    public List<ForeignLanguageSkillVo> queryList(ForeignLanguageSkillBo bo) {
        LambdaQueryWrapper<ForeignLanguageSkill> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

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

    /**
     * 新增外语能力水平
     */
    @Override
    public Boolean insertByBo(ForeignLanguageSkillBo bo) {
        ForeignLanguageSkill add = MapstructUtils.convert(bo, ForeignLanguageSkill.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改外语能力水平
     */
    @Override
    public Boolean updateByBo(ForeignLanguageSkillBo bo) {
        ForeignLanguageSkill update = MapstructUtils.convert(bo, ForeignLanguageSkill.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

    /**
     * 批量删除外语能力水平
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
