/***********************************************************************
* 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
{
///
/// 配送方式表 接口实现
///
public class CoreCmsShipServices : BaseServices, ICoreCmsShipServices
{
private readonly ICoreCmsShipRepository _dal;
private readonly IUnitOfWork _unitOfWork;
public CoreCmsShipServices(IUnitOfWork unitOfWork, ICoreCmsShipRepository dal)
{
this._dal = dal;
base.BaseDal = dal;
_unitOfWork = unitOfWork;
}
#region 实现重写增删改查操作==========================================================
///
/// 重写异步插入方法
///
/// 实体数据
///
public async Task InsertAsync(CoreCmsShip entity)
{
return await _dal.InsertAsync(entity);
}
///
/// 重写异步更新方法方法
///
///
///
public async Task UpdateAsync(CoreCmsShip entity)
{
return await _dal.UpdateAsync(entity);
}
///
/// 重写删除指定ID的数据
///
///
///
public async Task DeleteByIdAsync(int id)
{
return await _dal.DeleteByIdAsync(id);
}
#endregion
///
/// 设置是否默认
///
///
///
///
public async Task SetIsDefault(int id, bool isDefault)
{
return await _dal.SetIsDefault(id, isDefault);
}
#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
///
/// 获取配送费用
///
/// 地区id
/// 重量,单位g
/// 商品总价
///
public async Task 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;
}
///
/// 计算运费
///
/// 配送方式内容
/// 订单总重
/// 商品总价
/// 单独地区首重
/// 单独地区续重
/// 是否在地区内
///
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;
}
}
///
/// 根据地区获取配送方式
///
public async Task 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;
}
}
}