using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net; using System.Net.Http; using System.Security.Cryptography; using System.Web.Http; using siemensPda.ORM; using logtxtWrite; using Newtonsoft.Json; using siemensPda.Models; namespace siemensPda.Controllers { public class pdaController : ApiController { // sendToSap.ISapWcfService sendToSap.IapitestClient wmsApi = new sendToSap.IapitestClient(); /// /// 登录 /// /// /// [HttpPost] public msgs login([FromBody]user loginInfo) { msgs meta = new msgs(); try { using (dbModel mod = new dbModel()) { var logUser = mod.Sys_User.FirstOrDefault(x => x.UserName == loginInfo.UserName); // var logUser = users.FirstOrDefault(x => x.UserName == loginInfo.UserName); if (logUser != null) { string password = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(loginInfo.PassWord + "{MINGpin365}", "MD5").ToLower(); // if (logUser.UserPwd == password) if (logUser.UserPwd == password) { meta.msg = "登录成功"; meta.status = 200; meta.token = Encode(loginInfo.UserName + loginInfo.PassWord); } else { meta.msg = "密码错误"; meta.status = 400; logtxt.txtWrite("类名:pdaController/函数名:login密码错误 用户名:" + loginInfo.UserName + " 密码:" + loginInfo.PassWord, 2); } } else { meta.msg = "无此用户"; meta.status = 400; logtxt.txtWrite("类名:pdaController/函数名:login无此用户 用户名:" + loginInfo.UserName + " 密码:" + loginInfo.PassWord, 2); } } } catch (Exception) { logtxt.txtWrite("类名:pdaController/函数名:login登录异常 用户名:" + loginInfo.UserName + " 密码:" + loginInfo.PassWord, 2); } return meta; } /// /// 分拣信息 /// /// /// [HttpPost] public msgs sortingConfirm([FromBody]sorting sortingInfos) { msgs meta = new msgs(); string Products = ""; try { logtxt.txtWrite("类名:pdaController/函数名:sortingConfirm ,PDA接受完整数据: " + JsonConvert.SerializeObject(sortingInfos), 0); if (sortingInfos != null) { List plates = new List(); //判断托盘号是否为空 if (!string.IsNullOrEmpty(sortingInfos.plateCode)) { using (dbModel mod = new dbModel()) { //判断分拣物料数据不能为空 if (sortingInfos.plates.Count > 0) { foreach (var item in sortingInfos.plates) { sortingType plate = new sortingType();//moveMaterials //找到物料ID 可以改成物料编号少读一个表 Base_ProductInfo material = mod.Base_ProductInfo.FirstOrDefault(x => x.ProductCode == item.materialCode); if (material != null) { plate.product_Id = (int)material.Product_Id; plate.finishedQuantity = Convert.ToDecimal(item.pickQty); plate.trankNumber = item.trankingNumber; plate.creator = sortingInfos.creator; plates.Add(plate); } } //转换成字符串 Products = JsonConvert.SerializeObject(plates); string result = wmsApi.sortingConfirm(sortingInfos.plateCode, Products, sortingInfos.percentage); resultMsg resultinfo = JsonConvert.DeserializeObject(result); if (resultinfo != null) { if (resultinfo.result) { meta.status = 200; meta.msg = resultinfo.msg; } else { meta.status = 400; meta.msg = resultinfo.msg; } } //*/ logtxt.txtWrite("类名:pdaController/函数名:sortingConfirm ,PDA接受数据: " + Products, 0); } else { meta.status = 400; meta.msg = "分拣数量不能为空"; logtxt.txtWrite("类名:pdaController/函数名:sortingConfirm ,PDA分拣数量为空: " + Products, 2); } } } else { meta.status = 400; meta.msg = "托盘号不能为空"; logtxt.txtWrite("类名:pdaController/函数名:sortingConfirm ,PDA托盘号不能为空: " + Products, 2); } } else { meta.status = 400; meta.msg = "参数为空"; logtxt.txtWrite("类名:pdaController/函数名:sortingConfirm ,PDA参数为空: " + Products, 2); } } catch (Exception ex) { logtxt.txtWrite("类名:pdaController/函数名:sortingConfirm 分拣异常,接受数据: " + Products + "异常信息:" + ex.Message, 2); } return meta; } /// /// 获取托盘信息 /// /// /// [HttpPost] public IHttpActionResult getPlateInfo([FromBody]plate plateInfo) { msgs meta = new msgs(); if (string.IsNullOrEmpty(plateInfo.plateCode)) { meta.status = 400; meta.msg = "此托盘号不能为空"; return Json(meta); } string plateCode = plateInfo.plateCode; List materials = new List(); try { using (dbModel mod = new dbModel()) { List plateMaterial = mod.Base_ProductPosition.Where(x => x.PlateCode == plateCode).ToList(); if (plateMaterial.Count == 0) { meta.status = 400; meta.msg = "不需要分拣"; return Json(meta); } //获取库位的空间占比 [EditBy shaocx,2022-10-15] var queryPositionName = plateMaterial.First().PositionName; Base_Position position = mod.Base_Position.Where(x => x.PositionName == queryPositionName).First(); foreach (var item in plateMaterial) { plateInfo info = new plateInfo(); if (position.PositionLength == null) { info.positionLength = 100;//默认空间比是100 } else { info.positionLength = Convert.ToDecimal(position.PositionLength); } info.materialCode = item.ProductCode; info.materialName = item.ProductName; info.moveType = item.Remark; info.saleCode = item.SaleCode; decimal stockqty = 0M; try { stockqty = Convert.ToDecimal(item.ExtendField02); } catch (Exception) { stockqty = 0M; } info.pickQty = stockqty; info.stockQty = (decimal)item.ProductStorage; info.trankingNumber = item.ExtendField04; info.unit = item.ExtendField05; materials.Add(info); } } meta.status = 200; meta.materials = materials; meta.msg = "获取成功"; } catch (Exception) { throw; } return Json(meta); } /// /// 转换成token /// /// 用户名加密码 /// 返回用户名加密码的token private string Encode(string data) { byte[] byKey = System.Text.ASCIIEncoding.ASCII.GetBytes("qwertyui"); byte[] byIV = System.Text.ASCIIEncoding.ASCII.GetBytes("qwertyui"); DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider(); int i = cryptoProvider.KeySize; MemoryStream ms = new MemoryStream(); CryptoStream cst = new CryptoStream(ms, cryptoProvider.CreateEncryptor(byKey, byIV), CryptoStreamMode.Write); StreamWriter sw = new StreamWriter(cst); sw.Write(data); sw.Flush(); cst.FlushFinalBlock(); sw.Flush(); return Convert.ToBase64String(ms.GetBuffer(), 0, (int)ms.Length); } } public class msgs { /// /// 状态 /// public int status { get; set; } /// /// 返回信息 /// public string msg { get; set; } /// /// 物料信息 /// public List materials { get; set; } /// /// token /// public string token { get; set; } } public class user { /// /// 用户名 /// public string UserName { get; set; } /// /// 密码 /// public string PassWord { get; set; } } public class sorting { public string creator { get; set; } public List plates { get; set; } public string plateCode { get; set; } /// /// 百分比 /// public string percentage { get; set; } } public class plateInfo { /// /// 销售单号 /// public string saleCode { get; set; } /// /// 跟踪号 /// public string trankingNumber { get; set; } /// /// 物料名称 /// public string materialName { get; set; } /// /// 物料编号 /// public string materialCode { get; set; } /// /// 移动类型 /// public string moveType { get; set; } /// /// 库存单位 /// public string unit { get; set; } /// /// 库存总数 /// public decimal stockQty { get; set; } /// /// 分拣数量 /// public decimal pickQty { get; set; } /// /// 占用空间 [EditBy shaocx,2022-10-15] /// public decimal positionLength { get; set; } } public class sortingType { //产品ID public int product_Id { get; set; } //完成数量 public decimal finishedQuantity { get; set; } //跟踪号 public string trankNumber { get; set; } public string creator { get; set; } } public class plate { /// /// 托盘编号 /// public string plateCode { get; set; } } }