/*********************************************************************** * Project: baifenBinfa * ProjectName: 百分兵法管理系统 * Web: http://chuanyin.com * Author: * Email: * CreateTime: 2024/4/17 13:28:20 * Description: 暂无 ***********************************************************************/ using System; using System.Collections.Generic; using System.Linq.Expressions; using System.Threading.Tasks; using System.Xml.Linq; using CoreCms.Net.Configuration; using CoreCms.Net.IRepository; using CoreCms.Net.IRepository.UnitOfWork; using CoreCms.Net.IServices; using CoreCms.Net.Model.Entities; using CoreCms.Net.Model.Entities.Expression; using CoreCms.Net.Model.Entities.Order; using CoreCms.Net.Model.ViewModels.Basics; using CoreCms.Net.Model.ViewModels.UI; using CoreCms.Net.Utility.Helper; using Newtonsoft.Json.Linq; using Newtonsoft.Json; using SqlSugar; using static CoreCms.Net.Configuration.GlobalEnumVars; using static SKIT.FlurlHttpClient.Wechat.Api.Models.CgibinExpressIntracityQueryStoreResponse.Types; using static SKIT.FlurlHttpClient.Wechat.Api.Models.ProductSPUAddRequest.Types; using System.Linq; using CoreCms.Net.Model.Entities.baifenbingfa.apply; using CoreCms.Net.Model.ViewModels.DTO; using CoreCms.Net.Model.Entities.baifenbingfa.Promote; using static SKIT.FlurlHttpClient.Wechat.Api.Models.ScanProductAddV2Request.Types.Product.Types; using NPOI.SS.Formula.Functions; namespace CoreCms.Net.Services { /// /// 计划订单表 接口实现 /// public class CoreCmsPlanOrderServices : BaseServices, ICoreCmsPlanOrderServices { private readonly ICoreCmsPlanOrderRepository _dal; private readonly IUnitOfWork _unitOfWork; private readonly ICoreCmsPlanOrderItemRepository _itemDal; private readonly ICoreCmsUserServices _coreCmsUserServices; public CoreCmsPlanOrderServices(IUnitOfWork unitOfWork, ICoreCmsPlanOrderRepository dal , ICoreCmsPlanOrderItemRepository itemDal , ICoreCmsUserServices coreCmsUserServices) { this._dal = dal; base.BaseDal = dal; _unitOfWork = unitOfWork; _itemDal = itemDal; _coreCmsUserServices = coreCmsUserServices; } #region 实现重写增删改查操作========================================================== /// /// 重写异步插入方法 /// /// 实体数据 /// public async Task InsertAsync(CoreCmsPlanOrder entity) { return await _dal.InsertAsync(entity); } /// /// 重写异步更新方法方法 /// /// /// public async Task UpdateAsync(CoreCmsPlanOrder entity) { return await _dal.UpdateAsync(entity); } /// /// 重写异步更新方法方法 /// /// /// public async Task UpdateAsync(List entity) { return await _dal.UpdateAsync(entity); } /// /// 重写删除指定ID的数据 /// /// /// public async Task DeleteByIdAsync(object id) { return await _dal.DeleteByIdAsync(id); } /// /// 重写删除指定ID集合的数据(批量删除) /// /// /// public async Task DeleteByIdsAsync(int[] ids) { return await _dal.DeleteByIdsAsync(ids); } #endregion #region 获取缓存的所有数据========================================================== /// /// 获取缓存的所有数据 /// /// public async Task> GetCaChe() { return await _dal.GetCaChe(); } #endregion #region 重写根据条件查询分页数据 /// /// 重写根据条件查询分页数据 /// /// 判断集合 /// 排序方式 /// 当前页面索引 /// 分布大小 /// /// 是否使用WITH(NOLOCK) /// public async Task> QueryPageAsync(Expression> predicate, Expression> orderByExpression, OrderByType orderByType, int pageIndex = 1, int pageSize = 20, bool blUseNoLock = false) { return await _dal.QueryPageAsync(predicate, orderByExpression, orderByType, pageIndex, pageSize, blUseNoLock); } #endregion #region 获取计划订单列表微信小程序 /// /// 获取计划订单列表微信小程序 /// /// public async Task GetOrderList(int status = -1, int userId = 0, int page = 1, int limit = 5) { var jm = new WebApiCallBack { status = true }; var where = PredicateBuilder.True(); if (status > -1) { where = where.And(p => p.status == status); } if (userId > 0) { where = where.And(p => p.userId == userId); } var list = await _dal.QueryPageAsync(where, p => p.createTime, OrderByType.Desc, page, limit); if (list!=null && list.Count>0) { foreach (var order in list) { //获取相关状态描述说明转换 order.statusText = EnumHelper.GetEnumDescriptionByValue(order.status); } } jm.data = new { list, count = list.TotalCount, page, limit, status }; return jm; } #endregion /// /// 创建订单 /// /// 用户序列 /// 用户昵称 /// 计划订单状态 /// 明细id /// 明细数量 /// public async Task ToUpdate(string orderId, int userId, string userName, int status, string itemIds, string itemNums) { var jm = new WebApiCallBack() { methodDescription = "修改计划订单" }; try { //开始事务处理 _unitOfWork.BeginTran(); var order = await _dal.QueryByIdAsync(orderId); order.upDataBy = userName; order.upDataTime = DateTime.Now; List orderItems; var ids = CommonHelper.StringToIntArray(itemIds); var nums = CommonHelper.StringToStringArray(itemNums); orderItems = await _itemDal.QueryListByClauseAsync(x => x.orderId == orderId); decimal orderAmount = 0; for (int i=0;i< ids.Length; i++) { var id = ids[i]; int num = 0; int.TryParse(nums[i], out num); var item = orderItems.Where(p => p.id == id).FirstOrDefault(); item.nums = num; item.amount = item.price * (decimal)num; var jms = await _itemDal.UpdateAsync(item); orderAmount += item.amount; } order.orderAmount = orderAmount; order.keYongAmount = order.oldOrderAmount - order.orderAmount; if (order.keYongAmount < 0) { _unitOfWork.RollbackTran(); jm.status = false; jm.otherData = "可用余额不能为负"; } if (status == 2) { order.status = 2; if (order.keYongAmount > 0) { //修改用户余额 var jmsss = await _coreCmsUserServices.UpdateBalance(order.userId, order.keYongAmount, "用户计划订单款项结余"); } } //校验后再创建订单 var jmss = await _dal.UpdateAsync(order); _unitOfWork.CommitTran(); jm.status = true; jm.data = order; jm.msg = "修改成功"; } catch (Exception e) { _unitOfWork.RollbackTran(); jm.status = false; jm.otherData = e.ToString(); } return jm; } /// /// 创建订单 /// /// 计划订单 /// 用户序列 /// 用户昵称 /// public async Task ToAddJiHua(List jiHuaOrders, int userId, string userName) { var jm = new AdminUiCallBack() ; try { //开始事务处理 _unitOfWork.BeginTran(); CoreCmsPlanOrder order = new CoreCmsPlanOrder(); order.orderId = CommonHelper.GetSerialNumberType((int)GlobalEnumVars.SerialNumberType.计划订单编号); order.status = 1; order.shipMobile = jiHuaOrders[0].shipMobile; order.shipAddress = jiHuaOrders[0].shipAddress; order.shipName = jiHuaOrders[0].shipName; ; var user = await _unitOfWork.GetDbClient().Queryable().Where(x => x.mobile == order.shipMobile && x.isDelete == false && x.status == 1).FirstAsync(); if (user != null) { order.userId = user.id; } else { jm.code = 1; jm.msg = order.shipMobile + "没有找到用户"; return jm; } order.createBy = userName; order.createTime = DateTime.Now; order.upDataBy = userName; order.upDataTime = DateTime.Now; order.isdelete = false; decimal orderAmount = 0; for (int i = 0; i < jiHuaOrders.Count; i++) { var item = new CoreCmsPlanOrderItem(); item.orderId = order.orderId; item.name = jiHuaOrders[i].name; item.specification = jiHuaOrders[i].specification; item.price = jiHuaOrders[i].price; item.nums = jiHuaOrders[i].nums; ; item.amount = item.price * (decimal)jiHuaOrders[i].nums; item.createBy = userName; item.createTime = DateTime.Now; item.upDataBy = userName; item.upDataTime = DateTime.Now; item.isdelete = false; var jms = await _itemDal.InsertAsync(item); orderAmount += item.amount; } order.orderAmount = orderAmount; order.oldOrderAmount = orderAmount; order.keYongAmount = 0; //校验后再创建订单 var jmss = await _dal.InsertAsync(order); _unitOfWork.CommitTran(); jm.code = 0; jm.data = order; jm.msg = "导入成功"; } catch (Exception e) { _unitOfWork.RollbackTran(); jm.code = 0; jm.otherData = e.ToString(); } return jm; } } }