package com.kmall.admin.service.impl;

import com.kmall.admin.dao.AttributeDao;
import com.kmall.admin.dao.GoodsAttributeDao;
import com.kmall.admin.entity.AttributeEntity;
import com.kmall.admin.entity.GoodsAttributeEntity;
import com.kmall.admin.service.AttributeService;
import com.kmall.common.utils.RRException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service("attributeService")
public class AttributeServiceImpl implements AttributeService {
	@Autowired
	private AttributeDao attributeDao;
	@Autowired
	private GoodsAttributeDao goodsAttributeDao;
	
	@Override
	public AttributeEntity queryObject(Integer id){
		return attributeDao.queryObject(id);
	}
	
	@Override
	public List<AttributeEntity> queryList(Map<String, Object> map){
		return attributeDao.queryList(map);
	}
	
	@Override
	public int queryTotal(Map<String, Object> map){
		return attributeDao.queryTotal(map);
	}
	
	@Override
	public void save(AttributeEntity attribute){
		attributeDao.save(attribute);
	}
	
	@Override
	public void update(AttributeEntity attribute){
		attributeDao.update(attribute);
	}
	
	@Override
	public void delete(Integer id){
		attributeDao.delete(id);
	}
	
	@Override
	public void deleteBatch(Integer[] ids){
		for (Integer id : ids) {
			AttributeEntity attribute = queryObject(id);
			Map<String, Object> map = new HashMap<>();
			map.put("attributeId", id);
			List<GoodsAttributeEntity> goodsAttributeEntityList = goodsAttributeDao.queryList(map);

			if (goodsAttributeEntityList != null && goodsAttributeEntityList.size() > 0) {
				throw new RRException("商品参数【" + attribute.getName() + "】还有【" + goodsAttributeEntityList.size() + "】个商品在使用，删除失败！");
			}
			delete(id);
		}
	}
	
}
