package com.ghy.goods.service.impl;

import com.ghy.common.constant.UserConstants;
import com.ghy.common.core.domain.AjaxResult;
import com.ghy.common.core.domain.Ztree;
import com.ghy.common.core.text.Convert;
import com.ghy.common.utils.StringUtils;
import com.ghy.common.utils.bean.BeanUtils;
import com.ghy.goods.domain.DeptGoodsCategory;
import com.ghy.goods.domain.GoodsCategory;
import com.ghy.goods.domain.GoodsImgs;
import com.ghy.goods.mapper.DeptGoodsCategoryMapper;
import com.ghy.goods.service.DeptGoodsCategoryService;
import com.ghy.goods.service.GoodsCategoryService;
import com.ghy.goods.service.GoodsImgsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author clunt
 * 分公司使用类目
 */
@Slf4j
@Service
public class DeptGoodsCategoryServiceImpl implements DeptGoodsCategoryService {

    @Resource
    private GoodsCategoryService goodsCategoryService;
    @Resource
    private DeptGoodsCategoryMapper deptGoodsCategoryMapper;
    @Resource
    private GoodsImgsService goodsImgsService;

    @Override
    public List<DeptGoodsCategory> list(DeptGoodsCategory category) {
        return deptGoodsCategoryMapper.selectList(category);
    }

    @Override
    public int add(DeptGoodsCategory deptGoodsCategory) {
        return deptGoodsCategoryMapper.insertDeptGoodsCategory(deptGoodsCategory);
    }

    @Override
    public int edit(DeptGoodsCategory deptGoodsCategory) {
        if(StringUtils.isNotEmpty(deptGoodsCategory.getCategoryUrl())){
            String[] split = deptGoodsCategory.getCategoryUrl().split(",");
            List<GoodsImgs> list = new ArrayList<>();
            for (String url : split){
                GoodsImgs goodsImgs = new GoodsImgs();
                goodsImgs.setGoodsId(0L);
                goodsImgs.setImgUrl(url);
                goodsImgs.setImgType(3);
                goodsImgs.setRemark(String.valueOf(deptGoodsCategory.getDeptGoodsCategoryId()));
                list.add(goodsImgs);
            }
            goodsImgsService.batchInsert(list);
        }
        return deptGoodsCategoryMapper.updateDeptGoodsCategory(deptGoodsCategory);
    }

    @Override
    public int delete(Collection<Long> ids) {
        return deptGoodsCategoryMapper.deleteDeptGoodsCategoryByIds(ids);
    }

    @Override
    public DeptGoodsCategory get(long id) {
        return deptGoodsCategoryMapper.selectById(id);
    }

    @Override
    public List<Ztree> tree(Long parentId) {
        DeptGoodsCategory category = new DeptGoodsCategory();
        category.setDeptId(parentId);
        List<DeptGoodsCategory> list = list(category);
        return initZtree(list);
    }

    @Override
    public List<DeptGoodsCategory> appList(DeptGoodsCategory deptGoodsCategory) {
        // 第一层
        List<DeptGoodsCategory> goodsCategoryList = deptGoodsCategoryMapper.appList(deptGoodsCategory);
        // 第二层
        this.fillChild(goodsCategoryList);
        // 第三层
        for (DeptGoodsCategory category : goodsCategoryList) {
            if (StringUtils.isNotNull(category.getChild())) {
                this.fillChild(category.getChild());
            }
        }
        // 第四层
        for (DeptGoodsCategory category : goodsCategoryList) {
            if (StringUtils.isNotNull(category.getChild())) {
                for (DeptGoodsCategory result : category.getChild()) {
                    if (StringUtils.isNotNull(result.getChild())) {
                        this.fillChild(result.getChild());
                    }
                }
            }
        }
        return goodsCategoryList;
    }

    @Override
    public List<DeptGoodsCategory> listByStep(DeptGoodsCategory deptGoodsCategory) {
        return deptGoodsCategoryMapper.appList(deptGoodsCategory);
    }

    @Override
    public int delete(String ids) {
        Long[] array = Convert.toLongArray(ids);
        return delete(Arrays.asList(array));
    }

    @Override
    public DeptGoodsCategory selectOneByGoodsCategoryId(Long goodsCategoryId) {
        return deptGoodsCategoryMapper.selectOneByGoodsCategoryId(goodsCategoryId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult batchAdd(Long parentId, Long[] checked) {
        GoodsCategory goodsCategory = goodsCategoryService.selectById(checked[0]);
        Assert.notNull(goodsCategory, "不存在该类目: " + checked[0]);
        List<DeptGoodsCategory> oldList = deptGoodsCategoryMapper.selectByDeptId(parentId);
        Map<Long, Long> oldMap = oldList.stream().filter(x -> x.getType().equals(goodsCategory.getType()))
                .collect(Collectors.toMap(GoodsCategory::getGoodsCategoryId, DeptGoodsCategory::getDeptGoodsCategoryId));
        HashSet<Long> newSet = new HashSet<>(Arrays.asList(checked));
        Set<Long> intersection = oldList.stream().map(DeptGoodsCategory::getGoodsCategoryId).collect(Collectors.toSet());
        intersection.addAll(Arrays.asList(checked));
        for (Long id : intersection) {
            if (newSet.contains(id)) {
                if (!oldMap.containsKey(id)) {
                    DeptGoodsCategory category = new DeptGoodsCategory();
                    category.setDeptId(parentId);
                    category.setGoodsCategoryId(id);
                    deptGoodsCategoryMapper.insertDeptGoodsCategory(category);
                }
            } else if (oldMap.containsKey(id)) {
                deptGoodsCategoryMapper.deleteById(oldMap.get(id));
            }
        }
        return AjaxResult.success();
    }

    private void fillChild(List<DeptGoodsCategory> goodsCategoryList) {
        List<DeptGoodsCategory> childList;
        for (DeptGoodsCategory deptGoodsCategory : goodsCategoryList) {
            DeptGoodsCategory param = new DeptGoodsCategory();
            BeanUtils.copyProperties(deptGoodsCategory, param);
            param.setDeptGoodsCategoryId(null);
            childList = deptGoodsCategoryMapper.appList(param);
            if (childList.size() > 0) {
                deptGoodsCategory.setChild(childList);
            }
        }
    }

    public List<Ztree> initZtree(List<DeptGoodsCategory> list) {
        List<Ztree> ztrees = new ArrayList<>();
        for (GoodsCategory goodsCategory : list) {
            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;
    }
}
