Esempio n. 1
0
        /// <summary>
        /// 小票OCR结果匹配商铺自动积分规则
        /// </summary>
        /// <param name="OCRResult"></param>
        /// <returns></returns>
        private static Result VerifyOCRResult(ApplyPointOCRResult OCRResult)
        {
            //是否全部识别
            if (string.IsNullOrWhiteSpace(OCRResult.StoreCode) || OCRResult.TranAmount == 0 || OCRResult.TransDatetime == DefaultDateTime || string.IsNullOrWhiteSpace(OCRResult.ReceiptNo))
            {
                OCRResult.Status = 1;
            }
            else
            {
                OCRResult.Status = 2;
            }

            if (DbContext.Add(OCRResult) == 0) //添加至积分申请识别结果表
            {
                if (OCRResult.needVerify == 1) //当需要需要校验
                {
                    var ApplyPointModel = dal.GetModel <ApplyPoint>($"and ApplyPointID='{OCRResult.applyid}'");

                    Store StoreModel = CacheHandle <Store>($"Store{OCRResult.StoreID}", 1, $" and StoreId = '{OCRResult.StoreID}'");
                    if (string.IsNullOrWhiteSpace(OCRResult.StoreCode) || StoreModel.StoreCode != OCRResult.StoreCode)
                    {
                        return(new Result(false, "OCR店铺Code未识别正确!", null));
                    }

                    if (OCRResult.TransDatetime == DefaultDateTime && OCRResult.TransDatetime.ToString("yyyy-MM-dd") != ApplyPointModel.TransDate.ToString("yyyy-MM-dd"))
                    {
                        return(new Result(false, "OCR交易日期未识别正确!", null));
                    }

                    StoreOCR StoreOCRRule = CacheHandle <StoreOCR>($"StoreOCR{OCRResult.StoreID}", 0.5, $"and StoreId = '{OCRResult.StoreID.ToString()}'");

                    if (OCRResult.TranAmount < StoreOCRRule.MinValidReceiptValue || OCRResult.TranAmount > StoreOCRRule.MaxValidReceiptValue)
                    {
                        return(new Result(false, "OCR小票金额不在店铺规则范围之内!", null));
                    }

                    if (OCRResult.TranAmount != ApplyPointModel.TransAmt)
                    {
                        return(new Result(false, "OCR小票金额不一致!", null));
                    }

                    if (StoreOCRRule.MaxTicketPerDay != 0)                                                 //日自动交易笔数为0时 代表不限制
                    {
                        var TicketPerDay = DbContext.Query <int>($@"select count(*) from ApplyPoint
                               where StoreID = '{OCRResult.StoreID.ToString()}' and VerifyStatus = 1 
                               and SourceType=7 and DATEDIFF(dd,AuditDate,GETDATE())=0").FirstOrDefault(); //当日交易笔数
                        if (TicketPerDay >= StoreOCRRule.MaxTicketPerDay)
                        {
                            return(new Result(false, "今日已超过最大自动积分记录数量", null));
                        }
                    }

                    if ((StoreModel.IsStandardPOS == "1" ? 0 : 1) != StoreOCRRule.POSType)
                    {
                        return(new Result(false, "OCR商铺POS类型不一致", null));
                    }

                    MallOCRRule MallOCRRule = CacheHandle <MallOCRRule>($"MallOCRRule{OCRResult.StoreID}", 1, $" and MallID='{StoreModel.MallID}'");

                    var posUrl   = MallOCRRule.POSServeURL;
                    var posToken = MallOCRRule.POSServerToken;
                    var posUser  = MallOCRRule.POSServerUser;
                    var userPwd  = MallOCRRule.POSServerPassword;

                    //请求POS POSSID
                    //if (POSSID != StoreOCRRule.POSSID)
                    //{
                    //    return new Result(false, "OCR商铺POS代码不一致", null);
                    //}
                }
            }
            else
            {
                return(new Result(false, "ApplyPointOCRResult添加失败", null));
            }


            OCRResult.VerifyStatus = 1;
            if (DbContext.Update(OCRResult))
            {
                return(new Result(true, "", null));
            }
            else
            {
                return(new Result(false, "ApplyPointOCRResult修改VerifyStatus失败", null));
            }
        }
Esempio n. 2
0
        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));
            }
        }