Exemplo n.º 1
0
        /// <summary>
        /// 处理返回报价信息,写分成
        /// </summary>
        /// <param name="carNumber"></param>
        /// <param name="batchId"></param>
        /// <param name="rate"></param>
        /// <param name="rateType"></param>
        /// <param name="listPriceModel"></param>
        /// <returns></returns>
        private async Task UpdateAskPriceViolationInfo(string carNumber, string batchId, decimal rate, int rateType,
                                                       List <PriceResultOutput> listPriceModel)
        {
            if (!listPriceModel.IsNullOrEmpty())
            {
                //获取全部违章Code
                var uniqueCodes = listPriceModel.Select(x => x.UniqueCode).ToList();
                if (!uniqueCodes.IsNullOrEmpty())
                {
                    var carIds = await _batchCarRepository.GetAll()
                                 .Where(i => i.CarNumber == carNumber && i.BatchId == batchId).Select(i => i.Id)
                                 .ToDynamicListAsync <string>();

                    Expression <Func <BatchAskPriceViolationAgent, bool> > violationWhere = i =>
                                                                                            i.PriceFrom == 0 && uniqueCodes.Contains(i.Uniquecode);
                    if (!carIds.IsNullOrEmpty())
                    {
                        violationWhere = violationWhere.And(i => carIds.Contains(i.CarId));
                    }
                    //获取全部违章
                    var violations = await _violationAgentRepository.GetAllListAsync(violationWhere);

                    var violationIds = violations.Select(i => i.Id).ToList();
                    if (!violations.IsNullOrEmpty())
                    {
                        //插入的新分成
                        var newViolationDivisions = new List <CarViolationDivision>();

                        //获取已存在的分成
                        var oldViolationDivisions = await _carViolationDivisionRepository.GetAllListAsync(i =>
                                                                                                          violationIds.Contains(i.ViolationId));

                        //处理数据
                        foreach (var item in violations)
                        {
                            if (item == null)
                            {
                                continue;
                            }
                            decimal vat        = 0M;
                            decimal price      = 0M;
                            var     priceModel = listPriceModel.FirstOrDefault(x => x.UniqueCode == item.Uniquecode);

                            if (priceModel == null)
                            {
                                continue;
                            }

                            if (priceModel.CanProcess == 1)
                            {
                                #region 分成
                                //获取接口的分成保存到分成表中
                                if (!priceModel.fcQuery.IsNullOrEmpty())
                                {
                                    newViolationDivisions = priceModel.fcQuery.Select(fcEntity =>
                                                                                      new CarViolationDivision
                                    {
                                        CalculationExpression = "",
                                        Fc          = fcEntity.FC,
                                        Id          = Guid.NewGuid().ToString(),
                                        Fctype      = fcEntity.FCTYPE,
                                        Fcuserid    = fcEntity.FCUSERID,
                                        Gdlr        = 0,
                                        ProfitType  = fcEntity.ProfitType,
                                        WebSiteId   = fcEntity.WebSiteId,
                                        ViolationId = item.Id
                                    }).ToList();
                                }
                                #endregion

                                #region 计算最终价格
                                //违章报价 =(成本)+手价+加价
                                decimal violationPrice = priceModel.Poundage + priceModel.PlusPrice + priceModel.ParentPlusPrice;
                                if (rateType == 0)
                                {
                                    vat = Math.Round(violationPrice * rate / 100);
                                }
                                else if (rateType == 1)
                                {
                                    vat = Math.Round((item.Count + item.Latefine) * rate / 100);
                                }
                                else if (rateType == 2)
                                {
                                    vat = Math.Round((item.Count + item.Latefine + violationPrice) * rate / 100);
                                }
                                ;

                                price = Math.Round(violationPrice);

                                item.IsAskPrice    = false;
                                item.Status        = priceModel.Status.ToInt();
                                item.Poundage      = price;
                                item.CanProcess    = priceModel.CanProcess;
                                item.Vat           = vat;
                                item.Ddbjid        = priceModel.PriceId;
                                item.AgentUserId   = priceModel.UserId;
                                item.AgentUserName = priceModel.ShortName;
                                item.AgentPrice    = priceModel.Poundage;
                                item.ViolationType = priceModel.ViolationType;
                                #endregion
                            }
                            else
                            {
                                //如果已经在办理的违章 更新状态为在办理中 或者办理完毕
                                if (!string.IsNullOrEmpty(priceModel.Status))
                                {
                                    //更新违章
                                    item.IsAskPrice    = false;
                                    item.Status        = priceModel.Status.ToInt();
                                    item.Ddbjid        = priceModel.PriceId;
                                    item.CanprocessMsg = priceModel.CanprocessMsg;
                                }
                            }
                        }

                        if (!oldViolationDivisions.IsNullOrEmpty())
                        {
                            await _carViolationDivisionRepository.BulkDeleteAsync(oldViolationDivisions);
                        }
                        await(
                            _carViolationDivisionRepository.BulkInsertAsync(newViolationDivisions),
                            _violationAgentRepository.BulkUpdateAsync(violations));
                    }
                }
            }
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="batchId"></param>
        /// <param name="batchTableModels"></param>
        /// <returns></returns>
        private async Task InsertOrUpdateViolations(string batchId, List <BatchTableModelDto> batchTableModels)
        {
            if (batchId.IsNullOrWhiteSpace())
            {
                return;
            }

            try
            {
                var batchInfo = await _batchInfoRepository.FirstOrDefaultAsync(x => x.Id == batchId);

                if (batchId == null)
                {
                    return;
                }

                var oldBatchCars = await _batchCarRepository.GetAllListAsync(x => x.BatchId == batchId);

                var oldBatchViolations = await _violationRepository.GetAllListAsync(x => x.BatchId == batchId);

                //当前登录用户
                //var user = SessionHelper.User;

                var updateCarList = new List <BatchCar>();
                var addCarList    = new List <BatchCar>();

                var updateViolationList = new List <BatchAskPriceViolationAgent>();
                var addViolationList    = new List <BatchAskPriceViolationAgent>();

                //查询代办人列表
                var agentNames = batchTableModels.Select(x => x.代办方).ToList();
                //var users = GetUserInfoByKey(agentNames, SessionHelper.WebSite.WebSiteId);
                var batchCanComplete = false;

                foreach (var item in batchTableModels)
                {
                    if (item == null)
                    {
                        continue;
                    }
                    var oldCar = oldBatchCars.FirstOrDefault(x => x.CarNumber == item.车牌号 && x.CarCode == item.车架号 && x.EngineNo == item.发动机号);
                    var newCar = addCarList.FirstOrDefault(x => x.CarNumber == item.车牌号 && x.CarCode == item.车架号 && x.EngineNo == item.发动机号);

                    if (oldCar == null)
                    {
                        if (newCar == null)
                        {
                            newCar = _objectMapper.Map <BatchCar>(item);
                            addCarList.Add(newCar);
                        }
                    }
                    else
                    {
                        if (!updateCarList.Any(x => x.CarNumber == item.车牌号))
                        {
                            _objectMapper.Map(item, oldCar);
                            updateCarList.Add(oldCar);
                        }
                    }

                    var oldViolations = oldBatchViolations.Where(x => x.OrderByNo == item.序号.ToInt() && x.Uniquecode == item.Uniquecode).ToList();
                    BatchAskPriceViolationAgent newViolation = null;

                    if (oldViolations == null || !oldViolations.Any())
                    {
                        newViolation       = _objectMapper.Map <BatchAskPriceViolationAgent>(item);
                        newViolation.CarId = oldCar == null ? newCar.Id : oldCar.Id;

                        if (item.DataStatus == (int)ViolationDataStatusEnum.Normal || item.DataStatus == (int)ViolationDataStatusEnum.OtherBatchRepeat ||
                            item.DataStatus == (int)ViolationDataStatusEnum.ThisBatchRepeat)
                        {
                            batchCanComplete = true;
                        }

                        //在客服导入违章的时候 如果违章代码是空的且罚金和扣分都是0的 自动补充违章代码6050
                        if (newViolation.Code.IsNullOrWhiteSpace() && newViolation.Count == 0 && newViolation.Degree == 0)
                        {
                            newViolation.Code = "6050";
                        }

                        //价格来源
                        if (newViolation.Poundage != 0 || !newViolation.AgentUserId.IsNullOrEmpty())
                        {
                            newViolation.PriceFrom  = (int)PriceFromEnum.Person;                                                    //0系统,1人工
                            newViolation.Poundage   = Math.Round((decimal)newViolation.Poundage, 0, MidpointRounding.AwayFromZero); //人工报价金额-四舍五入
                            newViolation.CanProcess = 1;
                        }

                        addViolationList.Add(newViolation);
                    }
                    else
                    {
                        oldViolations = oldViolations.Where(x => (x.State == (int)ViolationStateEnum.WaitHandle || x.State == (int)ViolationStateEnum.Backed ||
                                                                  x.State == (int)ViolationStateEnum.ReSeted) && !updateViolationList.Any(y => y.OrderByNo == x.OrderByNo &&
                                                                                                                                          y.Uniquecode == x.Uniquecode)).ToList();

                        oldViolations.ForEach(x =>
                        {
                            _objectMapper.Map(item, x);

                            x.CarId = oldCar == null ? newCar.Id : oldCar.Id;

                            //在客服导入违章的时候 如果违章代码是空的且罚金和扣分都是0的 自动补充违章代码6050
                            if (item.违法代码.IsNullOrWhiteSpace() && item.罚金.ToInt() == 0 && item.扣分.ToInt() == 0)
                            {
                                x.Code = "6050";
                            }

                            //价格来源
                            if (item.手续费.ToDecimal(0) != 0 || !x.AgentUserId.IsNullOrEmpty())
                            {
                                x.PriceFrom  = (int)PriceFromEnum.Person;;                                               //0系统,1人工
                                x.Poundage   = Math.Round((decimal)item.手续费.ToDecimal(), MidpointRounding.AwayFromZero); //人工报价金额-四舍五入
                                x.CanProcess = 1;
                            }
                            else
                            {
                                x.PriceFrom  = (int)PriceFromEnum.System;
                                x.Poundage   = 0;
                                x.CanProcess = 0;
                            }
                        });

                        updateViolationList.AddRange(oldViolations);
                    }
                }

                batchInfo.CarCount += addCarList.Count;

                batchInfo.ViolationCount += addViolationList.Count;

                if (batchInfo.Status == 0)
                {
                    batchInfo.Status = (int)BatchStatusEnum.WaitHandle;
                }

                if (batchInfo.Status == (int)BatchStatusEnum.Completed && batchCanComplete)
                {
                    batchInfo.Status       = (int)BatchStatusEnum.Handling;
                    batchInfo.CompleteTime = null;
                }

                //车辆
                await _batchCarRepository.BulkInsertAsync(addCarList);

                await _batchCarRepository.BulkUpdateAsync(updateCarList);

                //违章
                await _violationRepository.BulkInsertAsync(addViolationList);

                await _violationRepository.BulkUpdateAsync(updateViolationList);

                await _batchInfoRepository.UpdateAsync(batchInfo);
            }
            catch (Exception)
            {
            }
        }