/// <summary> /// 创建积分申请单,校验信息成功并推送 /// 若原先存在积分申请单,失败的原因:校验失败 所有应该重新赋值 /// </summary> /// <param name="applyPointRequest"></param> /// <returns></returns> public static Result CreateApplyPoint(ApplyPointRequest applyPointRequest) { try { #region 图片上传 ImageResponse ImageResponse = null; try { var FileUploadResult = ImageUpload(Commom.FileUploadUrl, applyPointRequest.receiptOCR.Base64); if (!FileUploadResult.Success) { return(FileUploadResult); } ImageResponse = JsonConvert.DeserializeObject <ImageResponse>(FileUploadResult.Data.ToString()); } catch (Exception ex) { Log.Error("CreateApplyPoint-FileUpload", ex); return(new Result(false, ex.Message, null)); } #endregion Store StoreModel = CacheHandle <Store>( Key: $"Store{applyPointRequest.receiptOCR.StoreId}", Hour: double.Parse(ConfigurationUtil.GetSection("ObjectConfig:CacheExpiration:Store")), sqlWhere: $" and StoreId = '{applyPointRequest.receiptOCR.StoreId}'"); StoreOCR StoreOCRRule = CacheHandle <StoreOCR>( Key: $"StoreOCR{applyPointRequest.receiptOCR.StoreId}", Hour: double.Parse(ConfigurationUtil.GetSection("ObjectConfig:CacheExpiration:StoreOCR")), sqlWhere: $"and StoreId = '{applyPointRequest.receiptOCR.StoreId}'"); var ApplyPoint = dal.GetModel <ApplyPoint>($" and ReceiptNo='{applyPointRequest.receiptOCR.ReceiptNo}' and StoreID='{applyPointRequest.receiptOCR.StoreId}'"); ApplyPictureRecongnize applyPictureRecongnize = dal.GetModel <ApplyPictureRecongnize>($" and id='{applyPointRequest.receiptOCR.RecongnizelId}'"); var IsHas = false; // 是否原先存在积分申请单 默认没有 更新识别原始表 if (ApplyPoint == null) //判断该小票号 是否存在积分申请单 不存在则添加原始积分申请单 (已解析原始数据,校验失败) { var ApplyPointId = Guid.NewGuid(); ApplyPoint = new ApplyPoint { ApplyPointID = ApplyPointId, MallID = StoreModel.MallID, StoreID = applyPointRequest.receiptOCR.StoreId, CardID = Guid.Parse(applyPointRequest.cardId), ReceiptNo = applyPointRequest.receiptOCR.ReceiptNo, TransDate = applyPointRequest.receiptOCR.TransDatetime, TransAmt = applyPointRequest.receiptOCR.TranAmount, VerifyStatus = StoreOCRRule.needVerify == 0 ? 1 : 0, ReceiptPhoto = ImageResponse.fileURL }; applyPictureRecongnize.applyid = ApplyPointId; DbContext.Update <ApplyPictureRecongnize>(applyPictureRecongnize); } else { IsHas = true; ApplyPoint.MallID = StoreModel.MallID; ApplyPoint.StoreID = applyPointRequest.receiptOCR.StoreId; ApplyPoint.CardID = Guid.Parse(applyPointRequest.cardId); ApplyPoint.ReceiptNo = applyPointRequest.receiptOCR.ReceiptNo; ApplyPoint.TransDate = applyPointRequest.receiptOCR.TransDatetime; ApplyPoint.TransAmt = applyPointRequest.receiptOCR.TranAmount; ApplyPoint.VerifyStatus = StoreOCRRule.needVerify == 0 ? 1 : 0; } var VerifyRecognitionResult = VerifyRecognition(applyPointRequest.receiptOCR); //校验结果 ApplyPoint.AuditDate = DateTime.Now; if (VerifyRecognitionResult.Success) //校验成功 { ApplyPoint.RecongizeStatus = 2; ApplyPoint.VerifyStatus = 1; } else//校验失败 修改值 { ApplyPoint.RecongizeStatus = 3; ApplyPoint.VerifyStatus = 0; ApplyPoint.Status = 0; if (IsHas) { DbContext.Update <ApplyPoint>(ApplyPoint); } else { DbContext.Add <ApplyPoint>(ApplyPoint); } return(VerifyRecognitionResult); } var LastRes = true;//添加是否成功 if (IsHas) { if (!DbContext.Update <ApplyPoint>(ApplyPoint)) { LastRes = false; } } else { if (DbContext.Add <ApplyPoint>(ApplyPoint) != 0) { LastRes = false; } } if (LastRes) { List <Company> companyList = CacheHandle <List <Company> >( Key: "Company", Hour: double.Parse(ConfigurationUtil.GetSection("ObjectConfig:CacheExpiration:Company")), sqlWhere: ""); List <OrgInfo> orgList = CacheHandle <List <OrgInfo> >( Key: "OrgInfo", Hour: double.Parse(ConfigurationUtil.GetSection("ObjectConfig:CacheExpiration:OrgInfo")), sqlWhere: ""); //自动积分 推送 var arg = new WebPosArg { companyID = companyList.Where(s => s.CompanyId == StoreModel.CompanyID).FirstOrDefault()?.CompanyCode ?? "", storeID = StoreModel.StoreCode, cardID = dal.GetModel <Card>($" and CardID='{applyPointRequest.cardId}'")?.CardCode ?? "", cashierID = "CrmApplyPoint", discountPercentage = 0, orgID = orgList.Where(s => s.OrgId == StoreModel.OrgID).FirstOrDefault()?.OrgCode ?? "", receiptNo = applyPointRequest.receiptOCR.ReceiptNo, txnDateTime = applyPointRequest.receiptOCR.TransDatetime }; var argStr = JsonConvert.SerializeObject(arg); ProducerMQ(argStr); return(new Result(true, "校验成功,已申请积分!", null)); } return(new Result(false, "校验成功,对ApplyPoint操作失败!", null)); } catch (Exception ex) { Log.Error("CreateApplyPoint", ex); return(new Result(false, ex.Message, null)); } }
public static async Task <Result> ReceiptOCR(string Id) { try { var ApplyPointModel = dal.GetModel <ApplyPoint>($"and ApplyPointID='{Id}'"); //根据Id 获取 积分申请 if (ApplyPointModel == null) { return(new Result(false, "积分申请表查无数据", null)); } var ReceiptOCRResult = await Task.Run(async() => { #region 调用BAIDUOCR API,并将识别数据添加至原始数据表 var Image = (Bitmap)Bitmap.FromFile(ApplyPointModel.ReceiptPhoto); var base64 = Commom.ImgToBase64String(Image); //拿到积分申请中的图片转为Base64 base64 = System.Web.HttpUtility.UrlEncode(base64); //byte[] bytes = Convert.FromBase64String(base64); //var result = Commom.GeneralOCRBasic(s => s.Receipt(bytes)); //暂时为票据服务识别 //根据MallOCRRule配置的OCR相关配置进行OCR请求,暂不用SDK,缓存处理 MallOCRRule MallOCRRule = CacheHandle <MallOCRRule>($"MallOCRRule{Id}", 1, $" and MallID='{ApplyPointModel.MallID}'"); var result = HttpHelper.HttpPost(string.Format(MallOCRRule.OCRServerURL, MallOCRRule.OCRServerToken), $"image={base64}"); if (result.Contains("error_msg")) { var OCRErrorModel = JsonConvert.DeserializeObject <OCRErrorResult>(result); return(new Result(false, OCRErrorModel.error_msg, null)); } var RecongnizeModel = new ApplyPictureRecongnize { id = Guid.NewGuid(), applyid = Guid.Parse(Id), Lineno = 0, LineContent = result }; var AddResult = DbContext.Add(RecongnizeModel); //识别内容添加到识别原始表 #endregion if (AddResult == 0) { ApplyPointModel.RecongizeStatus = 1; var UpadateResult = DbContext.Update(ApplyPointModel); //已解析原始数据 if (UpadateResult) { //根据商铺规则明细取到OCR结果 var ApplyOCRResult = GetApplyPointOCRResult(Guid.Parse(Id), ApplyPointModel.StoreID, result); if (ApplyOCRResult.Success) { ApplyPointOCRResult OCRResult = (ApplyPointOCRResult)ApplyOCRResult.Data; //匹配商铺规则 var VerifyResult = VerifyOCRResult(OCRResult); if (VerifyResult.Success) { //更新积分申请表 ApplyPointModel.RecongizeStatus = 2; //成功匹配 ApplyPointModel.AuditDate = DateTime.Now; //修改审批日期 if (DbContext.Update(ApplyPointModel)) { Store StoreModel = CacheHandle <Store>($"Store{ApplyPointModel.StoreID}", 1, $" and StoreId = '{ApplyPointModel.StoreID}'"); OrgInfo orgInfo = CacheHandle <OrgInfo>($"OrgInfo{StoreModel.OrgID}", 24, $" and OrgId = '{StoreModel.OrgID}'"); Company company = CacheHandle <Company>($"Company{StoreModel.CompanyID}", 24, $" and CompanyId = '{StoreModel.CompanyID}'"); Card card = dal.GetModel <Card>($" and CardID = '{ApplyPointModel.CardID}'"); //自动积分 var webPosArg = new WebPosArg { cardID = card.CardCode, companyID = company.CompanyCode, orgID = orgInfo.OrgCode, storeID = StoreModel.StoreCode, cashierID = "crm", discountPercentage = 0, receiptNo = ApplyPointModel.ReceiptNo, txnDateTime = ApplyPointModel.TransDate }; var webPosResult = await WebPosForPoint(webPosArg); return(webPosResult); } else { return(new Result(false, "OCR信息校验成功后修改积分申请表失败", null)); } } else { return(new Result(false, VerifyResult.Message, null)); } } else { ApplyPointModel.RecongizeStatus = 3; //成功匹配 DbContext.Update(ApplyPointModel); return(new Result(false, ApplyOCRResult.Message, null)); } } else { return(new Result(false, "修改积分申请表失败", null)); } } else { return(new Result(false, "添加到原始表失败", null)); } }); return(ReceiptOCRResult); } catch (Exception ex) { Log.Error("ReceiptOCR", ex); return(new Result(false, ex.Message, null)); } }
/// <summary> /// 从OCR接口中 根据规则 获取详细内容 (暂无校验) /// </summary> /// <param name="OcrResult"></param> /// <returns></returns> private static Result RecognitOCRResult(string OcrResult) { try { var OCRResultModel = JsonConvert.DeserializeObject <OCRResult>(OcrResult); var WordList = OCRResultModel.words_result; //被识别的内容 var OCRStoreName = string.Empty; //被识别出来的商铺名称 //查询所有的商铺规则并缓存 List <StoreOCRDetail> AllStoreOCRDetailRuleList = CacheHandle <List <StoreOCRDetail> >( Key: "AllStoreOCRDetailRuleList" , Hour: double.Parse(ConfigurationUtil.GetSection("ObjectConfig:CacheExpiration:AllStoreOCRDetailRuleList")) , sqlWhere: ""); //所有商铺名称规则 var StoreNameRuleList = AllStoreOCRDetailRuleList.Where(s => s.OCRKeyType == (int)OCRKeyType.StoreName).Select(c => c.OCRKey).ToList(); var resultStoreName = FindStoreNameFromAllRule(WordList, StoreNameRuleList);//根据所有的店铺名规则匹配出的StoreName var StoreModel = dal.GetModel <Store>($" and StoreName like '%{resultStoreName}%'"); if (StoreModel == null) { return(new Result(false, "识别商铺名称失败!", "")); } //最后识别结果 var ReceiptOCRModel = new ReceiptOCR { StoreId = StoreModel.StoreId, StoreName = resultStoreName, StoreCode = StoreModel.StoreCode, ReceiptNo = "", TranAmount = 0, TransDatetime = Commom.DefaultDateTime, RecongnizelId = Guid.Empty, Base64 = "" }; //当前店铺的规则明细 var ThisStoreOCRDetail = AllStoreOCRDetailRuleList.Where(s => s.StoreId == StoreModel.StoreId).ToList(); //根据店铺规则明细 关键字类型 关键字 取值方法 匹配识别结果 foreach (var StoreDetailRule in ThisStoreOCRDetail) { for (int i = 0; i < WordList.Count(); i++) { Result ReturnResult = GetValue(WordList, i, StoreDetailRule); //根据规则取值 if (ReturnResult.Success) { var ReturnData = ReturnResult.Data.ToString(); if (!string.IsNullOrWhiteSpace(ReturnData)) { switch (StoreDetailRule.OCRKeyType) //枚举有注释,根据关键字类型赋值 { case (int)OCRKeyType.StoreName: continue; case (int)OCRKeyType.ReceiptNO: if (!string.IsNullOrWhiteSpace(ReturnData) && string.IsNullOrWhiteSpace(ReceiptOCRModel.ReceiptNo)) { ReceiptOCRModel.ReceiptNo = ReturnResult.Data.ToString(); continue; } break; case (int)OCRKeyType.DateTime: if (!string.IsNullOrWhiteSpace(ReturnData) && ReceiptOCRModel.TransDatetime == Commom.DefaultDateTime) { ReturnData = ReturnData.Replace(" ", "").Insert(10, " "); //可能会识别成 2019 - 05 - 1512:14:44 转datetime 报错 if (DateTime.TryParse(ReturnData, out var DateTimeResult)) { ReceiptOCRModel.TransDatetime = DateTimeResult; continue; } } break; case (int)OCRKeyType.Amount: if (!string.IsNullOrWhiteSpace(ReturnData) && ReceiptOCRModel.TranAmount == 0) { if (decimal.TryParse(ReturnResult.Data.ToString(), out var AmountResult)) { ReceiptOCRModel.TranAmount = AmountResult; continue; } } break; default: return(new Result(false, $"商铺未设置该关键字类型取值方法:{StoreDetailRule.OCRKeyType}", null)); } } } } } var RecongnizeModelId = Guid.NewGuid(); var RecongnizeModel = new ApplyPictureRecongnize { id = RecongnizeModelId, applyid = Guid.Empty, Lineno = 0, LineContent = JsonConvert.SerializeObject(WordList), OCRResult = JsonConvert.SerializeObject(ReceiptOCRModel) }; var AddResult = DbContext.Add(RecongnizeModel);//添加原始数据 applyid 等待积分申请 //添加成功后 出参RecongnizeModelId if (AddResult == 0) { ReceiptOCRModel.RecongnizelId = RecongnizeModelId; } else { return(new Result(false, "添加到ApplyPictureRecongnize失败", null)); } return(new Result(true, "识别成功", ReceiptOCRModel)); } catch (Exception ex) { Log.Error("VerifyOCRResult", ex); return(new Result(false, ex.Message, null)); } //同步寻找 string FindStoreNameFromAllRule(List <Words> words, List <string> StoreNameList) { foreach (var Name in StoreNameList) { foreach (var word in words) { if (word.words.Contains(Name)) { return(word.words); } } } return(""); } //异步寻找 async Task <string> FindStoreNameFromAllRuleAsync(List <Words> words, List <string> StoreNameList) { var Sresult = ""; foreach (var names in StoreNameList) { var LineCount = words.Count(); //识别内容数量 //二分异步寻找 var afterLine = LineCount % 2 == 1 ? (LineCount - 1) / 2 : LineCount / 2; //前一半 var beforeLine = LineCount - afterLine; //后一半 CancellationTokenSource ct = new CancellationTokenSource(); for (int i = 0; i < 2; i++) { var min = i % 2 == 0 ? 0 : afterLine; var max = i % 2 == 0 ? afterLine - 1 : LineCount; await Task.Run(() => { for (int j = min; j < max; j++) { if (ct.IsCancellationRequested) { if (words[j].words.Contains(names)) { ct.Cancel(); Sresult = words[j].words; } } } }, ct.Token); } } return(""); } //根据规则获取指定识别内容 Result GetValue(List <Words> words_result, int index, StoreOCRDetail StoreDetailRule) { var WordValue = ""; if (words_result[index].words.Contains(StoreDetailRule.OCRKey)) { switch (StoreDetailRule.GetValueWay)//可查看枚举注释 { case (int)GetValueWay.OCRKey: WordValue = words_result[index].words; break; case (int)GetValueWay.AfterOCRKey: var IndexOfKey = words_result[index].words.IndexOf(StoreDetailRule.OCRKey) + StoreDetailRule.OCRKey.Length + StoreDetailRule.SkipLines; WordValue = words_result[index].words.Substring(IndexOfKey); break; case (int)GetValueWay.NextLine: if (index + StoreDetailRule.SkipLines <= words_result.Count()) { WordValue = words_result[index + StoreDetailRule.SkipLines].words; } break; default: return(new Result(false, $"商铺未设置该关键字取值方法:{StoreDetailRule.GetValueWay}", null)); } } return(new Result(true, "", WordValue)); } }