Example #1
0
        ///// <summary>
        ///// 生成码信息
        ///// </summary>
        //public void Generate()
        //{
        //    List<List<string>> rulersResult = new List<List<string>>();
        //    List<string> allCode = new List<string>();
        //    for(int i=0;i<this.segRulers.Count;i++)
        //    {
        //        rulersResult.Add(this.segRulers[i].Generate(this.codeApply.ApplyAmount * this.outerPackageRatios[i]));
        //    }
        //    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];
        //    }
        //    ////#region测试代码
        //    //Dictionary<string, object> extentDictionary = new Dictionary<string, object>();
        //    //extentDictionary.Add("ExtentName","TEST");
        //    //extentDictionary.Add("ExtentType", "TestType");
        //    //List<Acctrue.CMC.Model.Report.ActivityCodes> activityCodes = allCode.Select(s => new Acctrue.CMC.Model.Report.ActivityCodes() { Code=s, Extent=extentDictionary }).ToList();
        //    //string activeStr =  Newtonsoft.Json.JsonConvert.SerializeObject(activityCodes);
        //    //CodeActiveUploadFactory.Instance.InsertNewActive(new Model.Systems.AppSettingInfo() { AppID="1", AppSettingID=1, AppStatus=0, CorpCode="1", CorpName="TEST", CreatedDate=DateTime.Now, SubCorpCode="01" }, new Model.Request.UploadActiveRequest() { ApplyId=33, ActiveName="激活", Memo="", ProductCode="01", WorkLine="01", ProductName="TEST", File= Tools.SerializeZipCode(activeStr) });
        //    ////#endregion
        //    InnerCodeFileRecord record = new InnerCodeFileRecord();
        //    record.ApplyId = codeApply.ApplyId;
        //    record.Content = Tools.SerializeZipCode(allCode);
        //    CodeDownloadFactory.Instance.AddInnerCodeFiledRecord(record);

        //    //更新码申请记录状态完成
        //    codeApply.ApplyStatus = ApplyStatus.Completed;
        //    codeApply.ProcessType = ProcessType.Completed;
        //    CodeApplyFactory.Instance.Update(codeApply);
        //}

        /// <summary>
        /// 将生成后的码信息逐个存储于mongodb
        /// </summary>
        ///<param name="ct">工作取消令牌</param>
        public void SaveBySingleCode(CancellationToken ct)
        {
            InnerCodeFileRecord record = CodeDownloadFactory.Instance.GetInnerCodeFileRecord(codeApply.ApplyId);

            //处理码格式
            List <string> codes       = new List <string>();
            List <string> originCodes = Tools.DeserializeZipCode(Convert.ToBase64String(record.Content));

            foreach (string oriCode in originCodes)
            {
                codes.Add(oriCode.Split(new char[] { '/' }).LastOrDefault());
            }

            List <ApplyCodes> applyCodes = new List <ApplyCodes>();

            ApplyRuleProcess applyRuleProcess = ApplyRuleProcessFactory.Instance.GetCodeRuleByApplyIdRuleId(this.codeApply.ApplyId, 0);

            if (applyRuleProcess == null)
            {
                applyRuleProcess = new ApplyRuleProcess()
                {
                    ApplyId = this.codeApply.ApplyId, CodeRuleId = 0, FirstCode = codes[1], EndCode = codes[codes.Count - 1], Status = ApplyRuleStatus.None, AttributeInfo = "0"
                };
                ApplyRuleProcessFactory.Instance.Add(applyRuleProcess);
            }
            int lastLineIndex = 0;

            int.TryParse(applyRuleProcess.AttributeInfo, out lastLineIndex);
            if (lastLineIndex < codes.Count - 1)
            {
                for (int i = lastLineIndex; i < codes.Count - 1; i++)
                {
                    applyCodes.Clear();
                    for (int j = 0; j < BatchSize && i < codes.Count - 1; j++, i++)
                    {
                        applyCodes.Add(new ApplyCodes()
                        {
                            AppId = codeApply.ApplyAppId, ApplyId = codeApply.ApplyId, ApplyType = codeApply.ApplyType, Code = codes[i + 1], CorpCode = codeApply.CorpCode, CorpName = codeApply.CorpName, CreateDate = codeApply.AuditDate == null ? DateTime.Now : (DateTime)codeApply.AuditDate, ProduceWorkline = codeApply.ProduceWorkLine, ProductCode = codeApply.ProductCode, ProductName = codeApply.ProductName, SubCorpCode = codeApply.ApplySubCorpCode, UseCorpCode = codeApply.UseCorpCode, UseCorpName = codeApply.UseCorpName, UseSubCorpCode = codeApply.UseSubCorpCode
                        });
                    }
                    ;


                    ApplyCodesFactory.Instance.BatchAdd(applyCodes);
                    applyRuleProcess.AttributeInfo = i.ToString();
                    ApplyRuleProcessFactory.Instance.Update(applyRuleProcess);
                    i--;
                }
            }

            //更新状态
            applyRuleProcess.Status = ApplyRuleStatus.Completed;
            ApplyRuleProcessFactory.Instance.Update(applyRuleProcess);
            for (int i = 0; i < this.segRulers.Count; i++)
            {
                if (applyRuleProcessDict[this.segRulers[i]].Status == ApplyRuleStatus.Stored)
                {
                    applyRuleProcessDict[this.segRulers[i]].Status = ApplyRuleStatus.Completed;
                    ApplyRuleProcessFactory.Instance.Update(applyRuleProcessDict[this.segRulers[i]]);
                }
            }
        }
Example #2
0
        /// <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);
                }
            }
        }