package org.dromara.main.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.main.domain.MainGoods;
import org.dromara.main.domain.bo.MainGoodsBo;
import org.dromara.main.domain.vo.MainGoodsVo;
import org.dromara.main.mapper.MainGoodsMapper;
import org.dromara.main.service.IMainGoodsService;
import org.springframework.stereotype.Service;

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

@RequiredArgsConstructor
@Service
public class MainGoodsServiceImpl implements IMainGoodsService {

    private final MainGoodsMapper baseMapper;

    @Override
    public MainGoodsVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    @Override
    public TableDataInfo<MainGoodsVo> queryPageList(MainGoodsBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<MainGoods> lqw = buildQueryWrapper(bo);
        Page<MainGoodsVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    @Override
    public List<MainGoodsVo> queryList(MainGoodsBo bo) {
        LambdaQueryWrapper<MainGoods> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<MainGoods> buildQueryWrapper(MainGoodsBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<MainGoods> lqw = Wrappers.lambdaQuery();
        lqw.like(ObjectUtil.isNotNull(bo.getProductName()), MainGoods::getProductName, bo.getProductName());
        lqw.eq(ObjectUtil.isNotNull(bo.getProductType()), MainGoods::getProductType, bo.getProductType());
        lqw.eq(ObjectUtil.isNotNull(bo.getPositionId()), MainGoods::getPositionId, bo.getPositionId());
        lqw.eq(ObjectUtil.isNotNull(bo.getContentType()), MainGoods::getContentType, bo.getContentType());
        return lqw;
    }

    @Override
    public Boolean insertByBo(MainGoodsBo bo) {
        MainGoods add = BeanUtil.toBean(bo, MainGoods.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    @Override
    public Boolean updateByBo(MainGoodsBo bo) {
        MainGoods update = BeanUtil.toBean(bo, MainGoods.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    private void validEntityBeforeSave(MainGoods entity) {
        // 在此处可以添加业务校验
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            // 在此处可以添加删除前的业务校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
