Ejemplo n.º 1
0
        ///// <summary>
        ///// 通过指定id获取BatchInfoListDto信息
        ///// </summary>

        //public async Task<BatchInfoListDto> GetById(EntityDto<string> input)
        //{
        //    var entity = await _entityRepository.GetAsync(input.Id);

        //    return entity.MapTo<BatchInfoListDto>();
        //}

        /// <summary>
        /// 删除批次
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>

        public async Task <ApiResult> Delete(string id)
        {
            var entity = await _entityRepository.FirstOrDefaultAsync(id);

            if (entity == null)
            {
                return(ApiResult.DataNotFound());
            }

            if (entity.Status == 2)
            {
                return(new ApiResult().Error("当前状态不允许删除"));
            }

            entity.IsDeleted = true;
            await _entityRepository.UpdateAsync(entity);

            return(new ApiResult().Success());
        }
        /// <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)
            {
            }
        }