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.ImgTypeBo;
import org.dromara.system.domain.vo.ImgTypeVo;
import org.dromara.system.domain.ImgType;
import org.dromara.system.mapper.ImgTypeMapper;
import org.dromara.system.service.IImgTypeService;

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

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author Lion Li
 * @date 2024-12-16
 */
@RequiredArgsConstructor
@Service
public class ImgTypeServiceImpl implements IImgTypeService {

    private final ImgTypeMapper baseMapper;

    /**
     * 查询【请填写功能名称】
     */
    @Override
    public ImgTypeVo queryById(String typeC){
        return baseMapper.selectVoById(typeC);
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @Override
    public TableDataInfo<ImgTypeVo> queryPageList(ImgTypeBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ImgType> lqw = buildQueryWrapper(bo);
        Page<ImgTypeVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @Override
    public List<ImgTypeVo> queryList(ImgTypeBo bo) {
        LambdaQueryWrapper<ImgType> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ImgType> buildQueryWrapper(ImgTypeBo bo) {
        //Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ImgType> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getTypeId()), ImgType::getTypeId, bo.getTypeId());
        lqw.eq(StringUtils.isNotBlank(bo.getTypeC()), ImgType::getTypeC, bo.getTypeC());
//        lqw.eq(StringUtils.isNotBlank(bo.getImgId()), ImgType::getImgId, bo.getImgId());
        return lqw;
    }

    /**
     * 新增【请填写功能名称】
     */
    @Override
    public Boolean insertByBo(ImgTypeBo bo) {
        ImgType add = MapstructUtils.convert(bo, ImgType.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setTypeC(add.getTypeC());
        }
        return flag;
    }

    /**
     * 修改【请填写功能名称】
     */
    @Override
    public Boolean updateByBo(ImgTypeBo bo) {
        ImgType update = MapstructUtils.convert(bo, ImgType.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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

    @Override
    public boolean batchDelete(List<String> list) {
        return baseMapper.batchDelete(list);
    }
}
