Tjiny
2025-05-19 4c8b2eea64bcd0ae14602a2a6e993b2d04c80ee0
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
using System;
using HH.WCS.Mobox3.RiDong.dto;
using HH.WCS.Mobox3.RiDong.generalMethod;
using HH.WCS.Mobox3.RiDong.models;
using HH.WCS.Mobox3.RiDong.util;
using HH.WCS.Mobox3.RiDong.wms;
 
namespace HH.WCS.Mobox3.RiDong.apiMethod;
 
/// <summary>
/// 综合性方法类
/// </summary>
public static class SynthesizeService
{
    /// <summary>
    /// 创建作业
    /// <remarks>存在中转货位(入库、出库、盘点入、盘点出)</remarks>
    /// </summary>
    /// <param name="input">作业模型</param>
    /// <returns></returns>
    public static ApiModel.SimpleResult AddOperation(OperationDto input)
    {
        var result = new ApiModel.SimpleResult();
 
        int group = 0;
 
        // 查询当前使用的容器
        var container = AdoSqlMethod<Container>.QueryFirst(p => p.S_CODE == input.cntrCode);
 
        // 判断当前容器是否存在
        if (container == null)
        {
            result.resultCode = -1;
            result.resultMsg = "当前容器不存在";
            Console.WriteLine("当前容器不存在");
            return result;
        }
 
        LogHelper.Info($"托盘{input.cntrCode}生成任务,该托盘的类型为{container.S_TYPE}");
 
        // 判断当前托盘是否有未完成的任务
        if (AdoSqlMethod<Operation>.QueryCount(p => p.S_CNTR_CODE == input.cntrCode && p.N_B_STATE < 2) > 0)
        {
            result.resultCode = -1;
            result.resultMsg = "当前容器已存在任务,请勿重复下发任务";
            Console.WriteLine("当前容器已存在任务,请勿重复下发任务");
            return result;
        }
 
        // 如果是入库,则需要计算入库的货位数据
        if (input.taskType == 1 || input.taskType == 3)
        {
            // 判断是否有容器货位表
            if (AdoSqlMethod<LocCntrRel>.QueryCount(p => p.S_CNTR_CODE == input.cntrCode) > 0)
            {
                result.resultCode = -1;
                result.resultMsg = "当前容器已在立库,请勿重复下发任务";
                Console.WriteLine("当前容器已在立库,请勿重复下发任务");
                return result;
            }
 
            // 根据容器类型以及容器对应的库区找到对应的货位信息
            var location = LocationMethod.QueryStereoscopicStorehouseLocation(container.S_TYPE);
 
            if (location == null)
            {
                result.resultCode = -1;
                result.resultMsg = "无可用货位";
                Console.WriteLine("无可用货位");
                return result;
            }
 
            // 重新计算终点、赋值
            input.endLocation = location.S_CODE;
            group = location.N_ROADWAY;
        }
        else
        {
            var location = AdoSqlMethod<Location>.QueryFirst(p=>p.S_CODE == input.startLocation);
            group = location.N_ROADWAY;
        }
 
        // 获取中转货位
        var queryRandomLocation = LocationMethod.QueryLocation(input);
 
        // 为空
        if (queryRandomLocation == null)
        {
            result.resultCode = -1;
            result.resultMsg = "输送线中转接驳位获取失败,请检查是否配置";
            Console.WriteLine("输送线中转接驳位获取失败,请检查是否配置");
            return result;
        }
 
        // 创建任务,不需要进行货位管控
        var operation = new Operation
        {
            // 任务号
            S_CODE = HelperMethod.GenerateTaskNo("作业号", "OP"),
            // 任务类型
            N_TYPE = input.taskType,
            // 任务类型
            S_TYPE = Operation.GetTypeStr(input.taskType),
            // 起点货位
            S_START_LOC = input.startLocation,
            // 起点库区编码
            S_START_AREA = Operation.GetArea(input.startLocation),
            // 拓展字段(中转货位)
            S_EXT_ATTR1 = queryRandomLocation.S_CODE,
            // 终点货位
            S_END_LOC = input.endLocation,
            // 终点库区编码
            S_END_AREA = Operation.GetArea(input.endLocation),
            // 容器编码
            S_CNTR_CODE = input.cntrCode,
            // 作业定义名称
            S_OP_DEF_NAME = Operation.GetTypeStr(input.taskType),
            // 盘点单号
            S_COUNT_NO = input.odd,
            // 拓展字段(任务组)
            S_EXT_ATTR2 = Operation.GetGroup(group),
            // 巷道
            N_ROADWAY = group
        };
 
        // 实现事务
        var sqlSugarClient = AdoSqlMethod<object>.QuerySqlSugarClient();
 
        try
        {
            sqlSugarClient.BeginTran();
            if (AdoSqlMethod<Operation>.AddFirstTran(sqlSugarClient, operation))
            {
                if (operation.N_TYPE == 1 || operation.N_TYPE == 3)
                {
                    // 货位需要上锁
                    LocationHelper.LockLoc(operation.S_END_LOC, 1);
                }
                else if (operation.N_TYPE == 4)
                {
                    // 货位需要上锁
                    LocationHelper.LockLoc(operation.S_START_LOC, 2);
 
                    container.C_ENABLE = 'N';
                    container.N_LOCK_STATE = 4;
                    AdoSqlMethod<Container>.UpdateFirst(container, p => new { p.C_ENABLE, p.N_LOCK_STATE });
                }
                else
                {
                    // 货位需要上锁
                    LocationHelper.LockLoc(operation.S_START_LOC, 2);
 
                    container.C_ENABLE = 'N';
                    AdoSqlMethod<Container>.UpdateFirst(container, p => new { p.C_ENABLE });
                }
 
                result.resultCode = 0;
                result.resultMsg = "作业创建成功";
            }
            else
            {
                result.resultCode = -1;
                result.resultMsg = "作业创建失败";
            }
 
            sqlSugarClient.CommitTran();
 
            return result;
        }
        catch (Exception e)
        {
            sqlSugarClient.RollbackTran();
            Console.WriteLine(e);
            throw;
        }
    }
}