123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342 |
- 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<MachineInfo> 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<MachineInfo>(sql+ " ORDER by a.FCode " + s , ref error);
- var modeCodes = data.Where(c => !string.IsNullOrEmpty(c.ModeCode)).Select(c => c.ModeCode).Distinct();
- List<Staff> staff = new List<Staff>();
- if (modeCodes != null)
- {
- var sss = string.Join(",", modeCodes.Select(c => $"'{c}'"));
- if (sss.Any())
- {
- IDatabase db= DbFactory.Base("eapslave");
- staff = db.FindList<Staff>($@"SELECT FCode,FName from staff where FCode in ({sss})").ToList();
- }
- }
- var dic = new Dictionary<string, string>();
- 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<Machine>($"select * from machine where ipadress='{m.IPAdress}'").FirstOrDefault();
- if (ent != null)
- {
- return "该IP地址与" + ent.FCode + "重复";
- }
- var e = Db.FindList<Machine>($"select * from machine where fcode='{m.FCode}'").FirstOrDefault();
- if (e != null)
- {
- return "该设备ID已存在";
- }
- var en = Db.FindList<Machine>($"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<Machine>(mac, m.ModeCode) > 0)
- {
- Db.Commit();
- Task.Factory.StartNew(() =>
- {
- GetService(mac.FCode,mac.ModeCode);
- });
- return " 数据新增成功";
- }
- }
- return "厂商数据处理失败";
- }
- catch (Exception ex)
- {
- return ex.ToString();
- }
- }
- public IEnumerable<SelectDto<string>> 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<Supplier>(sql);
- var dto =new List<SelectDto<string>>();
- dto.Add(new SelectDto<string>() { Label = "全部", Value = null });
- foreach (var item in data)
- {
- dto.Add(new SelectDto<string>() { Label = item.Model, Value = item.Model });
- }
- return dto;
- }
- public bool ReConnect(int ID, string fcode)
- {
- var data = Db.FindList<Machine>($@"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<Machine>($@" and id={id}", ref er).FirstOrDefault();
- StopService(x.FCode,fcode);
- if (Db.DeleteForCondition<Machine>($@" 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<Supplier>(sql, ref error).FirstOrDefault();
- if (s == null)
- {
- Supplier su = new Supplier()
- {
- SName = SName,
- Model = Model,
- FName = FName
- };
- if (Db.InsertFor<Supplier>(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<Machine>(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<Machine>($"select * from machine where ipadress='{m.IPAdress}'").FirstOrDefault();
- if (ent != null && ent.ID != m.ID)
- {
- return "该IP地址与" + ent.FCode + "重复";
- }
- var e = Db.FindList<Machine>($"select * from machine where fcode='{m.FCode}'").FirstOrDefault();
- if (e != null && e.ID != m.ID)
- {
- return "该设备ID已存在";
- }
- var en = Db.FindList<Machine>($"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<Machine>(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<string, string>();
- 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<bool>.Get(url, dic, ref error);
- }
- public bool StopService(string fcode, string usercode)
- {
- var dic = new Dictionary<string, string>();
- 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<bool>.Get(url, dic, ref error);
- }
- }
- }
|