package psdi.app.iface.mobile;

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

/**
 * 点检保养问题--应用
 * createBy liuxq  2023-5-24
 */
@Path("/udSpotrunLines")
public class UdSpotrunLinesService {

    /**
     * 关闭按钮--变更状态
     * @param received
     * @return
     */
    @POST
    @Path("/updateStatus")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public JSONObject updateStatus(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");
            String ORGID = jsonObject.getString("ORGID");//组织
            String SITEID = jsonObject.getString("SITEID");//地点
            String SPOTRUNLINESID = jsonObject.getString("SPOTRUNLINESID");//表ID
            String REMARKS = jsonObject.getString("REMARKS");//备注
            MboSetRemote SPOTRUNLINESSet = MXServer.getMXServer().getMboSet("SPOTRUNLINES",MXServer.getMXServer().getSystemUserInfo());
            SPOTRUNLINESSet.setWhere(" SPOTRUNLINESID="+SPOTRUNLINESID+"");
            SPOTRUNLINESSet.reset();
            MboRemote mbo = null;
            if(SPOTRUNLINESSet!=null && !SPOTRUNLINESSet.isEmpty()){
                mbo = SPOTRUNLINESSet.getMbo(0);
                mbo.setValue("PERPLAN","CLOSELINE",11L);//赋值--已关闭该问题
                mbo.setValue("REMARKS",REMARKS,11L);//赋值
                mbo.getThisMboSet().save();
                returnDate.put("errcode","200");
//                returnDate.put("errmsg","update success");
                returnDate.put("errmsg",new AMobileConfig().getTipByLang("udSpotrunLines","CloseSuccess","errmsg",LANGCODE));
            }
        }catch (Exception e){
            System.out.println("------faileMessage----->"+e.getMessage());
            returnDate.put("errcode","500");
//            returnDate.put("errmsg","update failure");
            returnDate.put("errmsg",new AMobileConfig().getTipByLang("udSpotrunLines","CloseFailure","errmsg",LANGCODE));
        }finally {
            return returnDate;
        }
    }

    /**
     * 转工单按钮--往日常维修工单新增一条数据
     * @param received
     * @return
     */
    @POST
    @Path("/createWO")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public JSONObject CreateWO(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");
            String ORGID = jsonObject.getString("ORGID");//组织
            String SITEID = jsonObject.getString("SITEID");//地点
            Integer SPOTRUNLINESID = jsonObject.getInteger("SPOTRUNLINESID");//表ID
            String DESCRIPTION = jsonObject.getString("DESCRIPTION");//点检任务描述---工单描述
            String ASSETNUM = jsonObject.getString("ASSETNUM");//资产编号
            String KOSTL = "";//成本中心
            String DEPARTMENT = "";//部门
            MboSetRemote assetSet = MXServer.getMXServer().getMboSet("ASSET",MXServer.getMXServer().getSystemUserInfo());
            assetSet.setWhere(" ASSETNUM='"+ASSETNUM+"' and orgid='"+ORGID+"' and siteid='"+SITEID+"'");
            assetSet.reset();
            if(assetSet!=null && !assetSet.isEmpty()){
                KOSTL = assetSet.getMbo(0).getString("COSTCENTER");
                DEPARTMENT = assetSet.getMbo(0).getString("SDSYBM");//使用部门
            }
            //--------默认执行组是维修组-----
            MboSetRemote UDCJZSet = MXServer.getMXServer().getMboSet("UDCJZ",MXServer.getMXServer().getSystemUserInfo());
            UDCJZSet.setWhere(" ASSETID='"+ASSETNUM+"' and orgid='"+ORGID+"' and siteid='"+SITEID+"' and type='MainGroup'");
            UDCJZSet.reset();
            String PERSONGROUP = "";//流程执行组
            if(UDCJZSet!=null && !UDCJZSet.isEmpty()){
                PERSONGROUP = UDCJZSet.getMbo(0).getString("UDZXZGH");
            }
            //-------------------
            String LOCATION = jsonObject.getString("LOCATION");//资产位置
            String STATUS = "EDIT";//状态
            String CREATEBY = jsonObject.getString("PERACTUAL");//实际执行人---创建人
            String REMARKS = jsonObject.getString("REMARKS");//备注
            String CREATEDATEStr = sdf.format(new Date());
            Date CREATEDATE = sdf.parse(CREATEDATEStr);
            String SPOTRUNNUM = jsonObject.getString("SPOTRUNNUM");//点检任务单编号
            String SPOTRUNLINENUM = jsonObject.getString("SPOTRUNLINENUM");//点检任务行号
            MboSetRemote woSet = MXServer.getMXServer().getMboSet("UDWORKORDER",MXServer.getMXServer().getSystemUserInfo());
            MboRemote mbo = woSet.add();//新增一条数据
            Integer UDWORKORDERID = mbo.getInt("UDWORKORDERID");//主键
            mbo.setValue("DESCRIPTION",DESCRIPTION);
            mbo.setValue("ASSETNUM",ASSETNUM);
            mbo.setValue("LOCATION",LOCATION);
            mbo.setValue("REMARKS",REMARKS);
            mbo.setValue("STATUS",STATUS);
            mbo.setValue("ORGID",ORGID);
            mbo.setValue("SITEID",SITEID);
            mbo.setValue("CREATEBY",CREATEBY);
            mbo.setValue("CREATEDATE",CREATEDATE);
            mbo.setValue("SPOTRUNNUM",SPOTRUNNUM);
            mbo.setValue("SPOTRUNLINENUM",SPOTRUNLINENUM);
            mbo.setValue("WORKSHOP",DEPARTMENT,11L);//部门/车间
            mbo.setValue("PERSONGROUP",PERSONGROUP,11L);//流程执行组
            mbo.setValue("KOSTL",KOSTL);//成本中心
            mbo.setValue("ISSTOP","N");//是否停机
            mbo.setValue("MAINTYPE","01");//维修类型
            mbo.setValue("APPNAME","UDWORKORDERAPP");//appname 给出默认值
            mbo.getThisMboSet().save();
            mbo.getThisMboSet().close();
            //更新该条记录状态  PERPLAN
            MboSetRemote SPOTRUNLINESSet = MXServer.getMXServer().getMboSet("SPOTRUNLINES",MXServer.getMXServer().getSystemUserInfo());
            SPOTRUNLINESSet.setWhere(" SPOTRUNLINESID="+SPOTRUNLINESID+"");
            SPOTRUNLINESSet.reset();
            if(SPOTRUNLINESSet!=null && !SPOTRUNLINESSet.isEmpty()){
                MboRemote spotrunlineMbo = SPOTRUNLINESSet.getMbo(0);
                spotrunlineMbo.setValue("PERPLAN","COMP",2L);//赋值--已生成工单
                spotrunlineMbo.setValue("UDWORKORDERID",UDWORKORDERID,2L);//赋值
                spotrunlineMbo.getThisMboSet().save();
                spotrunlineMbo.getThisMboSet().close();
            }
            returnDate.put("errcode","200");
//            returnDate.put("errmsg","add success");
            returnDate.put("errmsg",new AMobileConfig().getTipByLang("udSpotrunLinescreateWO","success","errmsg",LANGCODE));
            //----------------点检问题单的附件转到日常维修工单---liuxq------start------------------------------------------------------
            //1.查询缺陷单附件
            MboSetRemote doclinksSet = MXServer.getMXServer().getMboSet("DOCLINKS",MXServer.getMXServer().getSystemUserInfo());
            MboSetRemote docinfoSet = MXServer.getMXServer().getMboSet("DOCINFO",MXServer.getMXServer().getSystemUserInfo());
            doclinksSet.setWhere("ownertable='SPOTRUNLINES' and ownerid='"+SPOTRUNLINESID+"'");
            doclinksSet.reset();
            if(doclinksSet!=null && !doclinksSet.isEmpty()){
                for(int i=0;i<doclinksSet.count();i++){
                    MboRemote doclinkMbo = doclinksSet.getMbo(i);
                    int docinfoID = doclinkMbo.getInt("DOCINFOID");
                    docinfoSet.setWhere("DOCINFOID="+docinfoID+"");
                    docinfoSet.reset();
                    if(docinfoSet!=null && !docinfoSet.isEmpty()){
                        for(int k=0;k<docinfoSet.count();k++){
                            MboRemote docinfoMbo = docinfoSet.getMbo(k);
                            String fileName = docinfoMbo.getString("DESCRIPTION");//文件名
                            int ownerid = mbo.getInt("UDWORKORDERID");//日常维修工单的主表ID
                            String ownertable = "UDWORKORDER";//日常维修工单主表
                            String uploadPath = "/sunda/eamfile/attachments/";//linux服务器路径
                            setFileDoclink(fileName,ownerid,ownertable,uploadPath,CREATEBY,"","");//像附件表插入；
                        }
                    }
                }
            }
            //----------------点检问题单的附件转到日常维修工单---liuxq------end------------------------------------------------------
        }catch (Exception e){
            System.out.println("------faileMessage----->"+e.getMessage());
            returnDate.put("errcode","500");
//            returnDate.put("errmsg","add failure");
            returnDate.put("errmsg",new AMobileConfig().getTipByLang("udSpotrunLinescreateWO","failure","errmsg",LANGCODE));
        }finally {
            return returnDate;
        }
    }

    //向附件相关表插入数据
    public void setFileDoclink(String fileName, int ownerid, String ownertable, String uploadPath, String CREATEBY, String fileType, String reference)
    {
        try {
            if ((!fileName.equalsIgnoreCase("")) && (!ownertable.equalsIgnoreCase("")) && (ownerid > 1)) {
                SimpleDateFormat ftd = new SimpleDateFormat("yyyyMMddHHmmss");
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                Date UPLOADDATE = sdf.parse(sdf.format(new Date())); //系统生成上传日期
                MboSetRemote doclinksSet = MXServer.getMXServer().getMboSet("DOCLINKS",MXServer.getMXServer().getSystemUserInfo());
                MboSetRemote docinfoSet = MXServer.getMXServer().getMboSet("DOCINFO",MXServer.getMXServer().getSystemUserInfo());
                //-附件表插入start----------------------------------------------------------------------------------------------
                //docinfo
                MboRemote docinfoMbo = docinfoSet.add();
                Integer DOCINFOID = docinfoMbo.getInt("DOCINFOID");//表ID
                String CONTENTUID= String.valueOf(DOCINFOID);
                String LANGCODE = "ZH";
                int SHOW = 0; int USEDEFAULTFILEPATH = 0; int PRINTTHRULINKDFLT = 0;
                String DOCUMENT = CONTENTUID;//暂时这样设置
                String DESCRIPTION = fileName;//文件名
                String CHANGEBY = CREATEBY;
                String DOCTYPE = "Attachments";//固定值
                String URLTYPE = "FILE";//固定值
                String URLNAME = uploadPath+fileName;// /sunda/eamfile/attachments/企业微信截图_1694773145947.png
                docinfoMbo.setValue("CONTENTUID",CONTENTUID,11L);
                docinfoMbo.setValue("LANGCODE",LANGCODE,11L);
                docinfoMbo.setValue("SHOW",SHOW,11L);
                docinfoMbo.setValue("USEDEFAULTFILEPATH",USEDEFAULTFILEPATH,11L);
                docinfoMbo.setValue("PRINTTHRULINKDFLT",PRINTTHRULINKDFLT,11L);
                docinfoMbo.setValue("DOCUMENT",DOCUMENT,11L);
                docinfoMbo.setValue("DESCRIPTION",DESCRIPTION,11L);
                docinfoMbo.setValue("CREATEDATE",UPLOADDATE,11L);
                docinfoMbo.setValue("CHANGEDATE",UPLOADDATE,11L);
                docinfoMbo.setValue("CHANGEBY",CREATEBY,11L);
                docinfoMbo.setValue("CREATEBY",CREATEBY,11L);
                docinfoMbo.setValue("DOCTYPE",DOCTYPE,11L);
                docinfoMbo.setValue("URLTYPE",URLTYPE,11L);
                docinfoMbo.setValue("URLNAME",URLNAME,11L);
                docinfoMbo.getThisMboSet().save(11L);
                docinfoSet.close();
                //doclinks
                MboRemote doclinksMbo = doclinksSet.add();
                doclinksMbo.setValue("DOCUMENT",DOCUMENT,11L);
                doclinksMbo.setValue("OWNERTABLE",ownertable,11L);
                doclinksMbo.setValue("OWNERID",ownerid,11L);
                doclinksMbo.setValue("DOCTYPE",DOCTYPE,11L);
                doclinksMbo.setValue("GETLATESTVERSION",1,11L);
                doclinksMbo.setValue("CREATEDATE",UPLOADDATE,11L);
                doclinksMbo.setValue("CHANGEDATE",UPLOADDATE,11L);
                doclinksMbo.setValue("CHANGEBY",CREATEBY,11L);
                doclinksMbo.setValue("CREATEBY",CREATEBY,11L);
                doclinksMbo.setValue("PRINTTHRULINK",0,11L);
                doclinksMbo.setValue("COPYLINKTOWO",0,11L);
                doclinksMbo.setValue("DOCINFOID",DOCINFOID,11L);
                doclinksMbo.getThisMboSet().save(11L);
                doclinksSet.close();
                //-附件表插入end----------------------------------------------------------------------------------------------
            }
        } catch (RemoteException e) {
            e.printStackTrace();
        } catch (MXException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }
}
