package com.hai.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.hai.dao.HighGoodsTypeMapper;
import com.hai.entity.HighGoodsType;
import com.hai.entity.HighGoodsTypeExample;
import com.hai.service.HighGoodsTypeService;
import net.sf.jsqlparser.statement.select.First;
import org.apache.commons.collections4.MapUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @serviceName HighGoodsTypeServiceImpl.java
 * @author Sum1Dream
 * @version 1.0.0
 * @Description //TODO
 * @createTime 3:36 下午 2021/11/11
 **/
@Service("highGoodsTypeService")
public class HighGoodsTypeServiceImpl implements HighGoodsTypeService {

    @Resource
    private HighGoodsTypeMapper highGoodsTypeMapper;

    @Override
    public List<HighGoodsType> getListGoodsType(Map<String, String> map) {

        HighGoodsTypeExample example  = new HighGoodsTypeExample();
        HighGoodsTypeExample.Criteria criteria = example.createCriteria();

        if (MapUtils.getString(map, "title") != null) {
            criteria.andTitleLike("%" + MapUtils.getString(map, "title") + "%");
        }
        if (MapUtils.getString(map, "userService") != null) {
            criteria.andUserServiceEqualTo(MapUtils.getString(map, "userService"));
        }
        if (MapUtils.getInteger(map, "parentId") != null) {
            criteria.andParentIdEqualTo(MapUtils.getInteger(map, "parentId"));
        }else {
            criteria.andParentIdIsNull();
        }
        if (MapUtils.getLong(map, "companyId") != null) {
            criteria.andCompanyIdEqualTo(MapUtils.getLong(map, "companyId"));
        }

        criteria.andStatusEqualTo(1);

        example.setOrderByClause("created_time desc");

        return highGoodsTypeMapper.selectByExample(example);
    }

    @Override
    public HighGoodsType findById(Integer id) {
        return highGoodsTypeMapper.selectByPrimaryKey(id);
    }

    @Override
    public Boolean findByTitle(String title) {
        HighGoodsTypeExample example = new HighGoodsTypeExample();
        HighGoodsTypeExample.Criteria criteria = example.createCriteria();

        criteria.andTitleEqualTo(title).andStatusEqualTo(1);

        List<HighGoodsType> highGoodsTypes = highGoodsTypeMapper.selectByExample(example);

        return highGoodsTypes.size() > 0;
    }

    @Override
    public void insertGoodsType(HighGoodsType highGoodsType) {
        highGoodsTypeMapper.insert(highGoodsType);

    }

    @Override
    public void updateGoodsType(HighGoodsType highGoodsType) {
        highGoodsTypeMapper.updateByPrimaryKey(highGoodsType);

    }

    @Override
    public List<JSONObject>  getGoodsTypeTree(Integer businessType) {

        Map<String,String> map = new HashMap<>();
        map.put("businessType", String.valueOf(businessType));



        // 获取顶级
        List<HighGoodsType> goodsTypeList = getListGoodsType(map);

        List<JSONObject> list = new ArrayList<>();

        for (HighGoodsType goodsType: goodsTypeList) {
            JSONObject object = new JSONObject();
            object.put("title" , goodsType.getTitle());
            object.put("key" , goodsType.getId());
            object.put("img" , goodsType.getImg());

            map.clear();
            map.put("parentId" , goodsType.getId().toString());

            List<HighGoodsType> goodsTypesChild = getListGoodsType(map);

            // 判断是否存在子类
            if (goodsTypesChild.size() > 0) {
                List<JSONObject> listChild = new ArrayList<>();
                for (HighGoodsType goodsTypeChild: goodsTypesChild) {
                    JSONObject objectChild = new JSONObject();
                    objectChild.put("title" , goodsTypeChild.getTitle());
                    objectChild.put("key" , goodsTypeChild.getId());
                    objectChild.put("img" , goodsTypeChild.getImg());
                    objectChild.put("isLeaf" , true);
                    listChild.add(objectChild);
                }
                list.add(object);
                object.put("children" , listChild);
                object.put("selectable" , false);
            }

        }
        return list;
    }
}
