package com.kmall.admin.service.impl;

import com.google.common.collect.ImmutableBiMap;
import com.kmall.admin.dao.CategoryDao;
import com.kmall.admin.dao.GoodsDao;
import com.kmall.admin.dao.ProductStoreRelaDao;
import com.kmall.admin.entity.CategoryEntity;
import com.kmall.admin.entity.GoodsEntity;
import com.kmall.admin.entity.ProductStoreRelaEntity;
import com.kmall.admin.service.CategoryService;
import com.kmall.common.constant.Dict;
import com.kmall.common.utils.MapBeanUtil;
import com.kmall.common.utils.R;
import com.kmall.common.utils.RRException;
import com.kmall.common.utils.ValidatorUtil;
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 ProductStoreRelaDao productStoreRelaDao;

    @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("请选择父级分类！");
            }
        }

        Map<String, Object> valideDate = MapBeanUtil.fromObject(category);
        ImmutableBiMap.Builder builder = new ImmutableBiMap.Builder();
        builder.put("merchSn", "商户");
        builder.put("storeId", "门店");
        R r = ValidatorUtil.isEmpty(builder.build(), valideDate);
        if (Integer.valueOf(r.get("code").toString()) != 0) {
            throw new RRException(r.get("msg").toString());
        }
        if(category.getLevel().equalsIgnoreCase("L1")){
            category.setParentId(0);
        }

        return categoryDao.save(category);
    }

    @Override
    public int update(CategoryEntity category) {

        Map<String, Object> valideDate = MapBeanUtil.fromObject(category);
        ImmutableBiMap.Builder builder = new ImmutableBiMap.Builder();
        builder.put("merchSn", "商户");
        builder.put("storeId", "门店");
        R r = ValidatorUtil.isEmpty(builder.build(), valideDate);
        if (Integer.valueOf(r.get("code").toString()) != 0) {
            throw new RRException(r.get("msg").toString());
        }
        if(category.getLevel().equalsIgnoreCase("L1")){
            category.setParentId(0);
        }

        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());
                        map.put("isDelete", Dict.isDelete.item_1.getItem());
                        List<ProductStoreRelaEntity> storeRelaEntityList = productStoreRelaDao.queryList(map);
                        if (storeRelaEntityList != null) {
                            goodsTotal += storeRelaEntityList.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<ProductStoreRelaEntity> storeRelaEntityList = productStoreRelaDao.queryList(map);
                if (storeRelaEntityList != null && storeRelaEntityList.size() > 0) {
                    throw new RRException("【" + category.getName() + "】商品类型下还有【" + storeRelaEntityList.size() + "】个商品，无法删除！");
                }
            }

            delete(id);
        }
        return 1;
    }
}
