diff --git a/softwareDirectory/AutoVerScheme/basecommonapi.cpp b/softwareDirectory/AutoVerScheme/basecommonapi.cpp index 4b1f088..7a130d4 100644 --- a/softwareDirectory/AutoVerScheme/basecommonapi.cpp +++ b/softwareDirectory/AutoVerScheme/basecommonapi.cpp @@ -305,7 +305,7 @@ - //直流稳压电源检定装置 + //核查直流稳压电源检定装置 QListBaseCommonApi::getCheckDCPower(QString equipment_id,QStringList *headList) { @@ -336,9 +336,9 @@ } else { qDebug() << "Query execution failed: " << query.lastError().text(); } - *headList << "序号"<<"核査类型"<<"检测类型"<<"参数" - <<"单位"<<"频率" << "核査点" << "分辨力" - <<"循环次数"<< "Urel"<<"通道"<< "TFER"; + *headList <<"序号"<<"核查项目"<<"核查点"<<"单位"<<"分辨力"<<"Urel"<<"核查类型"<<"是否为基准点(1/0)"<<"循环次数" + <<"电阻值/频率"<<"电阻值单位/频率单位"<<"核查项备注"; + return result; } //根据id获取装置名称 @@ -435,7 +435,7 @@ { QList result; QSqlQuery query; - query.prepare("SELECT item_category_id,belong_standard_equipment FROM biz_business_device_measure_item_info bbdmii WHERE bbdmii.device_model_id =(SELECT eei.model_id from eqpt_equipment_info eei WHERE eei.id = :id "); + query.prepare("SELECT id,belong_standard_equipment FROM biz_business_device_measure_item_info bbdmii WHERE bbdmii.device_model_id =(SELECT eei.model_id from eqpt_equipment_info eei WHERE eei.id = ':id')"); // 将参数绑定到占位符 query.bindValue(":id", id); if (query.exec()) { @@ -443,27 +443,32 @@ JudgeCalibrationTable calibration; calibration.item_category_id = query.value("item_category_id").toString(); calibration.belong_standard_equipment = query.value("belong_standard_equipment").toString(); + QString equipmentValue = query.value("belong_standard_equipment").toString(); + result.append(calibration); } } else { qDebug() << "Query execution failed: " << query.lastError().text(); } + return result; } //被检设备管理-检定项目及检定点-多功能校准源 -QList BaseCommonApi::getVerMultiCal(QString itemId) +QList BaseCommonApi::getVerMultiCal(QString itemId,QStringList *headList) { QList result; + QSqlQuery query; query.prepare("SELECT bbdmidc.id ,bbdmidc.params ,bbdmidc.capacity ,bbdmidc.unit ,bbdmidc.frequency , bbdmidc.range_range , bbdmidc.standard_value , bbdmidc.resolution , bbdmidc.maximum_error , bbdmidc.error_param_a , bbdmidc.error_param_b " " FROM biz_business_device_measure_item_data_calibrator bbdmidc WHERE item_id = :itemId "); // 将参数绑定到占位符 query.bindValue(":itemId", itemId); + if (query.exec()) { while (query.next()) { VerMultiCal calibration; @@ -484,7 +489,7 @@ } else { qDebug() << "Query execution failed: " << query.lastError().text(); } - + *headList<<"序号"<<"参数"<<"量"<<"单位"<<"频率"<<"量程"<<"标准值"<<"分辨力"<<"最大允许误差"<<"误差参数a"<<"误差参数b"; return result; } @@ -746,8 +751,6 @@ programInfo.create_name = query.value("create_name").toString(); programInfo.create_time = query.value("create_time").toDateTime(); programInfo.remark = query.value("remark").toString(); - programInfo.comm_id = query.value("comm_id").toString(); - result.append(programInfo); } } else { @@ -789,7 +792,7 @@ return result; } //查询设备通信接口 -QList getVerificationProgramConfigInterface(QString deviceId) +QList BaseCommonApi::getVerificationProgramConfigInterface(QString deviceId) { QList result; QSqlQuery query; @@ -814,3 +817,239 @@ } return result; } +//查询指令库 +QList BaseCommonApi::getInstructionLib(QString device_id) +{ + QList result; + QSqlQuery query; + query.prepare("SELECT id,device_id,device_name,instruct_config,instruct_read,type,belong_vername,belong_checkname,create_time,update_time FROM zd_instruction_library WHERE device_id = :device_id "); + query.bindValue(":device_id", device_id); + if (query.exec()) { + while (query.next()) { + InstructionLib instruction; + instruction.id = query.value("id").toString(); + instruction.device_id = query.value("device_id").toString(); + instruction.device_name = query.value("device_name").toString(); + instruction.instruct_config = query.value("instruct_config").toString(); + instruction.instruct_read = query.value("instruct_read").toString(); + instruction.type = query.value("type").toString(); + instruction.belong_vername = query.value("belong_vername").toString(); + instruction.belong_checkname = query.value("belong_checkname").toString(); + instruction.create_time = query.value("create_time").toDateTime(); + instruction.update_time = query.value("update_time").toDateTime(); + + result.append(instruction); + } + } else { + qDebug() << "Query execution failed: " << query.lastError().text(); + } + return result; +} + + + +//新建检定程序 +bool BaseCommonApi::InsertVerificationProgramInfo(VerificationProgramInfo verInfo) +{ + QSqlQuery query; + query.prepare("INSERT INTO zd_verification_program_info (id,program_name,create_name,create_time, remark) " + " VALUES (':id', ':program_name',':create_name', ':create_time', ':remark');"); + + query.bindValue(":id", verInfo.id); + query.bindValue(":program_name", verInfo.program_name); + query.bindValue(":create_name", verInfo.create_name); + query.bindValue(":create_time", verInfo.create_time); + query.bindValue(":remark", verInfo.remark); + + bool isSuccess = query.exec(); + if (!isSuccess) { + qDebug() << "InsertTaskBasic query failed: " << query.lastError().text(); + } + + return isSuccess; +} +//新增检定配置 +bool BaseCommonApi::InsertVerificationProgramConfig(VerificationProgramConfig programConfig) +{ + QSqlQuery query; + query.prepare("INSERT INTO zd_verification_program_details (id,verification_id,img_path,tested_device_id,standard_device_id) " + " VALUES (':id',':verification_id',':img_path',':tested_device_id',':standard_device_id')"); + + query.bindValue(":id", programConfig.id); + query.bindValue(":verification_id", programConfig.verification_id); + query.bindValue(":img_path", programConfig.img_path); + query.bindValue(":tested_device_id", programConfig.tested_device_id); + query.bindValue(":standard_device_id", programConfig.standard_device_id); + + bool isSuccess = query.exec(); + if (!isSuccess) { + qDebug() << "InsertTaskBasic query failed: " << query.lastError().text(); + } + + return isSuccess; +} +//新增通讯接口 +bool BaseCommonApi::InsertVerificationProgramConfigInterface(VerificationProgramConfigInterface configInterdace) +{ + QSqlQuery query; + query.prepare("INSERT INTO zd_communication_setting (id,device_id,communication_type,serial_port,visa,create_time,update_time) " + " VALUES (':id',':device_id',':communication_type',':serial_port',':visa',':create_time',':update_time')"); + + query.bindValue(":id", configInterdace.id); + query.bindValue(":device_id", configInterdace.device_id); + query.bindValue(":communication_type", configInterdace.communication_type); + query.bindValue(":serial_port", configInterdace.serial_port); + query.bindValue(":visa", configInterdace.visa); + query.bindValue(":create_time", configInterdace.create_time); + query.bindValue(":update_time", configInterdace.update_time); + bool isSuccess = query.exec(); + if (!isSuccess) { + qDebug() << "InsertTaskBasic query failed: " << query.lastError().text(); + } + + return isSuccess; +} +//更新检定程序 +bool BaseCommonApi::UpdateVerificationProgramInfo(VerificationProgramInfo verInfo) +{ + QSqlQuery query; + query.prepare("UPDATE zd_verification_program_info " + " SET program_name = :program_name,create_name = :create_name, " + " create_time = :create_time, remark = :remark WHERE id = :id;"); + query.bindValue(":program_name", verInfo.program_name); + query.bindValue(":create_name", verInfo.create_name); + query.bindValue(":create_time", verInfo.create_time); + query.bindValue(":remark", verInfo.remark); + query.bindValue(":id", verInfo.id); + bool isSuccess = query.exec(); + if (!isSuccess) { + qDebug() << "InsertOrderSampleRelation query failed: " << query.lastError().text(); + } + return isSuccess; +} +//更新检定配置 +bool BaseCommonApi::UpdateVerificationProgramConfig(VerificationProgramConfig programConfig) +{ +QSqlQuery query; + query.prepare("UPDATE zd_verification_program_details " + " SET img_path = :img_path,tested_device_id = :tested_device_id,standard_device_id = :standard_device_id " + " WHERE verification_id = :verification_id;"); + query.bindValue(":img_path", programConfig.img_path); + query.bindValue(":tested_device_id", programConfig.tested_device_id); + query.bindValue(":standard_device_id", programConfig.standard_device_id); + query.bindValue(":verification_id", programConfig.verification_id); + bool isSuccess = query.exec(); + if (!isSuccess) { + qDebug() << "InsertOrderSampleRelation query failed: " << query.lastError().text(); + } + return isSuccess; +} +//更新通讯接口 +bool BaseCommonApi::UpdateVerificationProgramConfigInterface(VerificationProgramConfigInterface configInterdace) +{ + QSqlQuery query; + query.prepare("UPDATE zd_communication_setting SET communication_type = :communication_type, " + " serial_port = :serial_port,visa = :visa,update_time = :update_time " + " WHERE device_id = :device_id;"); + query.bindValue(":communication_type", configInterdace.communication_type); + query.bindValue(":serial_port", configInterdace.serial_port); + query.bindValue(":visa", configInterdace.visa); + query.bindValue(":update_time", configInterdace.update_time); + query.bindValue(":device_id", configInterdace.device_id); + bool isSuccess = query.exec(); + if (!isSuccess) { + qDebug() << "InsertOrderSampleRelation query failed: " << query.lastError().text(); + } + return isSuccess; +} +//删除检定程序 +bool BaseCommonApi::DeleteVerificationProgramInfo(QString id) +{ + QSqlQuery query; + //删除检定程序 + query.prepare("DELETE FROM zd_verification_program_info WHERE id = :id"); + query.bindValue(":id", id); + + if (!query.exec()) { + qDebug() << "删除失败: " << query.lastError().text(); + return false; + } + + //删除检定程序详情 + query.prepare("DELETE FROM zd_verification_program_details WHERE verification_id = :verification_id"); + query.bindValue(":verification_id", id); + + if (!query.exec()) { + qDebug() << "删除失败 " << query.lastError().text(); + return false; + } + + return true; +} +//新增指令库 +bool BaseCommonApi::InsertInstructionLib(InstructionLib instructionLib) +{ + QSqlQuery query; + query.prepare("INSERT INTO zd_instruction_library (id,device_id,device_name,instruct_config,instruct_read,type,verification_id,check_id,create_time,update_time) " + " VALUES (':id',':device_id',':device_name',':instruct_config',':instruct_read',':type',':belong_vername',':belong_checkname',':create_time',':update_time')"); + + query.bindValue(":id", instructionLib.id); + query.bindValue(":device_id", instructionLib.device_id); + query.bindValue(":device_name", instructionLib.device_name); + query.bindValue(":instruct_config", instructionLib.instruct_config); + query.bindValue(":instruct_read", instructionLib.instruct_read); + query.bindValue(":type", instructionLib.type); + query.bindValue(":belong_vername", instructionLib.belong_vername); + query.bindValue(":belong_checkname", instructionLib.belong_checkname); + query.bindValue(":create_time", instructionLib.create_time); + query.bindValue(":update_time", instructionLib.update_time); + bool isSuccess = query.exec(); + if (!isSuccess) { + qDebug() << "InsertTaskBasic query failed: " << query.lastError().text(); + } + + return isSuccess; +} +//删除指令库 +bool BaseCommonApi::DeleteInstructionLib(QString device_id) +{ + QSqlQuery query; + //删除检定程序 + query.prepare("DELETE FROM zd_instruction_library WHERE device_id = :device_id "); + query.bindValue(":device_id", device_id); + + if (!query.exec()) { + qDebug() << "删除失败: " << query.lastError().text(); + return false; + } + return true; +} +//更新指令库 +bool BaseCommonApi::UpdateInstructionLib(InstructionLib instructionLib) +{ + QSqlQuery query; + query.prepare("UPDATE zd_instruction_library SET device_id=:device_id," + "device_name = :device_name,instruct_config = :instruct_config,instruct_read = :instruct_read," + " type = :type,belong_vername = :belong_vername,belong_checkname = :belong_checkname," + "update_time = :update_time WHERE id = :id"); + query.bindValue(":device_id", instructionLib.device_id); + query.bindValue(":device_name", instructionLib.device_name); + query.bindValue(":instruct_config", instructionLib.instruct_config); + query.bindValue(":instruct_read", instructionLib.instruct_read); + query.bindValue(":type", instructionLib.type); + query.bindValue(":belong_vername", instructionLib.belong_vername); + query.bindValue(":belong_checkname", instructionLib.belong_checkname); + query.bindValue(":update_time", instructionLib.update_time); + query.bindValue(":id", instructionLib.id); + bool isSuccess = query.exec(); + if (!isSuccess) { + qDebug() << "InsertOrderSampleRelation query failed: " << query.lastError().text(); + } + return isSuccess; +} +//雪花算法生成id +qint64 BaseCommonApi::generateId() +{ + return this->idWorker.nextId(); +} + diff --git a/softwareDirectory/AutoVerScheme/basecommonapi.cpp b/softwareDirectory/AutoVerScheme/basecommonapi.cpp index 4b1f088..7a130d4 100644 --- a/softwareDirectory/AutoVerScheme/basecommonapi.cpp +++ b/softwareDirectory/AutoVerScheme/basecommonapi.cpp @@ -305,7 +305,7 @@ - //直流稳压电源检定装置 + //核查直流稳压电源检定装置 QListBaseCommonApi::getCheckDCPower(QString equipment_id,QStringList *headList) { @@ -336,9 +336,9 @@ } else { qDebug() << "Query execution failed: " << query.lastError().text(); } - *headList << "序号"<<"核査类型"<<"检测类型"<<"参数" - <<"单位"<<"频率" << "核査点" << "分辨力" - <<"循环次数"<< "Urel"<<"通道"<< "TFER"; + *headList <<"序号"<<"核查项目"<<"核查点"<<"单位"<<"分辨力"<<"Urel"<<"核查类型"<<"是否为基准点(1/0)"<<"循环次数" + <<"电阻值/频率"<<"电阻值单位/频率单位"<<"核查项备注"; + return result; } //根据id获取装置名称 @@ -435,7 +435,7 @@ { QList result; QSqlQuery query; - query.prepare("SELECT item_category_id,belong_standard_equipment FROM biz_business_device_measure_item_info bbdmii WHERE bbdmii.device_model_id =(SELECT eei.model_id from eqpt_equipment_info eei WHERE eei.id = :id "); + query.prepare("SELECT id,belong_standard_equipment FROM biz_business_device_measure_item_info bbdmii WHERE bbdmii.device_model_id =(SELECT eei.model_id from eqpt_equipment_info eei WHERE eei.id = ':id')"); // 将参数绑定到占位符 query.bindValue(":id", id); if (query.exec()) { @@ -443,27 +443,32 @@ JudgeCalibrationTable calibration; calibration.item_category_id = query.value("item_category_id").toString(); calibration.belong_standard_equipment = query.value("belong_standard_equipment").toString(); + QString equipmentValue = query.value("belong_standard_equipment").toString(); + result.append(calibration); } } else { qDebug() << "Query execution failed: " << query.lastError().text(); } + return result; } //被检设备管理-检定项目及检定点-多功能校准源 -QList BaseCommonApi::getVerMultiCal(QString itemId) +QList BaseCommonApi::getVerMultiCal(QString itemId,QStringList *headList) { QList result; + QSqlQuery query; query.prepare("SELECT bbdmidc.id ,bbdmidc.params ,bbdmidc.capacity ,bbdmidc.unit ,bbdmidc.frequency , bbdmidc.range_range , bbdmidc.standard_value , bbdmidc.resolution , bbdmidc.maximum_error , bbdmidc.error_param_a , bbdmidc.error_param_b " " FROM biz_business_device_measure_item_data_calibrator bbdmidc WHERE item_id = :itemId "); // 将参数绑定到占位符 query.bindValue(":itemId", itemId); + if (query.exec()) { while (query.next()) { VerMultiCal calibration; @@ -484,7 +489,7 @@ } else { qDebug() << "Query execution failed: " << query.lastError().text(); } - + *headList<<"序号"<<"参数"<<"量"<<"单位"<<"频率"<<"量程"<<"标准值"<<"分辨力"<<"最大允许误差"<<"误差参数a"<<"误差参数b"; return result; } @@ -746,8 +751,6 @@ programInfo.create_name = query.value("create_name").toString(); programInfo.create_time = query.value("create_time").toDateTime(); programInfo.remark = query.value("remark").toString(); - programInfo.comm_id = query.value("comm_id").toString(); - result.append(programInfo); } } else { @@ -789,7 +792,7 @@ return result; } //查询设备通信接口 -QList getVerificationProgramConfigInterface(QString deviceId) +QList BaseCommonApi::getVerificationProgramConfigInterface(QString deviceId) { QList result; QSqlQuery query; @@ -814,3 +817,239 @@ } return result; } +//查询指令库 +QList BaseCommonApi::getInstructionLib(QString device_id) +{ + QList result; + QSqlQuery query; + query.prepare("SELECT id,device_id,device_name,instruct_config,instruct_read,type,belong_vername,belong_checkname,create_time,update_time FROM zd_instruction_library WHERE device_id = :device_id "); + query.bindValue(":device_id", device_id); + if (query.exec()) { + while (query.next()) { + InstructionLib instruction; + instruction.id = query.value("id").toString(); + instruction.device_id = query.value("device_id").toString(); + instruction.device_name = query.value("device_name").toString(); + instruction.instruct_config = query.value("instruct_config").toString(); + instruction.instruct_read = query.value("instruct_read").toString(); + instruction.type = query.value("type").toString(); + instruction.belong_vername = query.value("belong_vername").toString(); + instruction.belong_checkname = query.value("belong_checkname").toString(); + instruction.create_time = query.value("create_time").toDateTime(); + instruction.update_time = query.value("update_time").toDateTime(); + + result.append(instruction); + } + } else { + qDebug() << "Query execution failed: " << query.lastError().text(); + } + return result; +} + + + +//新建检定程序 +bool BaseCommonApi::InsertVerificationProgramInfo(VerificationProgramInfo verInfo) +{ + QSqlQuery query; + query.prepare("INSERT INTO zd_verification_program_info (id,program_name,create_name,create_time, remark) " + " VALUES (':id', ':program_name',':create_name', ':create_time', ':remark');"); + + query.bindValue(":id", verInfo.id); + query.bindValue(":program_name", verInfo.program_name); + query.bindValue(":create_name", verInfo.create_name); + query.bindValue(":create_time", verInfo.create_time); + query.bindValue(":remark", verInfo.remark); + + bool isSuccess = query.exec(); + if (!isSuccess) { + qDebug() << "InsertTaskBasic query failed: " << query.lastError().text(); + } + + return isSuccess; +} +//新增检定配置 +bool BaseCommonApi::InsertVerificationProgramConfig(VerificationProgramConfig programConfig) +{ + QSqlQuery query; + query.prepare("INSERT INTO zd_verification_program_details (id,verification_id,img_path,tested_device_id,standard_device_id) " + " VALUES (':id',':verification_id',':img_path',':tested_device_id',':standard_device_id')"); + + query.bindValue(":id", programConfig.id); + query.bindValue(":verification_id", programConfig.verification_id); + query.bindValue(":img_path", programConfig.img_path); + query.bindValue(":tested_device_id", programConfig.tested_device_id); + query.bindValue(":standard_device_id", programConfig.standard_device_id); + + bool isSuccess = query.exec(); + if (!isSuccess) { + qDebug() << "InsertTaskBasic query failed: " << query.lastError().text(); + } + + return isSuccess; +} +//新增通讯接口 +bool BaseCommonApi::InsertVerificationProgramConfigInterface(VerificationProgramConfigInterface configInterdace) +{ + QSqlQuery query; + query.prepare("INSERT INTO zd_communication_setting (id,device_id,communication_type,serial_port,visa,create_time,update_time) " + " VALUES (':id',':device_id',':communication_type',':serial_port',':visa',':create_time',':update_time')"); + + query.bindValue(":id", configInterdace.id); + query.bindValue(":device_id", configInterdace.device_id); + query.bindValue(":communication_type", configInterdace.communication_type); + query.bindValue(":serial_port", configInterdace.serial_port); + query.bindValue(":visa", configInterdace.visa); + query.bindValue(":create_time", configInterdace.create_time); + query.bindValue(":update_time", configInterdace.update_time); + bool isSuccess = query.exec(); + if (!isSuccess) { + qDebug() << "InsertTaskBasic query failed: " << query.lastError().text(); + } + + return isSuccess; +} +//更新检定程序 +bool BaseCommonApi::UpdateVerificationProgramInfo(VerificationProgramInfo verInfo) +{ + QSqlQuery query; + query.prepare("UPDATE zd_verification_program_info " + " SET program_name = :program_name,create_name = :create_name, " + " create_time = :create_time, remark = :remark WHERE id = :id;"); + query.bindValue(":program_name", verInfo.program_name); + query.bindValue(":create_name", verInfo.create_name); + query.bindValue(":create_time", verInfo.create_time); + query.bindValue(":remark", verInfo.remark); + query.bindValue(":id", verInfo.id); + bool isSuccess = query.exec(); + if (!isSuccess) { + qDebug() << "InsertOrderSampleRelation query failed: " << query.lastError().text(); + } + return isSuccess; +} +//更新检定配置 +bool BaseCommonApi::UpdateVerificationProgramConfig(VerificationProgramConfig programConfig) +{ +QSqlQuery query; + query.prepare("UPDATE zd_verification_program_details " + " SET img_path = :img_path,tested_device_id = :tested_device_id,standard_device_id = :standard_device_id " + " WHERE verification_id = :verification_id;"); + query.bindValue(":img_path", programConfig.img_path); + query.bindValue(":tested_device_id", programConfig.tested_device_id); + query.bindValue(":standard_device_id", programConfig.standard_device_id); + query.bindValue(":verification_id", programConfig.verification_id); + bool isSuccess = query.exec(); + if (!isSuccess) { + qDebug() << "InsertOrderSampleRelation query failed: " << query.lastError().text(); + } + return isSuccess; +} +//更新通讯接口 +bool BaseCommonApi::UpdateVerificationProgramConfigInterface(VerificationProgramConfigInterface configInterdace) +{ + QSqlQuery query; + query.prepare("UPDATE zd_communication_setting SET communication_type = :communication_type, " + " serial_port = :serial_port,visa = :visa,update_time = :update_time " + " WHERE device_id = :device_id;"); + query.bindValue(":communication_type", configInterdace.communication_type); + query.bindValue(":serial_port", configInterdace.serial_port); + query.bindValue(":visa", configInterdace.visa); + query.bindValue(":update_time", configInterdace.update_time); + query.bindValue(":device_id", configInterdace.device_id); + bool isSuccess = query.exec(); + if (!isSuccess) { + qDebug() << "InsertOrderSampleRelation query failed: " << query.lastError().text(); + } + return isSuccess; +} +//删除检定程序 +bool BaseCommonApi::DeleteVerificationProgramInfo(QString id) +{ + QSqlQuery query; + //删除检定程序 + query.prepare("DELETE FROM zd_verification_program_info WHERE id = :id"); + query.bindValue(":id", id); + + if (!query.exec()) { + qDebug() << "删除失败: " << query.lastError().text(); + return false; + } + + //删除检定程序详情 + query.prepare("DELETE FROM zd_verification_program_details WHERE verification_id = :verification_id"); + query.bindValue(":verification_id", id); + + if (!query.exec()) { + qDebug() << "删除失败 " << query.lastError().text(); + return false; + } + + return true; +} +//新增指令库 +bool BaseCommonApi::InsertInstructionLib(InstructionLib instructionLib) +{ + QSqlQuery query; + query.prepare("INSERT INTO zd_instruction_library (id,device_id,device_name,instruct_config,instruct_read,type,verification_id,check_id,create_time,update_time) " + " VALUES (':id',':device_id',':device_name',':instruct_config',':instruct_read',':type',':belong_vername',':belong_checkname',':create_time',':update_time')"); + + query.bindValue(":id", instructionLib.id); + query.bindValue(":device_id", instructionLib.device_id); + query.bindValue(":device_name", instructionLib.device_name); + query.bindValue(":instruct_config", instructionLib.instruct_config); + query.bindValue(":instruct_read", instructionLib.instruct_read); + query.bindValue(":type", instructionLib.type); + query.bindValue(":belong_vername", instructionLib.belong_vername); + query.bindValue(":belong_checkname", instructionLib.belong_checkname); + query.bindValue(":create_time", instructionLib.create_time); + query.bindValue(":update_time", instructionLib.update_time); + bool isSuccess = query.exec(); + if (!isSuccess) { + qDebug() << "InsertTaskBasic query failed: " << query.lastError().text(); + } + + return isSuccess; +} +//删除指令库 +bool BaseCommonApi::DeleteInstructionLib(QString device_id) +{ + QSqlQuery query; + //删除检定程序 + query.prepare("DELETE FROM zd_instruction_library WHERE device_id = :device_id "); + query.bindValue(":device_id", device_id); + + if (!query.exec()) { + qDebug() << "删除失败: " << query.lastError().text(); + return false; + } + return true; +} +//更新指令库 +bool BaseCommonApi::UpdateInstructionLib(InstructionLib instructionLib) +{ + QSqlQuery query; + query.prepare("UPDATE zd_instruction_library SET device_id=:device_id," + "device_name = :device_name,instruct_config = :instruct_config,instruct_read = :instruct_read," + " type = :type,belong_vername = :belong_vername,belong_checkname = :belong_checkname," + "update_time = :update_time WHERE id = :id"); + query.bindValue(":device_id", instructionLib.device_id); + query.bindValue(":device_name", instructionLib.device_name); + query.bindValue(":instruct_config", instructionLib.instruct_config); + query.bindValue(":instruct_read", instructionLib.instruct_read); + query.bindValue(":type", instructionLib.type); + query.bindValue(":belong_vername", instructionLib.belong_vername); + query.bindValue(":belong_checkname", instructionLib.belong_checkname); + query.bindValue(":update_time", instructionLib.update_time); + query.bindValue(":id", instructionLib.id); + bool isSuccess = query.exec(); + if (!isSuccess) { + qDebug() << "InsertOrderSampleRelation query failed: " << query.lastError().text(); + } + return isSuccess; +} +//雪花算法生成id +qint64 BaseCommonApi::generateId() +{ + return this->idWorker.nextId(); +} + diff --git a/softwareDirectory/AutoVerScheme/basecommonapi.h b/softwareDirectory/AutoVerScheme/basecommonapi.h index 7fcdfa5..d9b2511 100644 --- a/softwareDirectory/AutoVerScheme/basecommonapi.h +++ b/softwareDirectory/AutoVerScheme/basecommonapi.h @@ -1,14 +1,16 @@ #ifndef BASECOMMONAPI_H #define BASECOMMONAPI_H + #include #include #include #include #include "QList" #include "databaseconnection.h" +#include "id\IdWorker.h" -// + //部门任务 class Dept { @@ -379,7 +381,6 @@ QString create_name; QDateTime create_time; QString remark; - QString comm_id; }; @@ -398,7 +399,7 @@ QString manufacture_no; //出厂编号 QString manufacturer; //生产厂家 }; -//检定程序管理-检定配置设备接口查询 +//检定程序管理-检定配置设备通信接口查询 class VerificationProgramConfigInterface { public: @@ -410,13 +411,29 @@ QDateTime create_time; QDateTime update_time; }; +//指令库 +class InstructionLib +{ +public: + QString id; + QString device_id; + QString device_name; + QString instruct_config; + QString instruct_read; + QString type; + QString belong_vername; + QString belong_checkname; + QDateTime create_time; + QDateTime update_time; +}; - +using namespace Jiawa::Core; class BaseCommonApi { public: BaseCommonApi(); + //部门任务 static QList getAllData(); //我的任务 @@ -452,7 +469,7 @@ //判断 哪个 装置表 传参 设备id 返回itemId static QList getJudgeCalibrationTable(QString id); //被检设备管理-检定项目及检定点-多功能校准源 - static QList getVerMultiCal(QString itemId); + static QList getVerMultiCal(QString itemId,QStringList *headList); // 获取检定数据管理 首页 static QList getVerificationData(); // 检定数据管理-检定基本信息 @@ -473,7 +490,33 @@ static QList getVerificationProgramConfig(QString verification_id); //查询设备通信接口 static QList getVerificationProgramConfigInterface(QString deviceId); + //新建检定程序 + static bool InsertVerificationProgramInfo(VerificationProgramInfo verInfo); + //查询指令库 + static QList getInstructionLib(QString device_id); + //新增指令库 + static bool InsertInstructionLib(InstructionLib instructionLib); + //删除指令库 + static bool DeleteInstructionLib(QString device_id); + //更新指令库 + static bool UpdateInstructionLib(InstructionLib instructionLib); + //新增检定配置 + static bool InsertVerificationProgramConfig(VerificationProgramConfig programConfig); + //新增通讯接口 + static bool InsertVerificationProgramConfigInterface(VerificationProgramConfigInterface configInterdace); + //更新检定程序 + static bool UpdateVerificationProgramInfo(VerificationProgramInfo verInfo); + //更新检定配置 + static bool UpdateVerificationProgramConfig(VerificationProgramConfig programConfig); + //更新通讯接口 + static bool UpdateVerificationProgramConfigInterface(VerificationProgramConfigInterface configInterdace); + //删除检定程序 + static bool DeleteVerificationProgramInfo(QString id); + // 雪花id生成工具 + qint64 generateId(); + //雪花算法生成id + IdWorker &idWorker = Singleton::instance(); }; diff --git a/softwareDirectory/AutoVerScheme/basecommonapi.cpp b/softwareDirectory/AutoVerScheme/basecommonapi.cpp index 4b1f088..7a130d4 100644 --- a/softwareDirectory/AutoVerScheme/basecommonapi.cpp +++ b/softwareDirectory/AutoVerScheme/basecommonapi.cpp @@ -305,7 +305,7 @@ - //直流稳压电源检定装置 + //核查直流稳压电源检定装置 QListBaseCommonApi::getCheckDCPower(QString equipment_id,QStringList *headList) { @@ -336,9 +336,9 @@ } else { qDebug() << "Query execution failed: " << query.lastError().text(); } - *headList << "序号"<<"核査类型"<<"检测类型"<<"参数" - <<"单位"<<"频率" << "核査点" << "分辨力" - <<"循环次数"<< "Urel"<<"通道"<< "TFER"; + *headList <<"序号"<<"核查项目"<<"核查点"<<"单位"<<"分辨力"<<"Urel"<<"核查类型"<<"是否为基准点(1/0)"<<"循环次数" + <<"电阻值/频率"<<"电阻值单位/频率单位"<<"核查项备注"; + return result; } //根据id获取装置名称 @@ -435,7 +435,7 @@ { QList result; QSqlQuery query; - query.prepare("SELECT item_category_id,belong_standard_equipment FROM biz_business_device_measure_item_info bbdmii WHERE bbdmii.device_model_id =(SELECT eei.model_id from eqpt_equipment_info eei WHERE eei.id = :id "); + query.prepare("SELECT id,belong_standard_equipment FROM biz_business_device_measure_item_info bbdmii WHERE bbdmii.device_model_id =(SELECT eei.model_id from eqpt_equipment_info eei WHERE eei.id = ':id')"); // 将参数绑定到占位符 query.bindValue(":id", id); if (query.exec()) { @@ -443,27 +443,32 @@ JudgeCalibrationTable calibration; calibration.item_category_id = query.value("item_category_id").toString(); calibration.belong_standard_equipment = query.value("belong_standard_equipment").toString(); + QString equipmentValue = query.value("belong_standard_equipment").toString(); + result.append(calibration); } } else { qDebug() << "Query execution failed: " << query.lastError().text(); } + return result; } //被检设备管理-检定项目及检定点-多功能校准源 -QList BaseCommonApi::getVerMultiCal(QString itemId) +QList BaseCommonApi::getVerMultiCal(QString itemId,QStringList *headList) { QList result; + QSqlQuery query; query.prepare("SELECT bbdmidc.id ,bbdmidc.params ,bbdmidc.capacity ,bbdmidc.unit ,bbdmidc.frequency , bbdmidc.range_range , bbdmidc.standard_value , bbdmidc.resolution , bbdmidc.maximum_error , bbdmidc.error_param_a , bbdmidc.error_param_b " " FROM biz_business_device_measure_item_data_calibrator bbdmidc WHERE item_id = :itemId "); // 将参数绑定到占位符 query.bindValue(":itemId", itemId); + if (query.exec()) { while (query.next()) { VerMultiCal calibration; @@ -484,7 +489,7 @@ } else { qDebug() << "Query execution failed: " << query.lastError().text(); } - + *headList<<"序号"<<"参数"<<"量"<<"单位"<<"频率"<<"量程"<<"标准值"<<"分辨力"<<"最大允许误差"<<"误差参数a"<<"误差参数b"; return result; } @@ -746,8 +751,6 @@ programInfo.create_name = query.value("create_name").toString(); programInfo.create_time = query.value("create_time").toDateTime(); programInfo.remark = query.value("remark").toString(); - programInfo.comm_id = query.value("comm_id").toString(); - result.append(programInfo); } } else { @@ -789,7 +792,7 @@ return result; } //查询设备通信接口 -QList getVerificationProgramConfigInterface(QString deviceId) +QList BaseCommonApi::getVerificationProgramConfigInterface(QString deviceId) { QList result; QSqlQuery query; @@ -814,3 +817,239 @@ } return result; } +//查询指令库 +QList BaseCommonApi::getInstructionLib(QString device_id) +{ + QList result; + QSqlQuery query; + query.prepare("SELECT id,device_id,device_name,instruct_config,instruct_read,type,belong_vername,belong_checkname,create_time,update_time FROM zd_instruction_library WHERE device_id = :device_id "); + query.bindValue(":device_id", device_id); + if (query.exec()) { + while (query.next()) { + InstructionLib instruction; + instruction.id = query.value("id").toString(); + instruction.device_id = query.value("device_id").toString(); + instruction.device_name = query.value("device_name").toString(); + instruction.instruct_config = query.value("instruct_config").toString(); + instruction.instruct_read = query.value("instruct_read").toString(); + instruction.type = query.value("type").toString(); + instruction.belong_vername = query.value("belong_vername").toString(); + instruction.belong_checkname = query.value("belong_checkname").toString(); + instruction.create_time = query.value("create_time").toDateTime(); + instruction.update_time = query.value("update_time").toDateTime(); + + result.append(instruction); + } + } else { + qDebug() << "Query execution failed: " << query.lastError().text(); + } + return result; +} + + + +//新建检定程序 +bool BaseCommonApi::InsertVerificationProgramInfo(VerificationProgramInfo verInfo) +{ + QSqlQuery query; + query.prepare("INSERT INTO zd_verification_program_info (id,program_name,create_name,create_time, remark) " + " VALUES (':id', ':program_name',':create_name', ':create_time', ':remark');"); + + query.bindValue(":id", verInfo.id); + query.bindValue(":program_name", verInfo.program_name); + query.bindValue(":create_name", verInfo.create_name); + query.bindValue(":create_time", verInfo.create_time); + query.bindValue(":remark", verInfo.remark); + + bool isSuccess = query.exec(); + if (!isSuccess) { + qDebug() << "InsertTaskBasic query failed: " << query.lastError().text(); + } + + return isSuccess; +} +//新增检定配置 +bool BaseCommonApi::InsertVerificationProgramConfig(VerificationProgramConfig programConfig) +{ + QSqlQuery query; + query.prepare("INSERT INTO zd_verification_program_details (id,verification_id,img_path,tested_device_id,standard_device_id) " + " VALUES (':id',':verification_id',':img_path',':tested_device_id',':standard_device_id')"); + + query.bindValue(":id", programConfig.id); + query.bindValue(":verification_id", programConfig.verification_id); + query.bindValue(":img_path", programConfig.img_path); + query.bindValue(":tested_device_id", programConfig.tested_device_id); + query.bindValue(":standard_device_id", programConfig.standard_device_id); + + bool isSuccess = query.exec(); + if (!isSuccess) { + qDebug() << "InsertTaskBasic query failed: " << query.lastError().text(); + } + + return isSuccess; +} +//新增通讯接口 +bool BaseCommonApi::InsertVerificationProgramConfigInterface(VerificationProgramConfigInterface configInterdace) +{ + QSqlQuery query; + query.prepare("INSERT INTO zd_communication_setting (id,device_id,communication_type,serial_port,visa,create_time,update_time) " + " VALUES (':id',':device_id',':communication_type',':serial_port',':visa',':create_time',':update_time')"); + + query.bindValue(":id", configInterdace.id); + query.bindValue(":device_id", configInterdace.device_id); + query.bindValue(":communication_type", configInterdace.communication_type); + query.bindValue(":serial_port", configInterdace.serial_port); + query.bindValue(":visa", configInterdace.visa); + query.bindValue(":create_time", configInterdace.create_time); + query.bindValue(":update_time", configInterdace.update_time); + bool isSuccess = query.exec(); + if (!isSuccess) { + qDebug() << "InsertTaskBasic query failed: " << query.lastError().text(); + } + + return isSuccess; +} +//更新检定程序 +bool BaseCommonApi::UpdateVerificationProgramInfo(VerificationProgramInfo verInfo) +{ + QSqlQuery query; + query.prepare("UPDATE zd_verification_program_info " + " SET program_name = :program_name,create_name = :create_name, " + " create_time = :create_time, remark = :remark WHERE id = :id;"); + query.bindValue(":program_name", verInfo.program_name); + query.bindValue(":create_name", verInfo.create_name); + query.bindValue(":create_time", verInfo.create_time); + query.bindValue(":remark", verInfo.remark); + query.bindValue(":id", verInfo.id); + bool isSuccess = query.exec(); + if (!isSuccess) { + qDebug() << "InsertOrderSampleRelation query failed: " << query.lastError().text(); + } + return isSuccess; +} +//更新检定配置 +bool BaseCommonApi::UpdateVerificationProgramConfig(VerificationProgramConfig programConfig) +{ +QSqlQuery query; + query.prepare("UPDATE zd_verification_program_details " + " SET img_path = :img_path,tested_device_id = :tested_device_id,standard_device_id = :standard_device_id " + " WHERE verification_id = :verification_id;"); + query.bindValue(":img_path", programConfig.img_path); + query.bindValue(":tested_device_id", programConfig.tested_device_id); + query.bindValue(":standard_device_id", programConfig.standard_device_id); + query.bindValue(":verification_id", programConfig.verification_id); + bool isSuccess = query.exec(); + if (!isSuccess) { + qDebug() << "InsertOrderSampleRelation query failed: " << query.lastError().text(); + } + return isSuccess; +} +//更新通讯接口 +bool BaseCommonApi::UpdateVerificationProgramConfigInterface(VerificationProgramConfigInterface configInterdace) +{ + QSqlQuery query; + query.prepare("UPDATE zd_communication_setting SET communication_type = :communication_type, " + " serial_port = :serial_port,visa = :visa,update_time = :update_time " + " WHERE device_id = :device_id;"); + query.bindValue(":communication_type", configInterdace.communication_type); + query.bindValue(":serial_port", configInterdace.serial_port); + query.bindValue(":visa", configInterdace.visa); + query.bindValue(":update_time", configInterdace.update_time); + query.bindValue(":device_id", configInterdace.device_id); + bool isSuccess = query.exec(); + if (!isSuccess) { + qDebug() << "InsertOrderSampleRelation query failed: " << query.lastError().text(); + } + return isSuccess; +} +//删除检定程序 +bool BaseCommonApi::DeleteVerificationProgramInfo(QString id) +{ + QSqlQuery query; + //删除检定程序 + query.prepare("DELETE FROM zd_verification_program_info WHERE id = :id"); + query.bindValue(":id", id); + + if (!query.exec()) { + qDebug() << "删除失败: " << query.lastError().text(); + return false; + } + + //删除检定程序详情 + query.prepare("DELETE FROM zd_verification_program_details WHERE verification_id = :verification_id"); + query.bindValue(":verification_id", id); + + if (!query.exec()) { + qDebug() << "删除失败 " << query.lastError().text(); + return false; + } + + return true; +} +//新增指令库 +bool BaseCommonApi::InsertInstructionLib(InstructionLib instructionLib) +{ + QSqlQuery query; + query.prepare("INSERT INTO zd_instruction_library (id,device_id,device_name,instruct_config,instruct_read,type,verification_id,check_id,create_time,update_time) " + " VALUES (':id',':device_id',':device_name',':instruct_config',':instruct_read',':type',':belong_vername',':belong_checkname',':create_time',':update_time')"); + + query.bindValue(":id", instructionLib.id); + query.bindValue(":device_id", instructionLib.device_id); + query.bindValue(":device_name", instructionLib.device_name); + query.bindValue(":instruct_config", instructionLib.instruct_config); + query.bindValue(":instruct_read", instructionLib.instruct_read); + query.bindValue(":type", instructionLib.type); + query.bindValue(":belong_vername", instructionLib.belong_vername); + query.bindValue(":belong_checkname", instructionLib.belong_checkname); + query.bindValue(":create_time", instructionLib.create_time); + query.bindValue(":update_time", instructionLib.update_time); + bool isSuccess = query.exec(); + if (!isSuccess) { + qDebug() << "InsertTaskBasic query failed: " << query.lastError().text(); + } + + return isSuccess; +} +//删除指令库 +bool BaseCommonApi::DeleteInstructionLib(QString device_id) +{ + QSqlQuery query; + //删除检定程序 + query.prepare("DELETE FROM zd_instruction_library WHERE device_id = :device_id "); + query.bindValue(":device_id", device_id); + + if (!query.exec()) { + qDebug() << "删除失败: " << query.lastError().text(); + return false; + } + return true; +} +//更新指令库 +bool BaseCommonApi::UpdateInstructionLib(InstructionLib instructionLib) +{ + QSqlQuery query; + query.prepare("UPDATE zd_instruction_library SET device_id=:device_id," + "device_name = :device_name,instruct_config = :instruct_config,instruct_read = :instruct_read," + " type = :type,belong_vername = :belong_vername,belong_checkname = :belong_checkname," + "update_time = :update_time WHERE id = :id"); + query.bindValue(":device_id", instructionLib.device_id); + query.bindValue(":device_name", instructionLib.device_name); + query.bindValue(":instruct_config", instructionLib.instruct_config); + query.bindValue(":instruct_read", instructionLib.instruct_read); + query.bindValue(":type", instructionLib.type); + query.bindValue(":belong_vername", instructionLib.belong_vername); + query.bindValue(":belong_checkname", instructionLib.belong_checkname); + query.bindValue(":update_time", instructionLib.update_time); + query.bindValue(":id", instructionLib.id); + bool isSuccess = query.exec(); + if (!isSuccess) { + qDebug() << "InsertOrderSampleRelation query failed: " << query.lastError().text(); + } + return isSuccess; +} +//雪花算法生成id +qint64 BaseCommonApi::generateId() +{ + return this->idWorker.nextId(); +} + diff --git a/softwareDirectory/AutoVerScheme/basecommonapi.h b/softwareDirectory/AutoVerScheme/basecommonapi.h index 7fcdfa5..d9b2511 100644 --- a/softwareDirectory/AutoVerScheme/basecommonapi.h +++ b/softwareDirectory/AutoVerScheme/basecommonapi.h @@ -1,14 +1,16 @@ #ifndef BASECOMMONAPI_H #define BASECOMMONAPI_H + #include #include #include #include #include "QList" #include "databaseconnection.h" +#include "id\IdWorker.h" -// + //部门任务 class Dept { @@ -379,7 +381,6 @@ QString create_name; QDateTime create_time; QString remark; - QString comm_id; }; @@ -398,7 +399,7 @@ QString manufacture_no; //出厂编号 QString manufacturer; //生产厂家 }; -//检定程序管理-检定配置设备接口查询 +//检定程序管理-检定配置设备通信接口查询 class VerificationProgramConfigInterface { public: @@ -410,13 +411,29 @@ QDateTime create_time; QDateTime update_time; }; +//指令库 +class InstructionLib +{ +public: + QString id; + QString device_id; + QString device_name; + QString instruct_config; + QString instruct_read; + QString type; + QString belong_vername; + QString belong_checkname; + QDateTime create_time; + QDateTime update_time; +}; - +using namespace Jiawa::Core; class BaseCommonApi { public: BaseCommonApi(); + //部门任务 static QList getAllData(); //我的任务 @@ -452,7 +469,7 @@ //判断 哪个 装置表 传参 设备id 返回itemId static QList getJudgeCalibrationTable(QString id); //被检设备管理-检定项目及检定点-多功能校准源 - static QList getVerMultiCal(QString itemId); + static QList getVerMultiCal(QString itemId,QStringList *headList); // 获取检定数据管理 首页 static QList getVerificationData(); // 检定数据管理-检定基本信息 @@ -473,7 +490,33 @@ static QList getVerificationProgramConfig(QString verification_id); //查询设备通信接口 static QList getVerificationProgramConfigInterface(QString deviceId); + //新建检定程序 + static bool InsertVerificationProgramInfo(VerificationProgramInfo verInfo); + //查询指令库 + static QList getInstructionLib(QString device_id); + //新增指令库 + static bool InsertInstructionLib(InstructionLib instructionLib); + //删除指令库 + static bool DeleteInstructionLib(QString device_id); + //更新指令库 + static bool UpdateInstructionLib(InstructionLib instructionLib); + //新增检定配置 + static bool InsertVerificationProgramConfig(VerificationProgramConfig programConfig); + //新增通讯接口 + static bool InsertVerificationProgramConfigInterface(VerificationProgramConfigInterface configInterdace); + //更新检定程序 + static bool UpdateVerificationProgramInfo(VerificationProgramInfo verInfo); + //更新检定配置 + static bool UpdateVerificationProgramConfig(VerificationProgramConfig programConfig); + //更新通讯接口 + static bool UpdateVerificationProgramConfigInterface(VerificationProgramConfigInterface configInterdace); + //删除检定程序 + static bool DeleteVerificationProgramInfo(QString id); + // 雪花id生成工具 + qint64 generateId(); + //雪花算法生成id + IdWorker &idWorker = Singleton::instance(); }; diff --git a/softwareDirectory/AutoVerScheme/id/IdWorker.h b/softwareDirectory/AutoVerScheme/id/IdWorker.h new file mode 100644 index 0000000..079bbcb --- /dev/null +++ b/softwareDirectory/AutoVerScheme/id/IdWorker.h @@ -0,0 +1,218 @@ +#ifndef _JW_CORE_ID_WORKER_H_ +#define _JW_CORE_ID_WORKER_H_ + +#include +#include +#include +#include +#include +#include "Noncopyable.h" +#include "Singleton.h" + +// 如果不使用 mutex, 则开启下面这个定义, 但是我发现, 还是开启 mutex 功能, 速度比较快 +// #define SNOWFLAKE_ID_WORKER_NO_LOCK + +namespace Jiawa { + + /** + * @brief 核心 + * 核心功能 + */ + namespace Core { + + /** + * @brief 分布式id生成类 + * https://segmentfault.com/a/1190000011282426 + * https://github.com/twitter/snowflake/blob/snowflake-2010/src/main/scala/com/twitter/service/snowflake/IdWorker.scala + * + * 64bit id: 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 + * || || || | | | + * |└---------------------------时间戳--------------------------┘└中心-┘└机器-┘ └----序列号----┘ + * | + * 不用 + * SnowFlake的优点: 整体上按照时间自增排序, 并且整个分布式系统内不会产生ID碰撞(由数据中心ID和机器ID作区分), 并且效率较高, 经测试, SnowFlake每秒能够产生26万ID左右. + */ + class SnowflakeIdWorker : private Noncopyable { + + // 实现单例 + friend class Singleton; + + public: + typedef unsigned int UInt; + typedef unsigned long long int UInt64; + +#ifdef SNOWFLAKE_ID_WORKER_NO_LOCK + typedef std::atomic AtomicUInt; + typedef std::atomic AtomicUInt64; +#else + typedef UInt AtomicUInt; + typedef UInt64 AtomicUInt64; +#endif + + void setWorkerId(UInt workerId) { + this->workerId = workerId; + } + + void setDatacenterId(UInt datacenterId) { + this->datacenterId = datacenterId; + } + + UInt64 getId() { + return nextId(); + } + + /** + * 获得下一个ID (该方法是线程安全的) + * + * @return SnowflakeId + */ + UInt64 nextId() { +#ifndef SNOWFLAKE_ID_WORKER_NO_LOCK + std::unique_lock lock{ mutex }; + AtomicUInt64 timestamp{ 0 }; +#else + static AtomicUInt64 timestamp{ 0 }; +#endif + timestamp = timeGen(); + + // 如果当前时间小于上一次ID生成的时间戳, 说明系统时钟回退过这个时候应当抛出异常 + if (timestamp < lastTimestamp) { + std::ostringstream s; + s << "clock moved backwards. Refusing to generate id for " << lastTimestamp - timestamp << " milliseconds"; + throw std::exception(std::runtime_error(s.str())); + } + + if (lastTimestamp == timestamp) { + // 如果是同一时间生成的, 则进行毫秒内序列 + sequence = (sequence + 1) & sequenceMask; + if (0 == sequence) { + // 毫秒内序列溢出, 阻塞到下一个毫秒,获得新的时间戳 + timestamp = tilNextMillis(lastTimestamp); + } + } else { + sequence = 0; + } + +#ifndef SNOWFLAKE_ID_WORKER_NO_LOCK + lastTimestamp = timestamp; +#else + lastTimestamp = timestamp.load(); +#endif + + // 移位并通过或运算拼到一起组成64位的ID + return ((timestamp - twepoch) << timestampLeftShift) + | (datacenterId << datacenterIdShift) + | (workerId << workerIdShift) + | sequence; + } + + protected: + SnowflakeIdWorker() : workerId(0), datacenterId(0), sequence(0), lastTimestamp(0) { } + + /** + * 返回以毫秒为单位的当前时间 + * + * @return 当前时间(毫秒) + */ + UInt64 timeGen() const { + auto t = std::chrono::time_point_cast(std::chrono::high_resolution_clock::now()); + return t.time_since_epoch().count(); + } + + /** + * 阻塞到下一个毫秒, 直到获得新的时间戳 + * + * @param lastTimestamp 上次生成ID的时间截 + * @return 当前时间戳 + */ + UInt64 tilNextMillis(UInt64 lastTimestamp) const { + UInt64 timestamp = timeGen(); + while (timestamp <= lastTimestamp) { + timestamp = timeGen(); + } + return timestamp; + } + + private: + +#ifndef SNOWFLAKE_ID_WORKER_NO_LOCK + std::mutex mutex; +#endif + + /** + * 开始时间截 (2018-01-01 00:00:00.000) + */ + const UInt64 twepoch = 1514736000000; + + /** + * 机器id所占的位数 + */ + const UInt workerIdBits = 5; + + /** + * 数据中心id所占的位数 + */ + const UInt datacenterIdBits = 5; + + /** + * 序列所占的位数 + */ + const UInt sequenceBits = 12; + + /** + * 机器ID向左移12位 + */ + const UInt workerIdShift = sequenceBits; + + /** + * 数据标识id向左移17位 + */ + const UInt datacenterIdShift = workerIdShift + workerIdBits; + + /** + * 时间截向左移22位 + */ + const UInt timestampLeftShift = datacenterIdShift + datacenterIdBits; + + /** + * 支持的最大机器id, 结果是31 + */ + const UInt maxWorkerId = -1 ^ (-1 << workerIdBits); + + /** + * 支持的最大数据中心id, 结果是31 + */ + const UInt maxDatacenterId = -1 ^ (-1 << datacenterIdBits); + + /** + * 生成序列的掩码, 这里为4095 + */ + const UInt sequenceMask = -1 ^ (-1 << sequenceBits); + + /** + * 工作机器id(0~31) + */ + UInt workerId; + + /** + * 数据中心id(0~31) + */ + UInt datacenterId; + + /** + * 毫秒内序列(0~4095) + */ + AtomicUInt sequence{ 0 }; + + /** + * 上次生成ID的时间截 + */ + AtomicUInt64 lastTimestamp{ 0 }; + + }; + + typedef SnowflakeIdWorker IdWorker; + } +} + +#endif // _JW_CORE_ID_WORKER_H_ diff --git a/softwareDirectory/AutoVerScheme/basecommonapi.cpp b/softwareDirectory/AutoVerScheme/basecommonapi.cpp index 4b1f088..7a130d4 100644 --- a/softwareDirectory/AutoVerScheme/basecommonapi.cpp +++ b/softwareDirectory/AutoVerScheme/basecommonapi.cpp @@ -305,7 +305,7 @@ - //直流稳压电源检定装置 + //核查直流稳压电源检定装置 QListBaseCommonApi::getCheckDCPower(QString equipment_id,QStringList *headList) { @@ -336,9 +336,9 @@ } else { qDebug() << "Query execution failed: " << query.lastError().text(); } - *headList << "序号"<<"核査类型"<<"检测类型"<<"参数" - <<"单位"<<"频率" << "核査点" << "分辨力" - <<"循环次数"<< "Urel"<<"通道"<< "TFER"; + *headList <<"序号"<<"核查项目"<<"核查点"<<"单位"<<"分辨力"<<"Urel"<<"核查类型"<<"是否为基准点(1/0)"<<"循环次数" + <<"电阻值/频率"<<"电阻值单位/频率单位"<<"核查项备注"; + return result; } //根据id获取装置名称 @@ -435,7 +435,7 @@ { QList result; QSqlQuery query; - query.prepare("SELECT item_category_id,belong_standard_equipment FROM biz_business_device_measure_item_info bbdmii WHERE bbdmii.device_model_id =(SELECT eei.model_id from eqpt_equipment_info eei WHERE eei.id = :id "); + query.prepare("SELECT id,belong_standard_equipment FROM biz_business_device_measure_item_info bbdmii WHERE bbdmii.device_model_id =(SELECT eei.model_id from eqpt_equipment_info eei WHERE eei.id = ':id')"); // 将参数绑定到占位符 query.bindValue(":id", id); if (query.exec()) { @@ -443,27 +443,32 @@ JudgeCalibrationTable calibration; calibration.item_category_id = query.value("item_category_id").toString(); calibration.belong_standard_equipment = query.value("belong_standard_equipment").toString(); + QString equipmentValue = query.value("belong_standard_equipment").toString(); + result.append(calibration); } } else { qDebug() << "Query execution failed: " << query.lastError().text(); } + return result; } //被检设备管理-检定项目及检定点-多功能校准源 -QList BaseCommonApi::getVerMultiCal(QString itemId) +QList BaseCommonApi::getVerMultiCal(QString itemId,QStringList *headList) { QList result; + QSqlQuery query; query.prepare("SELECT bbdmidc.id ,bbdmidc.params ,bbdmidc.capacity ,bbdmidc.unit ,bbdmidc.frequency , bbdmidc.range_range , bbdmidc.standard_value , bbdmidc.resolution , bbdmidc.maximum_error , bbdmidc.error_param_a , bbdmidc.error_param_b " " FROM biz_business_device_measure_item_data_calibrator bbdmidc WHERE item_id = :itemId "); // 将参数绑定到占位符 query.bindValue(":itemId", itemId); + if (query.exec()) { while (query.next()) { VerMultiCal calibration; @@ -484,7 +489,7 @@ } else { qDebug() << "Query execution failed: " << query.lastError().text(); } - + *headList<<"序号"<<"参数"<<"量"<<"单位"<<"频率"<<"量程"<<"标准值"<<"分辨力"<<"最大允许误差"<<"误差参数a"<<"误差参数b"; return result; } @@ -746,8 +751,6 @@ programInfo.create_name = query.value("create_name").toString(); programInfo.create_time = query.value("create_time").toDateTime(); programInfo.remark = query.value("remark").toString(); - programInfo.comm_id = query.value("comm_id").toString(); - result.append(programInfo); } } else { @@ -789,7 +792,7 @@ return result; } //查询设备通信接口 -QList getVerificationProgramConfigInterface(QString deviceId) +QList BaseCommonApi::getVerificationProgramConfigInterface(QString deviceId) { QList result; QSqlQuery query; @@ -814,3 +817,239 @@ } return result; } +//查询指令库 +QList BaseCommonApi::getInstructionLib(QString device_id) +{ + QList result; + QSqlQuery query; + query.prepare("SELECT id,device_id,device_name,instruct_config,instruct_read,type,belong_vername,belong_checkname,create_time,update_time FROM zd_instruction_library WHERE device_id = :device_id "); + query.bindValue(":device_id", device_id); + if (query.exec()) { + while (query.next()) { + InstructionLib instruction; + instruction.id = query.value("id").toString(); + instruction.device_id = query.value("device_id").toString(); + instruction.device_name = query.value("device_name").toString(); + instruction.instruct_config = query.value("instruct_config").toString(); + instruction.instruct_read = query.value("instruct_read").toString(); + instruction.type = query.value("type").toString(); + instruction.belong_vername = query.value("belong_vername").toString(); + instruction.belong_checkname = query.value("belong_checkname").toString(); + instruction.create_time = query.value("create_time").toDateTime(); + instruction.update_time = query.value("update_time").toDateTime(); + + result.append(instruction); + } + } else { + qDebug() << "Query execution failed: " << query.lastError().text(); + } + return result; +} + + + +//新建检定程序 +bool BaseCommonApi::InsertVerificationProgramInfo(VerificationProgramInfo verInfo) +{ + QSqlQuery query; + query.prepare("INSERT INTO zd_verification_program_info (id,program_name,create_name,create_time, remark) " + " VALUES (':id', ':program_name',':create_name', ':create_time', ':remark');"); + + query.bindValue(":id", verInfo.id); + query.bindValue(":program_name", verInfo.program_name); + query.bindValue(":create_name", verInfo.create_name); + query.bindValue(":create_time", verInfo.create_time); + query.bindValue(":remark", verInfo.remark); + + bool isSuccess = query.exec(); + if (!isSuccess) { + qDebug() << "InsertTaskBasic query failed: " << query.lastError().text(); + } + + return isSuccess; +} +//新增检定配置 +bool BaseCommonApi::InsertVerificationProgramConfig(VerificationProgramConfig programConfig) +{ + QSqlQuery query; + query.prepare("INSERT INTO zd_verification_program_details (id,verification_id,img_path,tested_device_id,standard_device_id) " + " VALUES (':id',':verification_id',':img_path',':tested_device_id',':standard_device_id')"); + + query.bindValue(":id", programConfig.id); + query.bindValue(":verification_id", programConfig.verification_id); + query.bindValue(":img_path", programConfig.img_path); + query.bindValue(":tested_device_id", programConfig.tested_device_id); + query.bindValue(":standard_device_id", programConfig.standard_device_id); + + bool isSuccess = query.exec(); + if (!isSuccess) { + qDebug() << "InsertTaskBasic query failed: " << query.lastError().text(); + } + + return isSuccess; +} +//新增通讯接口 +bool BaseCommonApi::InsertVerificationProgramConfigInterface(VerificationProgramConfigInterface configInterdace) +{ + QSqlQuery query; + query.prepare("INSERT INTO zd_communication_setting (id,device_id,communication_type,serial_port,visa,create_time,update_time) " + " VALUES (':id',':device_id',':communication_type',':serial_port',':visa',':create_time',':update_time')"); + + query.bindValue(":id", configInterdace.id); + query.bindValue(":device_id", configInterdace.device_id); + query.bindValue(":communication_type", configInterdace.communication_type); + query.bindValue(":serial_port", configInterdace.serial_port); + query.bindValue(":visa", configInterdace.visa); + query.bindValue(":create_time", configInterdace.create_time); + query.bindValue(":update_time", configInterdace.update_time); + bool isSuccess = query.exec(); + if (!isSuccess) { + qDebug() << "InsertTaskBasic query failed: " << query.lastError().text(); + } + + return isSuccess; +} +//更新检定程序 +bool BaseCommonApi::UpdateVerificationProgramInfo(VerificationProgramInfo verInfo) +{ + QSqlQuery query; + query.prepare("UPDATE zd_verification_program_info " + " SET program_name = :program_name,create_name = :create_name, " + " create_time = :create_time, remark = :remark WHERE id = :id;"); + query.bindValue(":program_name", verInfo.program_name); + query.bindValue(":create_name", verInfo.create_name); + query.bindValue(":create_time", verInfo.create_time); + query.bindValue(":remark", verInfo.remark); + query.bindValue(":id", verInfo.id); + bool isSuccess = query.exec(); + if (!isSuccess) { + qDebug() << "InsertOrderSampleRelation query failed: " << query.lastError().text(); + } + return isSuccess; +} +//更新检定配置 +bool BaseCommonApi::UpdateVerificationProgramConfig(VerificationProgramConfig programConfig) +{ +QSqlQuery query; + query.prepare("UPDATE zd_verification_program_details " + " SET img_path = :img_path,tested_device_id = :tested_device_id,standard_device_id = :standard_device_id " + " WHERE verification_id = :verification_id;"); + query.bindValue(":img_path", programConfig.img_path); + query.bindValue(":tested_device_id", programConfig.tested_device_id); + query.bindValue(":standard_device_id", programConfig.standard_device_id); + query.bindValue(":verification_id", programConfig.verification_id); + bool isSuccess = query.exec(); + if (!isSuccess) { + qDebug() << "InsertOrderSampleRelation query failed: " << query.lastError().text(); + } + return isSuccess; +} +//更新通讯接口 +bool BaseCommonApi::UpdateVerificationProgramConfigInterface(VerificationProgramConfigInterface configInterdace) +{ + QSqlQuery query; + query.prepare("UPDATE zd_communication_setting SET communication_type = :communication_type, " + " serial_port = :serial_port,visa = :visa,update_time = :update_time " + " WHERE device_id = :device_id;"); + query.bindValue(":communication_type", configInterdace.communication_type); + query.bindValue(":serial_port", configInterdace.serial_port); + query.bindValue(":visa", configInterdace.visa); + query.bindValue(":update_time", configInterdace.update_time); + query.bindValue(":device_id", configInterdace.device_id); + bool isSuccess = query.exec(); + if (!isSuccess) { + qDebug() << "InsertOrderSampleRelation query failed: " << query.lastError().text(); + } + return isSuccess; +} +//删除检定程序 +bool BaseCommonApi::DeleteVerificationProgramInfo(QString id) +{ + QSqlQuery query; + //删除检定程序 + query.prepare("DELETE FROM zd_verification_program_info WHERE id = :id"); + query.bindValue(":id", id); + + if (!query.exec()) { + qDebug() << "删除失败: " << query.lastError().text(); + return false; + } + + //删除检定程序详情 + query.prepare("DELETE FROM zd_verification_program_details WHERE verification_id = :verification_id"); + query.bindValue(":verification_id", id); + + if (!query.exec()) { + qDebug() << "删除失败 " << query.lastError().text(); + return false; + } + + return true; +} +//新增指令库 +bool BaseCommonApi::InsertInstructionLib(InstructionLib instructionLib) +{ + QSqlQuery query; + query.prepare("INSERT INTO zd_instruction_library (id,device_id,device_name,instruct_config,instruct_read,type,verification_id,check_id,create_time,update_time) " + " VALUES (':id',':device_id',':device_name',':instruct_config',':instruct_read',':type',':belong_vername',':belong_checkname',':create_time',':update_time')"); + + query.bindValue(":id", instructionLib.id); + query.bindValue(":device_id", instructionLib.device_id); + query.bindValue(":device_name", instructionLib.device_name); + query.bindValue(":instruct_config", instructionLib.instruct_config); + query.bindValue(":instruct_read", instructionLib.instruct_read); + query.bindValue(":type", instructionLib.type); + query.bindValue(":belong_vername", instructionLib.belong_vername); + query.bindValue(":belong_checkname", instructionLib.belong_checkname); + query.bindValue(":create_time", instructionLib.create_time); + query.bindValue(":update_time", instructionLib.update_time); + bool isSuccess = query.exec(); + if (!isSuccess) { + qDebug() << "InsertTaskBasic query failed: " << query.lastError().text(); + } + + return isSuccess; +} +//删除指令库 +bool BaseCommonApi::DeleteInstructionLib(QString device_id) +{ + QSqlQuery query; + //删除检定程序 + query.prepare("DELETE FROM zd_instruction_library WHERE device_id = :device_id "); + query.bindValue(":device_id", device_id); + + if (!query.exec()) { + qDebug() << "删除失败: " << query.lastError().text(); + return false; + } + return true; +} +//更新指令库 +bool BaseCommonApi::UpdateInstructionLib(InstructionLib instructionLib) +{ + QSqlQuery query; + query.prepare("UPDATE zd_instruction_library SET device_id=:device_id," + "device_name = :device_name,instruct_config = :instruct_config,instruct_read = :instruct_read," + " type = :type,belong_vername = :belong_vername,belong_checkname = :belong_checkname," + "update_time = :update_time WHERE id = :id"); + query.bindValue(":device_id", instructionLib.device_id); + query.bindValue(":device_name", instructionLib.device_name); + query.bindValue(":instruct_config", instructionLib.instruct_config); + query.bindValue(":instruct_read", instructionLib.instruct_read); + query.bindValue(":type", instructionLib.type); + query.bindValue(":belong_vername", instructionLib.belong_vername); + query.bindValue(":belong_checkname", instructionLib.belong_checkname); + query.bindValue(":update_time", instructionLib.update_time); + query.bindValue(":id", instructionLib.id); + bool isSuccess = query.exec(); + if (!isSuccess) { + qDebug() << "InsertOrderSampleRelation query failed: " << query.lastError().text(); + } + return isSuccess; +} +//雪花算法生成id +qint64 BaseCommonApi::generateId() +{ + return this->idWorker.nextId(); +} + diff --git a/softwareDirectory/AutoVerScheme/basecommonapi.h b/softwareDirectory/AutoVerScheme/basecommonapi.h index 7fcdfa5..d9b2511 100644 --- a/softwareDirectory/AutoVerScheme/basecommonapi.h +++ b/softwareDirectory/AutoVerScheme/basecommonapi.h @@ -1,14 +1,16 @@ #ifndef BASECOMMONAPI_H #define BASECOMMONAPI_H + #include #include #include #include #include "QList" #include "databaseconnection.h" +#include "id\IdWorker.h" -// + //部门任务 class Dept { @@ -379,7 +381,6 @@ QString create_name; QDateTime create_time; QString remark; - QString comm_id; }; @@ -398,7 +399,7 @@ QString manufacture_no; //出厂编号 QString manufacturer; //生产厂家 }; -//检定程序管理-检定配置设备接口查询 +//检定程序管理-检定配置设备通信接口查询 class VerificationProgramConfigInterface { public: @@ -410,13 +411,29 @@ QDateTime create_time; QDateTime update_time; }; +//指令库 +class InstructionLib +{ +public: + QString id; + QString device_id; + QString device_name; + QString instruct_config; + QString instruct_read; + QString type; + QString belong_vername; + QString belong_checkname; + QDateTime create_time; + QDateTime update_time; +}; - +using namespace Jiawa::Core; class BaseCommonApi { public: BaseCommonApi(); + //部门任务 static QList getAllData(); //我的任务 @@ -452,7 +469,7 @@ //判断 哪个 装置表 传参 设备id 返回itemId static QList getJudgeCalibrationTable(QString id); //被检设备管理-检定项目及检定点-多功能校准源 - static QList getVerMultiCal(QString itemId); + static QList getVerMultiCal(QString itemId,QStringList *headList); // 获取检定数据管理 首页 static QList getVerificationData(); // 检定数据管理-检定基本信息 @@ -473,7 +490,33 @@ static QList getVerificationProgramConfig(QString verification_id); //查询设备通信接口 static QList getVerificationProgramConfigInterface(QString deviceId); + //新建检定程序 + static bool InsertVerificationProgramInfo(VerificationProgramInfo verInfo); + //查询指令库 + static QList getInstructionLib(QString device_id); + //新增指令库 + static bool InsertInstructionLib(InstructionLib instructionLib); + //删除指令库 + static bool DeleteInstructionLib(QString device_id); + //更新指令库 + static bool UpdateInstructionLib(InstructionLib instructionLib); + //新增检定配置 + static bool InsertVerificationProgramConfig(VerificationProgramConfig programConfig); + //新增通讯接口 + static bool InsertVerificationProgramConfigInterface(VerificationProgramConfigInterface configInterdace); + //更新检定程序 + static bool UpdateVerificationProgramInfo(VerificationProgramInfo verInfo); + //更新检定配置 + static bool UpdateVerificationProgramConfig(VerificationProgramConfig programConfig); + //更新通讯接口 + static bool UpdateVerificationProgramConfigInterface(VerificationProgramConfigInterface configInterdace); + //删除检定程序 + static bool DeleteVerificationProgramInfo(QString id); + // 雪花id生成工具 + qint64 generateId(); + //雪花算法生成id + IdWorker &idWorker = Singleton::instance(); }; diff --git a/softwareDirectory/AutoVerScheme/id/IdWorker.h b/softwareDirectory/AutoVerScheme/id/IdWorker.h new file mode 100644 index 0000000..079bbcb --- /dev/null +++ b/softwareDirectory/AutoVerScheme/id/IdWorker.h @@ -0,0 +1,218 @@ +#ifndef _JW_CORE_ID_WORKER_H_ +#define _JW_CORE_ID_WORKER_H_ + +#include +#include +#include +#include +#include +#include "Noncopyable.h" +#include "Singleton.h" + +// 如果不使用 mutex, 则开启下面这个定义, 但是我发现, 还是开启 mutex 功能, 速度比较快 +// #define SNOWFLAKE_ID_WORKER_NO_LOCK + +namespace Jiawa { + + /** + * @brief 核心 + * 核心功能 + */ + namespace Core { + + /** + * @brief 分布式id生成类 + * https://segmentfault.com/a/1190000011282426 + * https://github.com/twitter/snowflake/blob/snowflake-2010/src/main/scala/com/twitter/service/snowflake/IdWorker.scala + * + * 64bit id: 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 + * || || || | | | + * |└---------------------------时间戳--------------------------┘└中心-┘└机器-┘ └----序列号----┘ + * | + * 不用 + * SnowFlake的优点: 整体上按照时间自增排序, 并且整个分布式系统内不会产生ID碰撞(由数据中心ID和机器ID作区分), 并且效率较高, 经测试, SnowFlake每秒能够产生26万ID左右. + */ + class SnowflakeIdWorker : private Noncopyable { + + // 实现单例 + friend class Singleton; + + public: + typedef unsigned int UInt; + typedef unsigned long long int UInt64; + +#ifdef SNOWFLAKE_ID_WORKER_NO_LOCK + typedef std::atomic AtomicUInt; + typedef std::atomic AtomicUInt64; +#else + typedef UInt AtomicUInt; + typedef UInt64 AtomicUInt64; +#endif + + void setWorkerId(UInt workerId) { + this->workerId = workerId; + } + + void setDatacenterId(UInt datacenterId) { + this->datacenterId = datacenterId; + } + + UInt64 getId() { + return nextId(); + } + + /** + * 获得下一个ID (该方法是线程安全的) + * + * @return SnowflakeId + */ + UInt64 nextId() { +#ifndef SNOWFLAKE_ID_WORKER_NO_LOCK + std::unique_lock lock{ mutex }; + AtomicUInt64 timestamp{ 0 }; +#else + static AtomicUInt64 timestamp{ 0 }; +#endif + timestamp = timeGen(); + + // 如果当前时间小于上一次ID生成的时间戳, 说明系统时钟回退过这个时候应当抛出异常 + if (timestamp < lastTimestamp) { + std::ostringstream s; + s << "clock moved backwards. Refusing to generate id for " << lastTimestamp - timestamp << " milliseconds"; + throw std::exception(std::runtime_error(s.str())); + } + + if (lastTimestamp == timestamp) { + // 如果是同一时间生成的, 则进行毫秒内序列 + sequence = (sequence + 1) & sequenceMask; + if (0 == sequence) { + // 毫秒内序列溢出, 阻塞到下一个毫秒,获得新的时间戳 + timestamp = tilNextMillis(lastTimestamp); + } + } else { + sequence = 0; + } + +#ifndef SNOWFLAKE_ID_WORKER_NO_LOCK + lastTimestamp = timestamp; +#else + lastTimestamp = timestamp.load(); +#endif + + // 移位并通过或运算拼到一起组成64位的ID + return ((timestamp - twepoch) << timestampLeftShift) + | (datacenterId << datacenterIdShift) + | (workerId << workerIdShift) + | sequence; + } + + protected: + SnowflakeIdWorker() : workerId(0), datacenterId(0), sequence(0), lastTimestamp(0) { } + + /** + * 返回以毫秒为单位的当前时间 + * + * @return 当前时间(毫秒) + */ + UInt64 timeGen() const { + auto t = std::chrono::time_point_cast(std::chrono::high_resolution_clock::now()); + return t.time_since_epoch().count(); + } + + /** + * 阻塞到下一个毫秒, 直到获得新的时间戳 + * + * @param lastTimestamp 上次生成ID的时间截 + * @return 当前时间戳 + */ + UInt64 tilNextMillis(UInt64 lastTimestamp) const { + UInt64 timestamp = timeGen(); + while (timestamp <= lastTimestamp) { + timestamp = timeGen(); + } + return timestamp; + } + + private: + +#ifndef SNOWFLAKE_ID_WORKER_NO_LOCK + std::mutex mutex; +#endif + + /** + * 开始时间截 (2018-01-01 00:00:00.000) + */ + const UInt64 twepoch = 1514736000000; + + /** + * 机器id所占的位数 + */ + const UInt workerIdBits = 5; + + /** + * 数据中心id所占的位数 + */ + const UInt datacenterIdBits = 5; + + /** + * 序列所占的位数 + */ + const UInt sequenceBits = 12; + + /** + * 机器ID向左移12位 + */ + const UInt workerIdShift = sequenceBits; + + /** + * 数据标识id向左移17位 + */ + const UInt datacenterIdShift = workerIdShift + workerIdBits; + + /** + * 时间截向左移22位 + */ + const UInt timestampLeftShift = datacenterIdShift + datacenterIdBits; + + /** + * 支持的最大机器id, 结果是31 + */ + const UInt maxWorkerId = -1 ^ (-1 << workerIdBits); + + /** + * 支持的最大数据中心id, 结果是31 + */ + const UInt maxDatacenterId = -1 ^ (-1 << datacenterIdBits); + + /** + * 生成序列的掩码, 这里为4095 + */ + const UInt sequenceMask = -1 ^ (-1 << sequenceBits); + + /** + * 工作机器id(0~31) + */ + UInt workerId; + + /** + * 数据中心id(0~31) + */ + UInt datacenterId; + + /** + * 毫秒内序列(0~4095) + */ + AtomicUInt sequence{ 0 }; + + /** + * 上次生成ID的时间截 + */ + AtomicUInt64 lastTimestamp{ 0 }; + + }; + + typedef SnowflakeIdWorker IdWorker; + } +} + +#endif // _JW_CORE_ID_WORKER_H_ diff --git a/softwareDirectory/AutoVerScheme/id/Noncopyable.h b/softwareDirectory/AutoVerScheme/id/Noncopyable.h new file mode 100644 index 0000000..4fc555a --- /dev/null +++ b/softwareDirectory/AutoVerScheme/id/Noncopyable.h @@ -0,0 +1,31 @@ +#ifndef _JW_CORE_NONCOPYABLE_H_ +#define _JW_CORE_NONCOPYABLE_H_ + + +// Private copy constructor and copy assignment ensure classes derived from +// class noncopyable cannot be copied. + +namespace Jiawa { + namespace Core { + + // protection from unintended ADL(Argument Dependent Lookup) + namespace Noncopyable_ { + + class Noncopyable + { + protected: + Noncopyable() = default; + ~Noncopyable() = default; + + Noncopyable(const Noncopyable&) = delete; + Noncopyable(const Noncopyable&&) = delete; + Noncopyable& operator=(const Noncopyable&) = delete; + Noncopyable& operator=(const Noncopyable&&) = delete; + }; + } + + typedef Noncopyable_::Noncopyable Noncopyable; + } +} + +#endif // _JW_CORE_NONCOPYABLE_H_ diff --git a/softwareDirectory/AutoVerScheme/basecommonapi.cpp b/softwareDirectory/AutoVerScheme/basecommonapi.cpp index 4b1f088..7a130d4 100644 --- a/softwareDirectory/AutoVerScheme/basecommonapi.cpp +++ b/softwareDirectory/AutoVerScheme/basecommonapi.cpp @@ -305,7 +305,7 @@ - //直流稳压电源检定装置 + //核查直流稳压电源检定装置 QListBaseCommonApi::getCheckDCPower(QString equipment_id,QStringList *headList) { @@ -336,9 +336,9 @@ } else { qDebug() << "Query execution failed: " << query.lastError().text(); } - *headList << "序号"<<"核査类型"<<"检测类型"<<"参数" - <<"单位"<<"频率" << "核査点" << "分辨力" - <<"循环次数"<< "Urel"<<"通道"<< "TFER"; + *headList <<"序号"<<"核查项目"<<"核查点"<<"单位"<<"分辨力"<<"Urel"<<"核查类型"<<"是否为基准点(1/0)"<<"循环次数" + <<"电阻值/频率"<<"电阻值单位/频率单位"<<"核查项备注"; + return result; } //根据id获取装置名称 @@ -435,7 +435,7 @@ { QList result; QSqlQuery query; - query.prepare("SELECT item_category_id,belong_standard_equipment FROM biz_business_device_measure_item_info bbdmii WHERE bbdmii.device_model_id =(SELECT eei.model_id from eqpt_equipment_info eei WHERE eei.id = :id "); + query.prepare("SELECT id,belong_standard_equipment FROM biz_business_device_measure_item_info bbdmii WHERE bbdmii.device_model_id =(SELECT eei.model_id from eqpt_equipment_info eei WHERE eei.id = ':id')"); // 将参数绑定到占位符 query.bindValue(":id", id); if (query.exec()) { @@ -443,27 +443,32 @@ JudgeCalibrationTable calibration; calibration.item_category_id = query.value("item_category_id").toString(); calibration.belong_standard_equipment = query.value("belong_standard_equipment").toString(); + QString equipmentValue = query.value("belong_standard_equipment").toString(); + result.append(calibration); } } else { qDebug() << "Query execution failed: " << query.lastError().text(); } + return result; } //被检设备管理-检定项目及检定点-多功能校准源 -QList BaseCommonApi::getVerMultiCal(QString itemId) +QList BaseCommonApi::getVerMultiCal(QString itemId,QStringList *headList) { QList result; + QSqlQuery query; query.prepare("SELECT bbdmidc.id ,bbdmidc.params ,bbdmidc.capacity ,bbdmidc.unit ,bbdmidc.frequency , bbdmidc.range_range , bbdmidc.standard_value , bbdmidc.resolution , bbdmidc.maximum_error , bbdmidc.error_param_a , bbdmidc.error_param_b " " FROM biz_business_device_measure_item_data_calibrator bbdmidc WHERE item_id = :itemId "); // 将参数绑定到占位符 query.bindValue(":itemId", itemId); + if (query.exec()) { while (query.next()) { VerMultiCal calibration; @@ -484,7 +489,7 @@ } else { qDebug() << "Query execution failed: " << query.lastError().text(); } - + *headList<<"序号"<<"参数"<<"量"<<"单位"<<"频率"<<"量程"<<"标准值"<<"分辨力"<<"最大允许误差"<<"误差参数a"<<"误差参数b"; return result; } @@ -746,8 +751,6 @@ programInfo.create_name = query.value("create_name").toString(); programInfo.create_time = query.value("create_time").toDateTime(); programInfo.remark = query.value("remark").toString(); - programInfo.comm_id = query.value("comm_id").toString(); - result.append(programInfo); } } else { @@ -789,7 +792,7 @@ return result; } //查询设备通信接口 -QList getVerificationProgramConfigInterface(QString deviceId) +QList BaseCommonApi::getVerificationProgramConfigInterface(QString deviceId) { QList result; QSqlQuery query; @@ -814,3 +817,239 @@ } return result; } +//查询指令库 +QList BaseCommonApi::getInstructionLib(QString device_id) +{ + QList result; + QSqlQuery query; + query.prepare("SELECT id,device_id,device_name,instruct_config,instruct_read,type,belong_vername,belong_checkname,create_time,update_time FROM zd_instruction_library WHERE device_id = :device_id "); + query.bindValue(":device_id", device_id); + if (query.exec()) { + while (query.next()) { + InstructionLib instruction; + instruction.id = query.value("id").toString(); + instruction.device_id = query.value("device_id").toString(); + instruction.device_name = query.value("device_name").toString(); + instruction.instruct_config = query.value("instruct_config").toString(); + instruction.instruct_read = query.value("instruct_read").toString(); + instruction.type = query.value("type").toString(); + instruction.belong_vername = query.value("belong_vername").toString(); + instruction.belong_checkname = query.value("belong_checkname").toString(); + instruction.create_time = query.value("create_time").toDateTime(); + instruction.update_time = query.value("update_time").toDateTime(); + + result.append(instruction); + } + } else { + qDebug() << "Query execution failed: " << query.lastError().text(); + } + return result; +} + + + +//新建检定程序 +bool BaseCommonApi::InsertVerificationProgramInfo(VerificationProgramInfo verInfo) +{ + QSqlQuery query; + query.prepare("INSERT INTO zd_verification_program_info (id,program_name,create_name,create_time, remark) " + " VALUES (':id', ':program_name',':create_name', ':create_time', ':remark');"); + + query.bindValue(":id", verInfo.id); + query.bindValue(":program_name", verInfo.program_name); + query.bindValue(":create_name", verInfo.create_name); + query.bindValue(":create_time", verInfo.create_time); + query.bindValue(":remark", verInfo.remark); + + bool isSuccess = query.exec(); + if (!isSuccess) { + qDebug() << "InsertTaskBasic query failed: " << query.lastError().text(); + } + + return isSuccess; +} +//新增检定配置 +bool BaseCommonApi::InsertVerificationProgramConfig(VerificationProgramConfig programConfig) +{ + QSqlQuery query; + query.prepare("INSERT INTO zd_verification_program_details (id,verification_id,img_path,tested_device_id,standard_device_id) " + " VALUES (':id',':verification_id',':img_path',':tested_device_id',':standard_device_id')"); + + query.bindValue(":id", programConfig.id); + query.bindValue(":verification_id", programConfig.verification_id); + query.bindValue(":img_path", programConfig.img_path); + query.bindValue(":tested_device_id", programConfig.tested_device_id); + query.bindValue(":standard_device_id", programConfig.standard_device_id); + + bool isSuccess = query.exec(); + if (!isSuccess) { + qDebug() << "InsertTaskBasic query failed: " << query.lastError().text(); + } + + return isSuccess; +} +//新增通讯接口 +bool BaseCommonApi::InsertVerificationProgramConfigInterface(VerificationProgramConfigInterface configInterdace) +{ + QSqlQuery query; + query.prepare("INSERT INTO zd_communication_setting (id,device_id,communication_type,serial_port,visa,create_time,update_time) " + " VALUES (':id',':device_id',':communication_type',':serial_port',':visa',':create_time',':update_time')"); + + query.bindValue(":id", configInterdace.id); + query.bindValue(":device_id", configInterdace.device_id); + query.bindValue(":communication_type", configInterdace.communication_type); + query.bindValue(":serial_port", configInterdace.serial_port); + query.bindValue(":visa", configInterdace.visa); + query.bindValue(":create_time", configInterdace.create_time); + query.bindValue(":update_time", configInterdace.update_time); + bool isSuccess = query.exec(); + if (!isSuccess) { + qDebug() << "InsertTaskBasic query failed: " << query.lastError().text(); + } + + return isSuccess; +} +//更新检定程序 +bool BaseCommonApi::UpdateVerificationProgramInfo(VerificationProgramInfo verInfo) +{ + QSqlQuery query; + query.prepare("UPDATE zd_verification_program_info " + " SET program_name = :program_name,create_name = :create_name, " + " create_time = :create_time, remark = :remark WHERE id = :id;"); + query.bindValue(":program_name", verInfo.program_name); + query.bindValue(":create_name", verInfo.create_name); + query.bindValue(":create_time", verInfo.create_time); + query.bindValue(":remark", verInfo.remark); + query.bindValue(":id", verInfo.id); + bool isSuccess = query.exec(); + if (!isSuccess) { + qDebug() << "InsertOrderSampleRelation query failed: " << query.lastError().text(); + } + return isSuccess; +} +//更新检定配置 +bool BaseCommonApi::UpdateVerificationProgramConfig(VerificationProgramConfig programConfig) +{ +QSqlQuery query; + query.prepare("UPDATE zd_verification_program_details " + " SET img_path = :img_path,tested_device_id = :tested_device_id,standard_device_id = :standard_device_id " + " WHERE verification_id = :verification_id;"); + query.bindValue(":img_path", programConfig.img_path); + query.bindValue(":tested_device_id", programConfig.tested_device_id); + query.bindValue(":standard_device_id", programConfig.standard_device_id); + query.bindValue(":verification_id", programConfig.verification_id); + bool isSuccess = query.exec(); + if (!isSuccess) { + qDebug() << "InsertOrderSampleRelation query failed: " << query.lastError().text(); + } + return isSuccess; +} +//更新通讯接口 +bool BaseCommonApi::UpdateVerificationProgramConfigInterface(VerificationProgramConfigInterface configInterdace) +{ + QSqlQuery query; + query.prepare("UPDATE zd_communication_setting SET communication_type = :communication_type, " + " serial_port = :serial_port,visa = :visa,update_time = :update_time " + " WHERE device_id = :device_id;"); + query.bindValue(":communication_type", configInterdace.communication_type); + query.bindValue(":serial_port", configInterdace.serial_port); + query.bindValue(":visa", configInterdace.visa); + query.bindValue(":update_time", configInterdace.update_time); + query.bindValue(":device_id", configInterdace.device_id); + bool isSuccess = query.exec(); + if (!isSuccess) { + qDebug() << "InsertOrderSampleRelation query failed: " << query.lastError().text(); + } + return isSuccess; +} +//删除检定程序 +bool BaseCommonApi::DeleteVerificationProgramInfo(QString id) +{ + QSqlQuery query; + //删除检定程序 + query.prepare("DELETE FROM zd_verification_program_info WHERE id = :id"); + query.bindValue(":id", id); + + if (!query.exec()) { + qDebug() << "删除失败: " << query.lastError().text(); + return false; + } + + //删除检定程序详情 + query.prepare("DELETE FROM zd_verification_program_details WHERE verification_id = :verification_id"); + query.bindValue(":verification_id", id); + + if (!query.exec()) { + qDebug() << "删除失败 " << query.lastError().text(); + return false; + } + + return true; +} +//新增指令库 +bool BaseCommonApi::InsertInstructionLib(InstructionLib instructionLib) +{ + QSqlQuery query; + query.prepare("INSERT INTO zd_instruction_library (id,device_id,device_name,instruct_config,instruct_read,type,verification_id,check_id,create_time,update_time) " + " VALUES (':id',':device_id',':device_name',':instruct_config',':instruct_read',':type',':belong_vername',':belong_checkname',':create_time',':update_time')"); + + query.bindValue(":id", instructionLib.id); + query.bindValue(":device_id", instructionLib.device_id); + query.bindValue(":device_name", instructionLib.device_name); + query.bindValue(":instruct_config", instructionLib.instruct_config); + query.bindValue(":instruct_read", instructionLib.instruct_read); + query.bindValue(":type", instructionLib.type); + query.bindValue(":belong_vername", instructionLib.belong_vername); + query.bindValue(":belong_checkname", instructionLib.belong_checkname); + query.bindValue(":create_time", instructionLib.create_time); + query.bindValue(":update_time", instructionLib.update_time); + bool isSuccess = query.exec(); + if (!isSuccess) { + qDebug() << "InsertTaskBasic query failed: " << query.lastError().text(); + } + + return isSuccess; +} +//删除指令库 +bool BaseCommonApi::DeleteInstructionLib(QString device_id) +{ + QSqlQuery query; + //删除检定程序 + query.prepare("DELETE FROM zd_instruction_library WHERE device_id = :device_id "); + query.bindValue(":device_id", device_id); + + if (!query.exec()) { + qDebug() << "删除失败: " << query.lastError().text(); + return false; + } + return true; +} +//更新指令库 +bool BaseCommonApi::UpdateInstructionLib(InstructionLib instructionLib) +{ + QSqlQuery query; + query.prepare("UPDATE zd_instruction_library SET device_id=:device_id," + "device_name = :device_name,instruct_config = :instruct_config,instruct_read = :instruct_read," + " type = :type,belong_vername = :belong_vername,belong_checkname = :belong_checkname," + "update_time = :update_time WHERE id = :id"); + query.bindValue(":device_id", instructionLib.device_id); + query.bindValue(":device_name", instructionLib.device_name); + query.bindValue(":instruct_config", instructionLib.instruct_config); + query.bindValue(":instruct_read", instructionLib.instruct_read); + query.bindValue(":type", instructionLib.type); + query.bindValue(":belong_vername", instructionLib.belong_vername); + query.bindValue(":belong_checkname", instructionLib.belong_checkname); + query.bindValue(":update_time", instructionLib.update_time); + query.bindValue(":id", instructionLib.id); + bool isSuccess = query.exec(); + if (!isSuccess) { + qDebug() << "InsertOrderSampleRelation query failed: " << query.lastError().text(); + } + return isSuccess; +} +//雪花算法生成id +qint64 BaseCommonApi::generateId() +{ + return this->idWorker.nextId(); +} + diff --git a/softwareDirectory/AutoVerScheme/basecommonapi.h b/softwareDirectory/AutoVerScheme/basecommonapi.h index 7fcdfa5..d9b2511 100644 --- a/softwareDirectory/AutoVerScheme/basecommonapi.h +++ b/softwareDirectory/AutoVerScheme/basecommonapi.h @@ -1,14 +1,16 @@ #ifndef BASECOMMONAPI_H #define BASECOMMONAPI_H + #include #include #include #include #include "QList" #include "databaseconnection.h" +#include "id\IdWorker.h" -// + //部门任务 class Dept { @@ -379,7 +381,6 @@ QString create_name; QDateTime create_time; QString remark; - QString comm_id; }; @@ -398,7 +399,7 @@ QString manufacture_no; //出厂编号 QString manufacturer; //生产厂家 }; -//检定程序管理-检定配置设备接口查询 +//检定程序管理-检定配置设备通信接口查询 class VerificationProgramConfigInterface { public: @@ -410,13 +411,29 @@ QDateTime create_time; QDateTime update_time; }; +//指令库 +class InstructionLib +{ +public: + QString id; + QString device_id; + QString device_name; + QString instruct_config; + QString instruct_read; + QString type; + QString belong_vername; + QString belong_checkname; + QDateTime create_time; + QDateTime update_time; +}; - +using namespace Jiawa::Core; class BaseCommonApi { public: BaseCommonApi(); + //部门任务 static QList getAllData(); //我的任务 @@ -452,7 +469,7 @@ //判断 哪个 装置表 传参 设备id 返回itemId static QList getJudgeCalibrationTable(QString id); //被检设备管理-检定项目及检定点-多功能校准源 - static QList getVerMultiCal(QString itemId); + static QList getVerMultiCal(QString itemId,QStringList *headList); // 获取检定数据管理 首页 static QList getVerificationData(); // 检定数据管理-检定基本信息 @@ -473,7 +490,33 @@ static QList getVerificationProgramConfig(QString verification_id); //查询设备通信接口 static QList getVerificationProgramConfigInterface(QString deviceId); + //新建检定程序 + static bool InsertVerificationProgramInfo(VerificationProgramInfo verInfo); + //查询指令库 + static QList getInstructionLib(QString device_id); + //新增指令库 + static bool InsertInstructionLib(InstructionLib instructionLib); + //删除指令库 + static bool DeleteInstructionLib(QString device_id); + //更新指令库 + static bool UpdateInstructionLib(InstructionLib instructionLib); + //新增检定配置 + static bool InsertVerificationProgramConfig(VerificationProgramConfig programConfig); + //新增通讯接口 + static bool InsertVerificationProgramConfigInterface(VerificationProgramConfigInterface configInterdace); + //更新检定程序 + static bool UpdateVerificationProgramInfo(VerificationProgramInfo verInfo); + //更新检定配置 + static bool UpdateVerificationProgramConfig(VerificationProgramConfig programConfig); + //更新通讯接口 + static bool UpdateVerificationProgramConfigInterface(VerificationProgramConfigInterface configInterdace); + //删除检定程序 + static bool DeleteVerificationProgramInfo(QString id); + // 雪花id生成工具 + qint64 generateId(); + //雪花算法生成id + IdWorker &idWorker = Singleton::instance(); }; diff --git a/softwareDirectory/AutoVerScheme/id/IdWorker.h b/softwareDirectory/AutoVerScheme/id/IdWorker.h new file mode 100644 index 0000000..079bbcb --- /dev/null +++ b/softwareDirectory/AutoVerScheme/id/IdWorker.h @@ -0,0 +1,218 @@ +#ifndef _JW_CORE_ID_WORKER_H_ +#define _JW_CORE_ID_WORKER_H_ + +#include +#include +#include +#include +#include +#include "Noncopyable.h" +#include "Singleton.h" + +// 如果不使用 mutex, 则开启下面这个定义, 但是我发现, 还是开启 mutex 功能, 速度比较快 +// #define SNOWFLAKE_ID_WORKER_NO_LOCK + +namespace Jiawa { + + /** + * @brief 核心 + * 核心功能 + */ + namespace Core { + + /** + * @brief 分布式id生成类 + * https://segmentfault.com/a/1190000011282426 + * https://github.com/twitter/snowflake/blob/snowflake-2010/src/main/scala/com/twitter/service/snowflake/IdWorker.scala + * + * 64bit id: 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 + * || || || | | | + * |└---------------------------时间戳--------------------------┘└中心-┘└机器-┘ └----序列号----┘ + * | + * 不用 + * SnowFlake的优点: 整体上按照时间自增排序, 并且整个分布式系统内不会产生ID碰撞(由数据中心ID和机器ID作区分), 并且效率较高, 经测试, SnowFlake每秒能够产生26万ID左右. + */ + class SnowflakeIdWorker : private Noncopyable { + + // 实现单例 + friend class Singleton; + + public: + typedef unsigned int UInt; + typedef unsigned long long int UInt64; + +#ifdef SNOWFLAKE_ID_WORKER_NO_LOCK + typedef std::atomic AtomicUInt; + typedef std::atomic AtomicUInt64; +#else + typedef UInt AtomicUInt; + typedef UInt64 AtomicUInt64; +#endif + + void setWorkerId(UInt workerId) { + this->workerId = workerId; + } + + void setDatacenterId(UInt datacenterId) { + this->datacenterId = datacenterId; + } + + UInt64 getId() { + return nextId(); + } + + /** + * 获得下一个ID (该方法是线程安全的) + * + * @return SnowflakeId + */ + UInt64 nextId() { +#ifndef SNOWFLAKE_ID_WORKER_NO_LOCK + std::unique_lock lock{ mutex }; + AtomicUInt64 timestamp{ 0 }; +#else + static AtomicUInt64 timestamp{ 0 }; +#endif + timestamp = timeGen(); + + // 如果当前时间小于上一次ID生成的时间戳, 说明系统时钟回退过这个时候应当抛出异常 + if (timestamp < lastTimestamp) { + std::ostringstream s; + s << "clock moved backwards. Refusing to generate id for " << lastTimestamp - timestamp << " milliseconds"; + throw std::exception(std::runtime_error(s.str())); + } + + if (lastTimestamp == timestamp) { + // 如果是同一时间生成的, 则进行毫秒内序列 + sequence = (sequence + 1) & sequenceMask; + if (0 == sequence) { + // 毫秒内序列溢出, 阻塞到下一个毫秒,获得新的时间戳 + timestamp = tilNextMillis(lastTimestamp); + } + } else { + sequence = 0; + } + +#ifndef SNOWFLAKE_ID_WORKER_NO_LOCK + lastTimestamp = timestamp; +#else + lastTimestamp = timestamp.load(); +#endif + + // 移位并通过或运算拼到一起组成64位的ID + return ((timestamp - twepoch) << timestampLeftShift) + | (datacenterId << datacenterIdShift) + | (workerId << workerIdShift) + | sequence; + } + + protected: + SnowflakeIdWorker() : workerId(0), datacenterId(0), sequence(0), lastTimestamp(0) { } + + /** + * 返回以毫秒为单位的当前时间 + * + * @return 当前时间(毫秒) + */ + UInt64 timeGen() const { + auto t = std::chrono::time_point_cast(std::chrono::high_resolution_clock::now()); + return t.time_since_epoch().count(); + } + + /** + * 阻塞到下一个毫秒, 直到获得新的时间戳 + * + * @param lastTimestamp 上次生成ID的时间截 + * @return 当前时间戳 + */ + UInt64 tilNextMillis(UInt64 lastTimestamp) const { + UInt64 timestamp = timeGen(); + while (timestamp <= lastTimestamp) { + timestamp = timeGen(); + } + return timestamp; + } + + private: + +#ifndef SNOWFLAKE_ID_WORKER_NO_LOCK + std::mutex mutex; +#endif + + /** + * 开始时间截 (2018-01-01 00:00:00.000) + */ + const UInt64 twepoch = 1514736000000; + + /** + * 机器id所占的位数 + */ + const UInt workerIdBits = 5; + + /** + * 数据中心id所占的位数 + */ + const UInt datacenterIdBits = 5; + + /** + * 序列所占的位数 + */ + const UInt sequenceBits = 12; + + /** + * 机器ID向左移12位 + */ + const UInt workerIdShift = sequenceBits; + + /** + * 数据标识id向左移17位 + */ + const UInt datacenterIdShift = workerIdShift + workerIdBits; + + /** + * 时间截向左移22位 + */ + const UInt timestampLeftShift = datacenterIdShift + datacenterIdBits; + + /** + * 支持的最大机器id, 结果是31 + */ + const UInt maxWorkerId = -1 ^ (-1 << workerIdBits); + + /** + * 支持的最大数据中心id, 结果是31 + */ + const UInt maxDatacenterId = -1 ^ (-1 << datacenterIdBits); + + /** + * 生成序列的掩码, 这里为4095 + */ + const UInt sequenceMask = -1 ^ (-1 << sequenceBits); + + /** + * 工作机器id(0~31) + */ + UInt workerId; + + /** + * 数据中心id(0~31) + */ + UInt datacenterId; + + /** + * 毫秒内序列(0~4095) + */ + AtomicUInt sequence{ 0 }; + + /** + * 上次生成ID的时间截 + */ + AtomicUInt64 lastTimestamp{ 0 }; + + }; + + typedef SnowflakeIdWorker IdWorker; + } +} + +#endif // _JW_CORE_ID_WORKER_H_ diff --git a/softwareDirectory/AutoVerScheme/id/Noncopyable.h b/softwareDirectory/AutoVerScheme/id/Noncopyable.h new file mode 100644 index 0000000..4fc555a --- /dev/null +++ b/softwareDirectory/AutoVerScheme/id/Noncopyable.h @@ -0,0 +1,31 @@ +#ifndef _JW_CORE_NONCOPYABLE_H_ +#define _JW_CORE_NONCOPYABLE_H_ + + +// Private copy constructor and copy assignment ensure classes derived from +// class noncopyable cannot be copied. + +namespace Jiawa { + namespace Core { + + // protection from unintended ADL(Argument Dependent Lookup) + namespace Noncopyable_ { + + class Noncopyable + { + protected: + Noncopyable() = default; + ~Noncopyable() = default; + + Noncopyable(const Noncopyable&) = delete; + Noncopyable(const Noncopyable&&) = delete; + Noncopyable& operator=(const Noncopyable&) = delete; + Noncopyable& operator=(const Noncopyable&&) = delete; + }; + } + + typedef Noncopyable_::Noncopyable Noncopyable; + } +} + +#endif // _JW_CORE_NONCOPYABLE_H_ diff --git a/softwareDirectory/AutoVerScheme/id/Singleton.h b/softwareDirectory/AutoVerScheme/id/Singleton.h new file mode 100644 index 0000000..daad45c --- /dev/null +++ b/softwareDirectory/AutoVerScheme/id/Singleton.h @@ -0,0 +1,60 @@ +#ifndef _JW_CORE_SINGLETON_H_ +#define _JW_CORE_SINGLETON_H_ + +namespace Jiawa { + namespace Core { + + // boost/container/detail/singleton.hpp + // http://blog.csdn.net/fullsail/article/details/8483106 + // T must be: no-throw default constructible and no-throw destructible + template + class Singleton { + private: + Singleton() = default; + ~Singleton() = default; + + private: + struct object_creator + { + // This constructor does nothing more than ensure that instance() + // is called before main() begins, thus creating the static + // T object before multithreading race issues can come up. + object_creator() { Singleton::instance(); } + inline void do_nothing() const { } + }; + static object_creator create_object; + + private: + Singleton(const Singleton&) = delete; + Singleton(const Singleton&&) = delete; + Singleton& operator=(const Singleton&) = delete; + Singleton& operator=(const Singleton&&) = delete; + + public: + typedef T object_type; + + // If, at any point (in user code), Singleton::instance() + // is called, then the following function is instantiated. + static object_type& instance() + { + // This is the object that we return a reference to. + // It is guaranteed to be created before main() begins because of + // the next line. + static object_type obj; + + // The following line does nothing else than force the instantiation + // of Singleton::create_object, whose constructor is + // called before main() begins. + create_object.do_nothing(); + + return obj; + } + }; + + template + typename Singleton::object_creator Singleton::create_object; + } +} + +#endif // _JW_CORE_SINGLETON_H_ + diff --git a/softwareDirectory/AutoVerScheme/basecommonapi.cpp b/softwareDirectory/AutoVerScheme/basecommonapi.cpp index 4b1f088..7a130d4 100644 --- a/softwareDirectory/AutoVerScheme/basecommonapi.cpp +++ b/softwareDirectory/AutoVerScheme/basecommonapi.cpp @@ -305,7 +305,7 @@ - //直流稳压电源检定装置 + //核查直流稳压电源检定装置 QListBaseCommonApi::getCheckDCPower(QString equipment_id,QStringList *headList) { @@ -336,9 +336,9 @@ } else { qDebug() << "Query execution failed: " << query.lastError().text(); } - *headList << "序号"<<"核査类型"<<"检测类型"<<"参数" - <<"单位"<<"频率" << "核査点" << "分辨力" - <<"循环次数"<< "Urel"<<"通道"<< "TFER"; + *headList <<"序号"<<"核查项目"<<"核查点"<<"单位"<<"分辨力"<<"Urel"<<"核查类型"<<"是否为基准点(1/0)"<<"循环次数" + <<"电阻值/频率"<<"电阻值单位/频率单位"<<"核查项备注"; + return result; } //根据id获取装置名称 @@ -435,7 +435,7 @@ { QList result; QSqlQuery query; - query.prepare("SELECT item_category_id,belong_standard_equipment FROM biz_business_device_measure_item_info bbdmii WHERE bbdmii.device_model_id =(SELECT eei.model_id from eqpt_equipment_info eei WHERE eei.id = :id "); + query.prepare("SELECT id,belong_standard_equipment FROM biz_business_device_measure_item_info bbdmii WHERE bbdmii.device_model_id =(SELECT eei.model_id from eqpt_equipment_info eei WHERE eei.id = ':id')"); // 将参数绑定到占位符 query.bindValue(":id", id); if (query.exec()) { @@ -443,27 +443,32 @@ JudgeCalibrationTable calibration; calibration.item_category_id = query.value("item_category_id").toString(); calibration.belong_standard_equipment = query.value("belong_standard_equipment").toString(); + QString equipmentValue = query.value("belong_standard_equipment").toString(); + result.append(calibration); } } else { qDebug() << "Query execution failed: " << query.lastError().text(); } + return result; } //被检设备管理-检定项目及检定点-多功能校准源 -QList BaseCommonApi::getVerMultiCal(QString itemId) +QList BaseCommonApi::getVerMultiCal(QString itemId,QStringList *headList) { QList result; + QSqlQuery query; query.prepare("SELECT bbdmidc.id ,bbdmidc.params ,bbdmidc.capacity ,bbdmidc.unit ,bbdmidc.frequency , bbdmidc.range_range , bbdmidc.standard_value , bbdmidc.resolution , bbdmidc.maximum_error , bbdmidc.error_param_a , bbdmidc.error_param_b " " FROM biz_business_device_measure_item_data_calibrator bbdmidc WHERE item_id = :itemId "); // 将参数绑定到占位符 query.bindValue(":itemId", itemId); + if (query.exec()) { while (query.next()) { VerMultiCal calibration; @@ -484,7 +489,7 @@ } else { qDebug() << "Query execution failed: " << query.lastError().text(); } - + *headList<<"序号"<<"参数"<<"量"<<"单位"<<"频率"<<"量程"<<"标准值"<<"分辨力"<<"最大允许误差"<<"误差参数a"<<"误差参数b"; return result; } @@ -746,8 +751,6 @@ programInfo.create_name = query.value("create_name").toString(); programInfo.create_time = query.value("create_time").toDateTime(); programInfo.remark = query.value("remark").toString(); - programInfo.comm_id = query.value("comm_id").toString(); - result.append(programInfo); } } else { @@ -789,7 +792,7 @@ return result; } //查询设备通信接口 -QList getVerificationProgramConfigInterface(QString deviceId) +QList BaseCommonApi::getVerificationProgramConfigInterface(QString deviceId) { QList result; QSqlQuery query; @@ -814,3 +817,239 @@ } return result; } +//查询指令库 +QList BaseCommonApi::getInstructionLib(QString device_id) +{ + QList result; + QSqlQuery query; + query.prepare("SELECT id,device_id,device_name,instruct_config,instruct_read,type,belong_vername,belong_checkname,create_time,update_time FROM zd_instruction_library WHERE device_id = :device_id "); + query.bindValue(":device_id", device_id); + if (query.exec()) { + while (query.next()) { + InstructionLib instruction; + instruction.id = query.value("id").toString(); + instruction.device_id = query.value("device_id").toString(); + instruction.device_name = query.value("device_name").toString(); + instruction.instruct_config = query.value("instruct_config").toString(); + instruction.instruct_read = query.value("instruct_read").toString(); + instruction.type = query.value("type").toString(); + instruction.belong_vername = query.value("belong_vername").toString(); + instruction.belong_checkname = query.value("belong_checkname").toString(); + instruction.create_time = query.value("create_time").toDateTime(); + instruction.update_time = query.value("update_time").toDateTime(); + + result.append(instruction); + } + } else { + qDebug() << "Query execution failed: " << query.lastError().text(); + } + return result; +} + + + +//新建检定程序 +bool BaseCommonApi::InsertVerificationProgramInfo(VerificationProgramInfo verInfo) +{ + QSqlQuery query; + query.prepare("INSERT INTO zd_verification_program_info (id,program_name,create_name,create_time, remark) " + " VALUES (':id', ':program_name',':create_name', ':create_time', ':remark');"); + + query.bindValue(":id", verInfo.id); + query.bindValue(":program_name", verInfo.program_name); + query.bindValue(":create_name", verInfo.create_name); + query.bindValue(":create_time", verInfo.create_time); + query.bindValue(":remark", verInfo.remark); + + bool isSuccess = query.exec(); + if (!isSuccess) { + qDebug() << "InsertTaskBasic query failed: " << query.lastError().text(); + } + + return isSuccess; +} +//新增检定配置 +bool BaseCommonApi::InsertVerificationProgramConfig(VerificationProgramConfig programConfig) +{ + QSqlQuery query; + query.prepare("INSERT INTO zd_verification_program_details (id,verification_id,img_path,tested_device_id,standard_device_id) " + " VALUES (':id',':verification_id',':img_path',':tested_device_id',':standard_device_id')"); + + query.bindValue(":id", programConfig.id); + query.bindValue(":verification_id", programConfig.verification_id); + query.bindValue(":img_path", programConfig.img_path); + query.bindValue(":tested_device_id", programConfig.tested_device_id); + query.bindValue(":standard_device_id", programConfig.standard_device_id); + + bool isSuccess = query.exec(); + if (!isSuccess) { + qDebug() << "InsertTaskBasic query failed: " << query.lastError().text(); + } + + return isSuccess; +} +//新增通讯接口 +bool BaseCommonApi::InsertVerificationProgramConfigInterface(VerificationProgramConfigInterface configInterdace) +{ + QSqlQuery query; + query.prepare("INSERT INTO zd_communication_setting (id,device_id,communication_type,serial_port,visa,create_time,update_time) " + " VALUES (':id',':device_id',':communication_type',':serial_port',':visa',':create_time',':update_time')"); + + query.bindValue(":id", configInterdace.id); + query.bindValue(":device_id", configInterdace.device_id); + query.bindValue(":communication_type", configInterdace.communication_type); + query.bindValue(":serial_port", configInterdace.serial_port); + query.bindValue(":visa", configInterdace.visa); + query.bindValue(":create_time", configInterdace.create_time); + query.bindValue(":update_time", configInterdace.update_time); + bool isSuccess = query.exec(); + if (!isSuccess) { + qDebug() << "InsertTaskBasic query failed: " << query.lastError().text(); + } + + return isSuccess; +} +//更新检定程序 +bool BaseCommonApi::UpdateVerificationProgramInfo(VerificationProgramInfo verInfo) +{ + QSqlQuery query; + query.prepare("UPDATE zd_verification_program_info " + " SET program_name = :program_name,create_name = :create_name, " + " create_time = :create_time, remark = :remark WHERE id = :id;"); + query.bindValue(":program_name", verInfo.program_name); + query.bindValue(":create_name", verInfo.create_name); + query.bindValue(":create_time", verInfo.create_time); + query.bindValue(":remark", verInfo.remark); + query.bindValue(":id", verInfo.id); + bool isSuccess = query.exec(); + if (!isSuccess) { + qDebug() << "InsertOrderSampleRelation query failed: " << query.lastError().text(); + } + return isSuccess; +} +//更新检定配置 +bool BaseCommonApi::UpdateVerificationProgramConfig(VerificationProgramConfig programConfig) +{ +QSqlQuery query; + query.prepare("UPDATE zd_verification_program_details " + " SET img_path = :img_path,tested_device_id = :tested_device_id,standard_device_id = :standard_device_id " + " WHERE verification_id = :verification_id;"); + query.bindValue(":img_path", programConfig.img_path); + query.bindValue(":tested_device_id", programConfig.tested_device_id); + query.bindValue(":standard_device_id", programConfig.standard_device_id); + query.bindValue(":verification_id", programConfig.verification_id); + bool isSuccess = query.exec(); + if (!isSuccess) { + qDebug() << "InsertOrderSampleRelation query failed: " << query.lastError().text(); + } + return isSuccess; +} +//更新通讯接口 +bool BaseCommonApi::UpdateVerificationProgramConfigInterface(VerificationProgramConfigInterface configInterdace) +{ + QSqlQuery query; + query.prepare("UPDATE zd_communication_setting SET communication_type = :communication_type, " + " serial_port = :serial_port,visa = :visa,update_time = :update_time " + " WHERE device_id = :device_id;"); + query.bindValue(":communication_type", configInterdace.communication_type); + query.bindValue(":serial_port", configInterdace.serial_port); + query.bindValue(":visa", configInterdace.visa); + query.bindValue(":update_time", configInterdace.update_time); + query.bindValue(":device_id", configInterdace.device_id); + bool isSuccess = query.exec(); + if (!isSuccess) { + qDebug() << "InsertOrderSampleRelation query failed: " << query.lastError().text(); + } + return isSuccess; +} +//删除检定程序 +bool BaseCommonApi::DeleteVerificationProgramInfo(QString id) +{ + QSqlQuery query; + //删除检定程序 + query.prepare("DELETE FROM zd_verification_program_info WHERE id = :id"); + query.bindValue(":id", id); + + if (!query.exec()) { + qDebug() << "删除失败: " << query.lastError().text(); + return false; + } + + //删除检定程序详情 + query.prepare("DELETE FROM zd_verification_program_details WHERE verification_id = :verification_id"); + query.bindValue(":verification_id", id); + + if (!query.exec()) { + qDebug() << "删除失败 " << query.lastError().text(); + return false; + } + + return true; +} +//新增指令库 +bool BaseCommonApi::InsertInstructionLib(InstructionLib instructionLib) +{ + QSqlQuery query; + query.prepare("INSERT INTO zd_instruction_library (id,device_id,device_name,instruct_config,instruct_read,type,verification_id,check_id,create_time,update_time) " + " VALUES (':id',':device_id',':device_name',':instruct_config',':instruct_read',':type',':belong_vername',':belong_checkname',':create_time',':update_time')"); + + query.bindValue(":id", instructionLib.id); + query.bindValue(":device_id", instructionLib.device_id); + query.bindValue(":device_name", instructionLib.device_name); + query.bindValue(":instruct_config", instructionLib.instruct_config); + query.bindValue(":instruct_read", instructionLib.instruct_read); + query.bindValue(":type", instructionLib.type); + query.bindValue(":belong_vername", instructionLib.belong_vername); + query.bindValue(":belong_checkname", instructionLib.belong_checkname); + query.bindValue(":create_time", instructionLib.create_time); + query.bindValue(":update_time", instructionLib.update_time); + bool isSuccess = query.exec(); + if (!isSuccess) { + qDebug() << "InsertTaskBasic query failed: " << query.lastError().text(); + } + + return isSuccess; +} +//删除指令库 +bool BaseCommonApi::DeleteInstructionLib(QString device_id) +{ + QSqlQuery query; + //删除检定程序 + query.prepare("DELETE FROM zd_instruction_library WHERE device_id = :device_id "); + query.bindValue(":device_id", device_id); + + if (!query.exec()) { + qDebug() << "删除失败: " << query.lastError().text(); + return false; + } + return true; +} +//更新指令库 +bool BaseCommonApi::UpdateInstructionLib(InstructionLib instructionLib) +{ + QSqlQuery query; + query.prepare("UPDATE zd_instruction_library SET device_id=:device_id," + "device_name = :device_name,instruct_config = :instruct_config,instruct_read = :instruct_read," + " type = :type,belong_vername = :belong_vername,belong_checkname = :belong_checkname," + "update_time = :update_time WHERE id = :id"); + query.bindValue(":device_id", instructionLib.device_id); + query.bindValue(":device_name", instructionLib.device_name); + query.bindValue(":instruct_config", instructionLib.instruct_config); + query.bindValue(":instruct_read", instructionLib.instruct_read); + query.bindValue(":type", instructionLib.type); + query.bindValue(":belong_vername", instructionLib.belong_vername); + query.bindValue(":belong_checkname", instructionLib.belong_checkname); + query.bindValue(":update_time", instructionLib.update_time); + query.bindValue(":id", instructionLib.id); + bool isSuccess = query.exec(); + if (!isSuccess) { + qDebug() << "InsertOrderSampleRelation query failed: " << query.lastError().text(); + } + return isSuccess; +} +//雪花算法生成id +qint64 BaseCommonApi::generateId() +{ + return this->idWorker.nextId(); +} + diff --git a/softwareDirectory/AutoVerScheme/basecommonapi.h b/softwareDirectory/AutoVerScheme/basecommonapi.h index 7fcdfa5..d9b2511 100644 --- a/softwareDirectory/AutoVerScheme/basecommonapi.h +++ b/softwareDirectory/AutoVerScheme/basecommonapi.h @@ -1,14 +1,16 @@ #ifndef BASECOMMONAPI_H #define BASECOMMONAPI_H + #include #include #include #include #include "QList" #include "databaseconnection.h" +#include "id\IdWorker.h" -// + //部门任务 class Dept { @@ -379,7 +381,6 @@ QString create_name; QDateTime create_time; QString remark; - QString comm_id; }; @@ -398,7 +399,7 @@ QString manufacture_no; //出厂编号 QString manufacturer; //生产厂家 }; -//检定程序管理-检定配置设备接口查询 +//检定程序管理-检定配置设备通信接口查询 class VerificationProgramConfigInterface { public: @@ -410,13 +411,29 @@ QDateTime create_time; QDateTime update_time; }; +//指令库 +class InstructionLib +{ +public: + QString id; + QString device_id; + QString device_name; + QString instruct_config; + QString instruct_read; + QString type; + QString belong_vername; + QString belong_checkname; + QDateTime create_time; + QDateTime update_time; +}; - +using namespace Jiawa::Core; class BaseCommonApi { public: BaseCommonApi(); + //部门任务 static QList getAllData(); //我的任务 @@ -452,7 +469,7 @@ //判断 哪个 装置表 传参 设备id 返回itemId static QList getJudgeCalibrationTable(QString id); //被检设备管理-检定项目及检定点-多功能校准源 - static QList getVerMultiCal(QString itemId); + static QList getVerMultiCal(QString itemId,QStringList *headList); // 获取检定数据管理 首页 static QList getVerificationData(); // 检定数据管理-检定基本信息 @@ -473,7 +490,33 @@ static QList getVerificationProgramConfig(QString verification_id); //查询设备通信接口 static QList getVerificationProgramConfigInterface(QString deviceId); + //新建检定程序 + static bool InsertVerificationProgramInfo(VerificationProgramInfo verInfo); + //查询指令库 + static QList getInstructionLib(QString device_id); + //新增指令库 + static bool InsertInstructionLib(InstructionLib instructionLib); + //删除指令库 + static bool DeleteInstructionLib(QString device_id); + //更新指令库 + static bool UpdateInstructionLib(InstructionLib instructionLib); + //新增检定配置 + static bool InsertVerificationProgramConfig(VerificationProgramConfig programConfig); + //新增通讯接口 + static bool InsertVerificationProgramConfigInterface(VerificationProgramConfigInterface configInterdace); + //更新检定程序 + static bool UpdateVerificationProgramInfo(VerificationProgramInfo verInfo); + //更新检定配置 + static bool UpdateVerificationProgramConfig(VerificationProgramConfig programConfig); + //更新通讯接口 + static bool UpdateVerificationProgramConfigInterface(VerificationProgramConfigInterface configInterdace); + //删除检定程序 + static bool DeleteVerificationProgramInfo(QString id); + // 雪花id生成工具 + qint64 generateId(); + //雪花算法生成id + IdWorker &idWorker = Singleton::instance(); }; diff --git a/softwareDirectory/AutoVerScheme/id/IdWorker.h b/softwareDirectory/AutoVerScheme/id/IdWorker.h new file mode 100644 index 0000000..079bbcb --- /dev/null +++ b/softwareDirectory/AutoVerScheme/id/IdWorker.h @@ -0,0 +1,218 @@ +#ifndef _JW_CORE_ID_WORKER_H_ +#define _JW_CORE_ID_WORKER_H_ + +#include +#include +#include +#include +#include +#include "Noncopyable.h" +#include "Singleton.h" + +// 如果不使用 mutex, 则开启下面这个定义, 但是我发现, 还是开启 mutex 功能, 速度比较快 +// #define SNOWFLAKE_ID_WORKER_NO_LOCK + +namespace Jiawa { + + /** + * @brief 核心 + * 核心功能 + */ + namespace Core { + + /** + * @brief 分布式id生成类 + * https://segmentfault.com/a/1190000011282426 + * https://github.com/twitter/snowflake/blob/snowflake-2010/src/main/scala/com/twitter/service/snowflake/IdWorker.scala + * + * 64bit id: 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 + * || || || | | | + * |└---------------------------时间戳--------------------------┘└中心-┘└机器-┘ └----序列号----┘ + * | + * 不用 + * SnowFlake的优点: 整体上按照时间自增排序, 并且整个分布式系统内不会产生ID碰撞(由数据中心ID和机器ID作区分), 并且效率较高, 经测试, SnowFlake每秒能够产生26万ID左右. + */ + class SnowflakeIdWorker : private Noncopyable { + + // 实现单例 + friend class Singleton; + + public: + typedef unsigned int UInt; + typedef unsigned long long int UInt64; + +#ifdef SNOWFLAKE_ID_WORKER_NO_LOCK + typedef std::atomic AtomicUInt; + typedef std::atomic AtomicUInt64; +#else + typedef UInt AtomicUInt; + typedef UInt64 AtomicUInt64; +#endif + + void setWorkerId(UInt workerId) { + this->workerId = workerId; + } + + void setDatacenterId(UInt datacenterId) { + this->datacenterId = datacenterId; + } + + UInt64 getId() { + return nextId(); + } + + /** + * 获得下一个ID (该方法是线程安全的) + * + * @return SnowflakeId + */ + UInt64 nextId() { +#ifndef SNOWFLAKE_ID_WORKER_NO_LOCK + std::unique_lock lock{ mutex }; + AtomicUInt64 timestamp{ 0 }; +#else + static AtomicUInt64 timestamp{ 0 }; +#endif + timestamp = timeGen(); + + // 如果当前时间小于上一次ID生成的时间戳, 说明系统时钟回退过这个时候应当抛出异常 + if (timestamp < lastTimestamp) { + std::ostringstream s; + s << "clock moved backwards. Refusing to generate id for " << lastTimestamp - timestamp << " milliseconds"; + throw std::exception(std::runtime_error(s.str())); + } + + if (lastTimestamp == timestamp) { + // 如果是同一时间生成的, 则进行毫秒内序列 + sequence = (sequence + 1) & sequenceMask; + if (0 == sequence) { + // 毫秒内序列溢出, 阻塞到下一个毫秒,获得新的时间戳 + timestamp = tilNextMillis(lastTimestamp); + } + } else { + sequence = 0; + } + +#ifndef SNOWFLAKE_ID_WORKER_NO_LOCK + lastTimestamp = timestamp; +#else + lastTimestamp = timestamp.load(); +#endif + + // 移位并通过或运算拼到一起组成64位的ID + return ((timestamp - twepoch) << timestampLeftShift) + | (datacenterId << datacenterIdShift) + | (workerId << workerIdShift) + | sequence; + } + + protected: + SnowflakeIdWorker() : workerId(0), datacenterId(0), sequence(0), lastTimestamp(0) { } + + /** + * 返回以毫秒为单位的当前时间 + * + * @return 当前时间(毫秒) + */ + UInt64 timeGen() const { + auto t = std::chrono::time_point_cast(std::chrono::high_resolution_clock::now()); + return t.time_since_epoch().count(); + } + + /** + * 阻塞到下一个毫秒, 直到获得新的时间戳 + * + * @param lastTimestamp 上次生成ID的时间截 + * @return 当前时间戳 + */ + UInt64 tilNextMillis(UInt64 lastTimestamp) const { + UInt64 timestamp = timeGen(); + while (timestamp <= lastTimestamp) { + timestamp = timeGen(); + } + return timestamp; + } + + private: + +#ifndef SNOWFLAKE_ID_WORKER_NO_LOCK + std::mutex mutex; +#endif + + /** + * 开始时间截 (2018-01-01 00:00:00.000) + */ + const UInt64 twepoch = 1514736000000; + + /** + * 机器id所占的位数 + */ + const UInt workerIdBits = 5; + + /** + * 数据中心id所占的位数 + */ + const UInt datacenterIdBits = 5; + + /** + * 序列所占的位数 + */ + const UInt sequenceBits = 12; + + /** + * 机器ID向左移12位 + */ + const UInt workerIdShift = sequenceBits; + + /** + * 数据标识id向左移17位 + */ + const UInt datacenterIdShift = workerIdShift + workerIdBits; + + /** + * 时间截向左移22位 + */ + const UInt timestampLeftShift = datacenterIdShift + datacenterIdBits; + + /** + * 支持的最大机器id, 结果是31 + */ + const UInt maxWorkerId = -1 ^ (-1 << workerIdBits); + + /** + * 支持的最大数据中心id, 结果是31 + */ + const UInt maxDatacenterId = -1 ^ (-1 << datacenterIdBits); + + /** + * 生成序列的掩码, 这里为4095 + */ + const UInt sequenceMask = -1 ^ (-1 << sequenceBits); + + /** + * 工作机器id(0~31) + */ + UInt workerId; + + /** + * 数据中心id(0~31) + */ + UInt datacenterId; + + /** + * 毫秒内序列(0~4095) + */ + AtomicUInt sequence{ 0 }; + + /** + * 上次生成ID的时间截 + */ + AtomicUInt64 lastTimestamp{ 0 }; + + }; + + typedef SnowflakeIdWorker IdWorker; + } +} + +#endif // _JW_CORE_ID_WORKER_H_ diff --git a/softwareDirectory/AutoVerScheme/id/Noncopyable.h b/softwareDirectory/AutoVerScheme/id/Noncopyable.h new file mode 100644 index 0000000..4fc555a --- /dev/null +++ b/softwareDirectory/AutoVerScheme/id/Noncopyable.h @@ -0,0 +1,31 @@ +#ifndef _JW_CORE_NONCOPYABLE_H_ +#define _JW_CORE_NONCOPYABLE_H_ + + +// Private copy constructor and copy assignment ensure classes derived from +// class noncopyable cannot be copied. + +namespace Jiawa { + namespace Core { + + // protection from unintended ADL(Argument Dependent Lookup) + namespace Noncopyable_ { + + class Noncopyable + { + protected: + Noncopyable() = default; + ~Noncopyable() = default; + + Noncopyable(const Noncopyable&) = delete; + Noncopyable(const Noncopyable&&) = delete; + Noncopyable& operator=(const Noncopyable&) = delete; + Noncopyable& operator=(const Noncopyable&&) = delete; + }; + } + + typedef Noncopyable_::Noncopyable Noncopyable; + } +} + +#endif // _JW_CORE_NONCOPYABLE_H_ diff --git a/softwareDirectory/AutoVerScheme/id/Singleton.h b/softwareDirectory/AutoVerScheme/id/Singleton.h new file mode 100644 index 0000000..daad45c --- /dev/null +++ b/softwareDirectory/AutoVerScheme/id/Singleton.h @@ -0,0 +1,60 @@ +#ifndef _JW_CORE_SINGLETON_H_ +#define _JW_CORE_SINGLETON_H_ + +namespace Jiawa { + namespace Core { + + // boost/container/detail/singleton.hpp + // http://blog.csdn.net/fullsail/article/details/8483106 + // T must be: no-throw default constructible and no-throw destructible + template + class Singleton { + private: + Singleton() = default; + ~Singleton() = default; + + private: + struct object_creator + { + // This constructor does nothing more than ensure that instance() + // is called before main() begins, thus creating the static + // T object before multithreading race issues can come up. + object_creator() { Singleton::instance(); } + inline void do_nothing() const { } + }; + static object_creator create_object; + + private: + Singleton(const Singleton&) = delete; + Singleton(const Singleton&&) = delete; + Singleton& operator=(const Singleton&) = delete; + Singleton& operator=(const Singleton&&) = delete; + + public: + typedef T object_type; + + // If, at any point (in user code), Singleton::instance() + // is called, then the following function is instantiated. + static object_type& instance() + { + // This is the object that we return a reference to. + // It is guaranteed to be created before main() begins because of + // the next line. + static object_type obj; + + // The following line does nothing else than force the instantiation + // of Singleton::create_object, whose constructor is + // called before main() begins. + create_object.do_nothing(); + + return obj; + } + }; + + template + typename Singleton::object_creator Singleton::create_object; + } +} + +#endif // _JW_CORE_SINGLETON_H_ + diff --git a/softwareDirectory/AutoVerScheme/id/Timer.h b/softwareDirectory/AutoVerScheme/id/Timer.h new file mode 100644 index 0000000..4b2967f --- /dev/null +++ b/softwareDirectory/AutoVerScheme/id/Timer.h @@ -0,0 +1,57 @@ +#ifndef _JW_CORE_TIMER_H_ +#define _JW_CORE_TIMER_H_ + +#include + +namespace Jiawa { + + /** + * @brief 核心 + * 核心功能 + */ + namespace Core { + + /** + * @brief 计时器类 + * 计时 + */ + template + class Timer { + public: + + typedef CLOCK clock; + typedef typename CLOCK::rep rep; + typedef typename CLOCK::duration duration; + typedef typename CLOCK::time_point time_point; + + Timer() : m_start(CLOCK::now()) { + } + + /** + * @brief 重置计时器 + * + * @return 开始的时间点 + */ + time_point reset() { + m_start = CLOCK::now(); + return m_start; + } + + /** + * @brief 得到流逝的时间 + * @param UNIT 返回时间的类型 + * + * @return 返回流逝的时间 + */ + template + typename UNIT::duration::rep elapsed() const { + return std::chrono::duration_cast(CLOCK::now() - m_start).count(); + } + + private: + time_point m_start; + }; + } +} + +#endif // _JW_CORE_TIMER_H_ \ No newline at end of file