SendBendsInfoService.cs 8.79 KB
using Autofac.Core;
using Hh.Mes.Common.Json;
using Hh.Mes.Common.log;
using Hh.Mes.POJO.ApiEntity;
using Hh.Mes.POJO.Entity;
using Hh.Mes.POJO.EnumEntitys;
using Hh.Mes.Service.Repository;
using MySqlX.XDevAPI.Common;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace Hh.Mes.Service.QuartzJobService
{
    public class SendBendsInfoService : RepositorySqlSugar<bus_pipebends_job>
    {
        public void Execute()
        {
            List<bus_pipebends_job> bends = Context.Queryable<bus_pipebends_job>().Where(x => x.state == "0").Take(30).ToList();  //初始的和未发送成功的均需要一起发送

            if (bends != null && bends.Count > 0)
            {
                Log4NetHelper.Instance.Info("开始发送弯管数据,数量:"+ bends.Count);
                PDAService service = new PDAService(null);
                //获取工位
                List<base_work_station> stations = Context.Queryable<base_work_station>().Where(x => x.workStationCode.Contains("BentPipe") && !string.IsNullOrEmpty(x.monitorIP)).ToList();

                foreach (bus_pipebends_job pipe in bends)
                {
                    try
                    {
                        pipe.state = "1"; 
                        pipe.updateTime = DateTime.Now;
                        pipe.msg = "";
                        List<base_work_station> stations2 = stations.Where(x => x.lineCode.ToUpper() == pipe.lineCode.ToUpper()).ToList();
                        if (stations2.Count == 0)
                        {
                            pipe.state = "3";
                            pipe.msg += $" 处理失败,暂未配置弯管下发ip端口";
                        }
                        foreach (base_work_station station in stations2)
                        {
                            POJO.Response.Response result = service.InBentPipeTask(pipe.workPieceNo, station.workStationCode);
                            if (result.Code == 200)
                            {
                                pipe.msg += $"{station.workStationCode} 处理成功";
                            }
                            else
                            {
                                pipe.state = "2";
                                pipe.msg += $"{station.workStationCode} 处理失败,错误信息{result.Message}";
                            }
                        }
                        if (pipe.msg.Length> 254)
                        {
                            pipe.msg = pipe.msg.Substring(0, 254);
                        }
                        Context.Updateable(pipe).UpdateColumns(t => new { t.state, t.updateby, t.updateTime, t.msg }).ExecuteCommand();
                    }
                    catch (Exception x)
                    {
                        pipe.msg += x.Message;
                        if (pipe.msg.Length > 254)
                        {
                            pipe.msg = pipe.msg.Substring(0, 254);
                        }
                        pipe.state = "2";
                        Context.Updateable(pipe).UpdateColumns(t => new { t.state, t.updateby, t.updateTime, t.msg }).ExecuteCommand();
                    }
                }

            }

            List<bus_pipebends_job> bends1 = Context.Queryable<bus_pipebends_job>().Where(x => x.state == "2").Take(30).ToList();  //初始的和未发送成功的均需要一起发送

            if (bends1 != null && bends1.Count > 0)
            {
                Log4NetHelper.Instance.Info("开始再次发送弯管数据,数量:" + bends1.Count);
                PDAService service = new PDAService(null);
                //获取工位
                List<base_work_station> stations = Context.Queryable<base_work_station>().Where(x => x.workStationCode.Contains("BentPipe") && !string.IsNullOrEmpty(x.monitorIP)).ToList();

                foreach (bus_pipebends_job pipe in bends1)
                {
                    try
                    {
                        pipe.state = "1";
                        pipe.updateTime = DateTime.Now;
                        pipe.msg = "";
                        List<base_work_station> stations2 = stations.Where(x => x.lineCode.ToUpper() == pipe.lineCode.ToUpper()).ToList();
                        if (stations2.Count == 0) {
                            pipe.state = "3";
                            pipe.msg += $" 处理失败,暂未配置弯管下发ip端口";
                        }
                        foreach (base_work_station station in stations2)
                        {
                            POJO.Response.Response result = service.InBentPipeTask(pipe.workPieceNo, station.workStationCode);
                            if (result.Code == 200)
                            {
                                pipe.msg += $"{station.workStationCode} 处理成功";
                            }
                            else
                            {
                                pipe.state = "2";
                                pipe.msg += $"{station.workStationCode} 处理失败,错误信息{result.Message}";
                            }
                        }
                        if (pipe.msg.Length > 254)
                        {
                            pipe.msg = pipe.msg.Substring(0, 254);
                        }
                        Context.Updateable(pipe).UpdateColumns(t => new { t.state, t.updateby, t.updateTime, t.msg }).ExecuteCommand();
                    }
                    catch (Exception x)
                    {
                        pipe.msg += x.Message;
                        if (pipe.msg.Length > 254)
                        {
                            pipe.msg = pipe.msg.Substring(0, 254);
                        }
                        pipe.state = "2";
                        Context.Updateable(pipe).UpdateColumns(t => new { t.state, t.updateby, t.updateTime, t.msg }).ExecuteCommand();
                    }
                }

            }


            List<bus_pipebends_job> bends2 = Context.Queryable<bus_pipebends_job>().Where(x => x.state == "3").Take(30).ToList();  //初始的和未发送成功的均需要一起发送

            if (bends2 != null && bends2.Count > 0)
            {
                Log4NetHelper.Instance.Info("开始检验配置发送弯管数据,数量:" + bends2.Count);
                PDAService service = new PDAService(null);
                //获取工位
                List<base_work_station> stations = Context.Queryable<base_work_station>().Where(x => x.workStationCode.Contains("BentPipe") && !string.IsNullOrEmpty(x.monitorIP)).ToList();

                foreach (bus_pipebends_job pipe in bends2)
                {
                    try
                    {
                        pipe.state = "1";
                        pipe.updateTime = DateTime.Now;
                        pipe.msg = "";
                        List<base_work_station> stations2 = stations.Where(x => x.lineCode.ToUpper() == pipe.lineCode.ToUpper()).ToList();
                        if (stations2.Count == 0)
                        {
                            pipe.state = "3";
                            pipe.msg += $" 处理失败,暂未配置弯管下发ip端口";
                        }
                        foreach (base_work_station station in stations2)
                        {
                            POJO.Response.Response result = service.InBentPipeTask(pipe.workPieceNo, station.workStationCode);
                            if (result.Code == 200)
                            {
                                pipe.msg += $"{station.workStationCode} 处理成功";
                            }
                            else
                            {
                                pipe.state = "2";
                                pipe.msg += $"{station.workStationCode} 处理失败,错误信息{result.Message}";
                            }
                        }
                        if (pipe.msg.Length > 254)
                        {
                            pipe.msg = pipe.msg.Substring(0, 254);
                        }
                        Context.Updateable(pipe).UpdateColumns(t => new { t.state, t.updateby, t.updateTime, t.msg }).ExecuteCommand();
                    }
                    catch (Exception x)
                    {
                        pipe.msg += x.Message;
                        if (pipe.msg.Length > 254)
                        {
                            pipe.msg = pipe.msg.Substring(0, 254);
                        }
                        pipe.state = "2";
                        Context.Updateable(pipe).UpdateColumns(t => new { t.state, t.updateby, t.updateTime, t.msg }).ExecuteCommand();
                    }
                }

            }
        }
    }
}