feat: 修改购物车商品查询逻辑和订单逻辑,根据用户身份调整商品价格计算方式,并更新相关文档

This commit is contained in:
mkm 2024-07-08 17:24:50 +08:00
parent 74c0456f1a
commit f6c8b7865e
4 changed files with 84 additions and 80 deletions

View File

@ -64,6 +64,10 @@ class CartList extends BaseAdminDataLists implements ListsSearchInterface, Lists
->toArray(); ->toArray();
$off_activity = Config::where('name', 'off_activity')->value('value'); $off_activity = Config::where('name', 'off_activity')->value('value');
$this->off_activity=$off_activity; $this->off_activity=$off_activity;
$user_ship = User::where('id', $userId)->value('user_ship');
if (in_array($user_ship, [4, 5, 6, 7])) {
$off_activity=1;
}
foreach ($list as $key => &$item) { foreach ($list as $key => &$item) {
$find = StoreBranchProduct::where(['product_id' => $item['product_id'], 'store_id' => $item['store_id']]) $find = StoreBranchProduct::where(['product_id' => $item['product_id'], 'store_id' => $item['store_id']])
->field('product_id,image,price,cost,store_name,unit,delete_time,vip_price') ->field('product_id,image,price,cost,store_name,unit,delete_time,vip_price')

View File

@ -3,7 +3,7 @@
namespace app\api\lists\product; namespace app\api\lists\product;
use app\admin\lists\BaseAdminDataLists; use app\api\lists\BaseApiDataLists;
use app\common\lists\ListsExtendInterface; use app\common\lists\ListsExtendInterface;
use app\common\lists\ListsSortInterface; use app\common\lists\ListsSortInterface;
use app\common\model\dict\DictType; use app\common\model\dict\DictType;
@ -12,6 +12,7 @@ use app\common\model\store_product\StoreProduct;
use app\common\lists\ListsSearchInterface; use app\common\lists\ListsSearchInterface;
use app\common\model\cate\Cate; use app\common\model\cate\Cate;
use app\common\model\Config; use app\common\model\Config;
use app\common\model\user\User;
//use app\common\model\goods\GoodsLabel; //use app\common\model\goods\GoodsLabel;
use think\facade\Db; use think\facade\Db;
@ -20,7 +21,7 @@ use think\facade\Db;
* Class goods * Class goods
* @package app\api\goods * @package app\api\goods
*/ */
class ProductLists extends BaseAdminDataLists implements ListsSearchInterface, ListsSortInterface, ListsExtendInterface class ProductLists extends BaseApiDataLists implements ListsSearchInterface, ListsSortInterface, ListsExtendInterface
{ {
@ -33,10 +34,9 @@ class ProductLists extends BaseAdminDataLists implements ListsSearchInterface, L
public function setSearch(): array public function setSearch(): array
{ {
return [ return [
'=' => ['store_id', 'cate_id','top_cate_id','two_cate_id'], '=' => ['store_id', 'cate_id', 'top_cate_id', 'two_cate_id'],
'%pipe_like%' => ['store_name' => 'store_name|bar_code'], '%pipe_like%' => ['store_name' => 'store_name|bar_code'],
]; ];
} }
/** /**
* @notes 设置支持排序字段 * @notes 设置支持排序字段
@ -71,28 +71,17 @@ class ProductLists extends BaseAdminDataLists implements ListsSearchInterface, L
*/ */
public function lists(): array public function lists(): array
{ {
// $class_all = $this->request->get('class_all'); $order = $this->request->get('order', '');
// if ($class_all) { $field = $this->request->get('field', '');
// //查3级别的 if (empty($order) || empty($field)) {
// $arr = Cate::where('pid', $class_all)->column('id');
// if ($arr) {
// $arr2 = Cate::where('pid', 'in', $arr)->column('id');
// $this->searchWhere[] = ['cate_id', 'in', array_merge($arr, $arr2)];
// } else {
// $this->searchWhere[] = ['cate_id', '=', $class_all];
// }
// }
$order = $this->request->get('order','');
$field = $this->request->get('field','');
if(empty($order)||empty($field)){
$order = $this->sortOrder; $order = $this->sortOrder;
}else{ } else {
$order = [$field => $order]; $order = [$field => $order];
} }
$fields = 'id,product_id,cate_id,store_name,cost,store_id,vip_price,purchase,price,bar_code,image,sales,store_info,delete_time,unit,batch,top_cate_id,two_cate_id,stock'; $fields = 'id,product_id,cate_id,store_name,cost,store_id,vip_price,purchase,price,bar_code,image,sales,store_info,delete_time,unit,batch,top_cate_id,two_cate_id,stock';
$this->searchWhere[] = ['status', '=', 1]; $this->searchWhere[] = ['status', '=', 1];
// $this->searchWhere[] = ['stock', '>', 0]; // $this->searchWhere[] = ['stock', '>', 0];
return StoreBranchProduct::where($this->searchWhere) return StoreBranchProduct::where($this->searchWhere)
->field($fields) ->field($fields)
->with(['className', 'unitName']) ->with(['className', 'unitName'])
@ -116,20 +105,26 @@ class ProductLists extends BaseAdminDataLists implements ListsSearchInterface, L
} }
public function extend() public function extend()
{ {
// $off_activity = Config::where('name', 'off_activity')->value('value'); $price = 'price';
// if($off_activity==1){ $op_price = 'price';
// $data=[ $off_activity = 0;
// 'off_activity' => $off_activity, if ($this->userId > 0) {
// 'price' => 'cost', $user_ship = User::where('id', $this->userId)->value('user_ship');
// 'op_price' => 'price', if (in_array($user_ship, [4, 5, 6, 7])) {
// ]; $price = 'cost';
// }else{ $op_price = 'price';
$data=[ }
'off_activity' => 0, }
'price' => 'price', $off_activity = Config::where('name', 'off_activity')->value('value');
'op_price' => 'price', if ($off_activity == 1) {
]; $price = 'cost';
// } $op_price = 'price';
}
$data = [
'off_activity' => $off_activity,
'price' => $price,
'op_price' => $op_price,
];
return $data; return $data;
} }
} }

View File

@ -55,7 +55,7 @@ class OrderLogic extends BaseLogic
public static $store_price; //门店零售价 public static $store_price; //门店零售价
public static $activity_price; public static $activity_price;
public static $deduction_price; public static $deduction_price;
public static $frozen_money;//返还金额 public static $frozen_money; //返还金额
/** /**
* @notes 获取购物车商品信息 * @notes 获取购物车商品信息
@ -77,8 +77,8 @@ class OrderLogic extends BaseLogic
self::$profit = 0; //利润 self::$profit = 0; //利润
self::$activity_price = 0; //活动减少 self::$activity_price = 0; //活动减少
self::$store_price = 0; //商户价 self::$store_price = 0; //商户价
self::$deduction_price =0; self::$deduction_price = 0;
self::$frozen_money =0;//返还金额 self::$frozen_money = 0; //返还金额
$deduction_price = 0; //抵扣金额 $deduction_price = 0; //抵扣金额
/** 计算价格 */ /** 计算价格 */
$off_activity = Config::where('name', 'off_activity')->value('value'); $off_activity = Config::where('name', 'off_activity')->value('value');
@ -86,23 +86,27 @@ class OrderLogic extends BaseLogic
foreach ($cart_select as $k => $v) { foreach ($cart_select as $k => $v) {
$find = StoreBranchProduct::where(['product_id' => $v['product_id'], 'store_id' => $params['store_id']])->field($field)->withTrashed()->find(); $find = StoreBranchProduct::where(['product_id' => $v['product_id'], 'store_id' => $params['store_id']])->field($field)->withTrashed()->find();
if (!$find) { if (!$find) {
// unset($cart_select[$k]); // unset($cart_select[$k]);
// continue; // continue;
$field = 'id branch_product_id,store_name,image,unit,price,vip_price,cost,purchase, id product_id'; $field = 'id branch_product_id,store_name,image,unit,price,vip_price,cost,purchase, id product_id';
$find = StoreProduct::where(['id' => $v['product_id']])->field($field)->withTrashed()->find(); $find = StoreProduct::where(['id' => $v['product_id']])->field($field)->withTrashed()->find();
$cart_select[$k]['status'] = 1;//缺货标识 $cart_select[$k]['status'] = 1; //缺货标识
} }
unset($cart_select[$k]['id']); unset($cart_select[$k]['id']);
$cart_select[$k]['total_price'] = bcmul($v['cart_num'], $find['price'], 2); //订单总价 $cart_select[$k]['total_price'] = bcmul($v['cart_num'], $find['price'], 2); //订单总价
// if ($off_activity == 1) { if ($off_activity == 1) {
// $price = $find['cost']; $price = $find['cost'];
// } else { } else {
$price = $find['price']; if ($user && in_array($user['user_ship'], [4, 5, 6, 7])) {
// } $price = 'cost';
} else {
$price = $find['price'];
}
}
$cart_select[$k]['price'] = $price; $cart_select[$k]['price'] = $price;
$cart_select[$k]['cost'] = $find['cost']; $cart_select[$k]['cost'] = $find['cost'];
$cart_select[$k]['vip'] = 0; $cart_select[$k]['vip'] = 0;
$cart_select[$k]['unit_name'] = StoreProductUnit::where(['id' => $find['unit']])->value('name')??''; //单位名称 $cart_select[$k]['unit_name'] = StoreProductUnit::where(['id' => $find['unit']])->value('name') ?? ''; //单位名称
//利润 //利润
// $cart_select[$k]['profit'] = bcmul($v['cart_num'], $onePrice, 2); //利润 // $cart_select[$k]['profit'] = bcmul($v['cart_num'], $onePrice, 2); //利润
@ -110,23 +114,23 @@ class OrderLogic extends BaseLogic
$cart_select[$k]['pay_price'] = bcmul($v['cart_num'], $price, 2); //订单支付金额 $cart_select[$k]['pay_price'] = bcmul($v['cart_num'], $price, 2); //订单支付金额
$cart_select[$k]['store_price'] = bcmul($v['cart_num'], $find['cost'], 2) ?? 0; //商户价 $cart_select[$k]['store_price'] = bcmul($v['cart_num'], $find['cost'], 2) ?? 0; //商户价
$cart_select[$k]['vip_price'] = bcmul($v['cart_num'], $find['vip_price'], 2) ?? 0; //vip售价 $cart_select[$k]['vip_price'] = bcmul($v['cart_num'], $find['vip_price'], 2) ?? 0; //vip售价
if($cart_select[$k]['total_price']>$cart_select[$k]['pay_price']){ if ($cart_select[$k]['total_price'] > $cart_select[$k]['pay_price']) {
$deduction_price=bcsub($cart_select[$k]['total_price'],$cart_select[$k]['pay_price'],2); $deduction_price = bcsub($cart_select[$k]['total_price'], $cart_select[$k]['pay_price'], 2);
$cart_select[$k]['deduction_price'] =$deduction_price;//抵扣金额 $cart_select[$k]['deduction_price'] = $deduction_price; //抵扣金额
} }
$cart_select[$k]['product_id'] = $find['product_id']; $cart_select[$k]['product_id'] = $find['product_id'];
$cart_select[$k]['old_cart_id'] = $v['id']; $cart_select[$k]['old_cart_id'] = $v['id'];
$cart_select[$k]['cart_num'] = $v['cart_num']; $cart_select[$k]['cart_num'] = $v['cart_num'];
$cart_select[$k]['verify_code'] = $params['verify_code'] ?? ''; $cart_select[$k]['verify_code'] = $params['verify_code'] ?? '';
$cart_select[$k]['vip_frozen_price']=0; $cart_select[$k]['vip_frozen_price'] = 0;
//会员待返回金额 //会员待返回金额
if ($user && $off_activity==0){ if ($user && $off_activity == 0) {
if($user['user_ship']==4){ if ($user['user_ship'] == 4) {
//商户 //商户
$cart_select[$k]['vip_frozen_price'] = bcsub($cart_select[$k]['pay_price'], $cart_select[$k]['store_price'], 2); $cart_select[$k]['vip_frozen_price'] = bcsub($cart_select[$k]['pay_price'], $cart_select[$k]['store_price'], 2);
}else{ } else {
//其他会员 //其他会员
$cart_select[$k]['vip_frozen_price'] = bcsub($cart_select[$k]['pay_price'],$cart_select[$k]['vip_price'], 2); $cart_select[$k]['vip_frozen_price'] = bcsub($cart_select[$k]['pay_price'], $cart_select[$k]['vip_price'], 2);
} }
} }
// d($cart_select[$k]['pay_price'],$cart_select[$k]['store_price'],$cart_select[$k]['vip_price'] ); // d($cart_select[$k]['pay_price'],$cart_select[$k]['store_price'],$cart_select[$k]['vip_price'] );
@ -144,8 +148,8 @@ class OrderLogic extends BaseLogic
self::$pay_price = bcadd(self::$pay_price, $cart_select[$k]['pay_price'], 2); self::$pay_price = bcadd(self::$pay_price, $cart_select[$k]['pay_price'], 2);
self::$cost = bcadd(self::$cost, $cart_select[$k]['purchase'], 2); self::$cost = bcadd(self::$cost, $cart_select[$k]['purchase'], 2);
self::$store_price = bcadd(self::$store_price, $cart_select[$k]['store_price'], 2); //商户价 self::$store_price = bcadd(self::$store_price, $cart_select[$k]['store_price'], 2); //商户价
self::$deduction_price=bcadd(self::$deduction_price,$deduction_price,2);//抵扣金额 self::$deduction_price = bcadd(self::$deduction_price, $deduction_price, 2); //抵扣金额
self::$frozen_money = bcadd(self::$frozen_money, $cart_select[$k]['vip_frozen_price'], 2);//返还金额 self::$frozen_money = bcadd(self::$frozen_money, $cart_select[$k]['vip_frozen_price'], 2); //返还金额
// self::$profit = bcadd(self::$profit, $cart_select[$k]['profit'], 2); // self::$profit = bcadd(self::$profit, $cart_select[$k]['profit'], 2);
} }
//加支付方式限制 //加支付方式限制
@ -153,7 +157,7 @@ class OrderLogic extends BaseLogic
// if ($user && $user['user_ship'] == 1 && $pay_type != 17) { // if ($user && $user['user_ship'] == 1 && $pay_type != 17) {
// $pay_price = self::$pay_price; // $pay_price = self::$pay_price;
// } else { // } else {
$pay_price = bcsub(self::$pay_price, self::$activity_price, 2); //减去活动优惠金额 $pay_price = bcsub(self::$pay_price, self::$activity_price, 2); //减去活动优惠金额
// } // }
//成本价 收益 //成本价 收益
$order = [ $order = [
@ -173,7 +177,7 @@ class OrderLogic extends BaseLogic
'activity_price' => self::$activity_price, 'activity_price' => self::$activity_price,
'activities' => self::$activity_price > 0 ? 1 : 0, 'activities' => self::$activity_price > 0 ? 1 : 0,
'deduction_price' => self::$deduction_price, 'deduction_price' => self::$deduction_price,
'frozen_money' => self::$frozen_money,//返还金额(活动关闭得时候有) 'frozen_money' => self::$frozen_money, //返还金额(活动关闭得时候有)
'source' => 0, 'source' => 0,
'is_storage' => $params['is_storage'] ?? 0, 'is_storage' => $params['is_storage'] ?? 0,
]; ];
@ -186,18 +190,18 @@ class OrderLogic extends BaseLogic
} }
//处理返回最近的店铺 //处理返回最近的店铺
$store_check = 0; $store_check = 0;
if(empty($user)){ if (empty($user)) {
$store_id = getenv('STORE_ID') ?? 1; $store_id = getenv('STORE_ID') ?? 1;
$store['near_store'] =SystemStore::where('id', $store_id)->field('id,name,phone,address,detailed_address,latitude,longitude')->find()??[]; $store['near_store'] = SystemStore::where('id', $store_id)->field('id,name,phone,address,detailed_address,latitude,longitude')->find() ?? [];
}else{ } else {
$checkOrderStore = StoreOrder::where('uid',$user['id'])->field('id,store_id') $checkOrderStore = StoreOrder::where('uid', $user['id'])->field('id,store_id')
->order('id','desc')->find(); ->order('id', 'desc')->find();
if($checkOrderStore){ if ($checkOrderStore) {
$store['near_store'] =SystemStore::where('id', $checkOrderStore['store_id'])->field('id,name,phone,address,detailed_address,latitude,longitude')->find()??[]; $store['near_store'] = SystemStore::where('id', $checkOrderStore['store_id'])->field('id,name,phone,address,detailed_address,latitude,longitude')->find() ?? [];
$store_check = 1; $store_check = 1;
}else{ } else {
$store_id = getenv('STORE_ID') ?? 1; $store_id = getenv('STORE_ID') ?? 1;
$store['near_store'] =SystemStore::where('id', $store_id)->field('id,name,phone,address,detailed_address,latitude,longitude')->find()??[]; $store['near_store'] = SystemStore::where('id', $store_id)->field('id,name,phone,address,detailed_address,latitude,longitude')->find() ?? [];
} }
} }
if (empty($store_check)) { if (empty($store_check)) {
@ -212,7 +216,7 @@ class OrderLogic extends BaseLogic
$nearestStore = $value; $nearestStore = $value;
} }
} }
$store['near_store'] =[]; $store['near_store'] = [];
if ($nearestStore) { if ($nearestStore) {
$store['near_store'] = $nearestStore; $store['near_store'] = $nearestStore;
} }
@ -281,10 +285,10 @@ class OrderLogic extends BaseLogic
$goods_list[$k]['uid'] = $uid; $goods_list[$k]['uid'] = $uid;
$goods_list[$k]['cart_id'] = implode(',', $cartId); $goods_list[$k]['cart_id'] = implode(',', $cartId);
$goods_list[$k]['delivery_id'] = $params['store_id']; //商家id $goods_list[$k]['delivery_id'] = $params['store_id']; //商家id
// $StoreBranchProduct = StoreBranchProduct::where('id', $v['branch_product_id'])->withTrashed()->find(); // $StoreBranchProduct = StoreBranchProduct::where('id', $v['branch_product_id'])->withTrashed()->find();
// if ($StoreBranchProduct['stock'] - $v['cart_num'] <= 0) { // if ($StoreBranchProduct['stock'] - $v['cart_num'] <= 0) {
// Db::name('store_product_cate')->where(['cate_id' => $StoreBranchProduct['cate_id'], 'store_id' => $params['store_id']])->update(['count' => 0]); // Db::name('store_product_cate')->where(['cate_id' => $StoreBranchProduct['cate_id'], 'store_id' => $params['store_id']])->update(['count' => 0]);
// } // }
} }
(new StoreOrderCartInfo())->saveAll($goods_list); (new StoreOrderCartInfo())->saveAll($goods_list);
$where = ['is_pay' => 0]; $where = ['is_pay' => 0];
@ -451,7 +455,7 @@ class OrderLogic extends BaseLogic
$find['goods_list'] = StoreOrderCartInfo::where('oid', $find['id']) $find['goods_list'] = StoreOrderCartInfo::where('oid', $find['id'])
->field('product_id,cart_num nums,store_id')->select()->each(function ($item) use ($find) { ->field('product_id,cart_num nums,store_id')->select()->each(function ($item) use ($find) {
$find = StoreBranchProduct::where('product_id', $item['product_id'])->where('store_id', $find['store_id'])->find(); $find = StoreBranchProduct::where('product_id', $item['product_id'])->where('store_id', $find['store_id'])->find();
if(empty($find)){ if (empty($find)) {
$find = StoreProduct::where('id', $item['product_id'])->withTrashed()->find(); $find = StoreProduct::where('id', $item['product_id'])->withTrashed()->find();
} }
$item['store_name'] = $find['store_name']; $item['store_name'] = $find['store_name'];
@ -534,7 +538,8 @@ class OrderLogic extends BaseLogic
(new StoreProductLog())->update( (new StoreProductLog())->update(
[ [
'store_id' => $params['store_id'] 'store_id' => $params['store_id']
], ['oid' => $order['id']] ],
['oid' => $order['id']]
); );
(new StoreOrderCartInfo())->update([ (new StoreOrderCartInfo())->update([
'verify_code' => $params['verify_code'] . '-1', 'verify_code' => $params['verify_code'] . '-1',
@ -563,7 +568,7 @@ class OrderLogic extends BaseLogic
$money = $financeFlow->where(['order_id' => $order['id'], 'financial_pm' => 1, 'financial_type' => 2])->value('number') ?? 0; $money = $financeFlow->where(['order_id' => $order['id'], 'financial_pm' => 1, 'financial_type' => 2])->value('number') ?? 0;
$financeFlowLogic->updateStatusStore($order['id'], $order['store_id'], $money, $deposit); $financeFlowLogic->updateStatusStore($order['id'], $order['store_id'], $money, $deposit);
//积分结算 //积分结算
if($order['is_storage']==0&&$order['source']==0){ if ($order['is_storage'] == 0 && $order['source'] == 0) {
UserSignLogic::WriteOff($order); UserSignLogic::WriteOff($order);
} }
Db::commit(); Db::commit();

View File

@ -588,7 +588,7 @@ class PayNotifyLogic extends BaseLogic
} }
//积分写入 //积分写入
if(in_array($order['pay_type'],[3,7,9,13,17])&&$order['uid']>0){ if(isset($user) && $user['user_ship']==0){
UserSignLogic::OrderWrite($order); UserSignLogic::OrderWrite($order);
} }
if ($off_activity == 1) { if ($off_activity == 1) {
@ -644,10 +644,10 @@ class PayNotifyLogic extends BaseLogic
} }
switch ($user_ship) { switch ($user_ship) {
case 1: // 厨师 case 1: // 厨师
case 4: // 商户 //case 4: // 商户
case 5: // 种养殖 //case 5: // 种养殖
case 6: // 酒店 // case 6: // 酒店
case 7: // 食堂 // case 7: // 食堂
case 8: // 一条龙 case 8: // 一条龙
CommissionLogic::setCook($order, $village_uid, $brigade_uid, $transaction_id); CommissionLogic::setCook($order, $village_uid, $brigade_uid, $transaction_id);
break; break;