package psdi.app.iface.mobile;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import psdi.mbo.MboRemote;
import psdi.mbo.MboSetRemote;
import psdi.server.MXServer;

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.text.SimpleDateFormat;
import java.util.Date;

/**
 * 点检任务取消申请
 * createby liuxq   2023-11-23
 */

@Path("/spotcan")
public class SpotcanServer {

    /**
     * 流程调用
     * 启动流程
     */
    @POST
    @Path("/startWF")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public JSONObject startWF(JSONObject jsonObject) {
        JSONObject returnDate = new JSONObject();
        //传参
        String processname ="" ;//工作流程名
        String mbo ="" ;//表名
        String key ="" ;//表ID
        String keyValue ="" ;//表ID值
        String LANGCODE = "";//语言环境
        try{
            LANGCODE = jsonObject.getString("LANGCODE");
            String APPNAME = (String) jsonObject.getString("APPNAME");
            String zxStr = jsonObject.getString("ZX")==null?"1":jsonObject.getString("ZX");
            int zx = Integer.parseInt(zxStr);//正向 1  反向(驳回) 0
            String loginid = (String) jsonObject.getString("USERID"); //登录人编号
            String desc = (String) jsonObject.getString("DESC"); //
            keyValue = String.valueOf(jsonObject.getInteger("OBJID")); //主表id值
            processname ="SPOTCAN" ; //去工作流设计器中查看processaname字段
            mbo ="SPOTCAN";
            key ="SPOTCANID";
            boolean isActive=WorkFlowUtil.isWFActive(mbo,keyValue);
            String result ="";
            if(!isActive){
                result=WorkFlowUtil.startWF(processname,mbo,keyValue,key,loginid); //启动流程
            }else{
                //测试
//                zx=1;
//                desc="sss";
//                loginid="0110994";
                result = WorkFlowUtil.wfGoOn(processname,mbo,keyValue,key,zx,desc,loginid); //审批流程
            }
            JSONObject resObj = (JSONObject) JSONObject.parse(result);
            String code = resObj.getString("code");
            if(resObj!=null && code !=null && !"".equals(code)){
                if("20001".equals(code)){
                    returnDate.put("errcode","201");
//                    returnDate.put("errmsg","start workflow failure,Please Contact Admin");//你不是当前审批人
                    returnDate.put("errmsg",new AMobileConfig().getTipByLang("flowApprove","person","errmsg",LANGCODE));
                }else if("10000".equals(code)){
                    returnDate.put("errcode","200");
//                    returnDate.put("errmsg","start workflow success");
                    returnDate.put("errmsg",new AMobileConfig().getTipByLang("flowStart","success","errmsg",LANGCODE));
                }
            }
        }catch (Exception e){
            System.out.println("------------failureMessage="+e.getMessage());
            returnDate.put("errcode","500");
//            returnDate.put("errmsg","start workflow failure,Please Contact Admin");
            returnDate.put("errmsg",new AMobileConfig().getTipByLang("flowStart","failure","errmsg",LANGCODE));
        }finally {
            return returnDate;
        }
    }

    /**
     * 流程调用
     * 流程审批
     */
    @POST
    @Path("/approveWF")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public JSONObject approveWF(JSONObject jsonObject) {
        JSONObject returnDate = new JSONObject();
        //传参
        String processname ="" ;//工作流程名
        String mbo ="" ;//表名
        String key ="" ;//表ID
        String keyValue ="" ;//表ID值
        int zx ; //1同意，0驳回
        String desc = "";//审批意见
        String loginid = "";//登录人
        String LANGCODE = "";//语言环境
        try{
            LANGCODE = jsonObject.getString("LANGCODE");
            String APPNAME = (String) jsonObject.getString("APPNAME");
            keyValue = String.valueOf(jsonObject.getInteger("OBJID")); //主表id值
            loginid = (String) jsonObject.getString("USERID"); //登录人编号
            zx = Integer.parseInt(jsonObject.getString("ZX"));
            desc = (String) jsonObject.getString("DESC"); //
            processname ="SPOTCAN" ;
            mbo ="SPOTCAN";
            key ="SPOTCANID";
            String result = WorkFlowUtil.wfGoOn(processname,mbo,keyValue,key,zx,desc,loginid); //审批流程
            JSONObject resObj = (JSONObject) JSONObject.parse(result);
            String code = resObj.getString("code");
            if(resObj!=null && code !=null && !"".equals(code)){
                if("10000".equals(code)){
                    if(1==zx) {//正向
                        returnDate.put("errcode","200");
//                        returnDate.put("errmsg","approve workflow success");
                        returnDate.put("errmsg",new AMobileConfig().getTipByLang("flowApprove","success","errmsg",LANGCODE));//流程审批成成功
                    }
                }
            }
        }catch (Exception e){
            System.out.println("------------failureMessage="+e.getMessage());
            returnDate.put("errcode","500");
//            returnDate.put("errmsg","approve workflow failure,Please Contact Admin");
            returnDate.put("errmsg",new AMobileConfig().getTipByLang("flowApprove","failure","errmsg",LANGCODE));//流程审批成失败
        }finally {
            return returnDate;
        }
    }
    /**
     * 流程调用
     * 流程审批人
     */
    @POST
    @Path("/WFAssignMent")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public JSONObject wFAssignMent(JSONObject jsonObject) {
        JSONObject returnDate = new JSONObject();
        //传参
        String key ="" ;//表ID
        String keyValue ="" ;//表ID值
        String LANGCODE = "";//语音环境
        try{
            LANGCODE = jsonObject.getString("LANGCODE");
            String APPNAME = (String) jsonObject.getString("APPNAME");
            keyValue = String.valueOf(jsonObject.getInteger("OBJID")); //主表id值
            key ="SPOTCAN"; //这里传表名
            JSONObject resObj = WorkFlowUtil.wfAssignMent(keyValue,key); //获取流程审批人
            String code = resObj.getString("code");
            if(resObj!=null && code !=null && !"".equals(code)){
                if("20001".equals(code)){
                    returnDate.put("errcode","201");
//                    returnDate.put("errmsg","No to-do tasks");
                    returnDate.put("errmsg",new AMobileConfig().getTipByLang("flowApprove","nextPerson","errmsg",LANGCODE));
                }else if("10000".equals(code)){
//                    returnDate.put("result",resObj);
                    returnDate.put("errcode","200");
//                    returnDate.put("errmsg","view success");
                    returnDate.put("errmsg",new AMobileConfig().getTipByLang("flowApprovePerson","success","errmsg",LANGCODE));
                    returnDate.put("result",resObj.getJSONArray("person"));
                }
            }
        }catch (Exception e){
            System.out.println("------------failureMessage="+e.getMessage());
            returnDate.put("errcode","500");
//            returnDate.put("errmsg","view failure");
            returnDate.put("errmsg",new AMobileConfig().getTipByLang("flowApprovePerson","failure","errmsg",LANGCODE));
        }finally {
            return returnDate;
        }
    }



    /**
     * 主表-数据保存
     * @param received
     * @return
     */
    @POST
    @Path("/saveData")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public JSONObject SaveOrSubmitData(String received) {
        //定义返回对象
        JSONObject returnDate = new JSONObject();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String LANGCODE = "";//语言环境
        try {
            //接收app数据
            JSONObject jsonObject = JSONObject.parseObject(received);
            LANGCODE = jsonObject.getString("LANGCODE");
            Integer SPOTCANID = jsonObject.getInteger("SPOTCANID");//主表ID
            String SPOTCANNUM = jsonObject.getString("SPOTCANNUM");//申请单号
            String REASON = jsonObject.getString("REASON");//取消原因
            String CREATEBY = jsonObject.getString("CREATEBY");//创建人
            String CREATEDATEStr = jsonObject.getString("CREATEDATE");//创建日期
            Date CREATEDATE = null;
            if(CREATEDATEStr!=null&&!"".equals(CREATEDATEStr)){
                CREATEDATE = sdf.parse(CREATEDATEStr);
            }
            String TASKDATEStr = jsonObject.getString("TASKDATE");//生成任务时间（开始）
            Date TASKDATE = null;
            if(TASKDATEStr!=null&&!"".equals(TASKDATEStr)){
                TASKDATE = sdf.parse(TASKDATEStr);
            }
            String TASKDATE2Str = jsonObject.getString("TASKDATE2");//生成任务时间（结束）
            Date TASKDATE2 = null;
            if(TASKDATE2Str!=null&&!"".equals(TASKDATE2Str)){
                TASKDATE2 = sdf.parse(TASKDATE2Str);
            }
            String ORGID = jsonObject.getString("ORGID");//组织
            String SITEID = jsonObject.getString("SITEID");//地点

            MboSetRemote UDINSPECTSet = MXServer.getMXServer().getMboSet("SPOTCAN",MXServer.getMXServer().getSystemUserInfo());
            UDINSPECTSet.setWhere("SPOTCANID="+SPOTCANID+"");
            UDINSPECTSet.reset();
            MboRemote mbo = null;
            if(UDINSPECTSet!=null && !UDINSPECTSet.isEmpty()){
                //编辑
                mbo = UDINSPECTSet.getMbo(0);
            }else{
                //新增
                mbo = UDINSPECTSet.add();
                mbo.setValue("STATUS","EDIT",11L);
                SPOTCANID = mbo.getInt("SPOTCANID");
                SPOTCANNUM = String.valueOf(SPOTCANID);
            }
            mbo.setValue("SPOTCANNUM",SPOTCANNUM,11L);
            mbo.setValue("TASKDATE",TASKDATE,11L);
            mbo.setValue("TASKDATE2",TASKDATE2,11L);
            mbo.setValue("REASON",REASON,11L);
            mbo.setValue("CREATEBY",CREATEBY,11L);
            mbo.setValue("CREATEDATE",CREATEDATE,11L);
            mbo.setValue("ORGID",ORGID,11L);
            mbo.setValue("SITEID",SITEID,11L);
            //保存
            mbo.getThisMboSet().save(11L);
            returnDate.put("SPOTCANID",SPOTCANID);//返回前端刷新页面
            returnDate.put("errcode","200");
//            returnDate.put("errmsg","save success");
            returnDate.put("errmsg",new AMobileConfig().getTipByLang("save","success","errmsg",LANGCODE));
        }catch (Exception e){
            e.printStackTrace();
            returnDate.put("errcode","500");
//            returnDate.put("errmsg","save failure");
            returnDate.put("errmsg",new AMobileConfig().getTipByLang("save","failure","errmsg",LANGCODE));
        }finally {
            return returnDate;
        }
    }

    /**
     *  子表点检任务保存
     */
    @POST
    @Path("/saveTaskData")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public JSONObject SaveTaskData(JSONObject jsonObject) {
        //定义返回对象
        JSONObject returnDate = new JSONObject();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String LANGCODE = "";//语言环境
        try {
            //接收app数据
//            Integer SPOTCANLINEID = jsonObject.getInteger("SPOTCANLINEID");//子表ID 无用
            LANGCODE = jsonObject.getString("LANGCODE");
            String PARENT = jsonObject.getString("PARENT");//父级
            String ORGID = jsonObject.getString("ORGID");//组织
            String SITEID = jsonObject.getString("SITEID");//地点
            JSONArray dataArr = jsonObject.getJSONArray("SPOTRUNNUMLIST");//多选
            MboSetRemote SPOTCANLINESet = MXServer.getMXServer().getMboSet("SPOTCANLINE",MXServer.getMXServer().getSystemUserInfo());
            if(dataArr!=null && !dataArr.isEmpty()){
                for (Object part : dataArr) {
                    MboRemote mbo = SPOTCANLINESet.add();
                    mbo.setValue("PARENT",PARENT,11L);
                    mbo.setValue("SPOTRUNNUM", (String) part,11L);
                    mbo.setValue("ORGID",ORGID,11L);
                    mbo.setValue("SITEID",SITEID,11L);
                    mbo.getThisMboSet().save(11L); //保存
                }
            }
            returnDate.put("errcode","200");
//            returnDate.put("errmsg","save success");
            returnDate.put("errmsg",new AMobileConfig().getTipByLang("save","success","errmsg",LANGCODE));
        }catch (Exception e){
            e.printStackTrace();
            returnDate.put("errcode","500");
//            returnDate.put("errmsg","save failure");
            returnDate.put("errmsg",new AMobileConfig().getTipByLang("save","failure","errmsg",LANGCODE));
        }finally {
            return returnDate;
        }
    }

    //---删除--------------------------------------------------------------------------------
    /**
     * 主表的删除
     * @param received
     * @return
     */
    @POST
    @Path("/deleteData")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public JSONObject DeleteData(String received) {
        //定义返回对象
        JSONObject returnDate = new JSONObject();
        String LANGCODE = "";//语言环境
        try {
            //接收app数据
            JSONObject jsonObject = JSONObject.parseObject(received);
            LANGCODE = jsonObject.getString("LANGCODE");
            Integer SPOTCANID = jsonObject.getInteger("SPOTCANID");
            MboSetRemote UDPLMAWTSet = MXServer.getMXServer().getMboSet("SPOTCAN",MXServer.getMXServer().getSystemUserInfo());
            UDPLMAWTSet.setWhere(" SPOTCANID="+SPOTCANID+"");//int 类型
            UDPLMAWTSet.reset();
            MboRemote mbo = UDPLMAWTSet.getMbo(0);//
            mbo.delete();//相当于delete语句，结束后，还得保存
            mbo.getThisMboSet().save();
            //任务删除
            MboSetRemote UDPARTICIPANTSSet = MXServer.getMXServer().getMboSet("SPOTCANLINE",MXServer.getMXServer().getSystemUserInfo());
            UDPARTICIPANTSSet.setWhere(" parent="+SPOTCANID+"");//int 类型
            UDPARTICIPANTSSet.reset();
            UDPARTICIPANTSSet.deleteAll();
            UDPARTICIPANTSSet.save();
            returnDate.put("errcode","200");
//            returnDate.put("errmsg","delete success");
            returnDate.put("errmsg",new AMobileConfig().getTipByLang("delete","success","errmsg",LANGCODE));
        }catch (Exception e){
            System.out.println("------faileMessage----->"+e.getMessage());
            returnDate.put("errcode","500");
//            returnDate.put("errmsg","delete failure");
            returnDate.put("errmsg",new AMobileConfig().getTipByLang("delete","failure","errmsg",LANGCODE));
        }finally {
            return returnDate;
        }
    }

    /**
     * 点检任务的删除
     * @param received
     * @return
     */
    @POST
    @Path("/deleteTaskData")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public JSONObject DeleteTaskData(String received) {
        //定义返回对象
        JSONObject returnDate = new JSONObject();
        String LANGCODE = "";//语言环境
        try {
            //接收app数据
            JSONObject jsonObject = JSONObject.parseObject(received);
            LANGCODE = jsonObject.getString("LANGCODE");
            Integer SPOTCANLINEID = jsonObject.getInteger("SPOTCANLINEID");
            MboSetRemote UDPLMAWTSet = MXServer.getMXServer().getMboSet("SPOTCANLINE",MXServer.getMXServer().getSystemUserInfo());
            UDPLMAWTSet.setWhere(" SPOTCANLINEID="+SPOTCANLINEID+"");//int 类型
            UDPLMAWTSet.reset();
            MboRemote mbo = UDPLMAWTSet.getMbo(0);//
            mbo.delete();//相当于delete语句，结束后，还得保存
            mbo.getThisMboSet().save();
            returnDate.put("errcode","200");
//            returnDate.put("errmsg","delete success");
            returnDate.put("errmsg",new AMobileConfig().getTipByLang("delete","success","errmsg",LANGCODE));
        }catch (Exception e){
            System.out.println("------faileMessage----->"+e.getMessage());
            returnDate.put("errcode","500");
//            returnDate.put("errmsg","delete failure");
            returnDate.put("errmsg",new AMobileConfig().getTipByLang("delete","failure","errmsg",LANGCODE));
        }finally {
            return returnDate;
        }
    }

}
