package psdi.app.iface.oa;

import com.alibaba.fastjson.JSONObject;
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("/assetdispo")
public class OA2EAMAssetdispoService {
    @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("assetdispoid");//表ID
        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("ASSETDISPO",MXServer.getMXServer().getSystemUserInfo());
        prSet.setWhere("assetdispoid="+udprnum+"");
        prSet.reset();
        if(null!=prSet && !prSet.isEmpty()){
            MboRemote prMbo=prSet.getMbo(0);//当前对象
            String ASSETNUM = prMbo.getString("ASSETNUM");//该单据的对应的设备
            prMbo.setValue("STATUS", status,2L);
            prMbo.getThisMboSet().save();
            //更新EAM中对应的设备运行状态------------------------------------------
            MboSetRemote assetSet = MXServer.getMXServer().getMboSet("ASSET",MXServer.getMXServer().getSystemUserInfo());
            assetSet.setWhere("ASSETNUM='"+ASSETNUM+"'");
            assetSet.reset();
            if(assetSet!=null && !assetSet.isEmpty()){
               MboRemote assetMbo = assetSet.getMbo(0);
               assetMbo.setValue("USESTATUS","Junk");//报废状态
                assetMbo.getThisMboSet().save();
            }
            //更新EAM中对应的设备运行状态------------------------------------------
            msgJSONObject.put("code", "10000");
            msgJSONObject.put("msg", "success");
        }else{
            msgJSONObject.put("code", "20001");
            msgJSONObject.put("errmsg", "EAM无该资产处置单("+udprnum+")");
        }
        return msgJSONObject.toString();
    }

}
