package psdi.app.iface.mobile;

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("/udjlwh")
public class UdJLWHService {

    /**
     * 数据保存or提交接口
     * @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 UDMETERID = jsonObject.getInteger("UDMETERID");//主表ID
            String UDMETERNUM = jsonObject.getString("UDMETERNUM");//计量编号
            String DESCRIPTION = jsonObject.getString("DESCRIPTION");//计量描述
            String ASSETNUM = jsonObject.getString("ASSETNUM");//资产编号
            String METERTYPE = jsonObject.getString("METERTYPE");//计量类型
            String MEASUREUNITID = jsonObject.getString("MEASUREUNITID");//计量单位
            String ORGID = jsonObject.getString("ORGID");//组织
            String SITEID = jsonObject.getString("SITEID");//地点
            MboSetRemote UDMETERSet = MXServer.getMXServer().getMboSet("UDMETER",MXServer.getMXServer().getSystemUserInfo());
            UDMETERSet.setWhere("UDMETERID="+UDMETERID+"");
            UDMETERSet.reset();
            MboRemote mbo = null;
            if(UDMETERSet!=null && !UDMETERSet.isEmpty()){
                //编辑
                mbo = UDMETERSet.getMbo(0);
            }else{
                //新增
                UDMETERSet.setWhere("ASSETNUM='"+ASSETNUM+"' AND MEASUREUNITID='"+MEASUREUNITID+"'");
                UDMETERSet.reset();
                if(UDMETERSet!=null && !UDMETERSet.isEmpty()){
                    returnDate.put("errcode","201");
//                    returnDate.put("errmsg","save failure,AssetNum and Unit already exists");
                    returnDate.put("errmsg",new AMobileConfig().getTipByLang("save","failure","errmsg",LANGCODE)+"-"+new AMobileConfig().getTipByLang("udjlwh","saveData","errmsg",LANGCODE));
                    return returnDate;
                }
                mbo = UDMETERSet.add();
//                mbo.setValue("STATUS","EDIT",2L);
                UDMETERID = mbo.getInt("UDMETERID");
                UDMETERNUM = String.valueOf(UDMETERID);
            }
            mbo.setValue("UDMETERNUM",UDMETERNUM,2L);
            mbo.setValue("DESCRIPTION",DESCRIPTION,2L);
            mbo.setValue("ASSETNUM",ASSETNUM,2L);
            mbo.setValue("METERTYPE",METERTYPE,2L);
            mbo.setValue("MEASUREUNITID",MEASUREUNITID,2L);
            mbo.setValue("ORGID",ORGID,2L);
            mbo.setValue("SITEID",SITEID,2L);
            //保存
            mbo.getThisMboSet().save();
            returnDate.put("UDMETERID",UDMETERID);//返回前端刷新页面
            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 "+e.getMessage());
            returnDate.put("errmsg",new AMobileConfig().getTipByLang("save","failure","errmsg",LANGCODE));
        }finally {
            return returnDate;
        }
    }

    /**
     * 计量表保存
     * @param received
     * @return
     */
    @POST
    @Path("/saveMeterLineData")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public JSONObject saveMeterLineData(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 UDMETERLINEID = jsonObject.getInteger("UDMETERLINEID");//表ID
            String UDMETERNUM = jsonObject.getString("UDMETERNUM");//计量编号
            String METERNAME = jsonObject.getString("METERNAME");//计量表编号
            String ORGID = jsonObject.getString("ORGID");//组织
            String SITEID = jsonObject.getString("SITEID");//地点
            MboSetRemote UDMETERLINESet = MXServer.getMXServer().getMboSet("UDMETERLINE",MXServer.getMXServer().getSystemUserInfo());
            UDMETERLINESet.setWhere("UDMETERLINEID="+UDMETERLINEID+"");
            UDMETERLINESet.reset();
            MboRemote mbo = null;
            if(UDMETERLINESet!=null && !UDMETERLINESet.isEmpty()){
                //编辑
                mbo = UDMETERLINESet.getMbo(0);
            }else{
                //新增
                mbo = UDMETERLINESet.add();
                UDMETERLINEID = mbo.getInt("UDMETERLINEID");
            }
            mbo.setValue("UDMETERNUM",UDMETERNUM,2L);
            mbo.setValue("METERNAME",METERNAME,2L);
            mbo.setValue("ORGID",ORGID,2L);
            mbo.setValue("SITEID",SITEID,2L);
            //保存
            mbo.getThisMboSet().save();
            returnDate.put("UDMETERLINEID",UDMETERLINEID);//返回前端刷新页面
            returnDate.put("errcode","200");
//            returnDate.put("errmsg","save success");
            returnDate.put("errmsg",new AMobileConfig().getTipByLang("save","success","errmsg",LANGCODE));
        }catch (Exception e){
            System.out.println("------faileMessage----->"+e.getMessage());
            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("/saveMeterUPData")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public JSONObject saveMeterUPData(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 UDMETERHISID = jsonObject.getInteger("UDMETERHISID");//表ID
            String UDMETERNUM = jsonObject.getString("UDMETERNUM");//计量编号
            String METERNAME1 = jsonObject.getString("METERNAME1");//计量表编号
            Double METERDS1 = jsonObject.getDouble("METERDS1");//读数
            String GXDATE1Str = jsonObject.getString("GXDATE1");//更新日期
            Date GXDATE1 = null;
            if(GXDATE1Str!=null && !"".equals(GXDATE1Str)){
                GXDATE1 = sdf.parse(GXDATE1Str);
            }
            String GXPEOPLE1 = jsonObject.getString("GXPEOPLE1");//更新人
            String ORGID = jsonObject.getString("ORGID");//组织
            String SITEID = jsonObject.getString("SITEID");//地点
            MboSetRemote UDMETERHISSet = MXServer.getMXServer().getMboSet("UDMETERHIS",MXServer.getMXServer().getSystemUserInfo());
            UDMETERHISSet.setWhere("UDMETERHISID="+UDMETERHISID+"");
            UDMETERHISSet.reset();
            MboRemote mbo = null;
            if(UDMETERHISSet!=null && !UDMETERHISSet.isEmpty()){
                //编辑
                mbo = UDMETERHISSet.getMbo(0);
            }else{
                //新增
                mbo = UDMETERHISSet.add();
                UDMETERHISID = mbo.getInt("UDMETERHISID");
            }
            mbo.setValue("UDMETERNUM",UDMETERNUM,2L);
            mbo.setValue("METERNAME1",METERNAME1,2L);
            mbo.setValue("METERDS1",METERDS1,2L);
            mbo.setValue("GXDATE1",GXDATE1,2L);
            mbo.setValue("GXPEOPLE1",GXPEOPLE1,2L);
            mbo.setValue("HSTYPE","UP",2L); //应用区分
            mbo.setValue("ORGID",ORGID,2L);
            mbo.setValue("SITEID",SITEID,2L);
            //保存
            mbo.getThisMboSet().save();
            returnDate.put("UDMETERHISID",UDMETERHISID);//返回前端刷新页面
            returnDate.put("errcode","200");
//            returnDate.put("errmsg","save success");
            returnDate.put("errmsg",new AMobileConfig().getTipByLang("save","success","errmsg",LANGCODE));
        }catch (Exception e){
            System.out.println("------faileMessage----->"+e.getMessage());
            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("/saveMeterHXData")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public JSONObject saveMeterHXData(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 UDMETERHISID = jsonObject.getInteger("UDMETERHISID");//表ID
            String UDMETERNUM = jsonObject.getString("UDMETERNUM");//计量编号
            String METERNAME2 = jsonObject.getString("METERNAME2");//表表编号
            Double METERDS2 = jsonObject.getDouble("METERDS2");//旧表读数
            String METERNAME22 = jsonObject.getString("METERNAME22");//新表编号
            Double METERDS22 = jsonObject.getDouble("METERDS22");//新表读数
            String GXDATE2Str = jsonObject.getString("GXDATE2");//更新日期
            Date GXDATE2 = null;
            if(GXDATE2Str!=null && !"".equals(GXDATE2Str)){
                GXDATE2 = sdf.parse(GXDATE2Str);
            }
            String GXPEOPLE2 = jsonObject.getString("GXPEOPLE2");//换表人
            String ORGID = jsonObject.getString("ORGID");//组织
            String SITEID = jsonObject.getString("SITEID");//地点
            MboSetRemote UDMETERHISSet = MXServer.getMXServer().getMboSet("UDMETERHIS",MXServer.getMXServer().getSystemUserInfo());
            UDMETERHISSet.setWhere("UDMETERHISID="+UDMETERHISID+"");
            UDMETERHISSet.reset();
            MboRemote mbo = null;
            if(UDMETERHISSet!=null && !UDMETERHISSet.isEmpty()){
                //编辑
                mbo = UDMETERHISSet.getMbo(0);
            }else{
                //新增
                mbo = UDMETERHISSet.add();
                UDMETERHISID = mbo.getInt("UDMETERHISID");
            }
            mbo.setValue("UDMETERNUM",UDMETERNUM,2L);
            mbo.setValue("METERNAME2",METERNAME2,2L);
            mbo.setValue("METERDS2",METERDS2,2L);
            mbo.setValue("METERNAME22",METERNAME22,2L);
            mbo.setValue("METERDS22",METERDS22,2L);
            mbo.setValue("GXDATE2",GXDATE2,2L);
            mbo.setValue("GXPEOPLE2",GXPEOPLE2,2L);
            mbo.setValue("HSTYPE","HX",2L); //应用区分
            mbo.setValue("ORGID",ORGID,2L);
            mbo.setValue("SITEID",SITEID,2L);
            //保存
            mbo.getThisMboSet().save();
            returnDate.put("UDMETERHISID",UDMETERHISID);//返回前端刷新页面
            returnDate.put("errcode","200");
//            returnDate.put("errmsg","save success");
            returnDate.put("errmsg",new AMobileConfig().getTipByLang("save","success","errmsg",LANGCODE));
        }catch (Exception e){
            System.out.println("------faileMessage----->"+e.getMessage());
            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 UDMETERID = jsonObject.getInteger("UDMETERID");//主表id
            String UDMETERNUM = jsonObject.getString("UDMETERNUM");//计量编号
            String ORGID = jsonObject.getString("ORGID");//组织
            String SITEID = jsonObject.getString("SITEID");//地点
            MboSetRemote UDMETERSet = MXServer.getMXServer().getMboSet("UDMETER",MXServer.getMXServer().getSystemUserInfo());
            UDMETERSet.setWhere(" UDMETERID="+UDMETERID+"");
            UDMETERSet.reset();
            MboRemote mbo = UDMETERSet.getMbo(0);//
            mbo.delete();//相当于delete语句，结束后，还得保存
            mbo.getThisMboSet().save();
            //删除计量表-子表
            MboSetRemote UDMETERLINESet = MXServer.getMXServer().getMboSet("UDMETERLINE",MXServer.getMXServer().getSystemUserInfo());
            UDMETERLINESet.setWhere(" UDMETERNUM='"+UDMETERNUM+"' and orgid='"+ORGID+"' and siteid='"+SITEID+"'");
            UDMETERLINESet.reset();
            UDMETERLINESet.deleteAll();
            UDMETERLINESet.save();
            //删除计量记录/换表记录-子表
            MboSetRemote UDMETERHISSet = MXServer.getMXServer().getMboSet("UDMETERHIS",MXServer.getMXServer().getSystemUserInfo());
            UDMETERHISSet.setWhere(" UDMETERNUM='"+UDMETERNUM+"' and orgid='"+ORGID+"' and siteid='"+SITEID+"'");
            UDMETERHISSet.reset();
            UDMETERHISSet.deleteAll();
            UDMETERHISSet.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("/deleteMeterLineData")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public JSONObject deleteMeterLineData(String received) {
        //定义返回对象
        JSONObject returnDate = new JSONObject();
        String LANGCODE = "";//语言环境
        try {
            //接收app数据
            JSONObject jsonObject = JSONObject.parseObject(received);
            LANGCODE = jsonObject.getString("LANGCODE");
            Integer UDMETERLINEID = jsonObject.getInteger("UDMETERLINEID");//表id
            MboSetRemote UDMETERSet = MXServer.getMXServer().getMboSet("UDMETERLINE",MXServer.getMXServer().getSystemUserInfo());
            UDMETERSet.setWhere(" UDMETERLINEID="+UDMETERLINEID+"");
            UDMETERSet.reset();
            MboRemote mbo = UDMETERSet.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;
        }
    }


    /**
     * 计量数据/换表 数据删除 接口
     * @param received
     * @return
     */
    @POST
    @Path("/deleteMeterHisData")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public JSONObject deleteMeterHisData(String received) {
        //定义返回对象
        JSONObject returnDate = new JSONObject();
        String LANGCODE = "";//语言环境
        try {
            //接收app数据
            JSONObject jsonObject = JSONObject.parseObject(received);
            LANGCODE = jsonObject.getString("LANGCODE");
            Integer UDMETERHISID = jsonObject.getInteger("UDMETERHISID");//主表id
            MboSetRemote UDMETERSet = MXServer.getMXServer().getMboSet("UDMETERHIS",MXServer.getMXServer().getSystemUserInfo());
            UDMETERSet.setWhere(" UDMETERHISID="+UDMETERHISID+"");
            UDMETERSet.reset();
            MboRemote mbo = UDMETERSet.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;
        }
    }


    //------------删除----结束--------------------------------------------------------------

}
