/// <summary> /// 获取码规则处理信息 /// </summary> /// <param name="codeRuleId">码规则Id</param> /// <param name="featureTag">特征标签</param> /// <returns></returns> public RuleProcess GetRuleProcess(int codeRuleId, string featureTag) { Condition where = Condition.Empty & CK.K["CodeRuleId"].Eq(codeRuleId) & CK.K["FeatureTag"].Eq(featureTag); RuleProcess rule = dbContext.From <RuleProcess>().Where(where).Select().FirstOrDefault(); return(rule); }
/// <summary> /// 修改码规则处理信息 /// </summary> /// <param name="info">码规则处理信息</param> /// <returns></returns> public void Update(RuleProcess info) { Acctrue.Library.Data.SqlEntry.KeyValueCollection keys = new Acctrue.Library.Data.SqlEntry.KeyValueCollection(); keys.Add(new Acctrue.Library.Data.SqlEntry.KeyValue("LastCode", info.LastCode)); dbContext.Update <RuleProcess>(keys, CK.K["CodeRuleId"].Eq(info.CodeRuleId) & CK.K["FeatureTag"].Eq(info.FeatureTag)); }
/// <summary> /// 添加码规则处理信息 /// </summary> /// <param name="info">码规则处理信息</param> /// <returns></returns> public void Add(RuleProcess info) { dbContext.Insert(info); }
/// <summary> /// 存储各码规则生成码 /// </summary> public void CodesStore() { List <List <string> > rulersResult = new List <List <string> >(); List <string> allCode = new List <string>(); Dictionary <string, RuleProcess> ruleLastCode = new Dictionary <string, RuleProcess>(); if (applyRuleProcessDict.Values.Where(s => s.Status < ApplyRuleStatus.Generated).Count() > 0) { throw new Exception($"码申请Id:{this.codeApply.ApplyId}存储码信息过程中,发现中各码规则未完成码申请。"); } // if (applyRuleProcessDict.Values.Where(s => s.Status != ApplyRuleStatus.Generated).Count() > 0) //{ // if (applyRuleProcessDict.Values.Where(s => s.Status == ApplyRuleStatus.Stored).Count() > 0) // { } // else // { // throw new Exception($"码申请Id:{this.codeApply.ApplyId}存储码信息过程中,发现中各码规则未完成码申请。"); // } //} if (applyRuleProcessDict.Values.Where(s => s.Status == ApplyRuleStatus.Generated).Count() > 0) { int outterFileCount = CodeDownloadFactory.Instance.GetOutterCodeFileCount(this.codeApply.ApplyId);//当前申请已存在的外码文件数量 for (int i = 0; i < this.segRulers.Count; i++) { if (applyRuleProcessDict[this.segRulers[i]].Status == ApplyRuleStatus.Generated) { byte[] originalCodeData; #region 加载码规则最后码信息(序列码段序列位置) RuleProcess ruleProcess = null; if (ruleLastCode.ContainsKey(this.codeRules[i].CodeRuleId + "|" + this.ruleFeatureTags[this.segRulers[i]])) { ruleProcess = ruleLastCode[this.codeRules[i].CodeRuleId + "|" + this.ruleFeatureTags[this.segRulers[i]]]; } else { ruleProcess = RuleProcessFactory.Instance.GetRuleProcess(this.codeRules[i].CodeRuleId, this.ruleFeatureTags[this.segRulers[i]]); if (ruleProcess == null) { ruleProcess = new RuleProcess() { CodeRuleId = this.codeRules[i].CodeRuleId, FeatureTag = this.ruleFeatureTags[this.segRulers[i]], LastCode = string.Empty }; RuleProcessFactory.Instance.Add(ruleProcess); } ruleLastCode.Add(this.codeRules[i].CodeRuleId + "|" + this.ruleFeatureTags[this.segRulers[i]], ruleProcess); } if (ruleProcess.LastCode != null && ruleProcess.LastCode != "") { this.segRulers[i].LastCode = ruleProcess.LastCode; } #endregion rulersResult.Add(this.segRulers[i].GetCodes(applyRuleProcessDict[this.segRulers[i]].AttributeInfo, this.codeApply.ApplyAmount * this.outerPackageRatios[i], out originalCodeData)); ruleProcess.LastCode = rulersResult[i][rulersResult[i].Count - 1]; if (originalCodeData != null && i >= outterFileCount)//判断有原始码数据且之前没有保存(内部码无原始码数据) { OutterCodeFileRecord outterFile = new OutterCodeFileRecord(); outterFile.ApplyId = codeApply.ApplyId; outterFile.Content = originalCodeData; CodeDownloadFactory.Instance.AddOutterCodeFiledRecord(outterFile); } } } if (this.segRulers.Count > 1) { for (int i = 0; i < this.codeApply.ApplyAmount; i++) { for (int j = 0; j < this.segRulers.Count; j++) { for (int k = 0; k < this.outerPackageRatios[j]; k++) { allCode.Add(rulersResult[j][i * this.outerPackageRatios[j] + k]); } } } } else { allCode = rulersResult[0]; } Dictionary <string, string> header = new Dictionary <string, string>(); header.Add("ApplyId", this.codeApply.ApplyId.ToString()); header.Add("CorpCode", this.codeApply.CorpCode); header.Add("UseCorpCode", this.codeApply.UseCorpCode); header.Add("Amount", allCode.Count.ToString()); CodeRule LastRule = codeRules[codeRules.Count - 1]; if (LastRule.CodeExtensions != "" || LastRule.CodeExtensions != null) { header.Add("UrlFormat", LastRule.CodeExtensions.Split('{')[0]); } string codeFileName = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString("N")); try { using (StreamWriter sw = new StreamWriter(codeFileName, false, Encoding.UTF8)) { sw.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(header)); foreach (string code in allCode) { //单行添加URL if (LastRule.CodeArguments == "ADD") { sw.WriteLine(string.Format(LastRule.CodeExtensions, code)); } else { sw.WriteLine(code); } } } } catch (Exception ex) { try { File.Delete(codeFileName); } catch { } throw new Exception($"码申请Id:{ this.codeApply.ApplyId }存储码信息过程中,发生成存储临时码文件异常:{ex.Message}", ex); } finally { } string zipFileName = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString("N")); InnerCodeFileRecord record = new InnerCodeFileRecord(); record.ApplyId = codeApply.ApplyId; try { Tools.ZipFile(codeFileName, zipFileName); using (FileStream fs = new FileStream(zipFileName, FileMode.Open)) { byte[] contentBytes = new byte[fs.Length]; fs.Read(contentBytes, 0, (int)fs.Length); record.Content = contentBytes; //record.Content = Convert.ToBase64String(contentBytes); } } catch (Exception ex) { throw new Exception($"码申请Id:{ this.codeApply.ApplyId }存储码信息过程中,发生压缩码文件异常:{ex.Message}", ex); } finally { try { File.Delete(codeFileName); File.Delete(zipFileName); } catch { } } CodeDownloadFactory.Instance.AddInnerCodeFiledRecord(record); //更新申请码规则状态 for (int i = 0; i < this.segRulers.Count; i++) { if (applyRuleProcessDict[this.segRulers[i]].Status == ApplyRuleStatus.Generated) { applyRuleProcessDict[this.segRulers[i]].FirstCode = rulersResult[i][0]; applyRuleProcessDict[this.segRulers[i]].EndCode = rulersResult[i][rulersResult[i].Count - 1]; applyRuleProcessDict[this.segRulers[i]].Status = ApplyRuleStatus.Stored; ApplyRuleProcessFactory.Instance.Update(applyRuleProcessDict[this.segRulers[i]]); } } //更新码规则最后码状态信息 foreach (RuleProcess process in ruleLastCode.Values) { RuleProcessFactory.Instance.Update(process); } } }