package com.sqx.modules.goods.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sqx.common.utils.Result;
import com.sqx.modules.goods.dao.GoodsClassifyDao;
import com.sqx.modules.goods.entity.Goods;
import com.sqx.modules.goods.entity.GoodsClassify;
import com.sqx.modules.goods.service.GoodsClassifyService;
import com.sqx.modules.goods.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class GoodsClassifyServiceImpl extends ServiceImpl<GoodsClassifyDao, GoodsClassify> implements GoodsClassifyService {

    @Autowired
    private GoodsService goodsService;

    @Override
    public Result insertClassify(GoodsClassify goodsClassify) {
        baseMapper.insert(goodsClassify);
        return Result.success();
    }

    @Override
    public Result updateClassify(GoodsClassify goodsClassify) {
        baseMapper.updateById(goodsClassify);
        return Result.success();
    }

    @Override
    public Result deleteClassify(Long classifyId) {
        int counts = goodsService.count(new QueryWrapper<Goods>().eq("classify_id", classifyId));
        if(counts>0){
            return Result.error("当前分类下有商品，请移除商品后操作！");
        }
        baseMapper.deleteById(classifyId);
        return Result.success();
    }
}
