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.CareerSkillNewMapper;
import org.springframework.stereotype.Service;
import org.dromara.ygpassport.domain.bo.CareerSkillBo;
import org.dromara.ygpassport.domain.vo.CareerSkillVo;
import org.dromara.ygpassport.domain.CareerSkill;
import org.dromara.ygpassport.mapper.CareerSkillMapper;
import org.dromara.ygpassport.service.ICareerSkillService;

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

/**
 * 职业技能证书Service业务层处理
 *
 * @author Lion Li
 * @date 2024-01-11
 */
@RequiredArgsConstructor
@Service
public class CareerSkillServiceImpl extends MPJBaseServiceImpl<CareerSkillNewMapper, CareerSkill> implements ICareerSkillService {

    private final CareerSkillMapper baseMapper;

    private final CareerSkillNewMapper newMapper;

    /**
     * 查询职业技能证书
     */
    @Override
    public CareerSkillVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询职业技能证书列表
     */
    @Override
    public TableDataInfo<CareerSkillVo> queryPageList(CareerSkillBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<CareerSkill> lqw = buildQueryWrapper(bo);
        Page<CareerSkillVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询职业技能证书列表
     */
    @Override
    public List<CareerSkillVo> queryList(CareerSkillBo bo) {
        LambdaQueryWrapper<CareerSkill> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    /**
     * 根据学号查询职业技能证书列表
     */
    @Override
    public List<CareerSkillVo> queryListByStuNo(String stuNo){
        return baseMapper.selectVoListByStuNo(stuNo);
    }

    private LambdaQueryWrapper<CareerSkill> buildQueryWrapper(CareerSkillBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<CareerSkill> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getStuNo()), CareerSkill::getStuNo, bo.getStuNo());
        lqw.eq(StringUtils.isNotBlank(bo.getExperienceFilePath()), CareerSkill::getExperienceFilePath, bo.getExperienceFilePath());
        return lqw;
    }

    /**
     * 新增职业技能证书
     */
    @Override
    public Boolean insertByBo(CareerSkillBo bo) {
        CareerSkill add = MapstructUtils.convert(bo, CareerSkill.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改职业技能证书
     */
    @Override
    public Boolean updateByBo(CareerSkillBo bo) {
        CareerSkill update = MapstructUtils.convert(bo, CareerSkill.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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