package psdi.app.iface.oa;

import com.alibaba.fastjson.JSONObject;
import psdi.app.iface.comm.AESUtil;
import psdi.app.iface.comm.UrlConstants;
import psdi.mbo.MboRemote;
import psdi.mbo.MboSetRemote;
import psdi.server.MXServer;
import psdi.util.MXException;

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import java.rmi.RemoteException;

/**
 * OA调用EAM跳转至工厂需求申请程序
 * edit 20230505
 */
@Path("/jump")
public class OA2EamPrJumpService {

    //value=udprgather 工厂需求申请应用程序ID
    String pcurl = UrlConstants.pcurl;
    String login="&login=true&token=";

    String taskpcurl = UrlConstants.udbudgettaskurl;
    String tasklogin="&login=true&token=";

    @POST
    @Path("/udprgather")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public String setStatusValue(String received) throws RemoteException, MXException {
        JSONObject msgJSONObject = new JSONObject();
        if("".equalsIgnoreCase(received) || null==received){
            msgJSONObject.put("code", "20001");
            msgJSONObject.put("errmsg", "参数不能为空");
            return msgJSONObject.toString();
        }
        JSONObject jsonObject = JSONObject.parseObject(received);
//        JSONObject jsonObject2 = JSONObject.parseObject(jsonObject.getString("SendData"));
//        if(null==jsonObject2){
//            msgJSONObject.put("code", "20001");
//            msgJSONObject.put("errmsg", "错误！入参格式不对");
//            return msgJSONObject.toString();
//        }
        String udprnum = jsonObject.getString("udprnum");//需求申请号
        String secret = jsonObject.getString("secret");//加密secret AESUtil.createCiphertext("0113992-OA2EAMIFACE"); 0113992工号 OA2EAMIFACE固定值
        MboSetRemote prSet = MXServer.getMXServer().getMboSet("PR",MXServer.getMXServer().getSystemUserInfo());
        prSet.setWhere("UDPRNUM='"+udprnum+"'");
        prSet.reset();
        if(null!=prSet && !prSet.isEmpty()){
            MboRemote prMbo=prSet.getMbo(0);
            int prid=prMbo.getInt("PRID");
            pcurl+=prid;
            String pub_srcret=AESUtil.createCiphertext("01101111-OA2EAMIFACE");//01101111 OA二进制转换后
            pcurl+=login+pub_srcret;
            msgJSONObject.put("code", "10000");
            msgJSONObject.put("msg", "success");
            msgJSONObject.put("pcurl", pcurl);
        }else{
            msgJSONObject.put("code", "20001");
            msgJSONObject.put("errmsg", "参数不存在");
        }
        return msgJSONObject.toString();
    }


    /**
     * 跳转至任务管理应用程序
     * @param received
     * @return
     * @throws RemoteException
     * @throws MXException
     */
    @POST
    @Path("/udbudgettask")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public String udbudgettask(String received) throws RemoteException, MXException {
        JSONObject msgJSONObject = new JSONObject();
        if("".equalsIgnoreCase(received) || null==received){
            msgJSONObject.put("code", "20001");
            msgJSONObject.put("errmsg", "参数不能为空");
            return msgJSONObject.toString();
        }
        JSONObject jsonObject = JSONObject.parseObject(received);
//        JSONObject jsonObject2 = JSONObject.parseObject(jsonObject.getString("SendData"));
//        if(null==jsonObject2){
//            msgJSONObject.put("code", "20001");
//            msgJSONObject.put("errmsg", "错误！入参格式不对");
//            return msgJSONObject.toString();
//        }
        String udprnum = jsonObject.getString("udprnum");//需求申请号
        String secret = jsonObject.getString("secret");//加密secret AESUtil.createCiphertext("0113992-OA2EAMIFACE"); 0113992工号 OA2EAMIFACE固定值
        MboSetRemote prSet = MXServer.getMXServer().getMboSet("UDBUDGETTASK",MXServer.getMXServer().getSystemUserInfo());
        prSet.setWhere("TASKNUM='"+udprnum+"'");
        prSet.reset();
        if(null!=prSet && !prSet.isEmpty()){
            MboRemote prMbo=prSet.getMbo(0);
            int prid=prMbo.getInt("UDBUDGETTASKID");
            taskpcurl+=prid;
            String pub_srcret=AESUtil.createCiphertext("01101111-OA2EAMIFACE");//01101111 OA二进制转换后
            taskpcurl+=tasklogin+pub_srcret;
            msgJSONObject.put("code", "10000");
            msgJSONObject.put("msg", "success");
            msgJSONObject.put("pcurl", taskpcurl);
        }else{
            msgJSONObject.put("code", "20001");
            msgJSONObject.put("errmsg", "参数不存在");
        }
        return msgJSONObject.toString();
    }
}
