lzh
2025-06-19 3a6436e0c88042c6ce8dca2fe8adb0109f0ad9e4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
--[[
 编码: GT-40-21
 名称: 作业启动
 作者:
 入口函数:OperationStart
 功能说明: 粉料入库,作业启动脚本里创建一段任务输送线从工位到3楼出库口的作业
 变更历史:
 --]]
wms_op = require("wms_operation")
wms_wh = require("wms_wh")
wms_task = require("wms_task")
require("GT-Base")
require("GT_InAndOutboundPolicies")
function OperationStart(strLuaDEID)
    local nRet, strRetInfo, strErr
    -- 获取作业对象, 从作业对象中获取{扩展数据}, 从{扩展数据}获取线体设备编码
    local operation
    nRet, operation = m3.GetSysCurEditDataObj(strLuaDEID, "Operation")
    if (nRet ~= 0) then
        lua.Error(strLuaDEID, debug.getinfo(1), "获取作业对象属性失败!" .. operation)
    end
    lua.Debug(strLuaDEID, debug.getinfo(1), '粉料入库operation参数!', operation)
 
    -- 解析作业中的扩展参数
    local ext_data, success
    success, ext_data = pcall(json.decode, operation.ext_data)
    if (success == false) then
        lua.Error(strLuaDEID, debug.getinfo(1), "operation_obj.ext_data 中内容JSON格式不合法!")
    end
    lua.Debug(strLuaDEID, debug.getinfo(1), '胶料入库作业扩展参数!', ext_data)
 
    -- 查询物料类型维护表获取物料所属类型
    local strCondition
    local item_type = ext_data.item_type -- 物料类型
    if (item_type == nil or item_type == '') then lua.Error(strLuaDEID, debug.getinfo(1), "物料类型不能为空!") end
 
    -- 通过终点判断接驳口
    local loc_code
    if (operation.end_area_code == 'HWPFL' or operation.end_area_code == 'PFL') then
        loc_code = "THREE-BGCKK-QY"
    else
        loc_code = "THREE-QGCKK-QY"
    end
 
    -- 获取终点信息
    local store_loc
    nRet, store_loc = wms_wh.GetLocInfo(loc_code)
    if (nRet ~= 0) then lua.Error(strLuaDEID, debug.getinfo(1), 'WMS_GetLocInfo失败!' .. store_loc) end
 
    -- V1.2 判断该巷道的任务是否超过阙值,如果超过则将作业设置为等待状态并将错误信息设置到页面
    -- 获取某种调度类型的任务数量
    strCondition = "N_SCHEDULE_TYPE = 3 AND ( N_B_STATE = " .. wms_base.Get_nConst(strLuaDEID, "任务状态-已推送")
    strCondition = strCondition ..
        " OR N_B_STATE = " .. wms_base.Get_nConst(strLuaDEID, "任务状态-执行") .. ") AND N_ROADWAY =" .. store_loc.roadway
    nRet, strRetInfo = mobox.getDataObjCount(strLuaDEID, "Task", strCondition)
    if (nRet ~= 0) then lua.Error(strLuaDEID, debug.getinfo(1), strRetInfo) end
    if (tonumber(strRetInfo) > tonumber(wms_base.Get_sConst(strLuaDEID, "Task-最大任务数"))) then
        local msg
        msg = "作业编码为=" .. operation.code .. " 创建任务失败, 巷道" .. store_loc.roadway .. "的任务数量已超过常量最大任务数!"
        lua.Warning(strLuaDEID, debug.getinfo(1), msg)
        lua.Wait(strLuaDEID, msg)
        return
    end
 
    -- 一楼粉料入库可以使用3,4,5,6巷道的堆垛机,查询任务最少的巷道分配
    local condition = "N_ROADWAY IN (SELECT N_ROADWAY FROM TN_Roadway WHERE N_ROADWAY IN(" ..
        wms_base.Get_sConst(strLuaDEID, "佳通-一楼粉料入库可分配堆垛机") .. ") AND N_LOCK_STATE = 0)"
    local tunnel_no
    nRet, tunnel_no = LeastTaskRoadway(strLuaDEID, wms_base.Get_sConst(strLuaDEID, "佳通-一楼粉料入库可分配堆垛机"))
    if (nRet ~= 0) then lua.Error(strLuaDEID, debug.getinfo(1), "获取任务数最少的巷道失败! " .. tunnel_no) end
 
 
    -- 创建一段 工位到三楼出库口 的搬运任务
    -- 一段任务不用加锁
    local task = m3.AllocObject(strLuaDEID, "Task")
    -- 创建输送机搬运任务
    task.op_code = operation.code    -- 作业编码
    task.op_name = "粉料入库"
    task.factory = operation.factory -- 工厂
    task.type = wms_base.Get_nConst(strLuaDEID, "任务类型-立库入库搬运")
    task.cntr_code = operation.cntr_code
    -- 起点
    task.start_wh_code = operation.start_wh_code
    task.start_area_code = operation.start_area_code
    task.start_loc_code = operation.start_loc_code
    -- 终点
    task.end_wh_code = store_loc.wh_code
    task.end_area_code = store_loc.area_code
    task.end_loc_code = store_loc.code
    task.schedule_type = wms_base.Get_nConst(strLuaDEID, "调度类型-国自") -- 设置调度类型
    task.roadway = tunnel_no -- 添加巷道
    nRet, task = m3.CreateDataObj(strLuaDEID, task)
    if (nRet ~= 0) then lua.Error(strLuaDEID, debug.getinfo(1), '创建【任务】失败!' .. task) end
 
    lua.Debug(strLuaDEID, debug.getinfo(1), 'task', task)
 
    -- 通过容器获取物料信息
    local cg_detail_list, cg_detail
    nRet, cg_detail_list = wms_cntr.Get_Container_Goods(strLuaDEID, task.cntr_code)
    nRet, cg_detail = m3.ObjAttrStrToLuaObj("CG_Detail", lua.table2str(cg_detail_list[1].attrs))
    if (nRet ~= 0) then lua.Error(strLuaDEID, debug.getinfo(1), cg_detail) end
 
    -- 获取起点绑定的WCS站点
    condition = "S_VALUE = '" .. task.start_loc_code .. "' AND S_NOTE LIKE '%入库站台%'"
    nRet, strRetInfo = m3.GetDataObjByCondition(strLuaDEID, "WMS_Const", condition)
    if (nRet ~= 0) then lua.Error(strLuaDEID, debug.getinfo(1), "获取【常量】信息失败! " .. strRetInfo) end
    local start_zd = strRetInfo.name
    -- 获取终点站点
    condition = "S_VALUE = '" .. task.end_loc_code .. "'"
    nRet, strRetInfo = m3.GetDataObjByCondition(strLuaDEID, "WMS_Const", condition)
    if (nRet ~= 0) then lua.Error(strLuaDEID, debug.getinfo(1), "获取【常量】信息失败! " .. strRetInfo) end
    local end_zd = strRetInfo.name
    -- 拼接数据下发给WCS
    -- 调用国自的任务下发接口
    local strCode = lua.guid() -- 生产一个GUID字符串
    local str_day_time = os.date("%Y-%m-%d %H:%M:%S")
    local url = wms_base.Get_sConst(strLuaDEID, "WCS-url")
    local strurl = url .. "/create"
    local strHeader = ""
    local strBody = {}
    local data = {
        req_no = strCode,
        task_type = 5, -- 1=货物入库;2=货物出库;3=托盘组入库;4=托盘组出库;5=移动(不过库位);6=移库
        task_no = task.code,
        tunnel_no = tunnel_no,
        from_pos = start_zd,
        to_pos = end_zd,
        mat_code = task.cntr_code,
        mat_type = cg_detail.item_code,
        mat_memo = cg_detail.item_name,
        req_time = str_day_time,
        mat_size = 3 -- 例:1,2,3 空托胶料为1 钢丝物料为2 粉料为3
    }
    strBody[1] = data
    lua.Debug(strLuaDEID, debug.getinfo(1), 'strBody', strBody)
    nRet, strRetInfo = CreateInterfaceExc(strLuaDEID, strurl, strHeader, strBody, "WCS", "任务创建")
    if (nRet ~= 0) then
        lua.Error(strLuaDEID, debug.getinfo(1), "调用WCS接口失败!" .. strRetInfo)
    end
 
    -- 设置状态未推送
    wms_task.SetStateByCode(strLuaDEID, task.code, "任务状态-已推送")
end