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.VolunteeringBo;
import org.dromara.ygpassport.domain.vo.VolunteeringVo;
import org.dromara.ygpassport.domain.Volunteering;
import org.dromara.ygpassport.mapper.VolunteeringMapper;
import org.dromara.ygpassport.service.IVolunteeringService;

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

/**
 * 志愿服务Service业务层处理
 *
 * @author Lion Li
 * @date 2024-03-16
 */
@RequiredArgsConstructor
@Service
public class VolunteeringServiceImpl implements IVolunteeringService {

    private final VolunteeringMapper baseMapper;

    /**
     * 查询志愿服务
     */
    @Override
    public VolunteeringVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询志愿服务列表
     */
    @Override
    public TableDataInfo<VolunteeringVo> queryPageList(VolunteeringBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<Volunteering> lqw = buildQueryWrapper(bo);
        Page<VolunteeringVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询志愿服务列表
     */
    @Override
    public List<VolunteeringVo> queryList(VolunteeringBo bo) {
        LambdaQueryWrapper<Volunteering> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    /**
     * 根据学号查询志愿服务列表
     */
    @Override
    public List<VolunteeringVo> queryListByStuNo(String stuNo){
        return baseMapper.selectVoListByStuNo(stuNo);
    }

    private LambdaQueryWrapper<Volunteering> buildQueryWrapper(VolunteeringBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<Volunteering> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getStuNo()), Volunteering::getStuNo, bo.getStuNo());
        lqw.eq(StringUtils.isNotBlank(bo.getActivitiesTitle()), Volunteering::getActivitiesTitle, bo.getActivitiesTitle());
        lqw.eq(StringUtils.isNotBlank(bo.getActivitiesIntroduce()), Volunteering::getActivitiesIntroduce, bo.getActivitiesIntroduce());
        lqw.eq(bo.getScore() != null, Volunteering::getScore, bo.getScore());
        lqw.eq(StringUtils.isNotBlank(bo.getExperienceFilePath()), Volunteering::getExperienceFilePath, bo.getExperienceFilePath());
        return lqw;
    }

    /**
     * 新增志愿服务
     */
    @Override
    public Boolean insertByBo(VolunteeringBo bo) {
        Volunteering add = MapstructUtils.convert(bo, Volunteering.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改志愿服务
     */
    @Override
    public Boolean updateByBo(VolunteeringBo bo) {
        Volunteering update = MapstructUtils.convert(bo, Volunteering.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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