using Cksoft.Data; using Cksoft.Data.Repository; using Cksoft.Unity; using DllEapEntity.Dtos; using DllEapEntity.RA; using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace DllEapDal.RA { public class MacrecipeDal { IDatabase Db; private ILogger logger; public MacrecipeDal(IDatabase db) { Db = db; logger = AppConfigurtaionServices.MyLog; } public IEnumerable get(MachineInfo m, int pageIndex, int pageSize, out int total) { string sql = " "; if (m.ID > 0) { sql += $@" and a.id='{m.ID}'"; } if (!string.IsNullOrEmpty(m.Factory)) sql += $@" and a.factory='{m.Factory}'"; if (!string.IsNullOrEmpty(m.FCode)) sql += $@" and a.FCode like '%{m.FCode}%'"; if (!string.IsNullOrEmpty(m.MCode)) sql += $@" and a.MCode='{m.MCode}'"; if (!string.IsNullOrEmpty(m.IPAdress)) sql += $@" and a.IPAdress='{m.IPAdress}'"; if (!string.IsNullOrEmpty(m.MacNum)) sql += $@" and a.MacNum like '%{m.MacNum}%'"; if (!string.IsNullOrEmpty(m.SName)) sql += $@" and b.SName='{m.SName}'"; if (!string.IsNullOrEmpty(m.FName)) sql += $@" and b.FName='{m.FName}'"; if (!string.IsNullOrEmpty(m.Model)) sql += $@" and b.Model='{m.Model}'"; if (!string.IsNullOrEmpty(m.Status)) sql += $@" and c.status='{m.Status}'"; string s = $@"limit {(pageIndex - 1) * pageSize},{pageSize}"; string ss = m.GetQueryTabSql(); string a = "select count(1) from" + ss + " where 1=1" + sql; total = Convert.ToInt32(Db.FindObject(a)); string error = ""; var data = Db.FindListForCondition(sql+ " ORDER by a.FCode " + s , ref error); var modeCodes = data.Where(c => !string.IsNullOrEmpty(c.ModeCode)).Select(c => c.ModeCode).Distinct(); List staff = new List(); if (modeCodes != null) { var sss = string.Join(",", modeCodes.Select(c => $"'{c}'")); if (sss.Any()) { IDatabase db= DbFactory.Base("eapslave"); staff = db.FindList($@"SELECT FCode,FName from staff where FCode in ({sss})").ToList(); } } var dic = new Dictionary(); dic.Add("-1", "离线"); dic.Add("0", "停机"); dic.Add("1", "运转"); dic.Add("2", "预约"); foreach (var item in data) { if (staff != null) { item.ModeCode = staff.FirstOrDefault(c => c.FCode == item.ModeCode)?.FName; } switch ((item.Status)) { case "-1": item.Statusname = "离线"; break; case "0": item.Statusname = "停机"; break; case "1": item.Statusname = "运转"; break; case "2": item.Statusname = "预约"; break; default: item.Statusname = "暂无"; break; } } return data; } public string Add(MachineInfo m) { try { Db.BeginTrans(); int id = Supplier(m.SName, m.FName, m.Model, m.ModeCode); if (id > 0) { var ent = Db.FindList($"select * from machine where ipadress='{m.IPAdress}'").FirstOrDefault(); if (ent != null) { return "该IP地址与" + ent.FCode + "重复"; } var e = Db.FindList($"select * from machine where fcode='{m.FCode}'").FirstOrDefault(); if (e != null) { return "该设备ID已存在"; } var en = Db.FindList($"select * from machine where mcode='{m.MCode}'").FirstOrDefault(); if (e != null) { return "该机身码与" + en.FCode + "重复"; } Machine mac = new Machine() { FCode = m.FCode, Factory = m.Factory, IPAdress = m.IPAdress, SupplierID = id, MCode = m.MCode, Port = m.Port, Remark = m.Remark, ModeCode = m.ModeCode, ModeTime = DateTime.Now, RecCode = m.ModeCode, RecTime = DateTime.Now, MacNum = m.MacNum }; if (Db.InsertFor(mac, m.ModeCode) > 0) { Db.Commit(); Task.Factory.StartNew(() => { GetService(mac.FCode,mac.ModeCode); }); return " 数据新增成功"; } } return "厂商数据处理失败"; } catch (Exception ex) { return ex.ToString(); } } public IEnumerable> GetModel() { string sql = $@" SELECT DISTINCT(b.Model) as model FROM machine a left JOIN supplier b on a.SupplierID = b.id where 1=1"; var data = Db.FindList(sql); var dto =new List>(); dto.Add(new SelectDto() { Label = "全部", Value = null }); foreach (var item in data) { dto.Add(new SelectDto() { Label = item.Model, Value = item.Model }); } return dto; } public bool ReConnect(int ID, string fcode) { var data = Db.FindList($@"select * from machine where id={ID}").FirstOrDefault(); if (data != null) { return GetService(data.FCode, fcode); } else { return false; } } public string delete(int id, string fcode) { string er = ""; var x = Db.FindListForCondition($@" and id={id}", ref er).FirstOrDefault(); StopService(x.FCode,fcode); if (Db.DeleteForCondition($@" and id={id}") > 0) { logger.LogError(fcode + "删除了" + x.FCode + DateTime.Now); return "删除成功"; } else { Task.Factory.StartNew(() => { GetService(x.FCode,fcode); }); } return "删除失败"; } private int Supplier(string SName, string FName, string Model, string ModeCode) { try { string sql = " "; if (!string.IsNullOrEmpty(SName)) sql += $@" and SName='{SName}'"; if (!string.IsNullOrEmpty(FName)) sql += $@" and FName='{FName}'"; if (!string.IsNullOrEmpty(Model)) sql += $@" and Model='{Model}'"; int id = 0; string error = ""; Supplier s = Db.FindListForCondition(sql, ref error).FirstOrDefault(); if (s == null) { Supplier su = new Supplier() { SName = SName, Model = Model, FName = FName }; if (Db.InsertFor(su, ModeCode) > 0) { var a = Db.FindObject($@"Select @@identity"); id = Convert.ToInt32(a); } } else { id = s.ID; } return id; } catch (Exception ex) { Console.WriteLine(ex.ToString()); return 0; } } public string Update(MachineInfo m) { try { Db.BeginTrans(); string sql = $@" and id='{m.ID}'"; string error = ""; Machine mac = Db.FindListForCondition(sql, ref error).FirstOrDefault(); if (mac == null) { Add(m); } else { int id = Supplier(m.SName, m.FName, m.Model, m.ModeCode); if (id > 0) { var ent = Db.FindList($"select * from machine where ipadress='{m.IPAdress}'").FirstOrDefault(); if (ent != null && ent.ID != m.ID) { return "该IP地址与" + ent.FCode + "重复"; } var e = Db.FindList($"select * from machine where fcode='{m.FCode}'").FirstOrDefault(); if (e != null && e.ID != m.ID) { return "该设备ID已存在"; } var en = Db.FindList($"select * from machine where mcode='{m.MCode}'").FirstOrDefault(); if (en != null && en.ID != m.ID) { return "该机身码与" + en.FCode + "重复"; } mac.FCode = m.FCode ?? mac.FCode; mac.Factory = m.Factory ?? mac.Factory; mac.IPAdress = m.IPAdress ?? mac.IPAdress; mac.SupplierID = id; mac.MCode = m.MCode ?? mac.MCode; mac.Port = m.Port ?? mac.Port; mac.Remark = m.Remark ?? mac.Remark; mac.ModeCode = m.ModeCode; mac.ModeTime = DateTime.Now; mac.MacNum = m.MacNum; if (Db.UpdateFor(mac, error) > 0) { Task.Factory.StartNew(() => { GetService(mac.FCode,mac.ModeCode); }); Db.Commit(); } } else { error = "厂商数据处理失败"; } } return error ?? "修改成功"; } catch (Exception ex) { return ex.ToString(); } } public bool GetService(string fcode, string usercode) { var dic = new Dictionary(); dic.Add("fcode", fcode); dic.Add("usercode", usercode); string error = string.Empty; string url = "http://192.168.56.31:5555/eap/api/AddMachine/Get"; return HttpRequestHelper.Get(url, dic, ref error); } public bool StopService(string fcode, string usercode) { var dic = new Dictionary(); dic.Add("fcode", fcode); dic.Add("usercode", usercode); string error = string.Empty; string url = "http://192.168.56.31:5555/eap/api/AddMachine/Stop"; return HttpRequestHelper.Get(url, dic, ref error); } } }