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.domain.StudentLeaderExperience;
import org.dromara.ygpassport.domain.bo.StudentLeaderExperienceBo;
import org.dromara.ygpassport.mapper.StudentLeaderExperienceNewMapper;
import org.springframework.stereotype.Service;
import org.dromara.ygpassport.domain.vo.StudentLeaderExperienceVo;
import org.dromara.ygpassport.mapper.StudentLeaderExperienceMapper;
import org.dromara.ygpassport.service.IStudentLeaderExperienceService;

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

/**
 * 学生干部经历Service业务层处理
 *
 * @author Lion Li
 * @date 2024-01-11
 */
@RequiredArgsConstructor
@Service
public class StudentLeaderExperienceServiceImpl  extends MPJBaseServiceImpl<StudentLeaderExperienceNewMapper, StudentLeaderExperience> implements IStudentLeaderExperienceService {

    private final StudentLeaderExperienceMapper baseMapper;

    private final StudentLeaderExperienceNewMapper newMapper;

    /**
     * 查询学生干部经历
     */
    @Override
    public StudentLeaderExperienceVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询学生干部经历列表
     */
    @Override
    public TableDataInfo<StudentLeaderExperienceVo> queryPageList(StudentLeaderExperienceBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<StudentLeaderExperience> lqw = buildQueryWrapper(bo);
        Page<StudentLeaderExperienceVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询学生干部经历列表
     */
    @Override
    public List<StudentLeaderExperienceVo> queryList(StudentLeaderExperienceBo bo) {
        LambdaQueryWrapper<StudentLeaderExperience> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<StudentLeaderExperience> buildQueryWrapper(StudentLeaderExperienceBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<StudentLeaderExperience> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getId() != null, StudentLeaderExperience::getId, bo.getId());
        lqw.eq(bo.getStuNo() != null, StudentLeaderExperience::getStuNo, bo.getStuNo());
        lqw.eq(StringUtils.isNotBlank(bo.getLeaderExperience()), StudentLeaderExperience::getLeaderExperience, bo.getLeaderExperience());
        lqw.eq(StringUtils.isNotBlank(bo.getExperienceFilePath()), StudentLeaderExperience::getExperienceFilePath, bo.getExperienceFilePath());
        lqw.eq(StringUtils.isNotBlank(bo.getRemark()), StudentLeaderExperience::getRemark, bo.getRemark());
        return lqw;
    }

    /**
     * 新增学生干部经历
     */
    @Override
    public Boolean insertByBo(StudentLeaderExperienceBo bo) {
        StudentLeaderExperience add = MapstructUtils.convert(bo, StudentLeaderExperience.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改学生干部经历
     */
    @Override
    public Boolean updateByBo(StudentLeaderExperienceBo bo) {
        StudentLeaderExperience update = MapstructUtils.convert(bo, StudentLeaderExperience.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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

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