package com.ghy.goods.service.impl;

import com.ghy.common.core.text.Convert;
import com.ghy.goods.domain.Goods;
import com.ghy.goods.domain.GoodsCategory;
import com.ghy.goods.mapper.GoodsCategoryMapper;
import com.ghy.goods.mapper.GoodsMapper;
import com.ghy.goods.service.GoodsCategoryService;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author clunt
 * 商品类别impl
 */
@Service
public class GoodsCategoryServiceImpl implements GoodsCategoryService {

    @Resource
    private GoodsMapper goodsMapper;
    @Resource
    GoodsCategoryMapper goodsCategoryMapper;

    @Override
    public int insertGoodsCategory(GoodsCategory goodsCategory) {
        return goodsCategoryMapper.insertCategoryGoods(goodsCategory);
    }

    @Override
    public int updateGoodsCategory(GoodsCategory goodsCategory) {
        return goodsCategoryMapper.updateGoodsCategory(goodsCategory);
    }

    @Override
    public List<GoodsCategory> selectGoodsCategoryList(GoodsCategory goodsCategory) {
        return goodsCategoryMapper.selectGoodsCategoryList(goodsCategory);
    }

    @Override
    public GoodsCategory selectById(Long goodsCategoryId) {
        return goodsCategoryMapper.selectById(goodsCategoryId);
    }

    @Override
    public int deleteGoodsCategoryByIds(String ids) {
        Long[] idArray = Convert.toLongArray(ids);
        for (Long id : idArray) {
            Goods goods = goodsMapper.selectOneByGoodsCategoryId(id);
            Assert.isNull(goods, "正在被使用的商品类别不能删除");
        }
        return goodsCategoryMapper.deleteGoodsCategoryByIds(idArray);
    }

    @Override
    public boolean checkGoodsCategoryNameUnique(GoodsCategory goodsCategory) {
        GoodsCategory category = goodsCategoryMapper.selectByGoodsCategoryName(goodsCategory.getGoodsCategoryName());
        if (category == null){
            return false;
        }
        return category.getGoodsCategoryId().equals(goodsCategory.getGoodsCategoryId());
    }

    @Override
    public boolean checkGoodsCategoryCodeUnique(GoodsCategory goodsCategory) {
        GoodsCategory category = goodsCategoryMapper.selectByGoodsCategoryCode(goodsCategory.getGoodsCategoryCode());
        if (category == null){
            return false;
        }
        return category.getGoodsCategoryId().equals(goodsCategory.getGoodsCategoryId());
    }
}
