using CoreCms.Net.Caching.AutoMate.RedisCache; using CoreCms.Net.IRepository.UnitOfWork; using CoreCms.Net.IRepository; using CoreCms.Net.IServices; using CoreCms.Net.IServices.baifenbingfa; using CoreCms.Net.Model.Entities; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using CoreCms.Net.Auth.HttpContextUser; using CoreCms.Net.Model.Entities.Expression; using CoreCms.Net.Model.ViewModels.Basics; using COSXML.Model.Tag; using NPOI.SS.Formula.Functions; using CoreCms.Net.Model.ViewModels.UI; using SqlSugar; using CoreCms.Net.Configuration; using Newtonsoft.Json; using CoreCms.Net.DTO; namespace CoreCms.Net.Services.baifenbingfa { /// /// 供应商发货单处理服务 /// public class DistributorDeliveryServices : BaseServices, IDistributorDeliveryServices { private readonly ICoreCmsBillDeliveryRepository _dal; private readonly ICoreCmsStoreServices _storeServices; private readonly ICoreCmsBillDeliveryItemServices _billDeliveryItemServices; private readonly ICoreCmsOrderLogServices _orderLogServices; private readonly ICoreCmsSettingServices _settingServices; private readonly IUnitOfWork _unitOfWork; private readonly IServiceProvider _serviceProvider; private readonly IRedisOperationRepository _redisOperationRepository; private readonly IHttpContextUser _user; public readonly ICoreCmsDistributionServices _coreCmsDistributionServices; public DistributorDeliveryServices( IUnitOfWork unitOfWork, IServiceProvider serviceProvider , ICoreCmsBillDeliveryRepository dal , ICoreCmsStoreServices storeServices , ICoreCmsBillDeliveryItemServices billDeliveryItemServices , ICoreCmsOrderLogServices orderLogServices , ICoreCmsSettingServices settingServices , IRedisOperationRepository redisOperationRepository , IHttpContextUser user ,ICoreCmsDistributionServices coreCmsDistributionServices ) { this._dal = dal; base.BaseDal = dal; _unitOfWork = unitOfWork; _serviceProvider = serviceProvider; _storeServices = storeServices; _billDeliveryItemServices = billDeliveryItemServices; _orderLogServices = orderLogServices; _settingServices = settingServices; _redisOperationRepository = redisOperationRepository; _user = user; _coreCmsDistributionServices = coreCmsDistributionServices; } public async Task<(List< CoreCmsBillDelivery>,int )> GetDeliveryList(GetDeliveryListPost Param) { var where = PredicateBuilder.True(); if(Param.Status != null ) { where= where.And(x => x.status == (int)Param.Status); } if (Param.distributionAcceptStatus != null) { where= where.And(x => x.sendDistributionAccept == Param.distributionAcceptStatus); } where= where.And(x => x.sendDistributionUserID == _user.ID); //必须是出于发货状态 //where= where.And(x => x.order.shipStatus ==(int ) GlobalEnumVars.OrderShipStatus.Yes); //必须是有效状态 //where= where.And(x => x.order.status == (int)GlobalEnumVars.OrderStatus.Normal); RefAsync totl = 0; var list= await _unitOfWork.GetDbClient().Queryable().Includes(x => x.order, order => order.Orderitems, ite => ite.productInfo) .Where(where).OrderByDescending(x => x.createTime) .ToPageListAsync(Param.page, Param.limit, totl); var listdata= new PageList(list, Param.page, Param.limit, totl); return (listdata,totl); //return await _dal.QueryPageAsync(where, x => x.createTime, SqlSugar.OrderByType.Desc, Param.page, Param.page); } public async Task GetNoAcceptedDeliveryCount() { return await _dal.GetCountAsync(x => x.sendDistributionUserID == _user.ID && x.sendDistributionAccept == sendDistributionAcceptType.NoAccted); } public async Task GetShowDelivery() { return (await _coreCmsDistributionServices.GetCountAsync(x => x.userId == _user.ID && x.verifyStatus == (int)GlobalEnumVars.DistributionVerifyStatus.VerifyYes&&x.profession=="校园代理", isDataCache: true, cacheTimes: 60)>0); } public async Task SetDeliveryAccepted(sendDistributionAcceptType acceptType, string DeliveryID, string CancelledDec = null) { WebApiCallBack ret = new WebApiCallBack(); var data = await _dal.QueryByIdAsync(DeliveryID); if (data == null) { ret.status = false; ret.msg = " 没有找到发货单"; return ret; } if (data.sendDistributionUserID != _user.ID) { ret.status = false; ret.msg = "该发货单不属于该该用户"; return ret; } var oder = await _unitOfWork.GetDbClient().Queryable().Where(x=>x.status== (int)GlobalEnumVars.OrderStatus.Normal).Includes(x => x.Orderitems).Where(x => x.orderId == data.orderId).FirstAsync(); if(oder == null) { ret.status = false; ret.msg = "订单已经完结,不可操作"; return ret; } var mQ = new ActiveDistributionSendOderMQ { Dec = CancelledDec, SendOderActive = acceptType, deliveryID = DeliveryID, userID = _user.ID }; //记录操作记录 await _redisOperationRepository.ListLeftPushAsync(RedisMessageQueueKey.ActiveDistributionSendOderMQ, JsonConvert.SerializeObject(mQ)); //用户不同意配送 try { _unitOfWork.BeginTran(); if (acceptType == sendDistributionAcceptType.Cancelled) { //如果是取消订单,需要把订单 //不接受 需要将原订单设置为为分配状态, if (oder.status != (int)GlobalEnumVars.OrderStatus.Complete && oder.status != (int)GlobalEnumVars.OrderStatus.Cancel &&oder.shipStatus== (int)GlobalEnumVars.OrderShipStatus.Yes ) { var Deliveryitem = await _unitOfWork.GetDbClient().Queryable().Where(x => x.deliveryId == data.deliveryId).ToListAsync(); foreach (var item in oder.Orderitems) { //处理已发货数据 item.sendNums -= Deliveryitem.Where(x => x.productId == item.productId).Select(x => x.nums).FirstOrDefault(); if(item.sendNums<0) item.sendNums = 0; item.updateTime = DateTime.Now; } oder.shipStatus =(int ) GlobalEnumVars.OrderShipStatus.No; oder.updateTime = DateTime.Now; await _unitOfWork.GetDbClient().UpdateNav( oder ).Include(x=>x.Orderitems).ExecuteCommandAsync() ; } } data.sendDistributionAccept = acceptType; data.updateTime= DateTime.Now; ret.status = true; var r= await _dal.UpdateAsync(data); if(r==true) _unitOfWork.CommitTran(); else { _unitOfWork.RollbackTran(); } ret.status = r; ret.msg = r ? "设置成功" : "设置失败"; return ret; } catch (Exception) { _unitOfWork.RollbackTran(); throw; } } } }