package com.zm.order.tool.server.stream.convert;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zm.order.tool.server.model.OrderPayDetails;
import com.zm.order.tool.server.stream.SQLCategory;
import com.zm.order.tool.server.stream.SQLRouteDefinition;
import org.apache.ibatis.session.defaults.DefaultSqlSession;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *
 * </p>
 *
 * {@link SQLCategory}
 *
 * {@link SQLRouteDefinition}
 *
 * @author nianchang
 * @date 2023/03/07 3:20 下午
 */
@Component
@SuppressWarnings("all")
public class PayDetailStreamConverter extends AbstractStreamConverter {

    @Override
    public Integer route() {
        return SQLCategory.ORDER_PAY_DETAILS.getCode();
    }

    @Override
    public Object streamIn(Object data) {
        // 区分单个和批量写入数据结构有所区别
        // StrictMap --> [(collection, ArrayList), (list, ArrayList);]
        if (data instanceof DefaultSqlSession.StrictMap) {
            DefaultSqlSession.StrictMap dataSource = (DefaultSqlSession.StrictMap) data;
            Object collection = dataSource.get(BATCH_DATA);
            if (collection instanceof List) {
                dataSource.put(BATCH_DATA, parseAndSet(collection));
            }
            Object list = dataSource.get(BATCH_DATA0);
            if (list instanceof List) {
                dataSource.put(BATCH_DATA0, parseAndSet(list));
            }
            return dataSource;
        }
        else if (data instanceof OrderPayDetails) {
            return parse(data, SQLCategory.ORDER_PAY_DETAILS);
        }
        return data;
    }

    private ArrayList parseAndSet(Object data) {
        ArrayList<OrderPayDetails> payList = (ArrayList) data;
        ArrayList<OrderPayDetails> convertedList = new ArrayList<>(payList.size());
        for (OrderPayDetails eachPay : payList) {
            convertedList.add((OrderPayDetails) parse(eachPay, SQLCategory.ORDER_PAY_DETAILS));
        }
        return convertedList;
    }

    private ArrayList tempParseAndSet(Object data) {
        List<JSONObject> payList = (List) data;
        ArrayList<OrderPayDetails> convertedList = new ArrayList<>(payList.size());
        for (JSONObject json : payList) {
            OrderPayDetails pay = JSON.parseObject(json.toString(), OrderPayDetails.class);
            convertedList.add((OrderPayDetails) parse(pay, SQLCategory.ORDER_PAY_DETAILS));
        }
        return convertedList;
    }

    @Override
    public Object streamOut(Object data) {
        if (data instanceof List) {
            return real(data);
        } else if (data instanceof OrderPayDetails) {
            return reverseParse(data, SQLCategory.ORDER_PAY_DETAILS);
        }
        return data;
    }

    private ArrayList real(Object data) {
        ArrayList<OrderPayDetails> list = (ArrayList) data;
        ArrayList<OrderPayDetails> convertedList = new ArrayList<>(list.size());
        if (!CollectionUtils.isEmpty(list)) {
            for (OrderPayDetails eachPay : list) {
                convertedList.add((OrderPayDetails) reverseParse(eachPay, SQLCategory.ORDER_PAY_DETAILS));
            }
        }
        return convertedList;
    }

    private ArrayList temp(Object data) {
        ArrayList<JSONObject> list = (ArrayList) data;
        ArrayList<OrderPayDetails> convertedList = new ArrayList<>(list.size());
        for (JSONObject json : list) {
            OrderPayDetails pay = JSON.parseObject(json.toString(), OrderPayDetails.class);
            convertedList.add((OrderPayDetails) reverseParse(pay, SQLCategory.ORDER_PAY_DETAILS));
        }
        return convertedList;
    }
}
