商品列表和订单逻辑查询

This commit is contained in:
liu 2024-06-03 16:11:14 +08:00
parent 5d24bbe2a0
commit cd53cc69f4
5 changed files with 285 additions and 16 deletions

View File

@ -0,0 +1,25 @@
<?php
namespace app\api\controller\product;
use app\api\controller\BaseApiController;
use app\api\lists\product\ProductLists;
class ProductController extends BaseApiController{
public $notNeedLogin = ['lists'];
/**
* 商品列表
*/
public function lists(){
return $this->dataLists(new ProductLists());
}
/**
* 商品列表
*/
public function mer_list(){
$this->request->__set('mer_id',$this->request->userInfo['merchant']['mer_id']??0);
return $this->dataLists(new ProductLists());
}
}

View File

@ -0,0 +1,215 @@
<?php
namespace app\api\lists\product;
use app\admin\lists\BaseAdminDataLists;
use app\common\model\store_branch_product\StoreBranchProduct;
use app\common\model\store_product\StoreProduct;
use app\common\lists\ListsSearchInterface;
use app\common\model\cate\Cate;
//use app\common\model\goods\GoodsLabel;
use think\facade\Db;
/**
* 商品列表列表
* Class goods
* @package app\api\goods
*/
class ProductLists extends BaseAdminDataLists implements ListsSearchInterface
{
/**
* @notes 设置搜索条件
* @return \string[][]
* @author likeadmin
* @date 2024/04/23 11:28
*/
public function setSearch(): array
{
$name=$this->request->get('name');
// $where= [
// '=' => ['class']
// ];
// if($name && preg_match('/[\x{4e00}-\x{9fff}]+/u', $name)==1){
// $where['%like%']=['store_name'];
$where[] = ['store_name','like','%'.$name.'%'];
// }else{
// $where['=']=['bar_code','cate_id'];
// }
return $where;
}
/**
* @notes 设置支持排序字段
* @return string[]
* @date 2021/12/29 10:07
* @remark 格式: ['前端传过来的字段名' => '数据库中的字段名'];
*/
public function setSortFields(): array
{
// return ['sell' => 'sell', 'sales' => 'sales',];
return ['sell' => 'ot_price', 'sales' => 'sales',];
}
/**
* @notes 设置默认排序
* @return string[]
* @date 2021/12/29 10:06
*/
public function setDefaultOrder(): array
{
return ['sales' => 'desc','ot_price' => 'asc'];
}
/**
* @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
{
$class_all=$this->request->get('class_all');
$name = $this->request->get('name','');
$order_param = $this->request->get('order');
$store_id = $this->request->get('store_id',2);
$where=[];
$order = [];
if($class_all){
$arr=[];
$arr2=[];
$arr=Cate::where('pid',$class_all)->column('id');
if($arr){
$arr2=Cate::where('pid','in',$arr)->column('id');
$where[]=['cate_id','in',array_merge($arr,$arr2)];
}else{
$where[]=['cate_id','=',$class_all];
}
}
if(!empty($order_param)){
if($order_param == 'asc'){
$order['price'] = 'asc';
}elseif ($order_param == 'desc') {
$order['price'] = 'desc';
}elseif ($order_param=='sales') {
$order['sales'] = 'desc';
}
}else{
$order['id'] = 'desc';
}
if($name){
if(preg_match('/[\x{4e00}-\x{9fff}]+/u', $name)==1){
$where[] = ['store_name','like','%'.$name.'%'];
}else{
$where[] = ['bar_code','=',$name];
}
}
$where[]=['store_id','=',$store_id];
$data =StoreBranchProduct::where($this->searchWhere)->where($where)
->field(['id', 'product_id','cate_id','store_name', 'store_id','price', 'bar_code','image','sales','store_info','delete_time'])
->limit($this->limitOffset, $this->limitLength)
->with(['className'])
->order($order)
->select()
->toArray();
foreach ($data as $k=> &$v){
$info= StoreProduct::alias('p')
->leftJoin('store_product_unit t','t.id = p.unit')
->where('p.id',$v['product_id'])
->field('p.unit,t.name,t.is_bulk')->find()??[];
if($info){
$v['unit_name'] = $info['name']??'';
$v['is_bulk'] = $info['is_bulk']??'';
}else{
unset($data[$k]);
}
}
return array_values($data);
// return StoreProduct::where($this->searchWhere)->where($where)
// ->field(['id', 'cate_id','store_name','unit', 'ot_price', 'bar_code','image','sales','store_info'])
// ->limit($this->limitOffset, $this->limitLength)
// ->with(['className','unitName'])
// ->order($order)
// ->select()
// ->toArray();
}
/**
* @notes 获取商品列表数量
* @return int
* @author likeadmin
* @date 2024/04/23 11:28
*/
public function count(): int
{
$class_all=$this->request->get('class_all');
$order_param = $this->request->get('order');
$store_id = $this->request->get('store_id',2);
$name = $this->request->get('name','');
$where=[];
$order = [];
if($class_all){
$arr=[];
$arr2=[];
$arr=Cate::where('pid',$class_all)->column('id');
if($arr){
$arr2=Cate::where('pid','in',$arr)->column('id');
$where[]=['cate_id','in',array_merge($arr,$arr2)];
}else{
$where[]=['cate_id','=',$class_all];
}
}
if(!empty($order_param)){
if($order_param == 'asc'){
$order['price'] = 'asc';
}elseif ($order_param == 'desc') {
$order['price'] = 'desc';
}elseif ($order_param=='sales') {
$order['sales'] = 'desc';
}
}else{
$order['id'] = 'desc';
}
if($name){
if(preg_match('/[\x{4e00}-\x{9fff}]+/u', $name)==1){
$where[] = ['store_name','like','%'.$name.'%'];
}else{
$where[] = ['bar_code','=',$name];
}
}
$where[]=['store_id','=',$store_id];
$data =StoreBranchProduct::where($this->searchWhere)->where($where)
->field(['id', 'product_id'])
->order($order)
->select()
->toArray();
foreach ($data as $k=> &$v){
$info= StoreProduct::alias('p')
->leftJoin('store_product_unit t','t.id = p.unit')
->where('p.id',$v['product_id'])
->field('p.unit,t.name,t.is_bulk')->find()??[];
if($info){
$v['unit_name'] = $info['name']??'';
$v['is_bulk'] = $info['is_bulk']??'';
}else{
unset($data[$k]);
}
}
return count($data);
}
}

View File

@ -41,8 +41,8 @@ class OrderLogic extends BaseLogic
static public function cartIdByOrderInfo($cartId, $addressId, $user = null, $params = [])
{
$where = ['is_pay' => 0, 'is_fail' => 0];
$cart_select = Cart::whereIn('cart_id', $cartId)->where($where)->field('goods_id as goods,cart_num')->select()->toArray();
$where = ['is_pay' => 0, 'is_del' => 0];
$cart_select = Cart::whereIn('id', $cartId)->where($where)->field('product_id as goods,cart_num')->select()->toArray();
if (empty($cart_select)) {
self::setError('购物车为空');
return false;
@ -51,24 +51,26 @@ class OrderLogic extends BaseLogic
self::$total = 0;
/** 计算价格 */
foreach ($cart_select as $k => $v) {
$find = Goods::where(['id' => $v['goods']])->field('name,imgs,unit,sell')->find();
$find = StoreProduct::where(['id' => $v['goods']])->field('store_name,image,unit,price')->find();
if(!$find){
continue;
}
$cart_select[$k]['total'] = bcmul($v['cart_num'], $find['sell'], 2);
$cart_select[$k]['price'] = $find['sell'];
$cart_select[$k]['name'] = $find['name'];
$cart_select[$k]['imgs'] = $find['imgs'];
$cart_select[$k]['unit_name'] = Unit::where(['id' => $find['unit']])->value('name');
$cart_select[$k]['total'] = bcmul($v['cart_num'], $find['price'], 2);//钱
$cart_select[$k]['price'] = $find['price'];
$cart_select[$k]['name'] = $find['store_name'];
$cart_select[$k]['imgs'] = $find['image'];
$cart_select[$k]['unit_name'] = StoreProductUnit::where(['id' => $find['unit']])->value('name');
self::$total=bcadd(self::$total, $cart_select[$k]['total'], 2);
}
$order = [
'time' => time(),
'number' => getNewOrderId('PF'),
'total' => self::$total,
'add_time' => time(),
'create_time' => time(),
'order_id' => getNewOrderId('PF'),
'total_price' => self::$total,//总价
'total_num' => count($cart_select),//总数
'pay_type' => $params['pay_type'] ?? 0,
'cart_id' => implode(',', $cartId),
'delivery_msg'=>' 预计48小时发货 '
// 'delivery_msg'=>' 预计48小时发货 '
];
} catch (\Exception $e) {
self::setError($e->getMessage());
@ -115,7 +117,7 @@ class OrderLogic extends BaseLogic
}
Db::startTrans();
try {
$order = Cashierclass::create($_order);
$order = StoreOrder::create($_order);
$goods_list = $orderInfo['cart_list'];
foreach ($goods_list as $k => $v) {
$goods_list[$k]['pid'] = $order->id;
@ -126,8 +128,8 @@ class OrderLogic extends BaseLogic
$goods_list[$k]['warehouse'] = 0;
$goods_list[$k]['nums'] = $v['cart_num'];
}
(new Cashierinfo())->saveAll($goods_list);
$where = ['is_pay' => 0, 'is_fail' => 0];
(new StoreOrderCartInfo())->saveAll($goods_list);
$where = ['is_pay' => 0, 'is_del' => 0];
Cart::whereIn('cart_id', $cartId)->where($where)->update(['is_pay'=>1]);
Db::commit();
return $order;

View File

@ -4,6 +4,9 @@ namespace app\common\model\store_branch_product;
use app\common\model\BaseModel;
use app\common\model\store_category\StoreCategory;
use app\common\model\store_product\StoreProduct;
use app\common\model\store_product_unit\StoreProductUnit;
use think\model\concern\SoftDelete;
@ -18,5 +21,20 @@ class StoreBranchProduct extends BaseModel
protected $name = 'store_branch_product';
protected $deleteTime = 'delete_time';
public function className()
{
return $this->hasOne(StoreCategory::class,'id','cate_id')->bind(['class_name'=>'name']);
}
public function store()
{
return $this->hasOne(StoreProduct::class,'id','product_id');
}
}

View File

@ -4,6 +4,7 @@ namespace app\common\model\store_product;
use app\common\model\BaseModel;
use app\common\model\store_category\StoreCategory;
use app\common\model\store_product_unit\StoreProductUnit;
use think\model\concern\SoftDelete;
@ -25,5 +26,13 @@ class StoreProduct extends BaseModel
return $this->hasOne(StoreProductUnit::class,'id','unit')->bind(['unit_name'=>'name','is_bulk']);
}
public function className()
{
return $this->hasOne(StoreCategory::class,'id','cate_id')->bind(['class_name'=>'name']);
}
}