using Tiger.Model; using SqlSugar; using System; using System.Collections.Generic; using System.Linq.Expressions; using System.Text; using System.Threading.Tasks; using Rhea.Common; using System.Net; using System.Linq; using Newtonsoft.Json; using Tiger.IBusiness; using Microsoft.AspNetCore.Http; using Tiger.Model.Entitys.MES.BasLabelTemp; using System.Security.Cryptography; using Tiger.Model.Entitys.MES.Position; namespace Tiger.Business.MES { public partial class BasLabelTemp : ILabelTemplate { public async Task DeleteLabelTemplate(string Id) { var result = new ApiAction(); try { var _label = await Biz.Db.Queryable().Where(x => x.ID == Id).FirstAsync(); if (_label == null) { result.IsSuccessed = false; result.LocaleMsg = new($"标签模板不存在,不能删除!"); return result; } if (Biz.Db.Queryable().Any(x => x.LABEL_CODE == _label.LABEL_CODE)) { result.IsSuccessed = false; result.LocaleMsg = new($"标签模板已经绑定到行为,不能删除!"); return result; } if (Biz.Db.Queryable().Any(x => x.LABEL_CODE == _label.LABEL_CODE)) { result.IsSuccessed = false; result.LocaleMsg = new($"标签模板已经绑定到行为,不能删除!"); return result; } var db = Biz.Db; var dbTran = db.UseTran(() => { db.Deleteable().Where(x => x.ID == Id).ExecuteCommand(); db.Deleteable().Where(x => x.LABEL_ID == Id).ExecuteCommand(); db.Deleteable().Where(x => x.LABEL_ID == Id).ExecuteCommand(); }); if (!dbTran.IsSuccess) { result.IsSuccessed = false; result.LocaleMsg = new($"删除标签模板异常"); } } catch (Exception ex) { result.CatchExceptionWithLog(ex, "删除标签模板异常"); } return result; } /// /// 保存标签模板 /// /// /// /// public async Task SaveLabelTemplate(BasLabelTempInput input) { var result = new ApiAction(); try { BAS_LABEL_TEMP? prodTemp = null; BAS_LABEL_TEMP? prodCustTemp = null; var ltemp = await Biz.Db.Queryable().Where(x => x.LABEL_CODE == input.Label.LABEL_CODE).FirstAsync(); //如果传入的标签客户编码不为空 if (!input.Label.CUST_CODE.IsNullOrEmpty()) { //如果查不到标签并且客户编码不为空 if (ltemp == null) { prodCustTemp = input.Label; prodTemp = input.Label.Clone(); prodTemp.CUST_CODE = ""; prodTemp.LABEL_CODE = GenerateCodeName(input.Label.LABEL_CODE, "_L"); } else { prodCustTemp = input.Label; if ((ltemp.PROD_CODE != input.Label.PROD_CODE && ltemp.CUST_CODE == input.Label.CUST_CODE) || (ltemp.PROD_CODE != input.Label.PROD_CODE && ltemp.CUST_CODE != input.Label.CUST_CODE) || (ltemp.PROD_CODE == input.Label.PROD_CODE && ltemp.CUST_CODE != input.Label.CUST_CODE)) { prodCustTemp.LABEL_CODE = GenerateCodeName(input.Label.LABEL_CODE, "_L"); } } } else { //如果客户编码为空 prodTemp = input.Label; if (ltemp != null && ltemp.PROD_CODE.IsNullOrEmpty() && ltemp.PROD_CODE != input.Label.PROD_CODE) { prodTemp.LABEL_CODE = GenerateCodeName(input.Label.LABEL_CODE, "_L"); } } var labelVarIds = input.LabelVars.Select(x => x.VAR_NAME).ToList(); var labelVars = Biz.Db.Queryable() .Where(q=> q.LABEL_ID == ltemp.ID) .WhereIF(labelVarIds.Count>0, q=> !labelVarIds.Contains(q.VAR_NAME)) .ToList(); var db = Biz.Db; var dbTran = db.UseTran(() => { if (prodTemp != null) { var x = db.Storageable(prodTemp) .WhereColumns(t => new { t.LABEL_CODE, t.PROD_CODE, t.CUST_CODE, t.GHOST_ROW }) .ToStorage(); x.AsInsertable.ExecuteCommand(); x.AsUpdateable.IgnoreColumns(x => x.ID).ExecuteCommand(); } if (prodCustTemp != null) { var y = db.Storageable(prodCustTemp) .WhereColumns(t => new { t.LABEL_CODE, t.PROD_CODE, t.CUST_CODE, t.GHOST_ROW }) .ToStorage(); y.AsInsertable.ExecuteCommand(); y.AsUpdateable.IgnoreColumns(x => x.ID).ExecuteCommand(); } var temp = db.Queryable().Where(x => x.LABEL_CODE == input.Label.LABEL_CODE && x.PROD_CODE == input.Label.PROD_CODE && x.CUST_CODE == input.Label.CUST_CODE).First(); //删除不在传入列表中的 if (labelVars.Count>0) { db.Deleteable(labelVars).ExecuteCommand(); } if (input.LabelVars != null && input.LabelVars.Count > 0) { if (temp != null && temp.ID != input.LabelVars[0].LABEL_ID) { foreach (var item in input.LabelVars) { item.LABEL_ID = temp.ID; } } var z = db.Storageable(input.LabelVars) .WhereColumns(t => new { t.LABEL_ID, t.VAR_NAME, t.GHOST_ROW }) .ToStorage(); z.AsInsertable.ExecuteCommand(); z.AsUpdateable.IgnoreColumns(x => x.ID).ExecuteCommand(); } if (!input.LabelVars.Any()) { db.Deleteable(labelVars).ExecuteCommand(); } if (input.LabelVarWos != null && input.LabelVarWos.Count > 0) { if (temp != null && temp.ID != input.LabelVarWos[0].LABEL_ID) { foreach (var item in input.LabelVarWos) { item.LABEL_ID = temp.ID; } } db.Deleteable(false).Where(x => x.LABEL_ID == input.LabelVarWos[0].LABEL_ID).ExecuteCommand(); var o = db.Storageable(input.LabelVarWos) .WhereColumns(t => new { t.LABEL_ID, t.WORK_ORDER, t.VAR_NAME, t.GHOST_ROW }) .ToStorage(); o.AsInsertable.ExecuteCommand(); o.AsUpdateable.IgnoreColumns(x => x.ID).ExecuteCommand(); } }); if (!dbTran.IsSuccess) { result.IsSuccessed = false; result.LocaleMsg = new($"保存标签模板异常!"); } } catch (Exception ex) { result.CatchExceptionWithLog(ex, "保存标签模板异常"); } return await Task.FromResult(result); } /// /// 保存标签模板变量 /// /// /// public async Task SaveLabelTemplateVar(BasLabelTempInput input) { var result = new ApiAction(); try { var db = Biz.Db; var dbTran = db.UseTran(() => { if (input.LabelVars != null && input.LabelVars.Count > 0) { db.Deleteable(false).Where(x => x.LABEL_ID == input.LabelVars[0].LABEL_ID).ExecuteCommand(); var z = db.Storageable(input.LabelVars) .WhereColumns(t => new { t.LABEL_ID, t.VAR_NAME, t.GHOST_ROW }) .ToStorage(); z.AsInsertable.ExecuteCommand(); z.AsUpdateable.IgnoreColumns(x => x.ID).ExecuteCommand(); } if (input.LabelVarWos != null && input.LabelVarWos.Count > 0) { db.Deleteable(false).Where(x => x.LABEL_ID == input.LabelVarWos[0].LABEL_ID).ExecuteCommand(); var o = db.Storageable(input.LabelVarWos) .WhereColumns(t => new { t.LABEL_ID, t.WORK_ORDER, t.VAR_NAME, t.GHOST_ROW }) .ToStorage(); o.AsInsertable.ExecuteCommand(); o.AsUpdateable.IgnoreColumns(x => x.ID).ExecuteCommand(); } }); if (!dbTran.IsSuccess) { result.IsSuccessed = false; result.LocaleMsg = new($"保存标签变量异常!"); } } catch (Exception ex) { result.CatchExceptionWithLog(ex, "保存标签变量异常"); } return await Task.FromResult(result); } /// /// 根据分隔符生成Code名 /// /// /// /// private string GenerateCodeName(string data, string separator) { string result = string.Empty; var strs = data.Split("_L"); result = strs.Length > 1 ? data.Replace(strs[1], DateTime.Now.ToString("yyyyMMddhhmmss")) : $"{data}_L{DateTime.Now.ToString("yyyyMMddhhmmss")}"; return result; } } }