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.CombatProjectNewMapper;
import org.springframework.stereotype.Service;
import org.dromara.ygpassport.domain.bo.CombatProjectBo;
import org.dromara.ygpassport.domain.vo.CombatProjectVo;
import org.dromara.ygpassport.domain.CombatProject;
import org.dromara.ygpassport.mapper.CombatProjectMapper;
import org.dromara.ygpassport.service.ICombatProjectService;

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

/**
 * 实战项目经历Service业务层处理
 *
 * @author Lion Li
 * @date 2024-01-11
 */
@RequiredArgsConstructor
@Service
public class CombatProjectServiceImpl extends MPJBaseServiceImpl<CombatProjectNewMapper, CombatProject> implements ICombatProjectService {

    private final CombatProjectMapper baseMapper;

    private final CombatProjectNewMapper newMapper;

    /**
     * 查询实战项目经历
     */
    @Override
    public CombatProjectVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询实战项目经历列表
     */
    @Override
    public TableDataInfo<CombatProjectVo> queryPageList(CombatProjectBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<CombatProject> lqw = buildQueryWrapper(bo);
        Page<CombatProjectVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询实战项目经历列表
     */
    @Override
    public List<CombatProjectVo> queryList(CombatProjectBo bo) {
        LambdaQueryWrapper<CombatProject> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    /**
     * 根据学号查询实战项目经历列表
     */
    @Override
    public List<CombatProjectVo> queryListByStuNo(String stuNo){
        return baseMapper.selectVoListByStuNo(stuNo);
    }

    private LambdaQueryWrapper<CombatProject> buildQueryWrapper(CombatProjectBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<CombatProject> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getStuNo()), CombatProject::getStuNo, bo.getStuNo());
        lqw.eq(bo.getSemester() != null, CombatProject::getSemester, bo.getSemester());
        lqw.like(StringUtils.isNotBlank(bo.getTutorName()), CombatProject::getTutorName, bo.getTutorName());
        lqw.like(StringUtils.isNotBlank(bo.getProjectName()), CombatProject::getProjectName, bo.getProjectName());
        lqw.eq(bo.getScore() != null, CombatProject::getScore, bo.getScore());
        return lqw;
    }

    /**
     * 新增实战项目经历
     */
    @Override
    public Boolean insertByBo(CombatProjectBo bo) {
        CombatProject add = MapstructUtils.convert(bo, CombatProject.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改实战项目经历
     */
    @Override
    public Boolean updateByBo(CombatProjectBo bo) {
        CombatProject update = MapstructUtils.convert(bo, CombatProject.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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