package com.seeyon.ctp.rest.resources.flowIntegration;

import com.alibaba.fastjson.JSONObject;
import com.seeyon.apps.src_flowIntegration.service.FlowIntegrationService;
import com.seeyon.ctp.common.AppContext;
import com.seeyon.ctp.common.log.CtpLogFactory;
import com.seeyon.ctp.rest.resources.BaseResource;
import org.apache.commons.logging.Log;
import www.seeyon.com.utils.StringUtil;

import javax.ws.rs.*;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.Response;
import java.util.HashMap;
import java.util.Map;


@Path("/third")
@Produces({"application/json", "application/xml"})
public class ThirdFlowIntegrationController extends BaseResource {
    private static final Log log = CtpLogFactory.getLog(ThirdFlowIntegrationController.class);
    private FlowIntegrationService flowIntegrationService = (FlowIntegrationService) AppContext.getBean("flowIntegrationService");


    @POST
    @Path("/flowcreate")
    @Produces({"application/json"})
    @Consumes({"application/json"})
    public Response flowCreate(JSONObject params ,@QueryParam("token") String urlToken, @Context HttpHeaders headers) {
        try {
//            获取当前请求中的token，可能通过url或者header中进行token获取
            String tokenString = headers.getHeaderString("token");
            String token = "";
            if(StringUtil.isNotEmpty(urlToken)){
                token = urlToken;
            }else{
                token = tokenString;
            }
            JSONObject r = flowIntegrationService.thirdFlowCreate(params,token);
            String code = r.getString("code");
            JSONObject res = new JSONObject();
            if("0".equals(code)){
                res.put("oaFlowId",r.getString("oaFlowId"));
                return success(res);
            }else{
                return fail(r.getString("msg"));
            }
        }catch (Exception e) {
            return fail(e.getMessage());
        }
    }

    @POST
    @Path("/flowquery")
    @Produces({"application/json"})
    @Consumes({"application/json"})
    public Response flowquery(JSONObject params ,@QueryParam("token") String urlToken, @Context HttpHeaders headers) {
        try {
//            获取当前请求中的token，可能通过url或者header中进行token获取

            JSONObject r = flowIntegrationService.thirdFlowQuery(params);
            return success(r);
        }catch (Exception e) {
            return fail(e.getMessage());
        }
    }

}
