package nccloud.openapi.so.m30;

import nc.vo.pub.VOStatus;
import nc.vo.pubapp.AppContext;
import nc.vo.pubapp.pattern.exception.ExceptionUtils;
import nc.vo.so.m30.entity.SaleOrderBVO;
import nc.vo.so.m30.entity.SaleOrderHVO;
import nc.vo.so.m30.entity.SaleOrderVO;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

/**
 * 销售订单接口适配2005代码
 * zhangxinah
 */


/**
 * openapi参数解析工具类
 *
 * @author Administrator
 */
public class OpenAPIParaUtil {

    /**
     * json转VO
     *
     * @param paramList
     * @param headtable
     * @param bodytable
     * @return
     */
    public static SaleOrderVO[] changeVO(List<Map<String, Object>> paramList, String headtable, String bodytable) {
        List<SaleOrderVO> aggVOList = new ArrayList<SaleOrderVO>();
        for (Map<String, Object> map : paramList) {
            if (!map.containsKey(headtable) || !map.containsKey(bodytable)) {
                ExceptionUtils.wrappBusinessException("传入数据异常，参数要包含表头信息和表体信息");
            }
            Map<String, String> headInfo = (Map<String, String>) map.get(headtable);
            List<Map<String, String>> bodyInfo = (List<Map<String, String>>) map.get(bodytable);

            SaleOrderVO aggvo = new SaleOrderVO();
            SaleOrderHVO hvo = new SaleOrderHVO();
            // 首先设置集团
            hvo.setAttributeValue(SaleOrderHVO.PK_GROUP, AppContext.getInstance().getPkGroup());
            hvo.setStatus(VOStatus.NEW);

            Iterator<Entry<String, String>> iterator = headInfo.entrySet().iterator();
            while (iterator.hasNext()) {
                Entry<String, String> headkey = iterator.next();
                hvo.setAttributeValue(headkey.getKey(), headkey.getValue());
            }
            aggvo.setParentVO(hvo);
            List<SaleOrderBVO> bvoList = new ArrayList<SaleOrderBVO>();
            for (Map<String, String> bodyMap : bodyInfo) {
                SaleOrderBVO bvo = new SaleOrderBVO();
                bvo.setAttributeValue(SaleOrderBVO.PK_GROUP, AppContext.getInstance().getPkGroup());
                hvo.setStatus(VOStatus.NEW);
                Iterator<Entry<String, String>> biterator = bodyMap.entrySet().iterator();
                while (biterator.hasNext()) {
                    Entry<String, String> next = biterator.next();
                    bvo.setAttributeValue(next.getKey(), next.getValue());
                }
                bvoList.add(bvo);
            }
            aggvo.setChildrenVO(bvoList.toArray(new SaleOrderBVO[bvoList.size()]));
            aggVOList.add(aggvo);
        }
        SaleOrderVO[] retvos = aggVOList.toArray(new SaleOrderVO[aggVOList.size()]);
        return retvos;
    }
}
