package psdi.app.iface.oa;

import com.alibaba.fastjson.JSONObject;
import psdi.app.iface.sap.Sap2EamHandler;
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;

/**
 * OA 审批资产转固申请后，将状态推送EAM
 */
@Path("/atfac")
public class OA2EamAtfacService {
    @POST
    @Path("/setStatus")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public String setOutSerStatus(String received) throws Exception {
        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 requestid = jsonObject.getString("requestid");
        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 budSet1 = MXServer.getMXServer().getMboSet("UDBUDGETTASK",MXServer.getMXServer().getSystemUserInfo());
        budSet1.setWhere("REQUESTID='"+requestid+"'");
        budSet1.reset();
        if(null!=budSet1 && !budSet1.isEmpty()){
            MboRemote mbo1 = budSet1.getMbo(0);
            mbo1.setValue("STATUS", status,2L);
            mbo1.getThisMboSet().save(2L);
            mbo1.getThisMboSet().close();
        }
        //新mbo
        MboSetRemote budSet = MXServer.getMXServer().getMboSet("UDBUDGETTASK",MXServer.getMXServer().getSystemUserInfo());
        budSet.setWhere("REQUESTID='"+requestid+"'");
        budSet.reset();
        if(null!=budSet && !budSet.isEmpty()){
            MboRemote budMbo=budSet.getMbo(0);
            msgJSONObject.put("code", "10000");
            msgJSONObject.put("msg", "success");
            //-同意的情况下，自动调用SAP转固接口-----------------------------------------------
            if("APPR".equalsIgnoreCase(status)){
                try{
                    System.out.println("------auto--start sap---------");
                    Sap2EamHandler Sap2EamHandler = new Sap2EamHandler();
                    Sap2EamHandler.Z_EAM_ASSET_ZG(budMbo);
                    System.out.println("------auto--end sap-------------");
                }catch (Exception e){
                    System.out.println("-------------e.getMessage()="+e.getMessage());
                }finally {
                    return msgJSONObject.toString();
                }
            }
            //------------------------------------------------------------------------------
        }else{
            msgJSONObject.put("code", "20001");
            msgJSONObject.put("errmsg", "EAM无该转固单("+requestid+")");
        }
        return msgJSONObject.toString();
    }

}
