package com.engine.htsc.equipment.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.engine.core.impl.Service;
import com.engine.htsc.equipment.entity.EquipmentEntity;
import weaver.conn.RecordSet;
import weaver.formmode.setup.ModeRightInfo;
import weaver.general.BaseBean;
import weaver.general.TimeUtil;
import weaver.general.Util;
import weaver.interfaces.htsc.comInfo.PropBean;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

public class EquipmentInformationService extends Service {

    /**
     * 创建设备信息
     * @param equipmentEntity
     * @return
     */
    public Map<String, Object> createEquipmentInfo(EquipmentEntity equipmentEntity){
        BaseBean bs = new BaseBean();
        PropBean pb = new PropBean();
        RecordSet rs = new RecordSet();
        Map<String, Object> result = new HashMap<>();

        try{
            if(equipmentEntity!=null){
                String dzsbmodeid = pb.getPropName("UF_DZSBXXCX_CUBEUUIUD");
                JSONObject jsonObject_dzsb = pb.getModeInfoByUUID(dzsbmodeid);
                int modeid = Util.getIntValue(jsonObject_dzsb.getString("modeid"));

                String uuid = UUID.randomUUID().toString();
                equipmentEntity.setModeuuid(uuid);
                equipmentEntity.setFormmodeid(modeid);
                equipmentEntity.setModedatacreater(Integer.valueOf(equipmentEntity.getSqr()));
                equipmentEntity.setModedatacreatertype(0);
                equipmentEntity.setModedatacreatedate(TimeUtil.getCurrentDateString());
                equipmentEntity.setModedatacreatetime(TimeUtil.getOnlyCurrentTimeString());

                bs.writeLog("equipmentEntity:"+ JSON.toJSONString(equipmentEntity));

                Class<?> clazz = equipmentEntity.getClass();
                Field[] fields = clazz.getDeclaredFields();
                String insertFields = " insert into uf_dzsbxxcx(";
                String paramsStr  = " values(";
                Object[] values = new Object[fields.length];
                for(int i=0;i<fields.length;i++){
                    fields[i].setAccessible( true );
                    if(i==fields.length-1){
                        insertFields += fields[i].getName()+" )";
                        paramsStr += "? )";
                    }else{
                        insertFields += fields[i].getName()+",";
                        paramsStr += "?,";
                    }
                    try{
                        values[i] = fields[i].get(equipmentEntity);
                    }catch (Exception e){
                        new BaseBean().writeLog("保存电子设备信息数据错误："+e);
                    }
                }
                String insertSql = insertFields + paramsStr;
                bs.writeLog("insertSql:"+insertSql);
                boolean res = rs.executeQuery(insertSql,values);
                bs.writeLog("res:"+res);

                if(res){
                    int billid = 0;
                    rs.executeQuery("select id from uf_dzsbxxcx where modeuuid='"+uuid+"'" );
                    if (rs.next()) {
                        billid = Util.getIntValue( rs.getString( "id" ) );
                        bs.writeLog("billid:"+billid);
                        ModeRightInfo moderight = new ModeRightInfo();
                        moderight.editModeDataShare(equipmentEntity.getModedatacreater(),equipmentEntity.getFormmodeid(),billid);
                    }

                    result.put("billid",billid);
                    result.put("status","1");
                    result.put("message","创建成功");
                }else {
                    result.put("billid","0");
                    result.put("status","-1");
                    result.put("message","创建失败");
                }
            }
        }catch (Exception e) {
            e.printStackTrace();
            result.put("billid","0");
            result.put("status","-2");
            result.put("message","系统错误,请联系系统管理员");
        }
        return result;
    }
}
