package com.weaver.seconddev.chapanda.beisen.util;


import com.weaver.ebuilder.form.client.entity.data.*;
import com.weaver.ebuilder.form.client.service.data.RemoteSimpleDataService;
import com.weaver.framework.rpc.annotation.RpcReference;
import org.springframework.stereotype.Component;
import com.weaver.seconddev.chapanda.beisen.constant.Constants;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class EbuilderUtils {

    @RpcReference(group="ebuilderform")
    private RemoteSimpleDataService remoteSimpleDataService;
    public EBDataChangeResult bacthInsertDbForm(String referrer_id,int points,String talent_id) {

        EBDataChangeReqDto ebDataChangeReqDto = new EBDataChangeReqDto();

        // 构建基础参数; objId 表单id, operator 操作人, tenantKey 租户
        ebDataChangeReqDto.setHeader(new EBDataReqHeader("1120080325150089217", Constants.SysUserId, Constants.TENANT_KEY));

        List<EBDataReqDto> datas = new ArrayList<EBDataReqDto>();

        EBDataReqOperation operation = new EBDataReqOperation();
        operation.setAsyncPostProcess(false);
        ebDataChangeReqDto.setOperation(operation);

        EBDataReqDto ebDataReqDto = new EBDataReqDto();
        // 主表数据参数
        List<EBDataReqDetailDto> mainData1 = new ArrayList<EBDataReqDetailDto>();
        // 单行文本
        mainData1.add(new EBDataReqDetailDto("1120080376580644871", referrer_id));
        mainData1.add(new EBDataReqDetailDto("1120080376580644872", String.valueOf(points)));
        ebDataReqDto.setMainDatas(mainData1);

        // 明细数据参数
        Map<Long, List<List<EBDataReqDetailDto>>> detailDatas = new HashMap<>();
        // 多条明细数据
        List<List<EBDataReqDetailDto>> subFormDatas = new ArrayList<>();
        // 单明细数据不同字段值 - 明细数据1
        List<EBDataReqDetailDto> subFormRowData1 = new ArrayList<>();
        subFormRowData1.add(new EBDataReqDetailDto("1120167723322155008", talent_id,false));
        subFormRowData1.add(new EBDataReqDetailDto("1120080728843460609", String.valueOf(points),false));
        subFormDatas.add(subFormRowData1);
        detailDatas.put(1120080376580644874L, subFormDatas);
        ebDataReqDto.setDetailDatas(detailDatas);

        datas.add(ebDataReqDto);
        ebDataChangeReqDto.setDatas(datas);
        EBDataChangeResult ebDataChangeResult = remoteSimpleDataService.saveFormData(ebDataChangeReqDto);


        return ebDataChangeResult;
    }


    public EBDataChangeResult bacthAppendEbDetail(String rcid,int points,String mainid) {

        EBDataChangeReqDto ebDataChangeReqDto = new EBDataChangeReqDto();
        ebDataChangeReqDto.setHeader(new EBDataReqHeader("1120080325150089217", Constants.SysUserId, Constants.TENANT_KEY));

        // 请求操作信息
        EBDataReqOperation ebDataReqOperation = new EBDataReqOperation();
        ebDataReqOperation.setUpdateType(EBDataUpdateType.ids);
        // 数据组装
        List<EBDataReqDto> datas = new ArrayList<>();
        // 第一个数据项
        EBDataReqDto ebDataReqDto1 = new EBDataReqDto();
        List<EBDataReqDetailDto> mainDatas1 = new ArrayList<>();
        // 主表数据组装
        mainDatas1.add(new EBDataReqDetailDto("id", mainid, false));
        ebDataReqDto1.setMainDatas(mainDatas1);

        /****************** 设置明细操作方式 - 不做覆盖 需要追加 ******************/
        Map<Long, EBDataReqOperationInfo> detailDatas = new HashMap<>();
        EBDataReqOperationInfo detailOperation = new EBDataReqOperationInfo();
        detailOperation.setCover(false);
        detailOperation.setNeedAdd(true);
        detailDatas.put(1120080376580644874L, detailOperation);
        ebDataReqOperation.setDetailDatas(detailDatas);

        // 明细行数据组装
        Map<Long, List<List<EBDataReqDetailDto>>> detailDatas1 = new HashMap<>();
        List<List<EBDataReqDetailDto>> detailDataList1 = new ArrayList<>();
        List<EBDataReqDetailDto> detailData1 = new ArrayList<EBDataReqDetailDto>();
        // 第一个明细表 第一条数据
        detailData1.add(new EBDataReqDetailDto("1120167723322155008", rcid, false));
        detailData1.add(new EBDataReqDetailDto("1120080728843460609", String.valueOf(points), false));
        detailDataList1.add(detailData1);
        detailDatas1.put(1120080376580644874L, detailDataList1);
        ebDataReqDto1.setDetailDatas(detailDatas1);
        datas.add(ebDataReqDto1);

        ebDataChangeReqDto.setOperation(ebDataReqOperation);

        // 设置数据
        ebDataChangeReqDto.setDatas(datas);
        EBDataChangeResult ebDataChangeResult = remoteSimpleDataService.updateFormData(ebDataChangeReqDto);

        return ebDataChangeResult;
    }

}
