移动系统liao
2024-10-15 94da0698c01915b1e340415e080aa03050700d97
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/***********************************************************************
 *            Project: baifenBinfa
 *        ProjectName: 百分兵法管理系统                               
 *                Web: http://chuanyin.com                     
 *             Author:                                        
 *              Email:                               
 *         CreateTime: 202403/02   
 *        Description: 暂无
 ***********************************************************************/
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
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.ViewModels.Basics;
using CoreCms.Net.Model.ViewModels.UI;
using CoreCms.Net.Utility.Extensions;
using CoreCms.Net.Utility.Helper;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using SqlSugar;
 
 
namespace CoreCms.Net.Services
{
    /// <summary>
    /// 配送方式表 接口实现
    /// </summary>
    public class CoreCmsShipServices : BaseServices<CoreCmsShip>, ICoreCmsShipServices
    {
        private readonly ICoreCmsShipRepository _dal;
        private readonly IUnitOfWork _unitOfWork;
        public CoreCmsShipServices(IUnitOfWork unitOfWork, ICoreCmsShipRepository dal)
        {
            this._dal = dal;
            base.BaseDal = dal;
            _unitOfWork = unitOfWork;
        }
 
 
        #region 实现重写增删改查操作==========================================================
 
        /// <summary>
        /// 重写异步插入方法
        /// </summary>
        /// <param name="entity">实体数据</param>
        /// <returns></returns>
        public async Task<AdminUiCallBack> InsertAsync(CoreCmsShip entity)
        {
            return await _dal.InsertAsync(entity);
        }
 
        /// <summary>
        /// 重写异步更新方法方法
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<AdminUiCallBack> UpdateAsync(CoreCmsShip entity)
        {
            return await _dal.UpdateAsync(entity);
        }
 
        /// <summary>
        /// 重写删除指定ID的数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<AdminUiCallBack> DeleteByIdAsync(int id)
        {
            return await _dal.DeleteByIdAsync(id);
        }
 
        #endregion
 
 
        /// <summary>
        /// 设置是否默认
        /// </summary>
        /// <param name="id"></param>
        /// <param name="isDefault"></param>
        /// <returns></returns>
        public async Task<AdminUiCallBack> SetIsDefault(int id, bool isDefault)
        {
            return await _dal.SetIsDefault(id, isDefault);
        }
 
        #region 重写根据条件查询分页数据
        /// <summary>
        ///     重写根据条件查询分页数据
        /// </summary>
        /// <param name="predicate">判断集合</param>
        /// <param name="orderByType">排序方式</param>
        /// <param name="pageIndex">当前页面索引</param>
        /// <param name="pageSize">分布大小</param>
        /// <param name="orderByExpression"></param>
        /// <param name="blUseNoLock">是否使用WITH(NOLOCK)</param>
        /// <returns></returns>
        public async Task<IPageList<CoreCmsShip>> QueryPageAsync(Expression<Func<CoreCmsShip, bool>> predicate,
            Expression<Func<CoreCmsShip, object>> orderByExpression, OrderByType orderByType, int pageIndex = 1,
            int pageSize = 20, bool blUseNoLock = false)
        {
            return await _dal.QueryPageAsync(predicate, orderByExpression, orderByType, pageIndex, pageSize, blUseNoLock);
        }
        #endregion
 
        /// <summary>
        /// 获取配送费用
        /// </summary>
        /// <param name="areaId">地区id</param>
        /// <param name="weight">重量,单位g</param>
        /// <param name="totalmoney">商品总价</param>
        /// <returns></returns>
        public async Task<decimal> GetShipCost(int areaId = 0, decimal weight = 0, decimal totalmoney = 0)
        {
            decimal postfee = 0;
 
            var idStr = areaId.ToString();
            //先判断是否子地区满足条件
            var def = await _dal.QueryByClauseAsync(p =>
                 p.status == (int)GlobalEnumVars.ShipStatus.Yes &&
                 p.areaType == (int)GlobalEnumVars.ShipAreaType.Part && p.areaFee.Contains(idStr));
            //没有子地区取默认
            if (def == null)
            {
                def = await _dal.QueryByClauseAsync(p => p.isDefault == true && p.status == (int)GlobalEnumVars.ShipStatus.Yes);
            }
            //没有默认取启用状态
            if (def == null)
            {
                def = await _dal.QueryByClauseAsync(p => p.status == (int)GlobalEnumVars.ShipStatus.Yes);
                if (def == null)
                {//没有配送方式,返回0
                    return postfee;
                }
            }
            //是否包邮
            if (def.isfreePostage == true)
            {
                return postfee;
            }
 
            if (def.areaType == (int)GlobalEnumVars.ShipAreaType.Part)
            {
                var areaFee = (JArray)JsonConvert.DeserializeObject(def.areaFee);
                if (areaFee != null && areaFee.Count > 0)
                {
                    var isIn = false;
                    foreach (var jToken in areaFee)
                    {
                        var item = (JObject)jToken;
                        //if (item.Property("area") == null) continue;
                        var area = item["area"].ObjectToString();
                        var firstunitAreaPrice = item["firstunitAreaPrice"].ObjectToDecimal(0);
                        var continueunitAreaPrice = item["continueunitAreaPrice"].ObjectToInt(0);
                        if (!string.IsNullOrEmpty(area))
                        {
                            var areaArr = CommonHelper.StringToIntArray(area);
                            if (areaArr.Contains(areaId))
                            {
                                isIn = true;
                                var total = calculate_fee(def, weight, totalmoney, firstunitAreaPrice, continueunitAreaPrice, true);
                                postfee = Math.Round(total, 2);
                                break;
                            }
                        }
                    }
                    if (!isIn)
                    {
                        var total = calculate_fee(def, weight, totalmoney, 0);
                        postfee = Math.Round(total, 2);
                    }
                }
                else
                {
                    var total = calculate_fee(def, weight, totalmoney, 0);
                    postfee = Math.Round(total, 2);
                }
            }
            else
            {
                var total = calculate_fee(def, weight, totalmoney, 0);
                postfee = Math.Round(total, 2);
            }
            return postfee;
        }
 
        /// <summary>
        /// 计算运费
        /// </summary>
        /// <param name="ship">配送方式内容</param>
        /// <param name="weight">订单总重</param>
        /// <param name="totalmoney">商品总价</param>
        /// <param name="firstunitAreaPrice">单独地区首重</param>
        /// <param name="continueunitAreaPrice">单独地区续重</param>
        /// <param name="isInChild">是否在地区内</param>
        /// <returns></returns>
        public decimal calculate_fee(CoreCmsShip ship, decimal weight, decimal totalmoney = 0, decimal firstunitAreaPrice = 0, decimal continueunitAreaPrice = 0, bool isInChild = false)
        {
            //满多少免运费
            if (ship.goodsMoney > 0 && totalmoney >= ship.goodsMoney)
            {
                return 0;
            }
 
            if (isInChild)
            {
                if (weight <= 0 || weight <= ship.firstUnit) return firstunitAreaPrice;
                var shipMoney = firstunitAreaPrice + (Math.Ceiling(Math.Abs(weight - ship.firstUnit) / ship.continueUnit) * continueunitAreaPrice);
                return shipMoney;
 
            }
            else
            {
                if (weight <= 0 || weight <= ship.firstUnit) return ship.firstunitPrice;
                var shipMoney = ship.firstunitPrice + (Math.Ceiling(Math.Abs(weight - ship.firstUnit) / ship.continueUnit) * ship.continueunitPrice);
                return shipMoney;
            }
 
 
        }
 
        /// <summary>
        /// 根据地区获取配送方式
        /// </summary>
        public async Task<CoreCmsShip> GetShip(int areaId = 0)
        {
            var idStr = areaId.ToString();
            //先判断是否子地区满足条件
            var def = await _dal.QueryByClauseAsync(p =>
                p.status == (int)GlobalEnumVars.ShipStatus.Yes &&
                p.areaType == (int)GlobalEnumVars.ShipAreaType.Part && p.areaFee.Contains(idStr));
            //没有子地区取默认
            if (def == null)
            {
                def = await _dal.QueryByClauseAsync(p => p.isDefault == true && p.status == (int)GlobalEnumVars.ShipStatus.Yes);
            }
            //没有默认取启用状态
            if (def == null)
            {
                def = await _dal.QueryByClauseAsync(p => p.status == (int)GlobalEnumVars.ShipStatus.Yes);
                return def;
            }
            return def;
        }
 
    }
}