package com.ruoyi.biz.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.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 com.ruoyi.biz.domain.bo.ArClassificationBo;
import com.ruoyi.biz.domain.vo.ArClassificationVo;
import com.ruoyi.biz.domain.ArClassification;
import com.ruoyi.biz.mapper.ArClassificationMapper;
import com.ruoyi.biz.service.IArClassificationService;

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

/**
 * 分类Service业务层处理
 *
 * @author gwd
 * @date 2023-11-10
 */
@RequiredArgsConstructor
@Service
public class ArClassificationServiceImpl implements IArClassificationService {

    private final ArClassificationMapper baseMapper;

    /**
     * 查询分类
     */
    @Override
    public ArClassificationVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询分类列表
     */
    @Override
    public TableDataInfo<ArClassificationVo> queryPageList(PageQuery pageQuery) {
        LambdaQueryWrapper<ArClassification> lqw = Wrappers.lambdaQuery();
        lqw.eq(ArClassification::getDelFlag,0);
        lqw.eq(ArClassification::getParentId,0);
        Page<ArClassificationVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        List<ArClassificationVo> list = result.getRecords();
        for(ArClassificationVo classification : list){
            LambdaQueryWrapper<ArClassification> clqw = Wrappers.lambdaQuery();
            clqw.eq(ArClassification::getDelFlag,0);
            clqw.eq(ArClassification::getParentId,classification.getId());
            classification.setList(baseMapper.selectVoList(clqw));
        }
        return TableDataInfo.build(result);
    }

    /**
     * 查询分类列表
     */
    @Override
    public List<ArClassificationVo> queryList(ArClassificationBo bo) {
        LambdaQueryWrapper<ArClassification> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ArClassification> buildQueryWrapper(ArClassificationBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ArClassification> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getName()), ArClassification::getName, bo.getName());
        lqw.eq(bo.getParentId() != null, ArClassification::getParentId, bo.getParentId());
        lqw.eq(StringUtils.isNotBlank(bo.getUrlHref()), ArClassification::getUrlHref, bo.getUrlHref());
        lqw.eq(StringUtils.isNotBlank(bo.getUrlImage()), ArClassification::getUrlImage, bo.getUrlImage());
        return lqw;
    }

    /**
     * 新增分类
     */
    @Override
    public Boolean insertByBo(ArClassificationBo bo) {
        ArClassification add = BeanUtil.toBean(bo, ArClassification.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改分类
     */
    @Override
    public Boolean updateByBo(ArClassificationBo bo) {
        ArClassification update = BeanUtil.toBean(bo, ArClassification.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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