package psdi.app.iface.oa;

import com.alibaba.fastjson.JSONObject;
import psdi.app.iface.srm.Srm2EamHandler;
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.io.IOException;
import java.nio.charset.StandardCharsets;
import java.rmi.RemoteException;
import java.util.Date;

/**
 * OA调用EAM更新需求状态接口
 * edit 20230422
 */
@Path("/udpr")
public class Oa2EamPrService {

    @POST
    @Path("/setStatusValue")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public String setStatusValue(String received) throws IOException, MXException {
        System.out.println("-------OA2EAM--received="+received.toString());
        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 status = jsonObject.getString("status");
        if(!"APPR".equalsIgnoreCase(status) && !"CAN".equalsIgnoreCase(status)){
            msgJSONObject.put("code", "20001");
            msgJSONObject.put("errmsg", "错误！状态值不对，仅限传入APPR/CAN");
            return msgJSONObject.toString();
        }
        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);
            prMbo.setFieldFlag("STATUS", 7L, false);
            prMbo.setFieldFlag("STATUSDATE", 7L, false);
            if("APPR".equalsIgnoreCase(status)){
                prMbo.setValue("STATUS", "OAAPPR",2L);
            }else{
                prMbo.setValue("STATUS", status,2L);
            }
            prMbo.setValue("STATUSDATE", new Date(),2L);
            if("APPR".equalsIgnoreCase(status)){//执行与SRM同步接口
                JSONObject result = new Srm2EamHandler().EAM_TO_SRM_PR(prMbo);
                //记录srm返回的信息
                if(result.toString().getBytes(StandardCharsets.UTF_8).length<=4000){
                    prMbo.setValue("JRESULT", result.toString(), 2L);
                }else{
                    prMbo.setValue("JRESULT", result.toString().substring(0,1333), 2L);
                }
                JSONObject RESPONSE = (JSONObject) result.get("RESPONSE");
                if(null!=RESPONSE){
                    JSONObject RETURN_DATA = (JSONObject) RESPONSE.get("RETURN_DATA");
                    if(null!=RETURN_DATA) {
                        String code=  RETURN_DATA.getString("status");
                        if("S".equalsIgnoreCase(code)){
                        prMbo.setValue("STATUS", "SRM",2L);
                        prMbo.setValue("STATUSDATE", new Date(),2L);
                        }
                    }
                }
            }
            prMbo.getThisMboSet().save();
            msgJSONObject.put("code", "10000");
            msgJSONObject.put("msg", "success");
        }else{
            msgJSONObject.put("code", "20001");
            msgJSONObject.put("errmsg", "参数("+udprnum+")单据在EAM中不存在");
        }
        return msgJSONObject.toString();
    }
}
