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

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.dozermapper.core.Mapper;
import com.zm.order.tool.server.model.RefundDetail;
import com.zm.order.tool.server.stream.SQLCategory;
import com.zm.order.tool.server.stream.SQLRouteDefinition;
import com.zm.order.tool.server.stream.factory.MapperFactory;
import org.apache.ibatis.session.defaults.DefaultSqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

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 RefundDetailsStreamConverter extends AbstractStreamConverter {

    @Autowired
    private Mapper mapper;

    @Override
    public Integer route() {
        return SQLCategory.REFUND_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 RefundDetail) {
            RefundDetail detail = JSON.parseObject(data.toString(), RefundDetail.class);
//            detail.setPayType(MapperFactory.payTypeMap(detail.getBizType(), detail.getPayType()));
            return detail;
        }
        return data;
    }

    private ArrayList parseAndSet(Object data) {
        ArrayList<RefundDetail> list = (ArrayList) data;
        ArrayList<RefundDetail> convertedList = new ArrayList<>(list.size());
        for (RefundDetail detail : list) {
            RefundDetail response = mapper.map(detail, RefundDetail.class);
            response.setPayType(MapperFactory.payTypeMap(response.getBizType(), response.getPayType()));
            convertedList.add(response);
        }
        return convertedList;
    }

    private ArrayList tempParseAndSet(Object data) {
        List<JSONObject> list = (List) data;
        ArrayList<RefundDetail> convertedList = new ArrayList<>(list.size());
        for (JSONObject json : list) {
            RefundDetail detail = JSON.parseObject(json.toString(), RefundDetail.class);
            detail.setPayType(MapperFactory.payTypeMap(detail.getBizType(), detail.getPayType()));
            convertedList.add(detail);
        }
        return convertedList;
    }

    @Override
    public Object streamOut(Object data) {
        if (data instanceof List) {
            return real(data);
        } else if (data instanceof RefundDetail) {
            RefundDetail rd = (RefundDetail) data;
            rd.setPayType(MapperFactory.reversePayTypeMap(rd.getBizType(), rd.getPayType()));
            return rd;
        }
        return data;
    }

    private ArrayList real(Object data) {
        ArrayList<RefundDetail> list = (ArrayList) data;
        ArrayList<RefundDetail> convertedList = new ArrayList<>(list.size());
        for (RefundDetail detail : list) {
            RefundDetail response = mapper.map(detail, RefundDetail.class);
            response.setPayType(MapperFactory.reversePayTypeMap(response.getBizType(), response.getPayType()));
            convertedList.add(response);
        }
        return convertedList;
    }

    private ArrayList temp(Object data) {
        ArrayList<JSONObject> list = (ArrayList) data;
        ArrayList<RefundDetail> convertedList = new ArrayList<>(list.size());
        for (JSONObject json : list) {
            RefundDetail detail = JSON.parseObject(json.toString(), RefundDetail.class);
            detail.setPayType(MapperFactory.reversePayTypeMap(detail.getBizType(), detail.getPayType()));
            convertedList.add(detail);
        }
        return convertedList;
    }
}
