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.Refund;
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>
 *     streamIn：转换写入参数，持久化至数据库中
 *
 *     streamOut：转换查询参数，返回至接口result
 * </p>
 *
 * {@link SQLCategory}
 *
 * {@link SQLRouteDefinition}
 *
 * @author nianchang
 * @date 2023/03/07 3:20 下午
 */
@Component
@SuppressWarnings("all")
public class RefundStreamConverter extends AbstractStreamConverter {

    @Override
    public Integer route() {
        return SQLCategory.REFUNDS.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 Refund) {
            return parse(data, SQLCategory.REFUNDS);
        }
        return data;
    }

    private ArrayList parseAndSet(Object data) {
        ArrayList<Refund> refundList = (ArrayList) data;
        ArrayList<Refund> convertedList = new ArrayList<>(refundList.size());
        if (!CollectionUtils.isEmpty(refundList)) {
            for (Refund refund : refundList) {
                convertedList.add((Refund) parse(refund, SQLCategory.REFUNDS));
            }
        }
        return convertedList;
    }

    private ArrayList tempParseAndSet(Object data) {
        List<JSONObject> orderList = (List) data;
        ArrayList<Refund> convertedList = new ArrayList<>(orderList.size());
        for (JSONObject json : orderList) {
            Refund refund = JSON.parseObject(json.toString(), Refund.class);
            convertedList.add((Refund) parse(refund, SQLCategory.REFUNDS));
        }
        return convertedList;
    }

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

    private ArrayList real(Object data) {
        ArrayList<Refund> refundList = (ArrayList) data;
        ArrayList<Refund> convertedList = new ArrayList<>(refundList.size());
        if (!CollectionUtils.isEmpty(refundList)) {
            for (Refund refund : refundList) {
                convertedList.add((Refund) reverseParse(refund, SQLCategory.REFUNDS));
            }
        }
        return convertedList;
    }

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