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.CareerPlansBo;
import org.dromara.ygpassport.domain.vo.CareerPlansVo;
import org.dromara.ygpassport.domain.CareerPlans;
import org.dromara.ygpassport.mapper.CareerPlansMapper;
import org.dromara.ygpassport.service.ICareerPlansService;

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

/**
 * 生涯规划相关信息Service业务层处理
 *
 * @author Lion Li
 * @date 2024-03-11
 */
@RequiredArgsConstructor
@Service
public class CareerPlansServiceImpl implements ICareerPlansService {

    private final CareerPlansMapper baseMapper;

    /**
     * 查询生涯规划相关信息
     */
    @Override
    public CareerPlansVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询生涯规划相关信息列表
     */
    @Override
    public TableDataInfo<CareerPlansVo> queryPageList(CareerPlansBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<CareerPlans> lqw = buildQueryWrapper(bo);
        Page<CareerPlansVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询生涯规划相关信息列表
     */
    @Override
    public List<CareerPlansVo> queryList(CareerPlansBo bo) {
        LambdaQueryWrapper<CareerPlans> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

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

    /**
     * 新增生涯规划相关信息
     */
    @Override
    public Boolean insertByBo(CareerPlansBo bo) {
        CareerPlans add = MapstructUtils.convert(bo, CareerPlans.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改生涯规划相关信息
     */
    @Override
    public Boolean updateByBo(CareerPlansBo bo) {
        CareerPlans update = MapstructUtils.convert(bo, CareerPlans.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

    /**
     * 批量删除生涯规划相关信息
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

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