Beispiel #1
0
        public async Task <HttpResponseMessage> GetReportClaim([FromUri] GetReportClaimRequest request)
        {
            logInfo.Info(string.Format("【收费服务】获取车辆【报备】信息接口请求串:{0}", Request.RequestUri));
            var viewModel = new ReportClaimHistoryViewModel();

            if (!ModelState.IsValid)
            {
                viewModel.BusinessStatus = -10000;
                string msg = ModelState.Values.Where(item => item.Errors.Count == 1).Aggregate(string.Empty, (current, item) => current + (item.Errors[0].ErrorMessage + ";   "));
                viewModel.StatusMessage = "输入参数错误," + msg;
                return(viewModel.ResponseToJson());
            }
            if (!request.LicenseNo.IsValidLicenseno())
            {
                viewModel.BusinessStatus = -10000;
                viewModel.StatusMessage  = "参数校验错误,请检查车牌号";
                return(viewModel.ResponseToJson());
            }
            try
            {
                var response = await _chargeService.GetReportClaim(request, Request.GetQueryNameValuePairs());

                if (response.Status == HttpStatusCode.RequestTimeout)
                {
                    viewModel.BusinessStatus = -10004;
                    viewModel.StatusMessage  = "请求超时 ,请稍候再试";
                    return(viewModel.ResponseToJson());
                }
                if (response.Status == HttpStatusCode.BadRequest || response.Status == HttpStatusCode.Forbidden)
                {
                    viewModel.BusinessStatus = -10001;
                    viewModel.StatusMessage  = "参数校验错误,请检查您的校验码";
                    return(viewModel.ResponseToJson());
                }
                if (response.Status == HttpStatusCode.ExpectationFailed)
                {
                    viewModel.BusinessStatus = -10003;
                    viewModel.StatusMessage  = "服务发生异常";
                }
                else
                {
                    viewModel.ReportClaims     = new List <ReportClaim>();
                    viewModel.HistoryContracts = new List <HistoryContract>();
                    if (response.ErrCode == 0)
                    {
                        viewModel.ReportClaims     = response.ClaimList.ConvertToReportClaimsViewModel();
                        viewModel.HistoryContracts = response.ContractList.ConvertToHistoryContract();
                        viewModel.LicenseNo        = request.LicenseNo;
                        viewModel.TotalCount       = response.TotalCount;
                        viewModel.UsedCount        = response.UsedCount;
                        viewModel.BusinessStatus   = 1;
                        viewModel.StatusMessage    = "成功";
                    }
                    else if (response.ErrCode == -5)
                    {
                        viewModel.BusinessStatus = -5;//余额不足
                        viewModel.StatusMessage  = "余额不足";
                    }
                    else if (response.ErrCode == -4)
                    {
                        viewModel.BusinessStatus = -4;//
                        viewModel.StatusMessage  = "没有获取到理赔信息";
                    }
                    else if (response.ErrCode == -3)
                    {
                        viewModel.BusinessStatus = -3;//
                        viewModel.StatusMessage  = "没有获取历史承保信息信息";
                    }
                    else if (response.ErrCode == -1202)
                    {
                        viewModel.BusinessStatus = -1;//请提供正确的车架号和发动机号
                        viewModel.StatusMessage  = "请提供正确的车架号和发动机号";
                    }
                    else if (response.ErrCode == -2)
                    {
                        viewModel.BusinessStatus = -2;//没有获取到
                        viewModel.StatusMessage  = "没有查询到车辆信息";
                    }
                }
            }
            catch (Exception ex)
            {
                logError.Info("发生异常:" + ex.Source + "\n" + ex.StackTrace + "\n" + ex.Message + "\n" + ex.InnerException);
            }

            return(viewModel.ResponseToJson());
        }
        public async Task <GetReportClaimResponse> GetReportClaim(GetReportClaimRequest request, IEnumerable <KeyValuePair <string, string> > pairs)
        {
            var response = new GetReportClaimResponse();

            //根据经纪人获取手机号
            IBxAgent agentModel = GetAgentModelFactory(request.Agent);

            //logInfo.Info("获取到的经纪人信息:"+agentModel.ToJson());
            //参数校验
            if (!agentModel.AgentCanUse())
            {
                response.Status = HttpStatusCode.Forbidden;
                return(response);
            }
            if (agentModel.IsUsed != 1)
            {
                response.Status = HttpStatusCode.Forbidden;
                return(response);
            }

            if (!ValidateReqest(pairs, agentModel.SecretKey, request.SecCode))
            {
                response.Status = HttpStatusCode.Forbidden;
                return(response);
            }

            bool      isSucceed = false;
            bx_charge charge    = _chargeRepository.Find(request.Agent, request.BusyKey); //2 :报备类型

            if (charge == null)
            {
                response.Status = HttpStatusCode.BadRequest;
                return(response);
            }

            try
            {
                string reportClaimFlagKey      = string.Format("{0}_claim_key", request.LicenseNo);
                string reportClaimCacheKey     = string.Format("{0}_{1}", request.LicenseNo, "reportClaim");
                string historyContractCacheKey = string.Format("{0}_{1}", request.LicenseNo, "historyContract");
                //CacheProvider.Remove(reportClaimCacheKey);
                //CacheProvider.Remove(historyContractCacheKey);
                //CacheProvider.Remove(reportClaimFlagKey);
                var reportClaimFlagCache = CacheProvider.Get <string>(reportClaimFlagKey);
                if (reportClaimFlagCache != null)
                {
                    isSucceed = true;
                    var reportClaimCache     = CacheProvider.Get <List <bx_report_claim> >(reportClaimCacheKey);
                    var historyContractCache = CacheProvider.Get <List <bx_history_contract> >(historyContractCacheKey);
                    response.ClaimList    = reportClaimCache;
                    response.ContractList = historyContractCache;
                }
                else
                {
                    //发送请求 checkneed
                    using (var client = new HttpClient())
                    {
                        client.Timeout = TimeSpan.FromMinutes(1);
                        string _choosebaobei = System.Configuration.ConfigurationManager.AppSettings["ChooseBaoBei"];
                        var    choseVal      = string.IsNullOrWhiteSpace(_choosebaobei) ? 0 : int.Parse(_choosebaobei);
                        var    baobeiUrl     = string.Empty;
                        string getUrl        = string.Empty;
                        if (choseVal == 0)
                        {
                            baobeiUrl = _baobeizhlhurl;
                            getUrl    =
                                string.Format(
                                    "/api/ZhongHua/GetUnderwriteClaimsInfo?licenceCode={0}&engineNo={1}&carVin={2}&type={3}",
                                    request.LicenseNo, request.EngineNo, request.CarVin, request.Type);
                        }
                        else
                        {
                            baobeiUrl = _baobeigscurl;
                            getUrl    =
                                string.Format(
                                    "/api/GuoShouCai/GetUnderwriteClaimsInfo?licenceCode={0}&engineNo={1}&carVin={2}&type={3}",
                                    request.LicenseNo, request.EngineNo, request.CarVin, request.Type);
                        }
                        client.BaseAddress = new Uri(baobeiUrl);
                        //getUrl =
                        //   string.Format(
                        //       "/api/GuoShouCai/GetUnderwriteClaimsInfo?licenceCode={0}&engineNo={1}&carVin={2}&type={3}",
                        //       request.LicenseNo, request.EngineNo, request.CarVin, request.Type);
                        HttpResponseMessage res = await client.GetAsync(getUrl);

                        if (res.IsSuccessStatusCode)
                        {
                            WaGuoShouCaiQuoteResponse result =
                                await res.Content.ReadAsAsync <WaGuoShouCaiQuoteResponse>();

                            if (result.ErrCode == 0)
                            {
                                var reportClaimList           = result.GuoShouCaiQuoteResult.ClaimsLinsInfo;
                                var existList                 = _reportClaimRepository.FindList(request.LicenseNo);
                                List <bx_report_claim> claims = new List <bx_report_claim>();
                                if (reportClaimList.Count != existList.Count)
                                {
                                    if (reportClaimList.Count > 0)
                                    {
                                        _reportClaimRepository.Remove(request.LicenseNo);
                                        claims =
                                            reportClaimList.Select(claimsListInfo => new bx_report_claim
                                        {
                                            AccidentPlace = claimsListInfo.AccidentPlace,
                                            AccidentPsss  = claimsListInfo.AccidentPsss,
                                            DriverName    = claimsListInfo.DriverName,
                                            IsCommerce    = claimsListInfo.IsCommerce,
                                            IsOwners      = claimsListInfo.IsOwners,
                                            IsThreecCar   = claimsListInfo.IsThreecCar,
                                            LicenseNo     = request.LicenseNo,
                                            ReportDate    =
                                                string.IsNullOrWhiteSpace(claimsListInfo.ReportDate)
                                                        ? DateTime.MinValue
                                                        : DateTime.Parse(claimsListInfo.ReportDate),
                                        }).ToList();
                                        _reportClaimRepository.Add(claims);
                                    }
                                }
                                else
                                {
                                    claims = existList;
                                }
                                //放入缓存
                                CacheProvider.Set(reportClaimCacheKey, claims, 86400);
                                response.ClaimList = claims;

                                var historyList      = result.GuoShouCaiQuoteResult.HistoryContractInfo;
                                var existHistoryList = _historyClaimRepository.FindList(request.LicenseNo);
                                List <bx_history_contract> historyContracts = new List <bx_history_contract>();
                                if (historyList != null && existHistoryList.Count != historyList.Count)
                                {
                                    if (historyList.Count > 0)
                                    {
                                        _historyClaimRepository.Remove(request.LicenseNo);
                                        historyContracts = historyList.Select(info => new bx_history_contract
                                        {
                                            Enddate           = DateTime.Parse(info.Enddate),
                                            InsureCompanyName = info.InsureCompanyName,
                                            IsCommerce        = info.IsCommerce,
                                            LicenseNo         = request.LicenseNo,
                                            PolicyNo          = info.PolicyNo,
                                            Strdate           = DateTime.Parse(info.Strdate),
                                        }).ToList();
                                        _historyClaimRepository.Add(historyContracts);
                                    }
                                }
                                else
                                {
                                    historyContracts = existHistoryList;
                                }
                                //放入缓存
                                CacheProvider.Set(historyContractCacheKey, historyContracts, 86400);
                                response.ContractList = historyContracts;


                                CacheProvider.Set(reportClaimFlagKey, 1, 86000);
                                isSucceed = true;
                                //#region

                                //////更新bx_charge
                                //if (charge.total_count == charge.used_count)
                                //{
                                //    //余额不足
                                //    response.Status = HttpStatusCode.OK;
                                //    response.ErrCode = -2;
                                //    response.ClaimList = new List<bx_report_claim>();
                                //    response.ContractList = new List<bx_history_contract>();
                                //    return response;
                                //}

                                //charge.used_count += 1;
                                //charge.update_time = DateTime.Now;
                                //var chargeCount = _chargeRepository.Update(charge);
                                //if (chargeCount == 1)
                                //{
                                //    //插入bx_charge_history
                                //    var history = new bx_charge_history
                                //    {
                                //        charge_id = charge.id,
                                //        license_no = request.LicenseNo,
                                //        result_status = 1 //成功

                                //    };
                                //    _chargeHistoryRepository.Add(history);
                                //}
                                //response.TotalCount = charge.total_count.HasValue ? charge.total_count.Value : 0;
                                //response.UsedCount = charge.used_count.HasValue ? charge.used_count.Value : 0;
                                ////response.List = result.ClaimList;
                                //response.Status = HttpStatusCode.OK;
                                //response.ErrCode = 0;
                                //return response;

                                //#endregion
                            }
                            else
                            {
                                CacheProvider.Remove(reportClaimFlagKey);
                                //插入bx_charge_history
                                var history = new bx_charge_history
                                {
                                    charge_id     = charge.id,
                                    license_no    = request.LicenseNo,
                                    result_status = 0 //失败
                                };
                                _chargeHistoryRepository.Add(history);
                                response.Status  = HttpStatusCode.OK;
                                response.ErrCode = result.ErrCode; //-2:查询车辆信息失败    -1202:请提供正确的车架号和发动机号 -3:获取历史承保信息信息失败  -4:获取理赔信息失败
                                return(response);
                            }
                        }
                        else
                        {
                            logError.Info("收费服务-获取车辆出险信息发生异常:调用接口getclaimhistory失败");
                        }
                    }
                }
                if (isSucceed)
                {
                    //更新bx_charge
                    if (charge.total_count <= charge.used_count)
                    {
                        //余额不足
                        response.Status       = HttpStatusCode.OK;
                        response.ClaimList    = new List <bx_report_claim>();
                        response.ContractList = new List <bx_history_contract>();
                        response.TotalCount   = charge.total_count.HasValue ? charge.total_count.Value : 0;
                        response.UsedCount    = charge.used_count.HasValue ? charge.used_count.Value : 0;
                        response.ErrCode      = -5;
                        return(response);
                    }

                    charge.used_count += 1;
                    charge.update_time = DateTime.Now;
                    var chargeCount = _chargeRepository.Update(charge);
                    if (chargeCount == 1)
                    {
                        //插入bx_charge_history
                        var history = new bx_charge_history
                        {
                            charge_id     = charge.id,
                            license_no    = request.LicenseNo,
                            result_status = 1 //成功
                        };
                        _chargeHistoryRepository.Add(history);
                    }
                    response.TotalCount = charge.total_count.HasValue ? charge.total_count.Value : 0;
                    response.UsedCount  = charge.used_count.HasValue ? charge.used_count.Value : 0;
                    //response.List = result.ClaimList;
                    response.Status  = HttpStatusCode.OK;
                    response.ErrCode = 0;
                }
                else
                {
                    CacheProvider.Remove(reportClaimFlagKey);
                }
                return(response);
            }
            catch (TaskCanceledException ex)
            {
                response.Status = HttpStatusCode.RequestTimeout;
            }
            catch (Exception ex)
            {
                response.Status = HttpStatusCode.ExpectationFailed;
                logError.Info("收费服务-获取车辆出险信息发生异常:" + ex.Source + "\n" + ex.StackTrace + "\n" + ex.Message + "\n" + ex.InnerException);
            }

            return(response);
        }