package com.ghy.goods.service.impl;

import com.ghy.common.constant.UserConstants;
import com.ghy.common.core.domain.Ztree;
import com.ghy.common.core.text.Convert;
import com.ghy.goods.domain.DeptGoodsCategory;
import com.ghy.goods.domain.GoodsCategory;
import com.ghy.goods.mapper.GoodsCategoryMapper;
import com.ghy.goods.service.DeptGoodsCategoryService;
import com.ghy.goods.service.GoodsCategoryService;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

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

    @Resource
    GoodsCategoryMapper goodsCategoryMapper;
    @Resource
    DeptGoodsCategoryService deptGoodsCategoryService;

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

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

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

    @Override
    public List<Ztree> selectCategoryTree(GoodsCategory goodsCategory) {
        List<GoodsCategory> goodsCategoryList = goodsCategoryMapper.selectGoodsCategoryList(goodsCategory);
        return initZtree(goodsCategoryList);
    }

    @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) {
            DeptGoodsCategory category = deptGoodsCategoryService.selectOneByGoodsCategoryId(id);
            Assert.isNull(category, "正在被使用的商品类别不能删除");
        }
        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());
    }

    @Override
    public List<GoodsCategory> selectByIds(Collection<Long> ids) {
        return goodsCategoryMapper.selectByIds(ids);
    }

    @Override
    public List<Ztree> tree(Long deptId, Integer type) {
        GoodsCategory goodsCategory = new GoodsCategory();
        goodsCategory.setType(type);
        List<GoodsCategory> goodsCategoryList = goodsCategoryMapper.selectGoodsCategoryList(goodsCategory);
        DeptGoodsCategory category = new DeptGoodsCategory();
        category.setDeptId(deptId);
        Set<Long> usedSet = deptGoodsCategoryService.list(category).stream()
                .map(DeptGoodsCategory::getGoodsCategoryId).collect(Collectors.toSet());
        return initZtree(goodsCategoryList, usedSet);
    }

    /**
     * List转带选中状态(checked)的ZTree
     *
     * @param goodsCategoryList 商品类目List
     * @param checked           选中的集合
     * @return
     */
    public List<Ztree> initZtree(List<GoodsCategory> goodsCategoryList, Collection<Long> checked) {
        List<Ztree> ztrees = new ArrayList<>();
        for (GoodsCategory goodsCategory : goodsCategoryList) {
            if (UserConstants.CATEGORY_NORMAL.equals(goodsCategory.getStatus())) {
                Ztree ztree = new Ztree();
                ztree.setId(goodsCategory.getGoodsCategoryId());
                ztree.setpId(goodsCategory.getParentCategoryId());
                ztree.setName(goodsCategory.getGoodsCategoryName());
                ztree.setTitle(goodsCategory.getGoodsCategoryName());
                ztrees.add(ztree);
                ztree.setChecked(checked.contains(goodsCategory.getGoodsCategoryId()));
            }
        }
        return ztrees;
    }

    /**
     * 对象转部门树
     *
     * @param goodsCategoryList 类目列表
     * @return 树结构列表
     */
    public List<Ztree> initZtree(List<GoodsCategory> goodsCategoryList) {
        List<Ztree> ztrees = new ArrayList<>();
        for (GoodsCategory goodsCategory : goodsCategoryList) {
            if (UserConstants.CATEGORY_NORMAL.equals(goodsCategory.getStatus())) {
                Ztree ztree = new Ztree();
                ztree.setId(goodsCategory.getGoodsCategoryId());
                ztree.setpId(goodsCategory.getParentCategoryId());
                ztree.setName(goodsCategory.getGoodsCategoryName());
                ztree.setTitle(goodsCategory.getGoodsCategoryName());
                ztrees.add(ztree);
            }
        }
        return ztrees;
    }

    private void setLevel(GoodsCategory goodsCategory) {
        Long parentId = goodsCategory.getParentCategoryId();
        GoodsCategory parent;
        if (parentId == null || (parent = goodsCategoryMapper.selectById(parentId)) == null) {
            goodsCategory.setLevel(1);
        } else {
            goodsCategory.setLevel(parent.getLevel() + 1);
        }
    }
}
