package com.malk.hongfeng.controller;

/**
 * 错误抛出与拦截详见 CatchException
 */

import com.alibaba.fastjson.JSON;
import com.malk.delegate.McDelegate;
import com.malk.server.aliwork.YDConf;
import com.malk.server.aliwork.YDParam;
import com.malk.server.common.McR;
import com.malk.service.aliwork.YDClient;
import com.malk.utils.UtilMap;
import com.malk.utils.UtilServlet;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping()
public class PJJSController {

    @Autowired
    private YDClient ydClient;
    @Autowired
    private McDelegate mcDelegate;

    @SneakyThrows
    @PostMapping("/insetXMC")
    McR insetXMC(HttpServletRequest request) {
        Map data = UtilServlet.getParamMap(request);
        String UUID="";
        String FORMID="";
        String ZD="";


        String XMLX=data.get("xmLx").toString();
        if (XMLX.equals("投前项目")){
            FORMID="textField_ltjj7pre";
            UUID="FORM-6EC75348FD8241D989DACA4B272CD8EBXFQK";
              ZD="associationFormField_lt1jaec6, textField_lt1jaecd, textField_lt1jaecc, selectField_lt1iahs9";

        }else if (XMLX.equals("投后项目")){
            FORMID="textField_ltjj7prg";
            UUID="FORM-2C96908BBA4C4C8D85D2F7C2D6E8C1B9GIYC";
            ZD="associationFormField_lt1ouopy, textField_lt1ouopz, textField_lt1ouoq2, selectField_lt1oy6qj";
        }

        log.info("项目池, {}", data);
        if (XMLX.equals("投前项目") || XMLX.equals("投后项目")) {
            List<Map> associations = YDConf.associationForm("APP_G951QZ32AUJNJUE4G127", "FORM-225404C233714BD0B77A2EF675AEF29A1VVA", data.get("xmcId").toString(), data.get("xmcLsh").toString(),null, false);
            log.info("associations: {}", associations);
            log.info("UUID, {}", UUID);
            log.info("ZD, {}", ZD);
             String tquid=   ydClient.operateData(YDParam.builder()
                     .appType("APP_G951QZ32AUJNJUE4G127")
                     .systemToken("HOA66I8176RID79L6QF3554SPPYH22VTBH1TLX6")
                        .formUuid(UUID)
                        .formDataJson(JSON.toJSONString(UtilMap.map(ZD, associations,data.get("xmName"),data.get("xmcLsh") , XMLX )))
                        .build(), YDConf.FORM_OPERATION.create).toString();////
                if (tquid!=""){
                    ydClient.operateData(YDParam.builder()
                            .appType("APP_G951QZ32AUJNJUE4G127")
                            .systemToken("HOA66I8176RID79L6QF3554SPPYH22VTBH1TLX6")
                            .formInstanceId(String.valueOf(data.get("xmcId")))
                            .updateFormDataJson(JSON.toJSONString(UtilMap.map(FORMID,tquid )))
                            .build(), YDConf.FORM_OPERATION.update);
                }
//            DDR_New ddrNew =  ydClient.queryData(YDParam.builder()
//                    .formUuid(UUID)
//                    .searchFieldJson(JSON.toJSONString(UtilMap.map("serialNumberField_lt1jaec7","F202402280023")))
//                    .build(),YDConf.FORM_QUERY.retrieve_search_form);
//
//            log.info("ddrNew, {}", ddrNew.getFormData());

        }
        return McR.success();
    }


}
