package org.dromara.ygpassport.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.ygpassport.domain.Bug;
import org.dromara.ygpassport.domain.bo.BugBo;
import org.dromara.ygpassport.domain.vo.BugVo;
import org.dromara.ygpassport.mapper.BugMapper;
import org.dromara.ygpassport.service.IBugService;
import org.springframework.stereotype.Service;

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

/**
 * Bug信息Service业务层处理
 *
 */
@RequiredArgsConstructor
@Service
public class BugServiceImpl implements IBugService {

    private final BugMapper baseMapper;

    /**
     * 查询Bug信息
     */
    @Override
    public BugVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询Bug信息列表
     */
    @Override
    public TableDataInfo<BugVo> queryPageList(BugBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<Bug> lqw = buildQueryWrapper(bo);
        Page<BugVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询Bug信息列表
     */
    @Override
    public List<BugVo> queryList(BugBo bo) {
        LambdaQueryWrapper<Bug> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<Bug> buildQueryWrapper(BugBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<Bug> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getStuNo()), Bug::getStuNo, bo.getStuNo());
        lqw.eq(bo.getBugType() != null, Bug::getBugType, bo.getBugType());
        lqw.eq(StringUtils.isNotBlank(bo.getBugDerp()), Bug::getBugDerp, bo.getBugDerp());
        return lqw;
    }

    /**
     * 新增Bug信息
     */
    @Override
    public Boolean insertByBo(BugBo bo) {
        Bug add = MapstructUtils.convert(bo, Bug.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改Bug信息
     */
    @Override
    public Boolean updateByBo(BugBo bo) {
        Bug update = MapstructUtils.convert(bo, Bug.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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

    @Override
    public List<BugVo> queryByBugCommiter(String bugCommiter) {
        return baseMapper.selectVoListByBugCommiter(bugCommiter);
    }
}
