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; 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(); }); 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; //如果传入的标签客户编码不为空 if (!input.Label.CUST_CODE.IsNullOrEmpty()) { //如果查不到标签并且客户编码不为空 if (!Biz.Db.Queryable().Any(x => x.LABEL_CODE == input.Label.LABEL_CODE)) { prodCustTemp = input.Label; prodTemp = input.Label.Clone(); prodTemp.CUST_CODE = ""; prodTemp.LABEL_CODE = $"{input.Label.LABEL_CODE}_{DateTime.Now.ToString("yyyyMMddhhmmss")}"; } else { prodCustTemp = input.Label; prodCustTemp.LABEL_CODE = $"{input.Label.LABEL_CODE}_{DateTime.Now.ToString("yyyyMMddhhmmss")}"; } } else { //如果客户编码为空 prodTemp = input.Label; if (Biz.Db.Queryable().Any(x => x.LABEL_CODE == input.Label.LABEL_CODE && x.PROD_CODE != input.Label.PROD_CODE)) { prodTemp.LABEL_CODE = $"{input.Label.LABEL_CODE}_{DateTime.Now.ToString("yyyyMMddhhmmss")}"; } } 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 (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.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; } } var o = db.Storageable(input.LabelVarWos) .WhereColumns(t => new { t.LABEL_ID, 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); } } }