package com.kmall.admin.service.impl;

import com.kmall.admin.dao.CategoryDao;
import com.kmall.admin.dao.GoodsDao;
import com.kmall.admin.entity.CategoryEntity;
import com.kmall.admin.entity.GoodsEntity;
import com.kmall.admin.service.CategoryService;
import com.kmall.api.contants.Dict;
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实现类
 *
 * @author Scott
 * @email
 * @date 2017-08-21 15:32:31
 */
@Service("categoryService")
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryDao categoryDao;
    @Autowired
    private GoodsDao goodsDao;

    @Override
    public CategoryEntity queryObject(Integer id) {
        return categoryDao.queryObject(id);
    }

    @Override
    public List<CategoryEntity> queryList(Map<String, Object> map) {
        return categoryDao.queryList(map);
    }

    @Override
    public int queryTotal(Map<String, Object> map) {
        return categoryDao.queryTotal(map);
    }

    @Override
    public int save(CategoryEntity category) {
        if (Dict.Level.item_L1.getItem().equals(category.getLevel())) {
            category.setParentId(0);
        } else {
            if (category.getParentId() == null) {
                throw new RRException("请选择父级分类！");
            }
        }
        return categoryDao.save(category);
    }

    @Override
    public int update(CategoryEntity category) {
        return categoryDao.update(category);
    }

    @Override
    public int delete(Integer id) {
        return categoryDao.delete(id);
    }

    @Override
    public int deleteBatch(Integer[] ids) {
        for (Integer id : ids) {
            CategoryEntity category = queryObject(id);
            Map<String, Object> map = new HashMap<>();
            if (category.getParentId() == 0) {
                map.put("parentId", id);
                List<CategoryEntity> categoryEntityList = queryList(map);

                if (categoryEntityList != null && categoryEntityList.size() > 0) {
                    int goodsTotal = 0;
                    for (CategoryEntity categoryEntity : categoryEntityList) {
                        map.clear();
                        map.put("categoryId", categoryEntity.getId());
                        List<GoodsEntity> goodsEntityList = goodsDao.queryList(map);
                        if (goodsEntityList != null) {
                            goodsTotal += goodsEntityList.size();
                        }
                    }

                    if (goodsTotal > 0) {
                        throw new RRException("【" + category.getName() + "】商品类型下还有【" + goodsTotal + "】个商品，无法删除！");
                    } else {
                        for (CategoryEntity categoryEntity : categoryEntityList) {
                            delete(categoryEntity.getId());
                        }
                    }
                }
            } else{
                map.clear();
                map.put("categoryId",id);
                List<GoodsEntity> goodsEntityList = goodsDao.queryList(map);
                if (goodsEntityList != null && goodsEntityList.size() > 0) {
                    throw new RRException("【" + category.getName() + "】商品类型下还有【" + goodsEntityList.size() + "】个商品，无法删除！");
                }
            }

            delete(id);
        }
        return 1;
    }
}
