feat: 添加批发商品列表功能

This commit is contained in:
mkm 2024-09-03 17:55:14 +08:00
parent 60c7b154a5
commit cbeb06426d
8 changed files with 288 additions and 34 deletions

View File

@ -6,6 +6,7 @@ use app\api\logic\order\CartLogic;
use app\api\validate\CartValidate;
use app\api\controller\BaseApiController;
use app\api\lists\order\CartList;
use app\api\lists\order\CartWholesaleList;
use app\common\model\order\Cart;
use app\common\model\store_product\StoreProduct;
use app\common\model\store_product_unit\StoreProductUnit;
@ -16,7 +17,13 @@ class CartController extends BaseApiController
{
return $this->dataLists(new CartList());
}
/**
* 批发商品列表
*/
public function wholesale_lists(){
return $this->dataLists(new CartWholesaleList());
}
/**
* @notes 添加购物车
*/

View File

@ -3,11 +3,14 @@
namespace app\api\controller\product;
use app\api\controller\BaseApiController;
use app\api\lists\product\ProductLists;
use app\api\lists\product\ProductWholesaleLists;
use app\api\lists\product\StoreProductLists;
use app\common\model\system_store\SystemStoreStaff;
class ProductController extends BaseApiController{
public $notNeedLogin = ['lists'];
/**
* 商品列表
*/
@ -16,6 +19,13 @@ class ProductController extends BaseApiController{
return $this->dataLists(new ProductLists());
}
/**
* 批发商品列表
*/
public function wholesale_lists(){
return $this->dataLists(new ProductWholesaleLists());
}
/**
* 商品列表
*/

View File

@ -45,9 +45,13 @@ class CateLists extends BaseAdminDataLists implements ListsSearchInterface
public function lists(): array
{
$level = Request()->get('level', 1);
$source = Request()->get('source');
$pid = $this->request->get('pid',0);
// $this->searchWhere[] = ['stock', '>', 0];
$this->searchWhere[] = ['is_show', '=', 1];
if($source && $source==4){
$this->searchWhere[] = ['product_type', '=', 5];
}
if($pid && $level ==2){
$this->searchWhere[] = ['top_cate_id','=',$pid];
$cate_arr = StoreProduct::where($this->searchWhere)->distinct()

View File

@ -0,0 +1,109 @@
<?php
namespace app\api\lists\order;
use app\admin\lists\BaseAdminDataLists;
use app\common\lists\ListsSearchInterface;
use app\common\model\order\Cart;
use app\common\lists\ListsExtendInterface;
use app\common\model\Config;
use app\common\model\dict\DictType;
use app\common\model\store_product\StoreProduct;
use app\common\model\store_product_attr_value\StoreProductAttrValue;
use app\common\model\store_product_unit\StoreProductUnit;
use app\common\model\user\User;
/**
* 购物车列表
* Class RetailOrderList
* @package app\api\order
*/
class CartWholesaleList extends BaseAdminDataLists implements ListsSearchInterface, ListsExtendInterface
{
protected $total_price = 0;
protected $activity_price = 0;
protected $off_activity = 0;
protected $user_ship = 0;
/**
* @notes 设置搜索条件
* @return \string[][]
* @author likeadmin
*/
public function setSearch(): array
{
return ['='=>['store_id','source']];
}
/**
* @notes 购物车列表
* @return array
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
* @date 2024/04/27 11:26
*/
public function lists($where = []): array
{
$userId = $this->request->userId;
if (!$userId) return [];
$where = [
'uid' => $userId,
'is_pay' => 0
];
$list = Cart::where($this->searchWhere)->where($where)
->limit($this->limitOffset, $this->limitLength)
->order(['id' => 'desc'])
->select()
->toArray();
$field = 'id,id product_id,image,price,cost,store_name,unit,delete_time,vip_price,top_cate_id,batch';
foreach ($list as $key => &$item) {
$find = StoreProduct::where(['id' => $item['product_id']])
->field($field)
->find();
if ($find) {
$item['goods_total_price'] = bcmul($item['cart_num'], $find['price'], 2);
$this->total_price = bcadd($this->total_price, $item['goods_total_price'], 2);
$item['batch'] = $find['batch'];
$item['imgs'] = $find['image'];
$item['price'] = $find['price'];
$item['cost'] = $find['cost'];
$item['goods_name'] = $find['store_name'];
$item['unit_name'] = StoreProductUnit::where('id', $find['unit'])->value('name');
}
}
return $list;
}
/**
* @notes 购物车数量
* @return int
* @date 2024/04/27 11:26
*/
public function count(): int
{
$userId = $this->request->userId;
if (!$userId) return 0;
$where = [
'uid' => $userId,
'is_pay' => 0
];
return Cart::where($this->searchWhere)->where($where)->count();
}
public function extend()
{
$data = [
'off_activity' => $this->off_activity,
'total_price' => $this->total_price,
'msg' => '',
'pay_price' => $this->total_price
];
return $data;
}
}

View File

@ -0,0 +1,117 @@
<?php
namespace app\api\lists\product;
use app\api\lists\BaseApiDataLists;
use app\common\lists\ListsExtendInterface;
use app\common\lists\ListsSortInterface;
use app\common\model\store_product\StoreProduct;
use app\common\lists\ListsSearchInterface;
use app\common\model\Config;
use app\common\model\user\User;
//use app\common\model\goods\GoodsLabel;
use think\facade\Db;
/**
* 商品列表列表
* Class goods
* @package app\api\goods
*/
class ProductWholesaleLists extends BaseApiDataLists implements ListsSearchInterface, ListsSortInterface, ListsExtendInterface
{
protected $off_activity = 0;
protected $user_ship = 0;
/**
* @notes 设置搜索条件
* @return \string[][]
* @author likeadmin
* @date 2024/04/23 11:28
*/
public function setSearch(): array
{
return [
'=' => [ 'cate_id', 'top_cate_id', 'two_cate_id'],
'%pipe_like%' => ['store_name' => 'store_name|bar_code'],
];
}
/**
* @notes 设置支持排序字段
* @return string[]
* @date 2021/12/29 10:07
* @remark 格式: ['前端传过来的字段名' => '数据库中的字段名'];
*/
public function setSortFields(): array
{
return ['sell' => 'price', 'sales' => 'sales',];
}
/**
* @notes 设置默认排序
* @return string[]
* @date 2021/12/29 10:06
*/
public function setDefaultOrder(): array
{
return ['price' => 'asc', 'id' => 'desc'];
}
/**
* @notes 获取商品列表列表
* @return array
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
* @author likeadmin
* @date 2024/04/23 11:28
*/
public function lists(): array
{
$order = $this->request->get('order', '');
$field = $this->request->get('field', '');
if (empty($order) || empty($field)) {
$order = $this->sortOrder;
} else {
$order = [$field => $order];
}
$fields = 'id,id product_id,top_cate_id,cate_id,store_name,cost,vip_price,purchase,price,bar_code,image,sales,store_info,delete_time,unit,batch,top_cate_id,two_cate_id,stock';
$this->searchWhere[] = ['is_show', '=', 1];
$this->searchWhere[] = ['product_type', '=', 5];
// $this->searchWhere[] = ['stock', '>', 0];
return StoreProduct::where($this->searchWhere)
->field($fields)
->with(['className', 'unitName'])
->limit($this->limitOffset, $this->limitLength)
->order($order)
->select()
->toArray();
}
/**
* @notes 获取商品列表数量
* @return int
* @author likeadmin
* @date 2024/04/23 11:28
*/
public function count(): int
{
return StoreProduct::where($this->searchWhere)
->count();
}
public function extend()
{
$price = 'price';
$op_price = 'price';
$data = [
'off_activity' => $this->off_activity,
'price' => $price,
'op_price' => $op_price,
];
return $data;
}
}

View File

@ -28,9 +28,10 @@ class CartLogic extends BaseLogic
*/
public static function add(array $params)
{
if ($params['store_id'] <= 0) {
if ($params['store_id'] < 0) {
throw new BusinessException('门店ID不能为空');
}
$source=$params['source'] ?? 0;
Db::startTrans();
try {
//check
@ -38,7 +39,8 @@ class CartLogic extends BaseLogic
'uid' => $params['uid'],
'store_id' => $params['store_id'],
'product_id' => $params['product_id'],
'is_pay' => 0
'is_pay' => 0,
'source' => $source,
])->field('id')->find();
if ($check) {
Cart::where('id', $check['id'])->inc('cart_num', $params['cart_num'])
@ -53,6 +55,7 @@ class CartLogic extends BaseLogic
'staff_id' => $params['staff_id'] ?? 0,
'cart_num' => $params['cart_num'],
'is_new' => $params['is_new'] ?? 0,
'source' =>$source,
]);
}
StoreProductLog::create([

View File

@ -66,12 +66,15 @@ class OrderLogic extends BaseLogic
*/
static public function cartIdByOrderInfo($cartId, $addressId, $user = null, $params = [], $createOrder = 0)
{
if(empty($params['store_id']) || $params['store_id'] <= 0){
if (empty($params['store_id']) || $params['store_id'] <= 0) {
throw new BusinessException('请选择门店');
}
$source=0;
if (isset($params['source']) && $params['source'] >0) {
$source=$params['source'];
}
$where = ['is_pay' => 0];
$cart_select = Cart::whereIn('id', $cartId)->where($where)->field('id,product_id,cart_num')->select()->toArray();
$cart_select = Cart::whereIn('id', $cartId)->where($where)->field('id,product_id,cart_num,source')->select()->toArray();
if (empty($cart_select)) {
throw new BusinessException('购物车为空');
}
@ -89,10 +92,10 @@ class OrderLogic extends BaseLogic
$off_activity = Config::where('name', 'off_activity')->value('value');
$field = 'id,store_name,image,unit,price,vip_price,cost,purchase,cate_id,store_info,rose';
foreach ($cart_select as $k => $v) {
if (isset($params['source']) && $params['source'] == 2) {
if ($source == 2) {
$field = 'product_id,product_id id,store_name,image,unit,price,vip_price,cost,purchase,cate_id,store_info,rose';
$find = StoreBranchProduct::where(['product_id' => $v['product_id'],'store_id'=>$params['store_id']])->field($field)->find();
}else{
$find = StoreBranchProduct::where(['product_id' => $v['product_id'], 'store_id' => $params['store_id']])->field($field)->find();
} else {
$find = StoreProduct::where(['id' => $v['product_id']])->field($field)->find();
}
if (!$find) {
@ -116,21 +119,26 @@ class OrderLogic extends BaseLogic
}
unset($cart_select[$k]['id']);
$cart_select[$k]['total_price'] = bcmul($v['cart_num'], $find['price'], 2); //订单总价
if ($off_activity == 1 || ($user != null && in_array($user['user_ship'], [4, 6, 7]))) {
$price = $find['cost'];
} else {
$price = $find['price'];
//单门店活动判断
if ($params['store_id'] == getenv('ACTIVITY_STORE_ID')) {
$storeBranchPrice = StoreBranchProduct::where('store_id', getenv('ACTIVITY_STORE_ID'))->where('product_id', $v['product_id'])->value('price');
if ($storeBranchPrice) {
$price = $storeBranchPrice;
if ($v['source'] != 4) {
if ($off_activity == 1 || ($user != null && in_array($user['user_ship'], [4, 6, 7]))) {
$price = $find['cost'];
} else {
$price = $find['price'];
//单门店活动判断
if ($params['store_id'] == getenv('ACTIVITY_STORE_ID')) {
$storeBranchPrice = StoreBranchProduct::where('store_id', getenv('ACTIVITY_STORE_ID'))->where('product_id', $v['product_id'])->value('price');
if ($storeBranchPrice) {
$price = $storeBranchPrice;
}
}
}
if ($off_activity == 0 && $find['top_cate_id'] == 15189 && $user && $user['user_ship'] == 5) {
$price = $find['cost'];
}
} else {
$price = $find['price'];
}
if ($off_activity == 0 && $find['top_cate_id'] == 15189 && $user && $user['user_ship'] == 5) {
$price = $find['cost'];
}
$cart_select[$k]['price'] = $price;
$cart_select[$k]['cost'] = $find['cost'];
$cart_select[$k]['vip'] = 0;
@ -173,7 +181,7 @@ class OrderLogic extends BaseLogic
$cart_select[$k]['imgs'] = $find['image'];
$cart_select[$k]['store_id'] = $params['store_id'] ?? 0;
$cart_select[$k]['unit_name'] = StoreProductUnit::where(['id' => $find['unit']])->value('name');
$cart_select[$k]['total_price'] =$cart_select[$k]['pay_price'];
$cart_select[$k]['total_price'] = $cart_select[$k]['pay_price'];
self::$total_price = bcadd(self::$total_price, $cart_select[$k]['total_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);
@ -195,7 +203,7 @@ class OrderLogic extends BaseLogic
$pay_price = self::$pay_price; // bcsub(self::$pay_price, self::$activity_price, 2); //减去活动优惠金额
//判断生鲜是否大于200
if ($createOrder == 1 && self::$fresh_price > 0) {
if ($createOrder == 1 && self::$fresh_price > 0 &&$source!=2) {
if (self::$pay_price < 200) {
throw new BusinessException('订单包含生鲜产品订单金额必须大于200元才能下单');
}
@ -223,7 +231,7 @@ class OrderLogic extends BaseLogic
'activities' => $off_activity,
'deduction_price' => self::$deduction_price, //抵扣金额
'frozen_money' => 0, //self::$frozen_money, //返还金额(活动关闭得时候有)
'source' => 0,
'source' => $source,
'is_storage' => $params['is_storage'] ?? 0,
'address_id' => 0,
];
@ -231,9 +239,6 @@ class OrderLogic extends BaseLogic
if ($params['store_id']) {
$order['default_delivery'] = SystemStore::where('id', $params['store_id'])->value('is_send');
}
if (isset($params['source']) && $params['source'] > 0) {
$order['source'] = $params['source'];
}
if (isset($params['store_id']) && $params['store_id'] > 0) {
$store_id = $params['store_id'];
@ -259,8 +264,6 @@ class OrderLogic extends BaseLogic
}
} catch (\Throwable $e) {
throw new BusinessException($e->getMessage());
}
return ['order' => $order, 'cart_list' => $cart_select, 'shopInfo' => $store['near_store'], 'alert' => $alert];
}
@ -335,7 +338,7 @@ class OrderLogic extends BaseLogic
$order = StoreOrder::create($_order);
$goods_list = $orderInfo['cart_list'];
foreach ($goods_list as $k => $v) {
$goods_list[$k]['oid'] = $order->id;
$goods_list[$k]['uid'] = $uid;
@ -443,7 +446,7 @@ class OrderLogic extends BaseLogic
// 回滚事务
Db::rollback();
Log::error('支付失败' . $e->getMessage() . '。like:' . $e->getLine());
throw new BusinessException('支付失败'. $e->getMessage());
throw new BusinessException('支付失败' . $e->getMessage());
}
}
@ -499,7 +502,7 @@ class OrderLogic extends BaseLogic
$find['goods_list'] = StoreOrderCartInfo::where('oid', $find['id'])
->field('product_id,cart_num nums,store_id')->select()->each(function ($item) use ($find) {
$find = StoreProduct::where('id', $item['product_id'])->withTrashed()->find();
$item['store_name'] = $find['store_name'];
$item['nums'] = floatval($item['nums']);
$item['image'] = $find['image'];

View File

@ -17,6 +17,7 @@ declare(strict_types=1);
namespace app\common\validate;
use app\common\service\JsonService;
use support\exception\BusinessException;
use taoser\Validate;
class BaseValidate extends Validate
@ -31,7 +32,7 @@ class BaseValidate extends Validate
public function post()
{
if (!(request()->method() == 'POST')) {
JsonService::throw('请求方式错误请使用post请求方式');
throw new BusinessException('请求方式错误请使用post请求方式');
}
$this->method = 'POST';
return $this;
@ -45,7 +46,7 @@ class BaseValidate extends Validate
public function get()
{
if (!(request()->method() == 'GET')) {
JsonService::throw('请求方式错误请使用get请求方式');
throw new BusinessException('请求方式错误请使用get请求方式');
}
return $this;
}
@ -79,7 +80,7 @@ class BaseValidate extends Validate
if (!$result) {
$exception = is_array($this->error) ? implode(';', $this->error) : $this->error;
JsonService::throw($exception);
throw new BusinessException($exception);
}
// 3.成功返回数据
return $params;