Ejemplo n.º 1
0
        /// <summary>
        /// 数据验证
        /// </summary>
        /// <param name="et"></param>
        /// <param name="strTravelBatchId">当前批次Id</param>
        /// <returns></returns>
        private bool ExcelTable2EntityList(ExeclTalbe et, string strTravelBatchId)
        {
            var modeles = new List <BaseRiskModel>();

            var result = true;

            Parallel.ForEach(et.rows, new ParallelOptions()
            {
                MaxDegreeOfParallelism = 8
            }, row =>
            {
                try
                {
                    //TODO 调整格式验证
                    var model = new BaseRiskModel();

                    model.PayCenterCode = Parameter.PayCenterCode;

                    model.PayCenterName = Parameter.PayCenterName;

                    model.TravelBatchId = strTravelBatchId;

                    //票价
                    var price = 0.0m;
                    if (Decimal.TryParse(row.columns[0].ColumnValue.Trim(), out price) && 0.0m < price)
                    {
                        model.TicketPrice = price;
                    }
                    //订单价
                    if (Decimal.TryParse(row.columns[1].ColumnValue.Trim(), out price) && 0.0m < price)
                    {
                        model.OrderAmount = price;
                    }

                    DateTime cellDate;

                    if (DateTime.TryParse(row.columns[2].ColumnValue.Trim(), out cellDate) && 1990 < cellDate.Year)
                    {
                        //起飞时间
                        model.DepartureTime = cellDate;
                    }
                    else
                    {
                        throw new Exception("起飞时间格式不正确");
                    }

                    if (DateTime.TryParse(row.columns[9].ColumnValue.Trim(), out cellDate) && 1990 < cellDate.Year)
                    {
                        //出票时间
                        model.TicketTime = cellDate;
                    }
                    else
                    {
                        throw new Exception("出票时间格式不正确");
                    }

                    //出发机场3字码
                    model.DepartCode = row.columns[3].ColumnValue.Trim();

                    if (!Regex.IsMatch(model.DepartCode, "^[A-Za-z]{3}$"))
                    {
                        throw new Exception("出发机场三字码格式不正确(例:PEK)!");
                    }

                    //到达机场
                    model.ArriveCode = row.columns[4].ColumnValue.Trim();

                    if (!Regex.IsMatch(model.DepartCode, "^[A-Za-z]{3}$"))
                    {
                        throw new Exception("到达机场三字码格式不正确(例:PEK)!");
                    }

                    //航班号
                    model.FlightNo = row.columns[5].ColumnValue.Trim();

                    if (!Regex.IsMatch(model.FlightNo, "^[A-Za-z0-9]{5,6}$"))
                    {
                        throw new Exception("航班号格式不正确(例:CA5698或CA234)!");
                    }

                    //仓位
                    model.Cabin = row.columns[6].ColumnValue.Trim();

                    if (!Regex.IsMatch(model.Cabin, "^[A-Za-z0-9]{1,2}$"))
                    {
                        throw new Exception("舱位格式不正确(例:Y或Z1)!");
                    }

                    //乘客姓名-不验证
                    model.PersonName = row.columns[7].ColumnValue.Trim();
                    if (string.IsNullOrEmpty(model.PersonName))
                    {
                        throw new Exception("乘客姓名不能为空!");
                    }

                    //票号13位数字
                    model.TicketNum = row.columns[8].ColumnValue.Replace("-", "").Trim();

                    if (!Regex.IsMatch(model.TicketNum, "^((?=.*[0-9].*))[0-9]{13}$"))
                    {
                        throw new Exception("票号格式不正确!");
                    }

                    model.PNR = row.columns[10].ColumnValue.Trim();

                    if (!Regex.IsMatch(model.PNR, "^[A-Za-z0-9]{6}$"))
                    {
                        throw new Exception("PNR格式不正确!");
                    }

                    if (1 > row.columns[11].ColumnValue.Split('|').Length)
                    {
                        throw new Exception("保理企业格式错误!");
                    }
                    var enterpriseId = 0;

                    Int32.TryParse(row.columns[11].ColumnValue.Split('|')[1].Trim(), out enterpriseId);
                    model.EnterpriseID = enterpriseId;

                    model.EnterpriseName = row.columns[11].ColumnValue.Split('|')[0];

                    //数据格式验证
                    modeles.Add(model);
                }
                catch (Exception e)
                {
                    this._verifyResults.Add(new Tuple <string, string>("", row.RIndex + "行" + e.Message));
                    result = false;
                }
            });

            _manageRiskModels = modeles.GroupBy(t => t.EnterpriseID).ToDictionary(t => t.Key, t => t.ToList());

            return(result);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// TODO TODO TODO ....
        /// </summary>
        protected override void ExecuteMethod()
        {
            try
            {
                if (null == _fileStream && !BaseRiskModels.Any())
                {
                    throw  new Exception("选择要上传的文件");
                }

                UserAccount ua = new UserAccount()
                {
                    PayCenterCode = Parameter.PayCenterCode
                };
                _userAccount = UserAccountRep.GetUserAccount(ua);

                if (!ValidUserFactoring())
                {
                    throw new Exception("未配置保理企业;请联系相关人员处理!");
                }
                //生成批次号
                var batchNo = DateTime.Now.Ticks;

                //文件上传
                if (null != _fileStream)
                {
                    ExeclTalbe et = ExcelHelper.ExcelImport(_fileStream);

                    //去除标题
                    et.rows.RemoveAt(0);

                    Debug.WriteLine("Excel 总条数:{0}", et.rows.Count);

                    //数据格式验证
                    if (!ExcelTable2EntityList(et, "" + batchNo))
                    {
                        this.Result.Data      = this._verifyResults.Select(t => t.Item2);
                        this.Result.Message   = "Excel数据验证失败!";
                        this.Result.ErrorCode = "200";
                        return;
                    }
                }
                else
                {
                    _manageRiskModels = BaseRiskModels.GroupBy(t => t.EnterpriseID)
                                        .ToDictionary(t => t.Key, t => t.ToList());
                }

                Debug.WriteLine("格式转换后条数:{0},错误数据:{1}", _manageRiskModels.Count, _verifyResults.Select(t => t.Item2));

                //去重,Excel中数据重复
                if (!RemovalTicket())
                {
                    this.Result.Message   = string.Join(",", this._verifyResults.Select(t => t.Item2));
                    this.Result.ErrorCode = "200";
                    return;
                }

                Debug.WriteLine("去重后条数:{0},错误数据:{1}", _manageRiskModels.Count, _verifyResults.Select(t => t.Item2));

                //验证企业状态
                if (!VerifyEnterpriseStatus())
                {
                    this.Result.Data    = "企业信息验证失败!";
                    this.Result.Message = JsonConvert.SerializeObject(this._verifyResults.Select(t => t.Item2));
                    return;
                }

                Debug.WriteLine("企业信息验证结果:{0},错误数据:{1}", _manageRiskModels.Count, _verifyResults.Select(t => t.Item2));

                //验证风控
                VerifyRisk();

                Debug.WriteLine("风控验证结果:{0},错误数据:{1}", _manageRiskModels.Count, _verifyResults.Select(t => t.Item2));

                //验证分销商余额
                if (!VerifyDistrbutorBalance())
                {
                    this.Result.Message   = "分销商余额不足!";
                    this.Result.ErrorCode = "200";
                    return;
                }

                Debug.WriteLine("分销商月限额验证结果");

                //异常信息返回
                if (0 < _verifyResults.Count)
                {
                    Result.Message        = string.Join("\r\n", _verifyResults.Select(t => t.Item2));
                    this.Result.ErrorCode = "200";
                }

                if (0 < _manageRiskModels.Count)
                {
                    //入库&写入队列
                    SaveOrderData();

                    Result.Message += "\r\n 成功上传条数:" + (_manageRiskModels.Values.SelectMany(x => x).ToList().Count);
                }
            }
            catch (Exception ex)
            {
                LoggerFactory.Instance.Logger_Error(ex, "TravelOrderImportService");
                this.Result.ErrorCode = "200";
                this.Result.Message   = "服务端程序错误,请联系相关人员!";
            }
        }