package org.dromara.system.service.impl.school;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
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.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.dromara.system.domain.school.SysSchoolName;
import org.dromara.system.domain.school.bo.SysSchoolNameBo;
import org.dromara.system.domain.school.vo.SysSchoolNameVo;
import org.dromara.system.mapper.school.SysSchoolNameMapper;
import org.dromara.system.service.school.ISysSchoolNameService;
import org.springframework.stereotype.Service;

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

/**
 * 学校名称Service业务层处理
 *
 * @author boman
 * @date 2023-08-21
 */
@RequiredArgsConstructor
@Service
public class SysSchoolNameServiceImpl implements ISysSchoolNameService {

    private final SysSchoolNameMapper baseMapper;

    /**
     * 查询学校名称
     */
    @Override
    public SysSchoolNameVo queryById(Long schoolNameId){
        return baseMapper.selectVoById(schoolNameId);
    }

    /**
     * 查询学校名称列表
     */
    @Override
    public TableDataInfo<SysSchoolNameVo> queryPageList(SysSchoolNameBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SysSchoolName> lqw = buildQueryWrapper(bo);
        Page<SysSchoolNameVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询学校名称列表
     */
    @Override
    public List<SysSchoolNameVo> queryList(SysSchoolNameBo bo) {
        LambdaQueryWrapper<SysSchoolName> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<SysSchoolName> buildQueryWrapper(SysSchoolNameBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SysSchoolName> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getInitial()), SysSchoolName::getInitial, bo.getInitial());
        lqw.like(StringUtils.isNotBlank(bo.getSchoolName()), SysSchoolName::getSchoolName, bo.getSchoolName());
        lqw.eq(StringUtils.isNotBlank(bo.getSchoolContacts()), SysSchoolName::getSchoolContacts, bo.getSchoolContacts());
        lqw.eq(StringUtils.isNotBlank(bo.getSchoolContactsPhone()), SysSchoolName::getSchoolContactsPhone, bo.getSchoolContactsPhone());
        return lqw;
    }

    /**
     * 新增学校名称
     */
    @Override
    public Boolean insertByBo(SysSchoolNameBo bo) {
        SysSchoolName add = MapstructUtils.convert(bo, SysSchoolName.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setSchoolNameId(add.getSchoolNameId());
        }
        return flag;
    }

    /**
     * 修改学校名称
     */
    @Override
    public Boolean updateByBo(SysSchoolNameBo bo) {
        SysSchoolName update = MapstructUtils.convert(bo, SysSchoolName.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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