package org.dromara.system.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.system.domain.bo.RegisterSchoolFileBo;
import org.dromara.system.domain.vo.RegisterSchoolFileVo;
import org.dromara.system.domain.RegisterSchoolFile;
import org.dromara.system.mapper.RegisterSchoolFileMapper;
import org.dromara.system.service.IRegisterSchoolFileService;

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

/**
 * 注册-学校-附件Service业务层处理
 *
 * @author boman
 * @date 2023-08-16
 */
@RequiredArgsConstructor
@Service
public class RegisterSchoolFileServiceImpl implements IRegisterSchoolFileService {

    private final RegisterSchoolFileMapper baseMapper;

    /**
     * 查询注册-学校-附件
     */
    @Override
    public RegisterSchoolFileVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询注册-学校-附件列表
     */
    @Override
    public TableDataInfo<RegisterSchoolFileVo> queryPageList(RegisterSchoolFileBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<RegisterSchoolFile> lqw = buildQueryWrapper(bo);
        Page<RegisterSchoolFileVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询注册-学校-附件列表
     */
    @Override
    public List<RegisterSchoolFileVo> queryList(RegisterSchoolFileBo bo) {
        LambdaQueryWrapper<RegisterSchoolFile> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoMapperList(lqw);
    }

    private LambdaQueryWrapper<RegisterSchoolFile> buildQueryWrapper(RegisterSchoolFileBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<RegisterSchoolFile> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getSchoolId() != null && bo.getSchoolId() != 0L, RegisterSchoolFile::getSchoolId, bo.getSchoolId());
        lqw.eq(bo.getDeptId() != null && bo.getSchoolId() != 0L, RegisterSchoolFile::getDeptId, bo.getDeptId());
        lqw.like(StringUtils.isNotBlank(bo.getName()), RegisterSchoolFile::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getUrl()), RegisterSchoolFile::getUrl, bo.getUrl());
        lqw.eq(StringUtils.isNotBlank(bo.getType()), RegisterSchoolFile::getType, bo.getType());
        return lqw;
    }

    /**
     * 新增注册-学校-附件
     */
    @Override
    public Boolean insertByBo(RegisterSchoolFileBo bo) {
        RegisterSchoolFile add = MapstructUtils.convert(bo, RegisterSchoolFile.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改注册-学校-附件
     */
    @Override
    public Boolean updateByBo(RegisterSchoolFileBo bo) {
        RegisterSchoolFile update = MapstructUtils.convert(bo, RegisterSchoolFile.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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