package org.dromara.web.service.impl;

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 lombok.extern.slf4j.Slf4j;
import org.dromara.web.domain.SuppliesCategory;
import org.dromara.web.domain.bo.SuppliesCategoryBo;
import org.dromara.web.domain.vo.SuppliesCategoryVo;
import org.dromara.web.mapper.SuppliesCategoryMapper;
import org.dromara.web.service.ISuppliesCategoryService;
import org.springframework.stereotype.Service;


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

/**
 * 耗材分类Service业务层处理
 *
 * @author Lion Li
 * @date 2025-06-27
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class SuppliesCategoryServiceImpl implements ISuppliesCategoryService {

    private final SuppliesCategoryMapper baseMapper;

    /**
     * 查询耗材分类
     *
     * @param categoryId 主键
     * @return 耗材分类
     */
    @Override
    public SuppliesCategoryVo queryById(Long categoryId) {
        return baseMapper.selectVoById(categoryId);
    }

    /**
     * 分页查询耗材分类列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 耗材分类分页列表
     */
    @Override
    public TableDataInfo<SuppliesCategoryVo> queryPageList(SuppliesCategoryBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SuppliesCategory> lqw = buildQueryWrapper(bo);
        Page<SuppliesCategoryVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的耗材分类列表
     *
     * @param bo 查询条件
     * @return 耗材分类列表
     */
    @Override
    public List<SuppliesCategoryVo> queryList(SuppliesCategoryBo bo) {
        LambdaQueryWrapper<SuppliesCategory> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<SuppliesCategory> buildQueryWrapper(SuppliesCategoryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SuppliesCategory> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(SuppliesCategory::getCategoryId);
        lqw.like(StringUtils.isNotBlank(bo.getCategoryName()), SuppliesCategory::getCategoryName, bo.getCategoryName());
        return lqw;
    }

    /**
     * 新增耗材分类
     *
     * @param bo 耗材分类
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(SuppliesCategoryBo bo) {
        SuppliesCategory add = MapstructUtils.convert(bo, SuppliesCategory.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setCategoryId(add.getCategoryId());
        }
        return flag;
    }

    /**
     * 修改耗材分类
     *
     * @param bo 耗材分类
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(SuppliesCategoryBo bo) {
        SuppliesCategory update = MapstructUtils.convert(bo, SuppliesCategory.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

    /**
     * 校验并批量删除耗材分类信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
