111
tjx
2025-11-27 fe9223b1cfa5bac6438afebf4ab6604fd89f3ed2
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
using MES.Service.DB;
using MES.Service.Dto.service;
using MES.Service.Modes;
 
namespace MES.Service.service.Warehouse;
 
public class XbRackingTaskSyxtLogManager : Repository<XbRackingTaskSyxtLog>
{
    /// <summary>
    /// 核验任务单号是否存在
    /// </summary>
    /// <param name="dto">任务完成上报参数</param>
    /// <returns>任务单号是否存在</returns>
    public bool ValidateTaskExists(TaskCompleteReportDto dto)
    {
        if (dto == null || string.IsNullOrWhiteSpace(dto.TaskCode))
        {
            return false;
        }
 
        var task = Context.Queryable<XbRackingTaskSyxtLog>()
            .Where(t => t.TaskCode == dto.TaskCode)
            .Any();
 
        return task;
    }
 
    /// <summary>
    /// 任务完成上报
    /// </summary>
    /// <param name="dto">任务完成上报参数</param>
    /// <returns>统一响应结果</returns>
    public RackingTaskResponse TaskCompleteReport(TaskCompleteReportDto dto)
    {
        if (dto == null)
        {
            return RackingTaskResponse.Fail("参数不能为空");
        }
 
        if (string.IsNullOrWhiteSpace(dto.TaskCode))
        {
            return RackingTaskResponse.Fail("任务号不能为空");
        }
 
        if (string.IsNullOrWhiteSpace(dto.PalletCode))
        {
            return RackingTaskResponse.Fail("托盘编码不能为空");
        }
 
        try
        {
            // 步骤1:验证任务号是否存在
            var task = Context.Queryable<XbRackingTaskSyxtLog>()
                .Where(t => t.TaskCode == dto.TaskCode)
                .First();
 
            if (task == null)
            {
                return RackingTaskResponse.Fail($"任务号[{dto.TaskCode}]不存在");
            }
 
            // 步骤2:验证托盘编码是否匹配
            if (task.PalletCode != dto.PalletCode)
            {
                return RackingTaskResponse.Fail($"托盘编码不匹配,期望[{task.PalletCode}],实际[{dto.PalletCode}]");
            }
 
            // 步骤3:转换库位编码为中文
            string? locCodeChinese = null;
            if (!string.IsNullOrEmpty(dto.LocCode))
            {
                // 验证库位编码格式
                if (dto.LocCode.Length != 6 || !dto.LocCode.All(char.IsDigit))
                {
                    return RackingTaskResponse.Fail("库位编码格式错误,应为6位数字");
                }
 
                // 转换库位编码
                locCodeChinese = ConvertLocCodeToChinese(dto.LocCode);
            }
 
            // 步骤4:更新任务日志表
            var updateResult = Context.Updateable<XbRackingTaskSyxtLog>()
                .SetColumns(t => t.LocCode == dto.LocCode)
                .SetColumns(t => t.LocCodeChinese == locCodeChinese)
                .SetColumns(t => t.Code == "200")
                .SetColumns(t => t.JsonMessage == "任务完成")
                .SetColumns(t => t.CodeMessage == "任务完成")
                .Where(t => t.TaskCode == dto.TaskCode)
                .ExecuteCommand();
 
            if (updateResult <= 0)
            {
                return RackingTaskResponse.Fail("更新任务日志失败");
            }
 
            return RackingTaskResponse.Success();
        }
        catch (Exception ex)
        {
            return RackingTaskResponse.Fail($"更新任务日志失败:{ex.Message}");
        }
    }
 
    /// <summary>
    /// 将库位编码转换为中文描述
    /// </summary>
    /// <param name="locCode">库位编码,格式如"010203"</param>
    /// <returns>中文描述,格式如"1排2层3列"</returns>
    private string ConvertLocCodeToChinese(string locCode)
    {
        // 解析排号(前两位)
        int row = int.Parse(locCode.Substring(0, 2));
        // 解析层号(中间两位)
        int level = int.Parse(locCode.Substring(2, 2));
        // 解析列号(后两位)
        int column = int.Parse(locCode.Substring(4, 2));
 
        return $"{row}排{level}层{column}列";
    }
 
 
    /// <summary>
    /// 异常任务上报
    /// </summary>
    /// <param name="dto">异常任务上报参数</param>
    /// <returns>统一响应结果</returns>
    public RackingTaskResponse TaskErrorReport(TaskErrorReportDto dto)
    {
        if (dto == null)
        {
            return RackingTaskResponse.Fail("参数不能为空");
        }
 
        if (string.IsNullOrWhiteSpace(dto.TaskCode))
        {
            return RackingTaskResponse.Fail("任务号不能为空");
        }
 
        if (string.IsNullOrWhiteSpace(dto.PalletCode))
        {
            return RackingTaskResponse.Fail("托盘编码不能为空");
        }
 
        if (string.IsNullOrWhiteSpace(dto.ErrorMessage))
        {
            return RackingTaskResponse.Fail("错误信息不能为空");
        }
 
        try
        {
            // 步骤1:验证任务号是否存在
            var task = Context.Queryable<XbRackingTaskSyxtLog>()
                .Where(t => t.TaskCode == dto.TaskCode)
                .First();
 
            if (task == null)
            {
                return RackingTaskResponse.Fail($"任务号[{dto.TaskCode}]不存在");
            }
 
            // 步骤2:验证托盘编码是否匹配
            if (task.PalletCode != dto.PalletCode)
            {
                return RackingTaskResponse.Fail($"托盘编码不匹配,期望[{task.PalletCode}],实际[{dto.PalletCode}]");
            }
 
            // 步骤3:更新任务日志表
            var updateResult = Context.Updateable<XbRackingTaskSyxtLog>()
                .SetColumns(t => t.Code == "500")
                .SetColumns(t => t.JsonMessage == dto.ErrorMessage)
                .SetColumns(t => t.CodeMessage == dto.ErrorMessage)
                .Where(t => t.TaskCode == dto.TaskCode)
                .ExecuteCommand();
 
            if (updateResult <= 0)
            {
                return RackingTaskResponse.Fail("更新任务日志失败");
            }
 
            return RackingTaskResponse.Success();
        }
        catch (Exception ex)
        {
            return RackingTaskResponse.Fail($"更新任务日志失败:{ex.Message}");
        }
    }
}