using Cksoft.Data; using Cksoft.Data.Repository; using DllEapDal; using DllEapEntity; using DllEapEntity.Dtos; using Microsoft.AspNetCore.Mvc; using System; using System.Collections.Generic; using System.Text; using DllEapCommon.Filters; using System.Linq; using Cksoft.Unity; using Cksoft.Unity.Log4NetConfig; using DllEapEntity.PM; using IdentityModel.Internal; using Newtonsoft.Json; using Microsoft.Extensions.Configuration; using System.Threading.Tasks; using Microsoft.AspNetCore.Authorization; using AutoMapper; using System.IO; namespace DllEapBll.Controllers { /// /// 机台 /// [Route("eap/api/[controller]/[action]")] [ApiController] [Authorize] public class MachineController : ControllerBase { public IConfiguration Configuration { get; set; } private IMapper _mapper; public MachineController(IConfiguration configuration, IMapper mapper) { Configuration = configuration; _mapper = mapper; } /// /// 机台列表 /// /// /// /// /// /// /// [HttpGet] public async Task> Get(string filter, int pageIndex = 1, int pageSize = 10, string sortField = "a.FCode", string sortOrder = "ascend") { string userCode = Request.Headers["usercode"]; if (sortOrder == "descend") { sortOrder = "desc"; } else { sortOrder = "asc"; } if (sortField == "null") { sortField = "a.FCode"; } int start, end; start = (pageIndex - 1) * pageSize + 1; end = start + pageSize; using (IDatabase db = DbFactory.Base("eapslave")) { db.BeginTrans(); var dal = new MachineDal(db, userCode); var total = dal.GetCount(filter); string errorinfo = string.Empty; var roles = dal.Get(start, pageSize, sortOrder, sortField, filter, ref errorinfo); return await Task.FromResult(new LayuiModel { code = 1, count = total, data = roles, msg = errorinfo }); } } /// /// 机台详情 /// /// /// [HttpGet] public async Task GetSingle(int id) { using (IDatabase db = DbFactory.Base("eapslave")) { var dal = new MachineDal(db); return await Task.FromResult(dal.Get(id)); } } /// /// 根据MAC地址获取机台 /// /// /// [HttpGet] public async Task GetSingleMac(string mac) { using (IDatabase db = DbFactory.Base("eapslave")) { var dal = new MachineDal(db); return await Task.FromResult(dal.GetMac(mac)); } } /// /// 根据传入的机台号获取对应的机台信息 /// /// /// [HttpGet] public async Task GetByCode(string code) { var res = new EapResponse(); using (IDatabase db = DbFactory.Base("eapslave")) { var dal = new MachineDal(db); var mac = dal.Get($" and a.FCode='{code}'").FirstOrDefault(); if (mac == null) { res.Code = -1; res.Msg = $"未找到编号为【{code.ToUpper()}】的机台"; return await Task.FromResult(res); } res.Data = mac; return await Task.FromResult(res); } } /// /// 新增/修改机台 /// /// /// [HttpPost] [ButtonFilter] public async Task Add([FromBody] Machine machine) { string usercode = Request.Headers["usercode"]; using (IDatabase db = DbFactory.Base("eap")) { db.BeginTrans(); var dal = new MachineDal(db); Machine preMac = null; string errorinfo = string.Empty; var response = new EapResponse() { Code = 1, Msg = string.Empty }; int id = -1; if (machine.ID == 0) { preMac = machine; id = dal.Add(machine, usercode, ref errorinfo); } else { preMac = dal.Get(machine.ID); id = dal.Update(machine, usercode, ref errorinfo); } if (id < 0) { db.Rollback(); response.Code = -1; response.Msg = errorinfo; } else { db.Commit(); if (machine.ID == 0) LogHelper.LogFatal("新增Machine-->" + Json.ToJson(machine), "用户操作", usercode); else LogHelper.LogFatal("修改Machine-->修改值:" + Json.ToJson(machine), "用户操作", usercode); if (this.ReConnect(preMac, id, ref errorinfo) < 0) { response.Msg = $"机台信息修改成功,自动重连操作失败,失败原因:{errorinfo}"; response.Code = -2; } } response.Id = id; return await Task.FromResult(response); } } /// /// 机台重连 /// /// /// [HttpPost] public EapResponse MachineReConnect([FromBody] int macId) { var res = new EapResponse { Code = 1, Msg = string.Empty }; string errorinfo = string.Empty; var dal = new MachineDal(null); var flag = dal.AutoReconnet(macId, ref errorinfo, moveMacServer: false); if (flag < 0 && flag != -2) { res.Code = -1; res.Msg = errorinfo; return res; } res.Code = flag; res.Msg = errorinfo; return res; } private int ReConnect(Machine preMac, int macId, ref string errorinfo) { if (!string.IsNullOrEmpty(Configuration["Client"]) && Configuration["Client"] == "Ofilm") { using (IDatabase serverDb = DbFactory.Base("eap")) { var machineDal = new MachineDal(serverDb); serverDb.BeginTrans(); var flag = machineDal.DoAutoReconnect(preMac, macId, ref errorinfo); if (flag < 0 && flag != -2) { serverDb.Rollback(); return -1; } serverDb.Commit(); return flag; } } return 1; } /// /// 修改真实ip /// /// /// [HttpPost] public EapResponse UpdateRealIP([FromBody] Machine programMst) { using (IDatabase db = DbFactory.Base("eap")) { db.BeginTrans(); var dal = new MachineDal(db); string errorinfo = string.Empty; var response = new EapResponse() { Code = 1, Msg = string.Empty }; int id = -1; id = dal.UpdateRealIP(programMst, ref errorinfo); if (id < 0) { db.Rollback(); response.Code = -1; response.Msg = errorinfo; } else { db.Commit(); // LogHelper.LogFatal("修改Machine-->修改值:" + Json.ToJson(programMst), "用户操作", "system-winform"); } response.Id = id; return response; } } /// /// 删除机台 /// /// /// [HttpPost] public EapResponse Delete([FromBody] int id) { IDatabase db = null; string errormsg = string.Empty; try { db = DbFactory.Base("eap"); var dal = new MachineDal(db); db.BeginTrans(); var model = dal.Get(id); var res = dal.Delete(id, ref errormsg); if (res < 0) { db.Rollback(); return new EapResponse() { Code = -1, Msg = errormsg }; } db.Commit(); LogHelper.LogFatal("删除Machine-->:" + Json.ToJson(model), "用户操作", Request.Headers["usercode"]); return new EapResponse() { Code = 1, Msg = "" }; } catch (Exception e) { errormsg = e.Message; return new EapResponse { Code = -1, Msg = errormsg }; } finally { if (db != null) db.Close(); } } /// /// 修改是否管控 /// /// /// /// [HttpGet] public int ChangeIsControl(int id, int isControl) { using (IDatabase db = DbFactory.Base("eap")) { var dal = new MachineDal(db); return dal.ChangeIsControl("IsControl", id, isControl); } } /// /// 修改机台是否连接 /// /// /// [HttpPost] public int ChangeIsConn([FromBody] IDictionary valuePairs) { int id = valuePairs["id"]; int isConn = valuePairs["isConn"]; using (IDatabase db = DbFactory.Base("eap")) { var dal = new MachineDal(db); return dal.ChangeIsControl("IsConn", id, isConn); } } /// /// 修改机台状态 /// /// /// [HttpPost] public int ChangeState([FromBody] IDictionary valuePairs) { int id = Convert.ToInt32(valuePairs["id"]); int state = Convert.ToInt32(valuePairs["state"]); string field = valuePairs["field"]; using (IDatabase db = DbFactory.Base("eap")) { var dal = new MachineDal(db); return dal.ChangeIsControl(field, id, state); } } /// /// 获取机台 /// /// /// [HttpGet] public IEnumerable GetMachines(string filter) { using (IDatabase db = DbFactory.Base("eapslave")) { var dal = new MachineDal(db); return dal.Get(filter); } } /// /// 绑定制程到机台 /// /// /// [HttpPost] public EapResponse BindMac(Dictionary valuePairs) { string userCode = Request.Headers["usercode"]; string pCode = valuePairs["pCode"]; string[] macIds = valuePairs["macIds"].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); string errorinfo = string.Empty; var eapRes = new EapResponse { Code = 1, Msg = string.Empty }; IDatabase db = null; try { db = DbFactory.Base("eap"); db.BeginTrans(); var dal = new MachineDal(db); if (dal.BindMachine(pCode, macIds, userCode, ref errorinfo) < 0) { eapRes.Code = -1; eapRes.Msg = errorinfo; db.Rollback(); } db.Commit(); LogHelper.LogFatal("新增MacTProcess-->原始值:" + valuePairs["macIds"] + " PCode:" + pCode, "用户操作", userCode); return eapRes; } catch (Exception e) { db.Rollback(); eapRes.Code = -1; eapRes.Msg = e.Message; return eapRes; } finally { if (db != null) db.Close(); } } /// /// 绑定机台 /// /// /// [HttpPost] public EapResponse BindMacToPM([FromBody] Dictionary valuePairs) { string userCode = Request.Headers["usercode"]; int pmId = Convert.ToInt32(valuePairs["pmId"]); string[] macIds = valuePairs["macIds"].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); string errorinfo = string.Empty; var eapRes = new EapResponse { Code = 1, Msg = string.Empty }; IDatabase db = null; try { db = DbFactory.Base("eap"); db.BeginTrans(); var dal = new MachineDal(db); if (dal.BindMachineToPM(pmId, macIds, userCode, ref errorinfo) < 0) { eapRes.Code = -1; eapRes.Msg = errorinfo; db.Rollback(); } db.Commit(); LogHelper.LogFatal("新增PMMachine-->原始值:" + valuePairs["macIds"] + " pmId:" + pmId, "用户操作", userCode); return eapRes; } catch (Exception e) { db.Rollback(); eapRes.Code = -1; eapRes.Msg = e.Message; return eapRes; } finally { if (db != null) db.Close(); } } /// /// 获取未授权的机台 /// /// /// [HttpGet] public LayuiModel GetUnAuthorizedMachines(string filter) { string errorinfo = string.Empty; var layModel = new LayuiModel(); using (IDatabase db = DbFactory.Base("eap")) { var dal = new MachineDal(db); var datas = dal.GetUnAuthorizedMachines(filter, ref errorinfo); if (!string.IsNullOrEmpty(errorinfo)) { layModel.code = -1; layModel.msg = errorinfo; return layModel; } layModel.code = 1; layModel.data = datas; return layModel; } } /// /// 获取未授权的机台ID /// /// /// [HttpGet] public LayuiModel GetAuthedMacIds(string staffCode) { string errorinfo = string.Empty; using (IDatabase db = DbFactory.Base("eap")) { var dal = new MachineDal(db); var ids = dal.GetAuthedMacs(staffCode, ref errorinfo); var res = new LayuiModel(); if (string.IsNullOrEmpty(errorinfo)) { res.code = 1; res.data = ids; } else { res.code = -1; } return res; } } /// /// 授权机台 /// /// /// [HttpPost] public EapResponse SetMacs([FromBody] Dictionary valuePairs) { string userCode = Request.Headers["usercode"]; var macIdsStr = valuePairs["macCodes"]; var staffCode = valuePairs["staffCode"]; string errorinfo = string.Empty; var res = new EapResponse() { Code = 1, Msg = string.Empty }; using (IDatabase db = DbFactory.Base("eap")) { var dal = new MachineDal(db); db.BeginTrans(); IEnumerable macIds = new List(); if (!string.IsNullOrEmpty(macIdsStr)) { var macCodes = macIdsStr.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); var filter = string.Join(",", macCodes.Select(c => $"'{c}'")); var machines = dal.Get($" and a.FCode in ({filter})"); macIds = machines.Select(c => c.ID); } var i = dal.SetMacs(staffCode, macIds, userCode, ref errorinfo); if (i < 0) { db.Rollback(); res.Code = -1; res.Msg = errorinfo; return res; } db.Commit(); LogHelper.LogFatal("新增StaffMachine-->原始值:macIds-->" + String.Join(",", macIds), "用户操作", userCode); return res; } } /// /// 批量修改是否控制机台 /// /// /// [HttpPost] public EapResponse SetControlByTrans([FromBody] Dictionary valuePairs) { var res = new EapResponse { Code = 1, Msg = string.Empty }; string errorinfo = string.Empty; if (valuePairs == null || valuePairs.Keys.Count <= 0) { res.Code = -1; res.Msg = "参数不能为空"; return res; } var ids = valuePairs["ids"]; var idArr = ids.Split(new char[] { ',' }).Select(c => Convert.ToInt32(c)); var field = valuePairs["field"]; var value = valuePairs["value"]; var userCode = Request.Headers["usercode"]; using (IDatabase db = DbFactory.Base("eap")) { var dal = new MachineDal(db); var i = dal.SetControlByTrans(idArr, field, value, ref errorinfo); if (i < 0) { res.Code = -1; res.Msg = errorinfo; return res; } var machines = dal.Get($" and a.id in ({ids})"); // 写日志 LogHelper.LogFatal($"修改机台字段[{field}]-->修改值:{value}, " + $"被修改机台号:{JsonConvert.SerializeObject(machines.Select(c => c.FCode))}", "用户操作", userCode); return res; } } /// /// 获取机台多选数据源 /// /// /// public IEnumerable> GetMultipalMacSelects(string filter) { using (IDatabase db = DbFactory.Base("eapslave")) { var dal = new MachineDal(db); return dal.GetMultipleSelects(filter).OrderBy(c => c.Label); } } /// /// 导出 /// /// /// [HttpPost] public async Task Export([FromBody] dynamic filterInfo) { string errorinfo = string.Empty; using (IDatabase db = DbFactory.Base("eapslave")) { var dal = new MachineDal(db, _mapper); var filter = Convert.ToString(filterInfo.filter); var book = await dal.Export(filter); MemoryStream ms = new MemoryStream(); ms.Position = 0; book.Write(ms); ms.Dispose(); ms.Close(); return File(ms.ToArray(), "application/octet-stream"); } } } }