Tjiny
2025-06-04 9067ccda40f7656ac352711727ce5a8b70382e16
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
using System;
using System.Collections.Generic;
using System.Linq;
using HH.WCS.Mobox3.Template.Entity;
using HH.WCS.Mobox3.Template.Util.Helper;
 
namespace HH.WCS.Mobox3.Template.Controller.Service;
 
/// <summary>
/// 任务
/// </summary>
public static class TaskService
{
    /// <summary>
    /// 创建作业 
    /// </summary>
    /// <returns></returns>
    public static void CreateTask()
    {
        // 1. 查询任务是否已创建
        var operation = AdoSqlHelper<Operation>.QueryFirstByDecs(p => p.N_B_STATE == 0, p => p.T_CREATE);
 
        if (operation == null)
        {
            return;
        }
        
        // 任务存在,根据任务类型做不同的操作
        if (operation.N_TYPE == 1)
        {
            var inputLocation = InputLocation(operation.S_CNTR_CODE);
 
            if (inputLocation == null)
            {
                LogHelper.Info($"当前容器未绑定物料,请检查", "出入库流程日志");
                return;
            }
 
            // 任务
            var task = new Task
            {
                // 作业编码
                S_OP_CODE = operation.S_CODE,
                // 任务号
                S_CODE = HelperMethod.GenerateTaskNo("任务号", "TA"),
                // 任务类型
                N_TYPE = operation.N_TYPE,
                // 任务类型
                S_TYPE = Task.GetStateType(operation.N_TYPE),
                // 起点货位
                S_START_LOC = operation.S_START_LOC,
                // 终点货位
                S_END_LOC = inputLocation.S_CODE,
                // 容器编码
                S_CNTR_CODE = operation.S_CNTR_CODE,
            };
 
            // 修改作业状态为执行中
            operation.N_B_STATE = 1;
            operation.S_B_STATE = "执行";
            operation.T_START_TIME = DateTime.Now;
 
            var querySqlSugarClient = AdoSqlHelper<object>.QuerySqlSugarClient();
 
            try
            {
                querySqlSugarClient.BeginTran();
                AdoSqlHelper<Task>.AddFirstTran(querySqlSugarClient, task);
                AdoSqlHelper<Operation>.UpdateFirstTran(querySqlSugarClient, operation,
                    p => new { p.N_B_STATE, p.S_B_STATE, p.T_START_TIME });
 
                querySqlSugarClient.CommitTran();
            }
            catch (Exception e)
            {
                if (operation.N_TYPE == 1)
                {
                    LogHelper.Info($"作业号为:{operation.S_CODE}的任务创建失败", "出入库流程日志");
                }
                else
                {
                    LogHelper.Info($"作业号为:{operation.S_CODE}的任务创建失败", "出入库流程日志");
                }
                querySqlSugarClient.RollbackTran();
            }
        }
    }
 
    /// <summary>
    /// 入库货位分配
    /// </summary>
    /// <returns></returns>
    private static Location InputLocation(string container)
    {
        var cntrItemDetail = AdoSqlHelper<CntrItemDetail>.QueryFirst(p => p.S_CNTR_CODE == container);
 
        if (cntrItemDetail == null)
        {
            return null;
        }
        
        return AdoSqlHelper<Location>.QueryInputLocation(cntrItemDetail.S_ITEM_CODE);
    }
}