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-6-20
 */
@Path("/udpd")
public class UdPDService {
    /**
     * 子表-扫码变更状态
     * @param received
     * @return
     */
    @POST
    @Path("/updateStatus")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public JSONObject SaveData(String received) {
        //定义返回对象
        JSONObject returnDate = new JSONObject();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            //接收app数据
            JSONObject jsonObject = JSONObject.parseObject(received);
            Integer UDPDID = jsonObject.getInteger("UDPDID");//盘点主表ID
            Integer UDPDLINEID = jsonObject.getInteger("UDPDLINEID");//盘点子表id
            String PDNUM = jsonObject.getString("PDNUM");//盘点单号
            String ASSETNUM = jsonObject.getString("ASSETNUM");//资产编号
            String PDPEO = jsonObject.getString("PDPEO");//盘点人
            Date PDDATE = sdf.parse(sdf.format(new Date()));//盘点日期
            String ORGID = jsonObject.getString("ORGID");//组织
            String SITEID = jsonObject.getString("SITEID");//地点

            MboSetRemote UDPDLINESet = MXServer.getMXServer().getMboSet("UDPDLINE",MXServer.getMXServer().getSystemUserInfo());
            UDPDLINESet.setWhere(" PDNUM="+PDNUM+" and ASSETNUM='"+ASSETNUM+"' and orgid='"+ORGID+"' and siteid='"+SITEID+"'");//int 类型
            UDPDLINESet.reset();
            MboRemote mbo = null;
            if(UDPDLINESet!=null && !UDPDLINESet.isEmpty()){
                //编辑
                mbo = UDPDLINESet.getMbo(0);
                if("Y".equals(mbo.getString("STATUS"))){
                    returnDate.put("UDPDID",UDPDID);//返回前端刷新页面
                    returnDate.put("UDPDLINEID",UDPDLINEID);
                    returnDate.put("errcode","202");
                    returnDate.put("errmsg","Already Checked");
                }else{
                    mbo.setValue("PDNUM",PDNUM,2L);
                    mbo.setValue("ASSETNUM",ASSETNUM,2L);
                    mbo.setValue("STATUS","Y",2L);
                    mbo.setValue("PDPEO",PDPEO,2L);
                    mbo.setValue("PDDATE",PDDATE,2L);
                    mbo.setValue("ORGID",ORGID,2L);
                    mbo.setValue("SITEID",SITEID,2L);
                    //保存
                    mbo.getThisMboSet().save();
                    returnDate.put("UDPDID",UDPDID);//返回前端刷新页面
                    returnDate.put("UDPDLINEID",UDPDLINEID);
                    returnDate.put("errcode","200");
                    returnDate.put("errmsg","Checked Success");
                }
            }else {
                returnDate.put("UDPDID",UDPDID);//返回前端刷新页面
                returnDate.put("UDPDLINEID",UDPDLINEID);//返回前端刷新页面
                returnDate.put("errcode","205");
                returnDate.put("errmsg","No Asset Included");
            }
        }catch (Exception e){
            System.out.println("------faileMessage----->"+e.getMessage());
            returnDate.put("errcode","500");
            returnDate.put("errmsg","update status failure");
        }finally {
            return returnDate;
        }
    }


    //--离线----------------------start-----------------------
    /**
     * 离线下载----主子信息
     */
    @POST
    @Path("/offDownload")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public JSONObject offlineDownload(JSONObject jsonObject) {
        //定义返回对象
        JSONObject returnData = new JSONObject();
        String LANGCODE = "";//语言环境
        try {
            //接收app数据-------start--------------------------------------------------------------
            String USERID = jsonObject.getString("USERID");//APP传过来的当前用户工号
            JSONArray dataArr = jsonObject.getJSONArray("IDLIST");//勾选的id
            LANGCODE = jsonObject.getString("LANGCODE");//登录环境
            String ORGID = jsonObject.getString("ORGID");//组织
            String SITEID = jsonObject.getString("SITEID");//地点
            //接收app数据--------end------------------------------------------------------------------
            if(USERID==null || "".equals(USERID) || LANGCODE==null || "".equals(LANGCODE) || ORGID==null || "".equals(ORGID) || SITEID==null || "".equals(SITEID)){
                returnData.put("errcode", "100");
                returnData.put("errmsg", "Parameters Lost");
                return returnData;
            }
            String strID = "";
            if(dataArr!=null && !dataArr.isEmpty()){
                for(int i=0;i<dataArr.size();i++){
                    strID +=dataArr.get(i) + ",";
                }
                strID = strID.trim();
                strID = strID.substring(0,strID.length()-1);
            } else {
                returnData.put("errcode", "100");
                returnData.put("errmsg", "No data downLoad");
                return returnData;
            }
            //地点表
            String COMPANY = "" ;//公司
            MboSetRemote siteSet = MXServer.getMXServer().getMboSet("SITE",MXServer.getMXServer().getSystemUserInfo());//地点表
            siteSet.setWhere("ORGID='"+ORGID+"' and SITEID='"+SITEID+"'");
            siteSet.reset();
            if(siteSet!=null && !siteSet.isEmpty()){
                MboRemote siteMbo = siteSet.getMbo(0);
                COMPANY = siteMbo.getString("COMPANY");
            }
            //获取主表信息
            MboSetRemote udpdSet = MXServer.getMXServer().getMboSet("UDPD",MXServer.getMXServer().getSystemUserInfo());//主表
            udpdSet.setWhere("UDPDID in("+strID+")");
            udpdSet.reset();
            if(udpdSet!=null && !udpdSet.isEmpty()){
                //盘点计划
                MboSetRemote pdplanSet = MXServer.getMXServer().getMboSet("UDPDPLAN",MXServer.getMXServer().getSystemUserInfo());
                //人员名称-没有多语言
                MboSetRemote personSet = MXServer.getMXServer().getMboSet("PERSON",MXServer.getMXServer().getSystemUserInfo());
                MboSetRemote directorSet = MXServer.getMXServer().getMboSet("PERSON",MXServer.getMXServer().getSystemUserInfo());
                MboSetRemote downloadPersonSet = MXServer.getMXServer().getMboSet("PERSON",MXServer.getMXServer().getSystemUserInfo());
                //部门多语言查询
                MboSetRemote deptSet = MXServer.getMXServer().getMboSet("DEPT",MXServer.getMXServer().getSystemUserInfo());
                MboSetRemote deptLangSet = MXServer.getMXServer().getMboSet("L_DEPT",MXServer.getMXServer().getSystemUserInfo());
                //状态多语言查询
                MboSetRemote alndomainSet = MXServer.getMXServer().getMboSet("ALNDOMAIN",MXServer.getMXServer().getSystemUserInfo());
                MboSetRemote alndomainLangSet = MXServer.getMXServer().getMboSet("L_ALNDOMAIN",MXServer.getMXServer().getSystemUserInfo());
                //盘点类型多语言查询
                MboSetRemote pdtypealndomainSet = MXServer.getMXServer().getMboSet("ALNDOMAIN",MXServer.getMXServer().getSystemUserInfo());
                MboSetRemote pdtypealndomainLangSet = MXServer.getMXServer().getMboSet("L_ALNDOMAIN",MXServer.getMXServer().getSystemUserInfo());
                //盘点数量
                MboSetRemote PDZSLTOTALSet = MXServer.getMXServer().getMboSet("UDPDLINE",MXServer.getMXServer().getSystemUserInfo());
                MboSetRemote YPSLSet = MXServer.getMXServer().getMboSet("UDPDLINE",MXServer.getMXServer().getSystemUserInfo());
                MboSetRemote PKSLSet = MXServer.getMXServer().getMboSet("UDPDLINE",MXServer.getMXServer().getSystemUserInfo());
                MboSetRemote PYSLSet = MXServer.getMXServer().getMboSet("UDPDLINE",MXServer.getMXServer().getSystemUserInfo());

                JSONArray taskDataArr = new JSONArray();//放主表任务
                for(int i=0;i<udpdSet.count();i++){
                    JSONObject taskData = new JSONObject();//主表JSON对象
                    MboRemote mbo = udpdSet.getMbo(i);//主表对象
                    Integer UDPDID = mbo.getInt("UDPDID");//盘点任务表ID
                    Integer UDPDPLANID = mbo.getInt("UDPDPLANID");//盘点计划表ID
                    String PDNUM = mbo.getString("PDNUM");//盘点单号
                    String DESCRIPTION = mbo.getString("DESCRIPTION");//盘点描述
                    String PDRESULT = mbo.getString("PDRESULT");//盘点结论
                    String REMARK = mbo.getString("REMARK");//备注
                    String STATUS = mbo.getString("STATUS");//状态
                    String DEPTNUM = mbo.getString("DEPTNUM");//部门编号
                    String CREATEBY = mbo.getString("CREATEBY");//创建人
                    Date CREATEDATE = mbo.getDate("CREATEDATE");//创建时间
                    //--------默认描述都是英文------------------------------------------------------------------------------------
                    String DEPTNAME = "";//部门描述
                    String CREATEBYDESC = "";//创建人名称
                    String DIRECTOR = "";//部门负责人
                    String DIRECTORNAME = "";//部门负责人名称
                    String USERIDDESC = "";//任务下载人名称
                    String PDTYPE = "";//盘点类型
                    String PDTYPEDESC = "";//盘点类型描述
                    String STATUSDESC = "";//状态描述
                    Integer PDZSLTOTAL = 0;//盘点总数
                    Integer YPSL = 0;//已盘数量
                    Integer PKSL = 0;//盘亏数量
                    Integer PYSL = 0;//盘盈数量
                    Integer STATUS_ALNDOMAINID = null;
                    Integer PDTYPE_ALNDOMAINID = null;
                    //盘点总数
                    PDZSLTOTALSet.setWhere("PDNUM='"+PDNUM+"' and orgid='"+ORGID+"' and SITEID='"+SITEID+"'");
                    PDZSLTOTALSet.reset();
                    if(PDZSLTOTALSet!=null && !PDZSLTOTALSet.isEmpty()){
                        PDZSLTOTAL = PDZSLTOTALSet.count();
                    }else{
                        PDZSLTOTAL = 0;
                    }
                    //盘亏数量
                    PKSLSet.setWhere("PDNUM='"+PDNUM+"' and orgid='"+ORGID+"' and SITEID='"+SITEID+"' and STATUS='PK'");
                    PKSLSet.reset();
                    if(PKSLSet!=null && !PKSLSet.isEmpty()){
                        PKSL = PKSLSet.count();
                    }else{
                        PKSL = 0;
                    }
                    //盘盈数量
                    PYSLSet.setWhere("PDNUM='"+PDNUM+"' and orgid='"+ORGID+"' and SITEID='"+SITEID+"' and STATUS='PY'");
                    PYSLSet.reset();
                    if(PYSLSet!=null && !PYSLSet.isEmpty()){
                        PYSL = PYSLSet.count();
                    }else{
                        PYSL = 0;
                    }
                    //已盘数量
                    YPSLSet.setWhere("PDNUM='"+PDNUM+"' and orgid='"+ORGID+"' and SITEID='"+SITEID+"' and STATUS='YP'");
                    YPSLSet.reset();
                    if(YPSLSet!=null && !YPSLSet.isEmpty()){
                        YPSL = YPSLSet.count();
                    }else{
                        YPSL = 0;
                    }
                    if("EN".equalsIgnoreCase(LANGCODE) || "FR".equalsIgnoreCase(LANGCODE)){
                        if("EDIT".equalsIgnoreCase(STATUS)){
                            STATUSDESC = "Undownload";
                        }else if("Appr".equalsIgnoreCase(STATUS)){
                            STATUS = "Download";
                        }else if("COMP".equalsIgnoreCase(STATUS)){
                            STATUS = "Upload";
                        }
                    }
                    //---英文状态下查询------------------------------------------------------------------------------------------------------
                    //查询部门的英文描述和表ID
                    deptSet.setWhere("deptnum='"+DEPTNUM+"' and orgid='"+ORGID+"' and SITEID='"+SITEID+"'");
                    deptSet.reset();
                    Integer DEPTID = null;//部门ID
                    if(deptSet!=null && !deptSet.isEmpty()){
                        MboRemote deptMbo = deptSet.getMbo(0);
                        DEPTID = deptMbo.getInt("DEPTID");
                        DEPTNAME = deptMbo.getString("DESCRIPTION");
                        DIRECTOR = deptMbo.getString("DIRECTOR");
                    }
                    //查询创建人名称
                    personSet.setWhere("personid='"+CREATEBY+"'");
                    personSet.reset();
                    if(personSet!=null && !personSet.isEmpty()){
                        MboRemote mboDesc = personSet.getMbo(0);
                        CREATEBYDESC = mboDesc.getString("DISPLAYNAME");
                    }
                    //查询部门负责人名称
                    directorSet.setWhere("personid='"+DIRECTOR+"'");
                    directorSet.reset();
                    if(directorSet!=null && !directorSet.isEmpty()){
                        MboRemote mboDesc = directorSet.getMbo(0);
                        DIRECTORNAME = mboDesc.getString("DISPLAYNAME");
                    }
                    //查询任务下载人名称
                    downloadPersonSet.setWhere("personid='"+USERID+"'");
                    downloadPersonSet.reset();
                    if(downloadPersonSet!=null && !downloadPersonSet.isEmpty()){
                        MboRemote mboDesc = downloadPersonSet.getMbo(0);
                        USERIDDESC = mboDesc.getString("DISPLAYNAME");
                    }
                    //盘点类型
                    pdplanSet.setWhere("UDPDPLANID="+UDPDPLANID+"");
                    pdplanSet.reset();
                    if(pdplanSet!=null && !pdplanSet.isEmpty()){
                        MboRemote pdplanMbo = pdplanSet.getMbo(0);
                        PDTYPE = pdplanMbo.getString("PDTYPE");//盘点类型
                    }
                    //类型
                    pdtypealndomainSet.setWhere("value='"+PDTYPE+"' and DOMAINID='PDTYPE'");
                    pdtypealndomainSet.reset();
                    if(pdtypealndomainSet!=null && !pdtypealndomainSet.isEmpty()){
                        MboRemote mboDesc = pdtypealndomainSet.getMbo(0);
                        PDTYPE_ALNDOMAINID = mboDesc.getInt("ALNDOMAINID");
                    }
                    //状态
                    alndomainSet.setWhere("value='"+STATUS+"' and DOMAINID='PDSTATUS'");
                    alndomainSet.reset();
                    if(alndomainSet!=null && !alndomainSet.isEmpty()){
                        MboRemote mboDesc = alndomainSet.getMbo(0);
                        STATUS_ALNDOMAINID = mboDesc.getInt("ALNDOMAINID");
                    }
                    //-非英文（ZH,FR）状态下，需要进行查询-------------start-----------------------------------------------------------------------
                    if(!"EN".equalsIgnoreCase(LANGCODE)){
                        //部门描述
                        deptLangSet.setWhere(" OWNERID="+DEPTID+" and LANGCODE='"+LANGCODE+"'");
                        deptLangSet.reset();
                        if(deptLangSet!=null && !deptLangSet.isEmpty()){
                            MboRemote mboDesc = deptLangSet.getMbo(0);
                            DEPTNAME = mboDesc.getString("DESCRIPTION");
                        }
                        //状态描述
                        alndomainLangSet.setWhere(" OWNERID="+STATUS_ALNDOMAINID+" and LANGCODE='"+LANGCODE+"'");
                        alndomainLangSet.reset();
                        if(alndomainLangSet!=null && !alndomainLangSet.isEmpty()){
                            MboRemote mboDesc = alndomainLangSet.getMbo(0);
                            STATUSDESC = mboDesc.getString("DESCRIPTION");
                        }
                        //盘点类型描述
                        pdtypealndomainLangSet.setWhere(" OWNERID="+PDTYPE_ALNDOMAINID+" and LANGCODE='"+LANGCODE+"'");
                        pdtypealndomainLangSet.reset();
                        if(pdtypealndomainLangSet!=null && !pdtypealndomainLangSet.isEmpty()){
                            MboRemote mboDesc = pdtypealndomainLangSet.getMbo(0);
                            PDTYPEDESC = mboDesc.getString("DESCRIPTION");
                        }
                    }
                    //-非英文（ZH,FR）状态下，需要进行查询-------------end-----------------------------------------------------------------------
                    taskData.put("UDPDID",UDPDID);//主表ID
                    taskData.put("UDPDPLANID",UDPDPLANID);//盘点计划主表ID
                    taskData.put("PDNUM",PDNUM);//盘点任务单号
                    taskData.put("DESCRIPTION",DESCRIPTION);//盘点任务描述
                    taskData.put("PDTYPE",PDTYPE);//盘点类型
                    taskData.put("PDTYPEDESC",PDTYPEDESC);//盘点类型描述
                    taskData.put("DEPTNUM",DEPTNUM);//部门编号
                    taskData.put("DEPTNUMDESC",DEPTNAME);//部门描述
                    taskData.put("DIRECTOR",DIRECTOR);//部门负责人编号
                    taskData.put("DIRECTORDESC",DIRECTORNAME);//部门负责人名称
                    taskData.put("PDRESULT",PDRESULT);//盘点结论
                    taskData.put("PDZSLTOTAL",PDZSLTOTAL);//盘点总数
                    taskData.put("PKSL",PKSL);//盘亏数量
                    taskData.put("PYSL",PYSL);//盘盈数量
                    taskData.put("YPSL",YPSL);//已盘数量
                    taskData.put("REMARK",REMARK);//备注
                    taskData.put("STATUS",STATUS);//盘点状态
                    taskData.put("STATUSDESC",STATUSDESC);//盘点状态描述
                    taskData.put("CREATEBY",CREATEBY);//创建人
                    taskData.put("CREATEBYDESC",CREATEBYDESC);//创建人名称
                    taskData.put("CREATEDATE",CREATEDATE);//创建时间
                    taskData.put("USERID",USERID);//下载人
                    taskData.put("USERIDDESC",USERIDDESC);//下载人名称
                    taskData.put("ORGID",ORGID);//组织
                    taskData.put("SITEID",SITEID);//地点
                    //-----获取子表对象----------start-------------------------------------------------------------------------------------
                    //设备的多语言表
                    MboSetRemote assetSet = MXServer.getMXServer().getMboSet("ASSET",MXServer.getMXServer().getSystemUserInfo());//设备表
                    MboSetRemote assetLangSet = MXServer.getMXServer().getMboSet("L_ASSET",MXServer.getMXServer().getSystemUserInfo());//设备多语言表
                    //子表盘点人
                    MboSetRemote linePDPersonSet = MXServer.getMXServer().getMboSet("PERSON",MXServer.getMXServer().getSystemUserInfo());
                    //固定资产信息表-没有多语言表
                    MboSetRemote udgdzcSet = MXServer.getMXServer().getMboSet("UDGDZC",MXServer.getMXServer().getSystemUserInfo());//固定资产信息表
                    //-----获取子表对象---------------
                    MboSetRemote udpdlineSet = MXServer.getMXServer().getMboSet("UDPDLINE",MXServer.getMXServer().getSystemUserInfo());//子表
                    udpdlineSet.setWhere("PDNUM='"+PDNUM+"' and orgid='"+ORGID+"' and SITEID='"+SITEID+"'");
                    udpdlineSet.reset();
                    udpdlineSet.setOrderBy("GDZCBH ASC");//固定资产编号排序
                    if(udpdlineSet!=null && !udpdlineSet.isEmpty()){
                        JSONArray taskLineDataArr = new JSONArray();//放子表任务
                        for(int k=0;k<udpdlineSet.count();k++){
                            JSONObject taskLineData = new JSONObject();//单个子表任务
                            MboRemote lineMbo = udpdlineSet.getMbo(k);//每个子表对象
                            Integer UDPDLINEID = lineMbo.getInt("UDPDLINEID");//子表ID
                            String GDZCBH = lineMbo.getString("GDZCBH");//固定资产编号
                            String ASSETNUM = lineMbo.getString("ASSETNUM");//盘盈标签页的资产名称
                            String STATUSLINE = lineMbo.getString("STATUS");//子表状态
                            String PDDATELINE = lineMbo.getString("PDDATE");//子表盘点日期
                            String PDPEOLINE = lineMbo.getString("PDPEO");//子表盘点人
                            String PDPEODESCLINE = "";//盘点人名称
                            String GDZCMC = "";//固定资产名称
                            String KOSTL = "";//成本中心编号
                            String KOSTLDESC = "";//成本中心名称
                            udgdzcSet.setWhere("ANLN1='"+GDZCBH+"' and BUKRS='"+COMPANY+"'");//通过公司和固定资产编号去获取
                            udgdzcSet.reset();
                            if(udgdzcSet!=null && !udgdzcSet.isEmpty()){
                                MboRemote gdzcMbo = udgdzcSet.getMbo(0);
                                GDZCMC = gdzcMbo.getString("TXT50");//固定资产名称
                                KOSTL = gdzcMbo.getString("KOSTL");//成本中心编号
                                KOSTLDESC = gdzcMbo.getString("KTEXT");//成本中心名称
                            }
                            //盘点人
                            linePDPersonSet.setWhere("personid='"+PDPEOLINE+"'");
                            linePDPersonSet.reset();
                            if(linePDPersonSet!=null && !linePDPersonSet.isEmpty()){
                                MboRemote mboDesc = linePDPersonSet.getMbo(0);
                                PDPEODESCLINE = mboDesc.getString("DISPLAYNAME");
                            }
                            //-英文状态下----------------------------------------------------------
                            Integer ASSETUID = null;
                            String ASSET = "";//关联设备台账表中的设备编号
                            String ASSETDESC = "";//设备名称
                            String MEMORYCODE = "";//助记码
                            assetSet.setWhere("SDGDZCBM='"+GDZCBH+"' and orgid='"+ORGID+"' and siteid='"+SITEID+"'");
                            assetSet.reset();
                            if(assetSet!=null && !assetSet.isEmpty()){
                                MboRemote mboDesc = assetSet.getMbo(0);
                                ASSETUID = mboDesc.getInt("ASSETUID");
                                ASSET = mboDesc.getString("ASSETNUM");
                                ASSETDESC = mboDesc.getString("DESCRIPTION");
                                MEMORYCODE = mboDesc.getString("MEMORYCODE");
                            }
                            //-非英文状态下，需要进行查询----------------------------------------------------------
                            if(!"EN".equalsIgnoreCase(LANGCODE)){
                                //设备查询
                                assetLangSet.setWhere(" OWNERID="+ASSETUID+" and LANGCODE='"+LANGCODE+"'");
                                assetLangSet.reset();
                                if(assetLangSet!=null && !assetLangSet.isEmpty()){
                                    MboRemote mboDesc = assetLangSet.getMbo(0);
                                    ASSETDESC = mboDesc.getString("DESCRIPTION");//设备名称
                                }
                            }
                            //子表参数
                            taskLineData.put("UDPDLINEID",UDPDLINEID);//子表ID
                            taskLineData.put("PDNUM",PDNUM);//关联字段
                            taskLineData.put("STATUS",STATUSLINE);//子表盘点状态
                            taskLineData.put("PDDATE",PDDATELINE);//子表盘点日期
                            taskLineData.put("PDPEO",PDPEOLINE);//子表盘点人
                            taskLineData.put("PDPEODESC",PDPEODESCLINE);//子表盘点人
                            taskLineData.put("GDZCBH",GDZCBH);//固定资产编号
                            taskLineData.put("GDZCBHDESC",GDZCMC);//固定资产名称
                            taskLineData.put("KOSTL",KOSTL);//成本中心编号
                            taskLineData.put("KOSTLDESC",KOSTLDESC);//成本中心名称
                            taskLineData.put("ASSET",ASSET);//设备编号---关联(PK,YP,WP)
                            taskLineData.put("ASSETNUMDESC",ASSETDESC);//设备名称
                            taskLineData.put("MEMORYCODE",MEMORYCODE);//设备助记码
                            taskLineData.put("ASSETNUM",ASSETNUM);//设备编号--实体字段（PY）
                            taskLineData.put("ORGID",ORGID);//组织
                            taskLineData.put("SITEID",SITEID);//地点
                            taskLineDataArr.add(taskLineData);
                        }
                        taskData.put("taskline",taskLineDataArr);//一个主表任务对应多个点检项目
                    }
                    //-----获取子表对象-----------end---------------------------------------------------------------------------------------
                    //主表对象加入主表数组
                    taskDataArr.add(taskData);
                    mbo.setValue("STATUS","APPR");//变为已下载
                    mbo.setValue("PDPEO",USERID);//下载人
                    mbo.setValue("PDDATE",new Date());//下载时间
                    mbo.getThisMboSet().save();
                }
                returnData.put("errcode","200");
                returnData.put("errmsg",new AMobileConfig().getTipByLang("download","success","errmsg",LANGCODE));
                returnData.put("taskdata",taskDataArr);//返回任务
            }
        }catch (Exception e){
            System.out.println("------faileMessage----->"+e.getMessage());
            returnData.put("errcode","500");
//            returnData.put("errmsg","save failure,"+e.getMessage());
            returnData.put("errmsg",new AMobileConfig().getTipByLang("download","failure","errmsg",LANGCODE)+"-"+e.getMessage());
        }finally {
            return returnData;
        }
    }
    //--离线----------------------end-----------------------


    //---在线---------------------start--------------------------
    /**
     * 盘点任务主表保存 - 接口
     * @param received
     * @return
     */
    @POST
    @Path("/mainSave")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public JSONObject mainSave(String received) {
        //定义返回对象
        JSONObject returnData = new JSONObject();
        String LANGCODE = "";//语言环境
        try {
            //接收app数据
            JSONObject jsonObject = JSONObject.parseObject(received);
            LANGCODE = jsonObject.getString("LANGCODE");
            Integer UDPDID = jsonObject.getInteger("UDPDID");//表ID
            String ORGID = jsonObject.getString("ORGID");//组织
            String SITEID = jsonObject.getString("SITEID");//地点
            String PDRESULT = jsonObject.getString("PDRESULT");//盘点结论
            String REMARK = jsonObject.getString("REMARK");//盘点备注

            MboSetRemote udpdSet = MXServer.getMXServer().getMboSet("UDPD",MXServer.getMXServer().getSystemUserInfo());
            udpdSet.setWhere(" UDPDID="+UDPDID+" AND ORGID='"+ORGID+"' AND SITEID='"+SITEID+"' ");
            udpdSet.reset();
            if(null!=udpdSet && !udpdSet.isEmpty()){
                MboRemote mbo=udpdSet.getMbo(0);
                mbo.setValue("PDRESULT",PDRESULT,11L);//盘点结论
                mbo.setValue("REMARK",REMARK,11L);//盘点备注
                //保存
                mbo.getThisMboSet().save(11L);
                returnData.put("UDPDID",UDPDID);//返回前端刷新页面
                returnData.put("errcode","200");
//                returnData.put("errmsg","save success");
                returnData.put("errmsg",new AMobileConfig().getTipByLang("save","success","errmsg",LANGCODE));
            }
        }catch (Exception e){
            System.out.println("------faileMessage----->"+e.getMessage());
            returnData.put("errcode","500");
//            returnData.put("errmsg","save failure,"+e.getMessage());
            returnData.put("errmsg",new AMobileConfig().getTipByLang("save","failure","errmsg",LANGCODE));
        }finally {
            return returnData;
        }
    }

    /**
     * 资产明细子表数据-批量保存或者离线批量上传
     * @return
     */
    @POST
    @Path("/batchSavePDlineData")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public JSONObject batchSavePDlineData(JSONObject JSOBJ) {
        //定义返回对象
        JSONObject returnData = new JSONObject();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String LANGCODE = "";//语言环境
        try {
            String UDPDID = JSOBJ.getString("UDPDID");//主表ID
            String PDRESULT = JSOBJ.getString("PDRESULT");//盘点结论
            String REMARK = JSOBJ.getString("REMARK");//备注
            String ORGID = JSOBJ.getString("ORGID");//组织
            String SITEID = JSOBJ.getString("SITEID");//地点
            LANGCODE = JSOBJ.getString("LANGCODE");//语言环境
            JSONArray jsonArray = JSOBJ.getJSONArray("TASKBATCH");//子对象放在一个数组中
            if(jsonArray!=null && !jsonArray.isEmpty()){
                MboSetRemote udpdlineSet = MXServer.getMXServer().getMboSet("UDPDLINE",MXServer.getMXServer().getSystemUserInfo());
                //接收app数据
                for(int i=0;i<jsonArray.size();i++){
                    JSONObject jsonObject = jsonArray.getJSONObject(i);//每一个json对象
                    String PDNUM = jsonObject.getString("PDNUM");//盘点单号
                    Integer UDPDLINEID = jsonObject.getInteger("UDPDLINEID");//表ID
                    String GDZCBH = jsonObject.getString("GDZCBH");//固定资产编号
                    String STATUS = jsonObject.getString("STATUS");//盘点状态
                    String ASSETNUM = jsonObject.getString("ASSETNUM");//设备名称
                    String PDPEO = jsonObject.getString("PDPEO");//盘点人工号
                    String PDDATEStr = jsonObject.getString("PDDATE");//盘点日期
                    Date PDDATE = null;
                    if(PDDATEStr!=null && !"".equalsIgnoreCase(PDDATEStr)){
                        PDDATE = sdf.parse(PDDATEStr);
                    }
                    udpdlineSet.setWhere(" UDPDLINEID="+UDPDLINEID+"");
                    udpdlineSet.reset();
                    MboRemote mbo = null;
                    if(udpdlineSet!=null && !udpdlineSet.isEmpty()){
                        //编辑
                        mbo = udpdlineSet.getMbo(0);
                    }else{
                        //新增--代表是盘盈
                        mbo = udpdlineSet.add();
                        UDPDLINEID = mbo.getInt("UDPDLINEID");
                        mbo.setValue("PDNUM",PDNUM,11L);
                        mbo.setValue("ORGID",ORGID,11L);
                        mbo.setValue("SITEID",SITEID,11L);
                        mbo.setValue("ASSETNUM",ASSETNUM);//设备名称
                        mbo.setValue("PYZC",1,11L);//盘盈资产标签页
                    }
                    mbo.setValue("STATUS",STATUS,11L);//盘点状态 默认盘盈
                    mbo.setValue("PDPEO",PDPEO,2L);//盘点人工号
                    mbo.setValue("PDDATE",PDDATE,11L);//盘点日期
                    //保存
                    mbo.getThisMboSet().save(11L);
                }
            }
            //主表保存
            MboSetRemote udpdSet = MXServer.getMXServer().getMboSet("UDPD",MXServer.getMXServer().getSystemUserInfo());
            udpdSet.setWhere(" UDPDID="+UDPDID+"");
            udpdSet.reset();
            if(null!=udpdSet && !udpdSet.isEmpty()){
                MboRemote mbo=udpdSet.getMbo(0);
                mbo.setValue("PDRESULT",PDRESULT,11L);//盘点结论
                mbo.setValue("REMARK",REMARK,11L);//盘点备注
                //保存
                mbo.getThisMboSet().save(11L);
                returnData.put("UDPDID",UDPDID);//返回前端刷新页面
                returnData.put("errcode","200");
                returnData.put("errmsg",new AMobileConfig().getTipByLang("save","success","errmsg",LANGCODE));
            }
        }catch (Exception e){
            System.out.println("------faileMessage----->"+e.getMessage());
            returnData.put("errcode","500");
//            returnData.put("errmsg","save failure,"+e.getMessage());
            returnData.put("errmsg",new AMobileConfig().getTipByLang("save","failure","errmsg",LANGCODE));
        }finally {
            return returnData;
        }
    }
    /**
     * 【盘盈】资产明细子表数据-保存
     * @param received
     * @return
     */
    @POST
    @Path("/savePylineData")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public JSONObject savePylineData(String received) {
        //定义返回对象
        JSONObject returnData = new JSONObject();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String LANGCODE = "";//语言环境
        try {
            //接收app数据
            JSONObject jsonObject = JSONObject.parseObject(received);
            Integer UDPDLINEID = jsonObject.getInteger("UDPDLINEID");//表ID
            LANGCODE = jsonObject.getString("LANGCODE");
            String PDNUM = jsonObject.getString("PDNUM");//盘点单号
            String ORGID = jsonObject.getString("ORGID");//ORGID
            String SITEID = jsonObject.getString("SITEID");//SITEID
            String ASSETNUM = jsonObject.getString("ASSETNUM");//设备名称
            String PDPEO = jsonObject.getString("PDPEO");//盘点人工号
            String PDDATE = jsonObject.getString("PDDATE");//盘点日期

            MboSetRemote udpdlineSet = MXServer.getMXServer().getMboSet("UDPDLINE",MXServer.getMXServer().getSystemUserInfo());
            udpdlineSet.setWhere(" UDPDLINEID="+UDPDLINEID+" AND ORGID='"+ORGID+"' AND SITEID='"+SITEID+"' ");
            udpdlineSet.reset();
            MboRemote mbo = null;
            if(udpdlineSet!=null && !udpdlineSet.isEmpty()){
                //编辑
                mbo = udpdlineSet.getMbo(0);
            }else{
                //新增
                mbo = udpdlineSet.add();
                UDPDLINEID = mbo.getInt("UDPDLINEID");
            }
            //PDNUM=:PDNUM and PYZC=1 关联值
            mbo.setValue("PDNUM",PDNUM,11L);
            mbo.setValue("ORGID",ORGID,11L);
            mbo.setValue("SITEID",SITEID,11L);
            mbo.setValue("PYZC",1,11L);

            mbo.setValue("ASSETNUM",ASSETNUM);//设备名称
            mbo.setValue("STATUS","PY",11L);//盘点状态 默认盘盈
            mbo.setValue("PDPEO",PDPEO,2L);//盘点人工号
            mbo.setValue("PDDATE",new Date());//盘点日期 默认保存时间

            //保存
            mbo.getThisMboSet().save(11L);
            returnData.put("UDPDLINEID",UDPDLINEID);//返回前端刷新页面
            returnData.put("errcode","200");
//            returnData.put("errmsg","save success");
            returnData.put("errmsg",new AMobileConfig().getTipByLang("save","success","errmsg",LANGCODE));
        }catch (Exception e){
            System.out.println("------faileMessage----->"+e.getMessage());
            returnData.put("errcode","500");
//            returnData.put("errmsg","save failure,"+e.getMessage());
            returnData.put("errmsg",new AMobileConfig().getTipByLang("save","failure","errmsg",LANGCODE));
        }finally {
            return returnData;
        }
    }
    /**
     * 【盘盈】资产明细子表数据-删除
     * @param received
     * @return
     */
    @POST
    @Path("/deletePylineData")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public JSONObject deletePylineData(String received) {
        //定义返回对象
        JSONObject returnData = new JSONObject();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String LANGCODE = "";//语言环境
        try {
            //接收app数据
            JSONObject jsonObject = JSONObject.parseObject(received);
            Integer UDPDLINEID = jsonObject.getInteger("UDPDLINEID");//表ID
            LANGCODE = jsonObject.getString("LANGCODE");
            String PDNUM = jsonObject.getString("PDNUM");//盘点单号
            String ORGID = jsonObject.getString("ORGID");//ORGID
            String SITEID = jsonObject.getString("SITEID");//SITEID

            MboSetRemote udpdlineSet = MXServer.getMXServer().getMboSet("UDPDLINE",MXServer.getMXServer().getSystemUserInfo());
            udpdlineSet.setWhere(" UDPDLINEID="+UDPDLINEID+" AND ORGID='"+ORGID+"' AND SITEID='"+SITEID+"' ");
            udpdlineSet.reset();
            if(null!=udpdlineSet && !udpdlineSet.isEmpty()){
                udpdlineSet.deleteAll(2L);
                //保存
                udpdlineSet.save(11L);
                returnData.put("UDPDLINEID",UDPDLINEID);//返回前端刷新页面
                returnData.put("errcode","200");
//                returnData.put("errmsg","save success");
                returnData.put("errmsg",new AMobileConfig().getTipByLang("delete","success","errmsg",LANGCODE));
            }

        }catch (Exception e){
            System.out.println("------faileMessage----->"+e.getMessage());
            returnData.put("errcode","500");
//            returnData.put("errmsg","save failure,"+e.getMessage());
            returnData.put("errmsg",new AMobileConfig().getTipByLang("delete","failure","errmsg",LANGCODE));
        }finally {
            return returnData;
        }
    }

    //---在线---------------------end------------------------------------------------------------------------

    /**
     * 盘点明细子表数据保存-删除------------app端不允许除了（盘盈状态）的删除------------用不上
     * @param received
     * @return
     */
    @POST
    @Path("/deleteLineData")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public JSONObject deleteLineData(String received) {
        //定义返回对象
        JSONObject returnData = new JSONObject();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String LANGCODE = "";//语言环境
        try {
            //接收app数据
            JSONObject jsonObject = JSONObject.parseObject(received);
            Integer UDPDLINEID = jsonObject.getInteger("UDPDLINEID");//表ID
            LANGCODE = jsonObject.getString("LANGCODE");
            String ORGID = jsonObject.getString("ORGID");//ORGID
            String SITEID = jsonObject.getString("SITEID");//SITEID

            MboSetRemote udpdlineSet = MXServer.getMXServer().getMboSet("UDPDLINE",MXServer.getMXServer().getSystemUserInfo());
            udpdlineSet.setWhere(" UDPDLINEID="+UDPDLINEID+" AND ORGID='"+ORGID+"' AND SITEID='"+SITEID+"' ");
            udpdlineSet.reset();
            if(null!=udpdlineSet && !udpdlineSet.isEmpty()){
                udpdlineSet.deleteAll(2L);
                //保存
                udpdlineSet.save(11L);
                returnData.put("UDPDLINEID",UDPDLINEID);//返回前端刷新页面
                returnData.put("errcode","200");
//                returnData.put("errmsg","save success");
                returnData.put("errmsg",new AMobileConfig().getTipByLang("delete","success","errmsg",LANGCODE));
            }
        }catch (Exception e){
            System.out.println("------faileMessage----->"+e.getMessage());
            returnData.put("errcode","500");
//            returnData.put("errmsg","save failure,"+e.getMessage());
            returnData.put("errmsg",new AMobileConfig().getTipByLang("delete","failure","errmsg",LANGCODE));
        }finally {
            return returnData;
        }
    }


    /**
     * 盘点任务主表保存 - 删除-----------------------app端不允许删除，用不上
     * @param received
     * @return
     */
    @POST
    @Path("/mainDelete")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public JSONObject mainDelete(String received) {
        //定义返回对象
        JSONObject returnData = new JSONObject();
        String LANGCODE = "";//语言环境
        try {
            //接收app数据
            JSONObject jsonObject = JSONObject.parseObject(received);
            LANGCODE = jsonObject.getString("LANGCODE");
            Integer UDPDID = jsonObject.getInteger("UDPDID");//表ID
            String ORGID = jsonObject.getString("ORGID");//组织
            String SITEID = jsonObject.getString("SITEID");//地点

            MboSetRemote udpdlineSet = MXServer.getMXServer().getMboSet("UDPDLINE",MXServer.getMXServer().getSystemUserInfo());
            udpdlineSet.setWhere(" PDNUM in(SELECT PDNUM FROM UDPD WHERE UDPDID="+UDPDID+" AND ORGID='"+ORGID+"' AND SITEID='"+SITEID+"' ) AND ORGID='"+ORGID+"' AND SITEID='"+SITEID+"' ");
            udpdlineSet.reset();
            MboSetRemote udpdSet = MXServer.getMXServer().getMboSet("UDPD",MXServer.getMXServer().getSystemUserInfo());
            udpdSet.setWhere(" UDPDID="+UDPDID+" AND ORGID='"+ORGID+"' AND SITEID='"+SITEID+"'");
            udpdSet.reset();

            if(null!=udpdlineSet && !udpdlineSet.isEmpty() && null!=udpdSet && !udpdSet.isEmpty()){
                udpdlineSet.deleteAll(2L);
                udpdlineSet.save(11L);
                udpdSet.deleteAll(2L);
                udpdSet.save();
                returnData.put("UDPDID",UDPDID);//返回前端刷新页面
                returnData.put("errcode","200");
//                returnData.put("errmsg","save success");
                returnData.put("errmsg",new AMobileConfig().getTipByLang("delete","success","errmsg",LANGCODE));
            }
        }catch (Exception e){
            System.out.println("------faileMessage----->"+e.getMessage());
            returnData.put("errcode","500");
//            returnData.put("errmsg","save failure,"+e.getMessage());
            returnData.put("errmsg",new AMobileConfig().getTipByLang("delete","failure","errmsg",LANGCODE));
        }finally {
            return returnData;
        }
    }

    /**
     * 盘点明细子表数据保存-----------------------app端暂时用不上单个明细的保存
     * @param received
     * @return
     */
    @POST
    @Path("/saveLineData")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public JSONObject saveLineData(String received) {
        //定义返回对象
        JSONObject returnData = new JSONObject();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String LANGCODE = "";//语言环境
        try {
            //接收app数据
            JSONObject jsonObject = JSONObject.parseObject(received);
            Integer UDPDLINEID = jsonObject.getInteger("UDPDLINEID");//表ID
            LANGCODE = jsonObject.getString("LANGCODE");
            String PDNUM = jsonObject.getString("PDNUM");//盘点单号
            String ORGID = jsonObject.getString("ORGID");//ORGID
            String SITEID = jsonObject.getString("SITEID");//SITEID
            String GDZCBH = jsonObject.getString("GDZCBH");//固定资产编号
            String STATUS = jsonObject.getString("STATUS");//盘点状态
            String PDDATE = jsonObject.getString("PDDATE");//盘点日期
            String PDPEO = jsonObject.getString("PDPEO");//盘点人工号

            MboSetRemote udpdlineSet = MXServer.getMXServer().getMboSet("UDPDLINE",MXServer.getMXServer().getSystemUserInfo());
            udpdlineSet.setWhere(" UDPDLINEID="+UDPDLINEID+" AND ORGID='"+ORGID+"' AND SITEID='"+SITEID+"' ");
            udpdlineSet.reset();
            MboRemote mbo = null;
            if(udpdlineSet!=null && !udpdlineSet.isEmpty()){
                //编辑
                mbo = udpdlineSet.getMbo(0);
            }else{
                //新增
                mbo = udpdlineSet.add();
                UDPDLINEID = mbo.getInt("UDPDLINEID");
            }
            //PDNUM=:PDNUM and PYZC=0 关联值
            mbo.setValue("PDNUM",PDNUM,11L);
            mbo.setValue("ORGID",ORGID,11L);
            mbo.setValue("SITEID",SITEID,11L);
            mbo.setValue("PYZC",0,11L);

            mbo.setValue("GDZCBH",GDZCBH);//固定资产编号
            mbo.setValue("STATUS",STATUS);//盘点状态
            mbo.setValue("PDPEO",PDPEO);//盘点人工号
            //保存
            mbo.getThisMboSet().save(11L);
            returnData.put("UDPDLINEID",UDPDLINEID);//返回前端刷新页面
            returnData.put("errcode","200");
//            returnData.put("errmsg","save success");
            returnData.put("errmsg",new AMobileConfig().getTipByLang("save","success","errmsg",LANGCODE));
        }catch (Exception e){
            System.out.println("------faileMessage----->"+e.getMessage());
            returnData.put("errcode","500");
//            returnData.put("errmsg","save failure,"+e.getMessage());
            returnData.put("errmsg",new AMobileConfig().getTipByLang("save","failure","errmsg",LANGCODE));
        }finally {
            return returnData;
        }
    }



}
