package com.weaver.seconddev.chapanda.sap.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 com.weaver.publishkit.api.util.PublishKitRuntimeUtil;
import com.weaver.seconddev.chapanda.sap.constant.Constants;
import com.weaver.seconddev.chapanda.sap.dao.FormFieldDao;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

@Component
public class EbuilderOperateUtils {

    private final static Logger log = LoggerFactory.getLogger(EbuilderOperateUtils.class);

    @Autowired
    FormFieldDao formFieldDao;

    @Autowired
    private PublishKitRuntimeUtil publishKitRuntimeUtil;

    @RpcReference(group = "ebuilderform")
    private RemoteSimpleDataService remoteSimpleDataService;

    public EBDataChangeResult bacthInsertEbForm(String formTable,List<EBDataReqDto> datas) {

        log.error("bacthInsertDbForm");
        EBDataChangeResult ebDataChangeResult = new EBDataChangeResult();

        Map<String,Object> recordMap= formFieldDao.queryTableFormId(formTable);

        String formId = recordMap.get("id").toString();
        String appId = recordMap.get("app_id").toString();
        log.error("formId:{}",formId);
        log.error("appId:{}",appId);

        if(StringUtils.isNotBlank(formId) && StringUtils.isNotBlank(appId)){

            RemoteSimpleDataService remoteSimpleDataService = publishKitRuntimeUtil.buildRpcService(RemoteSimpleDataService.class, "ebuilderform", appId);

            EBDataChangeReqDto ebDataChangeReqDto = new EBDataChangeReqDto();

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

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

            // 主表数据参数
//            Map<String, Object> fieldMap = formFieldDao.queryFromTableField(formTable,fieldList);
//            if("uf_yzgl".equals(formTable)){
//                for(int i=0;i<dataList.size();i++){
//                    Map<String,Object> dataMap = dataList.get(i);
//                    EBDataReqDto ebDataReqDto = new EBDataReqDto();
//                    List<EBDataReqDetailDto> mainData = new ArrayList<EBDataReqDetailDto>();
//                    for(int n=0;n<fieldList.size();n++){
//                        if(fieldList.get(n).equals("yzzl")){
//                            String fieldId = fieldMap.get(fieldList.get(n)).toString();
//                            mainData.add(new EBDataReqDetailDto(fieldId, "2"));
//                        }else{
//                            if(dataMap.containsKey(fieldList.get(n)) && fieldMap.containsKey(fieldList.get(n))){
//                                String value = dataMap.get(fieldList.get(n)).toString();
//                                String fieldId = fieldMap.get(fieldList.get(n)).toString();
//
//                                log.error("fieldId:{}",fieldId);
//                                log.error("value:{}",value);
//
//                                mainData.add(new EBDataReqDetailDto(fieldId, value));
//                            }
//                        }
//                    }
//                    ebDataReqDto.setMainDatas(mainData);
//                    datas.add(ebDataReqDto);
//                }
//            }else{
//                for(int i=0;i<dataList.size();i++){
//                    Map<String,Object> dataMap = dataList.get(i);
//                    EBDataReqDto ebDataReqDto = new EBDataReqDto();
//                    List<EBDataReqDetailDto> mainData = new ArrayList<EBDataReqDetailDto>();
//                    for(int n=0;n<fieldList.size();n++){
//                        if(dataMap.containsKey(fieldList.get(n)) && fieldMap.containsKey(fieldList.get(n))){
//                            String value = dataMap.get(fieldList.get(n)).toString();
//                            String fieldId = fieldMap.get(fieldList.get(n)).toString();
//
//                            log.error("fieldId:{}",fieldId);
//                            log.error("value:{}",value);
//
//                            mainData.add(new EBDataReqDetailDto(fieldId, value));
//                        }
//                    }
//                    ebDataReqDto.setMainDatas(mainData);
//                    datas.add(ebDataReqDto);
//                }
//            }
            ebDataChangeReqDto.setDatas(datas);
            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;
//    }

    public EBDataChangeResult bacthEditEbForm(String formTable,List<EBDataReqDto> datas) {

        EBDataChangeResult ebDataChangeResult = new EBDataChangeResult();

        Map<String,Object> recordMap= formFieldDao.queryTableFormId(formTable);

        String formId = recordMap.get("id").toString();
        String appId = recordMap.get("app_id").toString();
        log.error("formId:{}",formId);
        log.error("appId:{}",appId);

        if (StringUtils.isNotBlank(formId) && StringUtils.isNotBlank(appId)) {

            RemoteSimpleDataService remoteSimpleDataService = publishKitRuntimeUtil.buildRpcService(RemoteSimpleDataService.class, "ebuilderform", appId);

            /**
             * 直接通过id更新数据， 即update table set a = '11' where id = 1
             */
            EBDataChangeReqDto ebDataChangeReqDto = new EBDataChangeReqDto();
            // 构建基础参数; objId 表单id, operator 操作人, tenantKey 租户
            ebDataChangeReqDto.setHeader(new EBDataReqHeader(formId,  Constants.SysUserId, Constants.TENANT_KEY));
            // 请求操作信息
            EBDataReqOperation ebDataReqOperation = new EBDataReqOperation();
            ebDataReqOperation.setUpdateType(EBDataUpdateType.ids);
            // 若数据写入后就要从前台看到数据或者立即操作本次写入的数据, 需要调整后置处理为同步 权限处理完成再返回
            ebDataReqOperation.setAsyncPostProcess(false);
            ebDataChangeReqDto.setOperation(ebDataReqOperation);
            // 数据没有找到的时候是否新增数据
            EBDataReqOperationInfo ebDataReqOperationInfo = new EBDataReqOperationInfo();
            ebDataReqOperationInfo.setNeedAdd(true);
            ebDataReqOperation.setMainData(ebDataReqOperationInfo);
            ebDataChangeReqDto.setOperation(ebDataReqOperation);
            ebDataChangeReqDto.setDatas(datas);
            ebDataChangeResult = remoteSimpleDataService.updateFormData(ebDataChangeReqDto);
        }
        return ebDataChangeResult;
    }


}
