package com.hfkj.service.goods.impl;

import com.hfkj.dao.GoodsTypeMapper;
import com.hfkj.entity.GoodsType;
import com.hfkj.entity.GoodsTypeExample;
import com.hfkj.model.goods.GoodsTypeModel;
import com.hfkj.service.goods.GoodsTypeService;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

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

@Service("goodsTypeService")
public class GoodsTypeServiceImpl implements GoodsTypeService {

    @Resource
    private GoodsTypeMapper goodsTypeMapper;

    @Override
    public void create(GoodsType goodsType) {
        goodsTypeMapper.insert(goodsType);
    }

    @Override
    public void update(GoodsType goodsType) {
        goodsTypeMapper.updateByPrimaryKey(goodsType);
    }

    @Override
    public void delete(Long id , Boolean fullDelete) {
        if (fullDelete) {
            goodsTypeMapper.deleteByPrimaryKey(id);
        } else {
            GoodsType goodsType = queryDetail(id);
            goodsType.setStatus(0);
            goodsType.setUpdateTime(new Date());
            update(goodsType);
        }
    }

    @Override
    public GoodsType queryDetail(Long id) {
        return goodsTypeMapper.selectByPrimaryKey(id);
    }

    @Override
    public GoodsType queryDetailByMap(Map<String, Object> map) {

        GoodsTypeExample example = new GoodsTypeExample();
        GoodsTypeExample.Criteria criteria = example.createCriteria().andStatusNotEqualTo(0);

        if (MapUtils.getLong(map, "parentId") != null) {
            criteria.andParentIdEqualTo(MapUtils.getLong(map, "parentId"));
        }

        if (MapUtils.getString(map, "title") != null) {
            criteria.andTitleEqualTo(MapUtils.getString(map, "title"));
        }

        if (MapUtils.getInteger(map, "businessType") != null) {
            criteria.andBusinessTypeEqualTo(MapUtils.getInteger(map, "businessType"));
        }

        List<GoodsType> list = goodsTypeMapper.selectByExample(example);

        if (!list.isEmpty()) {
            return list.get(0);
        }

        return null;
    }

    @Override
    public List<GoodsType> getList(Map<String, Object> map) {

        GoodsTypeExample example = new GoodsTypeExample();
        GoodsTypeExample.Criteria criteria = example.createCriteria().andStatusNotEqualTo(0);

        if (MapUtils.getLong(map, "parentId") != null) {
            criteria.andParentIdEqualTo(MapUtils.getLong(map, "parentId"));
        }
        if (StringUtils.isNotBlank(MapUtils.getString(map, "title"))) {
            criteria.andTitleLike("%"+MapUtils.getString(map, "title")+"%");
        }
        if (MapUtils.getInteger(map, "businessType") != null) {
            criteria.andBusinessTypeEqualTo(MapUtils.getInteger(map, "businessType"));
        }
        if (MapUtils.getInteger(map, "status") != null) {
            criteria.andStatusEqualTo(MapUtils.getInteger(map, "status"));
        } else {
            criteria.andStatusNotEqualTo(0);
        }
        example.setOrderByClause("sort asc");

        return goodsTypeMapper.selectByExample(example);
    }

    @Override
    public List<GoodsTypeModel> getGoodsTypeModelList(List<GoodsType> list) {

        List<GoodsTypeModel> goodsTypeModels = new ArrayList<>();
        GoodsTypeModel goodsTypeModel;

        // 获取最顶层菜单
        List<GoodsType> goodsTypeList = list.stream()
                .filter(o -> o.getParentId() == null)
                .collect(Collectors.toList());


        for (GoodsType goodsType : goodsTypeList) {
            goodsTypeModel = new GoodsTypeModel();
            BeanUtils.copyProperties(goodsType, goodsTypeModel);
            // 获取下级菜单
            goodsTypeModel.setChildren(recursionGoodsType(list , goodsType.getId()));
            goodsTypeModels.add(goodsTypeModel);
            if (goodsTypeModel.getChildren().isEmpty()){
                goodsTypeModel.setChildren(null);
            }

        }

        return goodsTypeModels;
    }

    /**
     * 递归获取菜单
     * @param dataSource 数据源
     * @param parentId 父级菜单id
     * @return
     */
    public List<GoodsTypeModel> recursionGoodsType(List<GoodsType> dataSource, Long parentId){
        List<GoodsTypeModel> goodsTypeModels = new ArrayList<>();
        GoodsTypeModel goodsTypeModel;
        // 获取最顶层菜单
        List<GoodsType> goodsTypeList = dataSource.stream()
                .filter(o -> Objects.equals(o.getParentId(), parentId))
                .collect(Collectors.toList());

        for (GoodsType goodsType : goodsTypeList) {
            goodsTypeModel = new GoodsTypeModel();
            BeanUtils.copyProperties(goodsType, goodsTypeModel);
            // 获取下级菜单
            goodsTypeModel.setChildren(recursionGoodsType(dataSource , goodsType.getId()));
            if (goodsTypeModel.getChildren().isEmpty()){
                goodsTypeModel.setChildren(null);
            }
            goodsTypeModels.add(goodsTypeModel);
        }

        return goodsTypeModels;
    }

    @Override
    public String findGoodsType(Long id) {
        GoodsType goodsType = queryDetail(id);
        return goodsType.getTitle();
    }
}
