package com.malk.luyi.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.service.dingtalk.DDClient;
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.RequestBody;
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 DDClient dd;

    @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();
        String hangye1=data.get("hangye1").toString();//行业1
        String hangye2=data.get("hangye2").toString();//行业2
        String zyyw=data.get("zyyw").toString();//主营业务
        if (XMLX.equals("投前项目")){
            FORMID="textField_ltjj7pre";
            UUID="FORM-6EC75348FD8241D989DACA4B272CD8EBXFQK";
            ZD="associationFormField_lt1jaec6, textField_lt1jaecd, textField_lt1jaecc, selectField_lt1iahs9, selectField_lt1iahsi, selectField_lt1iahsj, textField_ltr1vksn";

        }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,hangye1,hangye2,zyyw )))//这里
                    .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);
            }





        }
        return McR.success();
    }
    @SneakyThrows
    @PostMapping("/insetTHXM")
    McR insetTHXM(HttpServletRequest request) {

        Map data = UtilServlet.getParamMap(request);
        log.info("data:"+data);
        if (data.get("SFZJ").toString().equals("否")){

        List<Map> dataList = (List<Map>) ydClient.queryData(YDParam.builder()
                .appType("APP_G951QZ32AUJNJUE4G127")
                .systemToken("HOA66I8176RID79L6QF3554SPPYH22VTBH1TLX6")
                .formUuid("FORM-225404C233714BD0B77A2EF675AEF29A1VVA")
                .searchFieldJson(JSON.toJSONString(UtilMap.map("serialNumberField_lt1i52jt",data.get("XMBH").toString())))
                .build(), YDConf.FORM_QUERY.retrieve_search_form).getData();
        if (dataList!=null && dataList.size()>0){
            log.info("dataList:"+dataList);
            String XMCSLID=dataList.get(0).get("formInstanceId").toString();
            String  FORMID="textField_ltjj7prg";
            String  UUID="FORM-2C96908BBA4C4C8D85D2F7C2D6E8C1B9GIYC";
            String  ZD="associationFormField_lt1ouopy, textField_lt1ouopz, textField_lt1ouoq2, selectField_lt1oy6qj";
            List<Map> associations = YDConf.associationForm("APP_G951QZ32AUJNJUE4G127", "FORM-225404C233714BD0B77A2EF675AEF29A1VVA", XMCSLID, data.get("XMBH").toString(),null, false);
            String tquid=   ydClient.operateData(YDParam.builder()
                    .appType("APP_G951QZ32AUJNJUE4G127")
                    .systemToken("HOA66I8176RID79L6QF3554SPPYH22VTBH1TLX6")
                    .formUuid(UUID)
                    .formDataJson(JSON.toJSONString(UtilMap.map(ZD, associations,data.get("XMMC"),data.get("XMBH") , "投后项目" )))
                    .build(), YDConf.FORM_OPERATION.create).toString();////
            if (tquid!=""){
                ydClient.operateData(YDParam.builder()
                        .appType("APP_G951QZ32AUJNJUE4G127")
                        .systemToken("HOA66I8176RID79L6QF3554SPPYH22VTBH1TLX6")
                        .formInstanceId(String.valueOf(XMCSLID))
                        .updateFormDataJson(JSON.toJSONString(UtilMap.map(FORMID,tquid )))
                        .build(), YDConf.FORM_OPERATION.update);
            }
        }
        log.info("request, {}", dataList);
        }

        ///
//        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();////
//        ydClient.operateData(YDParam.builder()
//                .appType("APP_G951QZ32AUJNJUE4G127")
//                .systemToken("HOA66I8176RID79L6QF3554SPPYH22VTBH1TLX6")
//                .formInstanceId(String.valueOf(data.get("xmcId")))
//                .updateFormDataJson(JSON.toJSONString(UtilMap.map("textField_ltjj7prg",tquid )))
//                .build(), YDConf.FORM_OPERATION.update);

//        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);


        //mapList

        return McR.success();
    }



    ///
    @SneakyThrows
    @PostMapping("report/PJJStemporary")
    McR temporary(@RequestBody Map<String,String> data ) {
        String formid=data.get("fomid") ;
        log.info("formid,{}",formid);
        Map formdata=    ydClient.queryData(YDParam.builder()
                .appType("APP_G951QZ32AUJNJUE4G127")
                .systemToken("HOA66I8176RID79L6QF3554SPPYH22VTBH1TLX6")
                .formUuid("FORM-04F671D6ABB048B2953763C3DAE14AE6ICGU")
                .formInstanceId(String.valueOf(formid))
                .build(), YDConf.FORM_QUERY.retrieve_id).getFormData();

        List<Map> list=(List<Map>) JSON.parse(String.valueOf(formdata.get("attachmentField_lt468w10")));
       //String ddapptonken= dd.getAccessToken("dingraobfxqulra4mp9r","KN28UwWycxxCkKkug1x61lsWWAhEaNpQbc8efrgMKyNUXayw-O2sRxDkhh9LTh8y");
       // log.info("ddapptonken, {}",ddapptonken);
       // log.info("ddapptonken, {}",list.get(0));
       // System.out.println("list.get(0):"+list.get(0).toString());
       // return McR.success(ydClient.convertTemporaryUrl(list.get(0).get("url").toString(),"APP_G951QZ32AUJNJUE4G127","HOA66I8176RID79L6QF3554SPPYH22VTBH1TLX6",ddapptonken));
        //log.info("url, {}",McR.success(ydClient.convertTemporaryUrl_PJJS(list.get(0).get("url").toString() )));
   //System.out.println("URL："+McR.success(ydClient.convertTemporaryUrl_PJJS(list.get(0).get("url").toString() )));
        System.out.println("afasdf:"+ydClient.convertTemporaryUrl_PJJS(list.get(0).get("url").toString() ));
       return McR.success(ydClient.convertTemporaryUrl_PJJS(list.get(0).get("url").toString() ));

//
//       System.out.println("url:"+list.get(0) );
//      return McR.success(list.get(0).get("downloadUrl").toString());




    }







}
