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.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.rmi.RemoteException;
import java.util.Date;

/**
 * OA 审批委外服务申请后，将状态推送EAM
 */
@Path("/outSer")
public class OA2EAMOutService {
    @POST
    @Path("/setStatus")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public String setOutSerStatus(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);
        String udprnum = jsonObject.getString("udoutserid");
        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("UDOUTSER",MXServer.getMXServer().getSystemUserInfo());
        prSet.setWhere("UDOUTSERID="+udprnum+"");
        prSet.reset();
        if(null!=prSet && !prSet.isEmpty()){
            MboRemote prMbo=prSet.getMbo(0);
            prMbo.setValue("STATUS", status,2L);
            prMbo.getThisMboSet().save();
            msgJSONObject.put("code", "10000");
            msgJSONObject.put("msg", "success");
        }else{
            msgJSONObject.put("code", "20001");
            msgJSONObject.put("errmsg", "EAM无该委外服务单("+udprnum+")");
        }
        return msgJSONObject.toString();
    }

}
