package com.malk.pake.controller;

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

import com.alibaba.fastjson.JSON;
import com.malk.server.aliwork.YDConf;
import com.malk.server.aliwork.YDParam;
import com.malk.server.common.McR;
import com.malk.service.aliwork.YDService;
import com.malk.utils.UtilMap;
import com.malk.utils.UtilServlet;
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 PKController {

    @Autowired
    private YDService ydService;

    @Autowired
    private YDConf ydConf;

    /**
     * 关联表单自关联, 业务自动化下传
     */
    @PostMapping("association")
    McR association(HttpServletRequest request) {

        Map data = UtilServlet.getParamMap(request);
        log.info("关联表单自关联, 业务自动化下传, {}", data);
        List<Map> association = ydConf.associationForm(data.get("formUuid").toString(), data.get("formInstId").toString(), data.get("title").toString(), UtilMap.getString(data, "subTitle"), true);

        ydService.operateData(YDParam.builder()
                .formInstId(String.valueOf(data.get("formInstId")))
                .updateFormDataJson(JSON.toJSONString(UtilMap.map(data.get("compId").toString(), association)))
                .build(), YDConf.FORM_OPERATION.update);
        return McR.success();
    }

    @PostMapping("test")
    McR test() {

        return McR.success();
    }
}
