-- METER_WORK_MONITOR memtx space
-- Field order matches Oracle LIMS.METER_WORK_MONITOR (66 cols) + _dirty + _updated_at
-- Run in Tarantool console: dofile('meter_work_monitor.lua')

local SPACE = 'METER_WORK_MONITOR'

if box.space[SPACE] ~= nil then
    box.space[SPACE]:drop()
end

local s = box.schema.space.create(SPACE, {
    engine = 'memtx',
    if_not_exists = true,
    format = {
        { name = 'POINT_NO', type = 'string' },
        { name = 'POINT_NAME', type = 'string', is_nullable = true },
        { name = 'VALID_WGT', type = 'string', is_nullable = true },
        { name = 'VALID_CARNO', type = 'string', is_nullable = true },
        { name = 'VALID_TARE_TIME', type = 'string', is_nullable = true },
        { name = 'VALID_TIME_INTERVAL', type = 'string', is_nullable = true },
        { name = 'VALID_PARK_STATUS', type = 'string', is_nullable = true },
        { name = 'VALID_EXCEED_WGT', type = 'string', is_nullable = true },
        { name = 'VALID_PREDICTION_DIFF', type = 'string', is_nullable = true },
        { name = 'VALID_FLAG', type = 'string', is_nullable = true },
        { name = 'VALID_MAT_TIME', type = 'string', is_nullable = true },
        { name = 'CAR_MONITOR', type = 'string', is_nullable = true },
        { name = 'CAR_MONITOR_LVL', type = 'number', is_nullable = true },
        { name = 'IS_HELP', type = 'string', is_nullable = true },
        { name = 'IS_TAKE_OVER', type = 'string', is_nullable = true },
        { name = 'LED_WRITER', type = 'string', is_nullable = true },
        { name = 'PRINT_NUM', type = 'number', is_nullable = true },
        { name = 'COLLECTION_CODE', type = 'string', is_nullable = true },
        { name = 'IS_OVER_WGT', type = 'string', is_nullable = true },
        { name = 'USER_NO', type = 'string', is_nullable = true },
        { name = 'MONITOR_USER_NO', type = 'string', is_nullable = true },
        { name = 'SET_CAR', type = 'string', is_nullable = true },
        { name = 'LED_KEEP', type = 'string', is_nullable = true },
        { name = 'RFID_CAR', type = 'string', is_nullable = true },
        { name = 'PHOTO_CAR', type = 'string', is_nullable = true },
        { name = 'SCAN_CAR', type = 'string', is_nullable = true },
        { name = 'EDIT_CAR', type = 'string', is_nullable = true },
        { name = 'CAR_ERR', type = 'string', is_nullable = true },
        { name = 'WGT_ERR', type = 'string', is_nullable = true },
        { name = 'LAST_TIME', type = 'number', is_nullable = true },
        { name = 'STOP_OVER_TIME', type = 'string', is_nullable = true },
        { name = 'MSG_INFO', type = 'string', is_nullable = true },
        { name = 'WGT', type = 'number', is_nullable = true },
        { name = 'IS_REMOTE', type = 'string', is_nullable = true },
        { name = 'WEIGH_INSTRUMENT_MODEL', type = 'string', is_nullable = true },
        { name = 'WEIGH_INSTRUMENT_PARAM', type = 'string', is_nullable = true },
        { name = 'MOXA_IP', type = 'string', is_nullable = true },
        { name = 'MOXA_PORT', type = 'string', is_nullable = true },
        { name = 'VALID_LIANDA', type = 'string', is_nullable = true },
        { name = 'IS_OPEN_SCANNING', type = 'string', is_nullable = true },
        { name = 'VALID_IS_PRE', type = 'string', is_nullable = true },
        { name = 'USER_IP', type = 'string', is_nullable = true },
        { name = 'POWER_STATE', type = 'string', is_nullable = true },
        { name = 'DVR_STATE', type = 'string', is_nullable = true },
        { name = 'MOXA_STATE', type = 'string', is_nullable = true },
        { name = 'PLC_STATE', type = 'string', is_nullable = true },
        { name = 'LEFT_INFRARED_RAY_STATE', type = 'string', is_nullable = true },
        { name = 'RIGHT_INFRARED_RAY_STATE', type = 'string', is_nullable = true },
        { name = 'AMPLIFIER_STATE', type = 'string', is_nullable = true },
        { name = 'LAMP_STATE', type = 'string', is_nullable = true },
        { name = 'SPOT_TYPE_NO', type = 'string', is_nullable = true },
        { name = 'RED_GREEN_LIGHT_STATE', type = 'string', is_nullable = true },
        { name = 'FRONT_INFRARED_RAY_STATE', type = 'string', is_nullable = true },
        { name = 'REAR_INFRARED_RAY_STATE', type = 'string', is_nullable = true },
        { name = 'RFID_STATE', type = 'string', is_nullable = true },
        { name = 'VALID_AREA', type = 'string', is_nullable = true },
        { name = 'WEIGHT_STATUS', type = 'string', is_nullable = true },
        { name = 'FERROALLOY_CONFIRM', type = 'string', is_nullable = true },
        { name = 'FERROALLOY_RESULT', type = 'string', is_nullable = true },
        { name = 'ZERO_STATE', type = 'string', is_nullable = true },
        { name = 'ZERO_WEIGHT', type = 'number', is_nullable = true },
        { name = 'IS_ZERO_STATE', type = 'string', is_nullable = true },
        { name = 'IS_VALID_SCAN', type = 'string', is_nullable = true },
        { name = 'IS_ENABLE_SOUND', type = 'string', is_nullable = true },
        { name = 'PHOTO_CAR2', type = 'string', is_nullable = true },
        { name = '_dirty', type = 'boolean', is_nullable = true },
        { name = '_updated_at', type = 'number', is_nullable = true },
    }
})

s:create_index('primary', {
    type = 'hash',
    parts = { 1, 'string' },
    if_not_exists = true,
})

-- optional secondary index for dirty scan
s:create_index('dirty', {
    type = 'tree',
    unique = false,
    parts = { 66, 'boolean' },
    if_not_exists = true,
})

return s
