package psdi.app.iface.sap;

import com.alibaba.fastjson.JSONArray;
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;

/**
 * 数据方向 EAM-->Sap
 * 请求方式： Sap-->EAM
 * @author liuxingqiang
 * 注册的接口函数：Z_SAP_STATUS_TO_EAM----------------该接口取消
 * 对于 业务人员来讲，只关心固定资产是否创建，至于是否结算成功是财务关心的。
 */
@Path("/updateInfo")
public class Eam2SapService {

    /**
     * SAP 将转固结算业务完成，返回EAM一个状态值即可
     */
    @POST
    @Path("/changeStatus")
    @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();
        }
        try {
            //参数转换
            JSONObject jsonObject = JSONObject.parseObject(received);
            JSONObject returnJs = jsonObject.getJSONObject("REQUEST").getJSONObject("REQUEST_DATA");
            JSONObject jsonObject2 = returnJs.getJSONObject("SendData");//获取对象
            String BUKRS = jsonObject2.getString("BUKRS");//公司代码
            String AUFNR = jsonObject2.getString("AUFNR");//内部订单号
            String UDBUDGETTASKID = jsonObject2.getString("ZASSET");//EAM任务单号
            String ANLN1 = jsonObject2.getString("ANLN1");//固定资产主编号
            String SUBNUMBER = jsonObject2.getString("SUBNUMBER");//固定资产子编号
            String PROZS = jsonObject2.getString("PROZS");//资产报废率
            String MSGTY = jsonObject2.getString("MSGTY");//消息类型 S.维护成功，E-维护失败
            String MSGTX = jsonObject2.getString("MSGTX");//消息描述 维护状态S时，消息：规则维护成功；维护状态E时，消息：维护失败+SAP返回的标准消息
            //将消息信息保存
            MboSetRemote udbudgetTaskSet = MXServer.getMXServer().getMboSet("UDBUDGETTASK", MXServer.getMXServer().getSystemUserInfo());
            udbudgetTaskSet.setWhere("UDBUDGETTASKID='" + UDBUDGETTASKID + "'");
            udbudgetTaskSet.reset();
            if (udbudgetTaskSet != null && !udbudgetTaskSet.isEmpty()) {
                MboRemote mbo = udbudgetTaskSet.getMbo(0);
                if ("S".equalsIgnoreCase(MSGTY)) {
                    mbo.setValue("STATUS", "COMP", 2L);//结算规则成功
                } else {
                    mbo.setValue("STATUS", "FAILCOMP", 2L);//结算规则失败
                }
                mbo.setValue("DESCRIPTION", MSGTX, 2L);////sap返回的消息提示
                mbo.getThisMboSet().save(2L);
                msgJSONObject.put("code", "S");
                msgJSONObject.put("errmsg", "success");
            }
        }catch(Exception e) {
            msgJSONObject.put("code", "E");
            msgJSONObject.put("errmsg", "failure");
        }finally {
            return msgJSONObject.toString();
        }
    }
}