package cn.pluss.platform.impl;

import cn.jiguang.common.utils.StringUtils;
import cn.pluss.platform.SxfMccInfoService;
import cn.pluss.platform.entity.SxfMccInfo;
import cn.pluss.platform.mapper.SxfMccInfoMapper;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Djh
 * @since 2020-09-22
 */
@Service
public class SxfMccInfoServiceImpl extends ServiceImpl<SxfMccInfoMapper, SxfMccInfo> implements SxfMccInfoService {

    @Override
    public Page<SxfMccInfo> pageData(Page<SxfMccInfo> page, String keyWord) {
        QueryWrapper<SxfMccInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.ne("F_mcc_type", "线上");
        queryWrapper.and(StringUtils.isNotEmpty(keyWord), wrapper -> wrapper.like(StringUtils.isNotEmpty(keyWord), "F_grandpa_txt", keyWord)
                .or()
                .like(StringUtils.isNotEmpty(keyWord), "F_mcc_txt", keyWord)
                .or()
                .like(StringUtils.isNotEmpty(keyWord), "F_father_txt", keyWord));

        return baseMapper.selectPage(page, queryWrapper);
    }

    @Override
    public JSONArray treeData() {
        List<SxfMccInfo> sxfMccInfos = baseMapper.selectList(new QueryWrapper<>());

        JSONArray root = new JSONArray();
        Map<String, JSONObject> nodeMap = new HashMap<>();

        // 获取一级结点
        for (SxfMccInfo item : sxfMccInfos) {
            String key = item.getFGrandpaTxt();
            if (nodeMap.get(key) != null) {
                continue;
            }

            JSONObject jsonObj = new JSONObject();
            jsonObj.put("mccName", item.getFGrandpaTxt());
            jsonObj.put("mccFatherList", new JSONArray());
            root.add(jsonObj);
            nodeMap.put(key, jsonObj);
        }

        // 获取二级结点
        for (SxfMccInfo item : sxfMccInfos) {
            String key = item.getFGrandpaTxt() + "_" + item.getFFatherTxt();
            if (nodeMap.get(key) != null) {
                continue;
            }

            JSONObject jsonObj = new JSONObject();
            jsonObj.put("mccName", item.getFFatherTxt());
            jsonObj.put("mccList", new JSONArray());

            JSONObject jsonObj2 = nodeMap.get(item.getFGrandpaTxt());
            JSONArray jsonArray = jsonObj2.getJSONArray("mccFatherList");
            jsonArray.add(jsonObj);

            nodeMap.put(key, jsonObj);
        }

        // 获取三级结点
        for (SxfMccInfo item : sxfMccInfos) {
            String key = item.getFGrandpaTxt() + "_" + item.getFFatherTxt() + "_" + item.getFMccTxt();
            if (nodeMap.get(key) != null) {
                continue;
            }

            JSONObject jsonObj = new JSONObject();
            jsonObj.put("mccName", item.getFMccTxt());
            jsonObj.put("mccCode", item.getFMccCode());

            JSONObject jsonObj2 = nodeMap.get(item.getFGrandpaTxt() + "_" + item.getFFatherTxt());
            JSONArray jsonArray = jsonObj2.getJSONArray("mccList");
            jsonArray.add(jsonObj);

            nodeMap.put(key, jsonObj);
        }


        return root;
    }

    @Override
    public SxfMccInfo getByMccCode(String mccCode) {
        QueryWrapper<SxfMccInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("F_mcc_code", mccCode);

        List<SxfMccInfo> sxfMccInfos = baseMapper.selectList(queryWrapper);

        if (sxfMccInfos == null || sxfMccInfos.size() == 0) {
            return null;
        }

        return sxfMccInfos.get(0);
    }
}
