分支自 SuZhouGuanHong/TaiYuanTaiZhong

dengjunjie
2024-07-03 a05b17c400067475a256071c2e92a9f1b3abe343
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
/*
 *所有关于dt_pattern类的业务代码应在此处编写
*可使用repository.调用常用方法,获取EF/Dapper等信息
*如果需要事务请使用repository.DbContextBeginTransaction
*也可使用DBServerProvider.手动获取数据库相关信息
*用户信息、权限、角色等使用UserContext.Current操作
*dt_patternService对增、删、改查、导入、导出、审核业务代码扩展参照ServiceFunFilter
*/
using WIDESEA_Core.BaseProvider;
using WIDESEA_Core.Extensions.AutofacManager;
using WIDESEA_Entity.DomainModels;
using System.Linq;
using WIDESEA_Core.Utilities;
using System.Linq.Expressions;
using WIDESEA_Core.Extensions;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Http;
using WIDESEA_WCS.IRepositories;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Newtonsoft.Json;
using WIDESEA_Core.EFDbContext;
using WIDESEA_WCS.Repositories;
using WIDESEA_Comm;
using WIDESEA_WCS.WCSClient;
using System.Xml.Linq;
using OfficeOpenXml.FormulaParsing.Excel.Functions.Math;
 
namespace WIDESEA_WCS.Services
{
    public partial class dt_patternService
    {
        private readonly IHttpContextAccessor _httpContextAccessor;
        private readonly Idt_patternRepository _repository;//访问数据库
 
        [ActivatorUtilitiesConstructor]
        public dt_patternService(
            Idt_patternRepository dbRepository,
            IHttpContextAccessor httpContextAccessor
            )
        : base(dbRepository)
        {
            _httpContextAccessor = httpContextAccessor;
            _repository = dbRepository;
            //多租户会用到这init代码,其他情况可以不用
            //base.Init(dbRepository);
        }
        /// <summary>
        /// 模式1
        /// </summary>
        /// <param name="v"></param>
        /// <returns></returns>
        public WebResponseContent UpdateAuto(string v)
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                var model = JsonConvert.DeserializeObject<dt_pattern>(v);
                if (model.pattern_state == 1) throw new Exception("当前就是模式1");
                var Gantry_client = PLCClient.Clients.FirstOrDefault(t => t.PLCName == "桁架");
                if (Gantry_client == null) throw new Exception("桁架调度服务未开启!");
                if (!Gantry_client.IsConnected) throw new Exception("与桁架连接超时!");
                VOLContext context = new VOLContext();
                Idt_stationinfoRepository stationinfoRepository = new dt_stationinfoRepository(context);
                var stations = stationinfoRepository.Find(x => x.stationCode == "X01001001" || x.stationCode == "X01001002" || x.stationCode == "X02001001" || x.stationCode == "X02001002").ToList();
                if (stations.Where(x => x.tray_status == TrayStateEnum.StrogeTray.ToString()).Any())
                    throw new Exception("设置为模式1失败!下料位存在车轮!");
 
 
                #region 写入信号告诉桁架切换模式
                var ok = Gantry_client.ReadByOrder<bool>("o_enable_change_mode", model.pattern_name);
                #endregion
                if (ok)
                {
                    Gantry_client.WriteByOrder("i_mode", (Int16)1, model.pattern_name);
                    model.pattern_state = 1;
                    var cont = _repository.Update(model, true);
                    if (cont > 0)
                        content.OK(model.pattern_name + "设置为模式1成功!");
                    else
                        throw new Exception(model.pattern_name + "设置为模式2失败!");
                    foreach (var stat in stations)
                    {
                        stat.enable = true;
                    }
                    stationinfoRepository.UpdateRange(stations, x => new { x.enable }, true);
                }
                else
                    content.Error(model.pattern_name + "设置为模式1失败!桁架未允许设置为模式1");
 
            }
            catch (Exception ex)
            {
                content.Error(ex.Message);
            }
            return content;
        }
        /// <summary>
        /// 模式2
        /// </summary>
        /// <param name="v"></param>
        /// <returns></returns>
        public WebResponseContent UpdateManual(string v)
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                var model = JsonConvert.DeserializeObject<dt_pattern>(v);
                if (model.pattern_state == 0) throw new Exception("当前就是模式2");
                var Gantry_client = PLCClient.Clients.FirstOrDefault(t => t.PLCName == "桁架");
                if (Gantry_client == null) throw new Exception("桁架调度服务未开启!");
                if (!Gantry_client.IsConnected) throw new Exception("与桁架连接超时!");
                VOLContext context = new VOLContext();
                Idt_stationinfoRepository stationinfoRepository = new dt_stationinfoRepository(context);
                var stations = stationinfoRepository.Find(x => x.stationCode == "X01001001" || x.stationCode == "X01001002" || x.stationCode == "X02001001" || x.stationCode == "X02001002").ToList();
                if (stations.Where(x => x.tray_status == TrayStateEnum.StrogeTray.ToString()).Any())
                    throw new Exception("设置为模式2失败!下料位存在车轮!");
                #region 写入信号告诉桁架切换模式
                var ok = Gantry_client.ReadByOrder<bool>("o_enable_change_mode", model.pattern_name);
                #endregion
                if (ok)
                {
                    Gantry_client.WriteByOrder("i_mode", (Int16)2, model.pattern_name);
                    model.pattern_state = 0;
                    var cont = _repository.Update(model, true);
                    if (cont > 0)
                        content.OK(model.pattern_name + "设置为模式2成功!");
                    else
                        throw new Exception(model.pattern_name + "设置为模式2失败!");
                    foreach (var stat in stations)
                    {
                        stat.enable = false;
                    }
                    stationinfoRepository.UpdateRange(stations, x => new { x.enable }, true);
                }
                else
                    content.Error(model.pattern_name + "设置为模式2失败!桁架未允许设置为模式2");
 
            }
            catch (Exception ex)
            {
                content.Error(ex.Message);
            }
            return content;
        }
    }
}