2024-08-10 10:18:04 +08:00
< ? php
namespace app\api\logic ;
use app\common\basics\Logic ;
use app\common\model\LocalStore ;
2024-09-16 21:53:19 +08:00
use app\common\model\localstore\LocalGoodsStore ;
2024-09-01 09:53:16 +08:00
use app\common\model\LocalStoreOrder ;
2024-09-29 17:14:43 +08:00
use app\common\model\LocalStorePostStation ;
2024-08-10 10:18:04 +08:00
use think\facade\Db ;
2024-09-16 23:23:40 +08:00
use think\facade\Config ;
2024-09-29 17:14:43 +08:00
2024-08-10 10:18:04 +08:00
class LocalStoreLogic extends Logic
{
2024-09-05 16:31:02 +08:00
public function getLocalList ( $data )
{
2024-08-10 10:18:04 +08:00
$model = new LocalStore ;
$distance = " " ;
2024-09-05 16:31:02 +08:00
$s = $s1 = " " ;
if ( isset ( $data [ 'c_name' ])) {
if ( $data [ 'c_name' ] != " " ) {
$s1 = " address like '% " . $data [ 'c_name' ] . " %' " ;
$s = " and address like '% " . $data [ 'c_name' ] . " %' " ;
2024-08-10 10:34:18 +08:00
}
}
2024-09-05 16:31:02 +08:00
$count = $model -> where ( 'is_delete' , 0 )
2024-09-29 20:03:22 +08:00
-> where ( 'lstore_id' , 22 )
2024-08-10 10:38:31 +08:00
-> where ( $s1 )
2024-09-05 16:31:02 +08:00
-> where ( " store_state " , 1 )
2024-08-10 15:55:35 +08:00
-> where ( " shopLogo is not null " )
2024-08-10 10:37:16 +08:00
-> count ();
2024-08-10 10:18:04 +08:00
// $lists = $model->where('is_delete',0)
// ->where($s)
// ->page($data['page'], $data['size'])
// ->order('lstore_id desc')
// ->select();
2024-08-10 10:40:10 +08:00
$size = $data [ 'page_size' ];
2024-09-05 16:31:02 +08:00
$page = ( $data [ 'page_no' ] - 1 ) * $data [ 'page_size' ];
2024-08-10 14:58:01 +08:00
// $sql = "SELECT *, ST_Distance_Sphere(POINT(".$data['longitude'].",".$data['latitude']."),POINT(longitude, latitude)) AS distance
// FROM ls_local_store where is_delete = 0 $s
// ORDER BY distance limit $page,$size";
2024-09-29 20:03:22 +08:00
$sql = " SELECT * FROM ls_local_store where lstore_id = 22 and is_delete = 0 and store_state = 1 and shopLogo is not null $s ORDER BY lstore_id asc limit " . $page . " , " . $size ;
2024-08-10 16:01:35 +08:00
// var_dump($sql);
2024-08-10 10:18:04 +08:00
$lists = Db :: query ( $sql );
2024-09-29 20:04:47 +08:00
// foreach ($lists as $key => $value) {
// $lists[$key]['distance'] = round($this->distance($data['latitude'], $data['longitude'], $value['latitude'], $value['longitude']) / 1000, 2) . "公里";
// $lists[$key]['store_nickname'] = $value["shopName"];
// }
2024-08-10 10:18:04 +08:00
$data = [
'list' => $lists ,
2024-08-10 10:23:02 +08:00
'page' => $data [ 'page_no' ],
'size' => $data [ 'page_size' ],
2024-08-10 10:18:04 +08:00
'count' => $count ,
2024-08-10 10:23:02 +08:00
'more' => is_more ( $count , $data [ 'page_no' ], $data [ 'page_size' ])
2024-08-10 10:18:04 +08:00
];
2024-08-10 16:01:35 +08:00
// var_dump($data);
2024-08-10 10:18:04 +08:00
return $data ;
}
2024-08-11 16:58:13 +08:00
2024-09-05 16:31:02 +08:00
public function getLocalOrderList ( $data )
{
2024-09-01 09:53:16 +08:00
$count = LocalStoreOrder :: where ([ 'lstore_id' => $data [ 'lstore_id' ], 'pay_shop_type' => 1 , 'state' => 1 ]) -> count ();
// $count = Db::name("local_store_order")
// ->where("lstore_id",$data['lstore_id'])
// ->where("pay_shop_type",1)
// ->where('state',1)
// ->count();
2024-08-11 16:58:13 +08:00
// $lists = $model->where('is_delete',0)
// ->where($s)
// ->page($data['page'], $data['size'])
// ->order('lstore_id desc')
// ->select();
2024-09-01 09:53:16 +08:00
// $size = $data['page_size'];
// $page = ($data['page_no']-1)*$data['page_size'];
2024-08-11 16:58:13 +08:00
// $sql = "SELECT *, ST_Distance_Sphere(POINT(".$data['longitude'].",".$data['latitude']."),POINT(longitude, latitude)) AS distance
// FROM ls_local_store where is_delete = 0 $s
// ORDER BY distance limit $page,$size";
2024-09-01 09:53:16 +08:00
// $sql = "SELECT * FROM ls_local_store_order where state = 1 and
// pay_shop_type = 1 and lstore_id = ".$data['lstore_id']."
// ORDER BY create_time desc limit ".$page.",".$size;
// $lists = Db::query($sql);
$lists = LocalStoreOrder :: alias ( 'lso' )
-> field ( 'lso.*, u.id, u.nickname' )
-> join ( 'user u' , 'lso.user_id = u.id' )
2024-09-01 22:31:16 +08:00
-> where ([ 'lso.state' => 1 , 'lso.pay_shop_type' => 1 , 'lso.lstore_id' => $data [ 'lstore_id' ]])
2024-09-01 09:53:16 +08:00
-> order ( 'lso.create_time desc' )
-> page ( $data [ 'page_no' ], $data [ 'page_size' ])
-> select ();
2024-08-11 16:58:13 +08:00
$data = [
'list' => $lists ,
'page' => $data [ 'page_no' ],
'size' => $data [ 'page_size' ],
'count' => $count ,
'more' => is_more ( $count , $data [ 'page_no' ], $data [ 'page_size' ])
];
// var_dump($data);
return $data ;
}
2024-08-12 10:31:55 +08:00
2024-09-05 16:31:02 +08:00
public function getLocalUserOrderList ( $data )
{
2024-08-12 10:31:55 +08:00
$count = Db :: name ( " local_store_order " )
2024-09-05 16:31:02 +08:00
-> where ( " user_id " , $data [ 'user_id' ])
-> where ( " pay_shop_type " , 1 )
-> where ( 'state' , 1 )
2024-08-12 10:31:55 +08:00
-> count ();
$size = $data [ 'page_size' ];
2024-09-05 16:31:02 +08:00
$page = ( $data [ 'page_no' ] - 1 ) * $data [ 'page_size' ];
2024-08-12 10:31:55 +08:00
$sql = " SELECT * FROM ls_local_store_order where state = 1 and
2024-09-05 16:31:02 +08:00
pay_shop_type = 1 and user_id = " . $data['user_id'] . "
ORDER BY lstore_id desc limit " . $page . " , " . $size ;
2024-08-12 10:31:55 +08:00
$lists = Db :: query ( $sql );
$data = [
'list' => $lists ,
'page' => $data [ 'page_no' ],
'size' => $data [ 'page_size' ],
'count' => $count ,
'more' => is_more ( $count , $data [ 'page_no' ], $data [ 'page_size' ])
];
return $data ;
}
2024-09-05 16:31:02 +08:00
function distance ( $lat1 , $lon1 , $lat2 , $lon2 )
{
2024-08-10 10:18:04 +08:00
$earthRadius = 6371000 ; // 地球平均半径,单位为米
// 将经纬度转换为弧度
$lat1 = deg2rad ( $lat1 );
$lon1 = deg2rad ( $lon1 );
$lat2 = deg2rad ( $lat2 );
$lon2 = deg2rad ( $lon2 );
// 使用haversine公式计算距离
2024-09-05 16:31:02 +08:00
$diffLat = $lat2 - $lat1 ;
$diffLon = $lon2 - $lon1 ;
2024-08-10 10:18:04 +08:00
$a = sin ( $diffLat / 2 ) * sin ( $diffLat / 2 ) + cos ( $lat1 ) * cos ( $lat2 ) * sin ( $diffLon / 2 ) * sin ( $diffLon / 2 );
2024-09-05 16:31:02 +08:00
$c = 2 * atan2 ( sqrt ( $a ), sqrt ( 1 - $a ));
2024-08-10 10:18:04 +08:00
$distance = $earthRadius * $c ;
return $distance ;
}
2024-09-05 16:31:02 +08:00
public static function record ( $page , $size , $user_id )
{
2024-09-05 16:41:42 +08:00
$list = LocalStore :: where ([ 'user_id' => $user_id , 'is_delete' => 0 ]) -> page ( $page , $size ) -> order ( 'lstore_id desc' ) -> select () -> toArray ();
2024-09-05 16:31:02 +08:00
$count = LocalStore :: where ([ 'user_id' => $user_id , 'is_delete' => 0 ]) -> count ();
$more = is_more ( $count , $page , $size ); //是否有下一页
return [
'list' => $list ,
'page_no' => $page ,
'page_size' => $size ,
'count' => $count ,
'more' => $more ,
];
}
2024-09-16 15:40:03 +08:00
2024-09-16 15:41:32 +08:00
public static function submitOrder ( $post , $user_id ){
2024-09-16 15:40:03 +08:00
$goods_msg = Db :: name ( " local_store_goods " ) -> where ( " id " , $post [ 'goods_id' ]) -> find ();
2024-09-16 15:54:31 +08:00
if ( $post [ 'item_id' ] > 0 ){
if ( $goods_msg [ 'is_multiple_spec' ] > 0 ){
$goods_sku_msg = Db :: name ( " local_store_goods_item " ) -> where ( " id " , $post [ 'item_id' ]) -> find ();
if ( $goods_sku_msg [ 'stock' ] < $post [ 'goods_num' ]){
return - 1 ;
}
$data [ 'goods_total_price' ] = round ( $goods_sku_msg [ 'price' ] * $post [ 'goods_num' ], 2 );
Db :: name ( " local_store_goods_item " )
-> where ( " id " , $post [ 'item_id' ])
-> dec ( " stock " , $post [ 'goods_num' ])
-> update ();
} else {
$data [ 'goods_total_price' ] = round ( $goods_msg [ 'price' ] * $post [ 'goods_num' ], 2 );
2024-09-16 15:40:03 +08:00
}
} else {
$data [ 'goods_total_price' ] = round ( $goods_msg [ 'price' ] * $post [ 'goods_num' ], 2 );
2024-09-16 15:54:31 +08:00
2024-09-16 15:40:03 +08:00
}
2024-09-17 00:56:47 +08:00
$data [ 'order_no' ] = createSn ( 'local_goods_order' , 'order_no' );
2024-09-16 15:40:03 +08:00
$data [ 'lstore_id' ] = $post [ 'lstore_id' ];
2024-09-16 15:54:31 +08:00
$data [ 'item_id' ] = $post [ 'item_id' ];
2024-09-16 15:40:03 +08:00
$data [ 'goods_id' ] = $post [ 'goods_id' ];
$data [ 'goods_num' ] = $post [ 'goods_num' ];
$data [ 'user_id' ] = $user_id ;
$data [ 'dtime' ] = date ( " Y-m-d H:i:s " );
$res = Db :: name ( " local_goods_order " ) -> insertGetId ( $data );
$result [ 'lsg_order_id' ] = $res ;
return $result ;
}
2024-09-16 15:41:32 +08:00
public static function orderDetails ( $post ){
2024-09-16 15:40:03 +08:00
$result = Db :: name ( " local_goods_order " ) -> where ( " lsg_order_id " , $post [ 'lsg_order_id' ]) -> find ();
$goods = Db :: name ( " local_store_goods " ) -> where ( " id " , $result [ 'goods_id' ]) -> find ();
$result [ 'goods_msg' ] = $goods ;
2024-09-17 00:40:35 +08:00
$image = Db :: name ( " local_store_goods_pic " ) -> where ( " lsg_id " , $result [ 'goods_id' ]) -> select () -> toArray ();
foreach ( $image as $k => $v ){
$image [ $k ][ 'url' ] = Config :: get ( 'aliyunoss.domain' ) . $v [ 'url' ];
}
$result [ 'goods_image' ] = $image ;
2024-09-16 15:40:03 +08:00
$item = Db :: name ( " local_store_goods_item " ) -> where ( " id " , $result [ 'item_id' ]) -> find ();
2024-09-17 00:41:53 +08:00
if ( $item != null ){
$item [ 'image' ] = Config :: get ( 'aliyunoss.domain' ) . $item [ 'image' ];
}
2024-09-16 15:40:03 +08:00
$result [ 'sku_msg' ] = $item ;
$local = Db :: name ( " local_store " ) -> field ( " shopName,shopLogo " ) -> where ( " lstore_id " , $result [ 'lstore_id' ]) -> find ();
$result [ 'local_msg' ] = $local ;
return $result ;
}
2024-09-16 17:21:05 +08:00
public static function cancelOrder ( $post ){
$result = Db :: name ( " local_goods_order " )
-> where ( " lsg_order_id " , $post [ 'lsg_order_id' ])
-> find ();
if ( $result [ 'item_id' ] > 0 ){
Db :: name ( " local_store_goods_item " )
-> where ( " id " , $result [ 'item_id' ])
-> inc ( " stock " , $result [ 'goods_num' ])
-> update ();
}
$res = Db :: name ( " local_goods_order " )
-> where ( " lsg_order_id " , $post [ 'lsg_order_id' ])
-> update ([ " state " => 2 ]);
return $res ;
}
2024-09-16 21:53:19 +08:00
public static function myOrderList ( $post )
2024-09-16 23:37:43 +08:00
{
2024-09-16 21:53:19 +08:00
$s = " " ;
2024-09-17 00:26:05 +08:00
if ( isset ( $post [ 'state' ])){
2024-09-17 00:15:06 +08:00
$s = " state= " . $post [ 'state' ] . " " ;
2024-09-17 00:26:05 +08:00
}
2024-09-17 00:19:33 +08:00
$list = LocalGoodsStore :: where ( 'user_id' , $post [ 'user_id' ])
2024-09-16 21:53:19 +08:00
-> where ( $s )
2024-09-16 22:53:23 +08:00
-> where ( " state <> 3 " )
2024-09-16 21:53:19 +08:00
-> page ( $post [ 'page_no' ], $post [ 'page_size' ])
2024-09-16 22:53:23 +08:00
-> order ( 'dtime desc' )
2024-09-16 21:53:19 +08:00
-> select ()
-> toArray ();
foreach ( $list as $key => $value ){
$goods = Db :: name ( " local_store_goods " ) -> where ( " id " , $value [ 'goods_id' ]) -> find ();
2024-09-16 23:37:43 +08:00
$image = Db :: name ( " local_store_goods_pic " ) -> where ( " lsg_id " , $value [ 'goods_id' ]) -> select () -> toArray ();
2024-09-16 23:23:40 +08:00
foreach ( $image as $k => $v ){
2024-09-16 23:37:43 +08:00
$image [ $k ][ 'url' ] = Config :: get ( 'aliyunoss.domain' ) . $v [ 'url' ];
2024-09-16 23:23:40 +08:00
}
2024-09-18 14:27:40 +08:00
$sku_msg = Db :: name ( " local_store_goods_item " )
-> field ( " image,spec_value_str " )
-> where ( " id " , $value [ 'item_id' ])
-> find ();
2024-09-18 14:29:31 +08:00
if ( $sku_msg != null ){
$sku_msg [ 'image' ] = Config :: get ( 'aliyunoss.domain' ) . $sku_msg [ 'image' ];
}
2024-09-18 14:27:40 +08:00
$list [ $key ][ 'sku_msg' ] = $sku_msg ;
2024-09-16 21:53:19 +08:00
$list [ $key ][ 'goods_msg' ] = $goods ;
2024-09-16 23:23:40 +08:00
$list [ $key ][ 'goods_image' ] = $image ;
2024-09-16 22:58:50 +08:00
$local = Db :: name ( " local_store " ) -> field ( " shopName,shopLogo " ) -> where ( " lstore_id " , $value [ 'lstore_id' ]) -> find ();
$list [ $key ][ 'local_msg' ] = $local ;
2024-09-16 21:53:19 +08:00
}
2024-09-17 00:19:33 +08:00
$count = LocalGoodsStore :: where ( 'user_id' , $post [ 'user_id' ])
2024-09-17 00:10:49 +08:00
-> where ( $s )
2024-09-16 22:55:33 +08:00
-> where ( " state <> 3 " )
-> count ();
2024-09-18 11:42:03 +08:00
$all_count = LocalGoodsStore :: where ( 'user_id' , $post [ 'user_id' ])
-> where ( " state <> 3 " )
-> count ();
$wait_count = LocalGoodsStore :: where ( 'user_id' , $post [ 'user_id' ])
-> where ( " state " , 0 )
-> where ( " state <> 3 " )
-> count ();
$already_count = LocalGoodsStore :: where ( 'user_id' , $post [ 'user_id' ])
-> where ( " state " , 1 )
-> where ( " state <> 3 " )
-> count ();
$cancel_count = LocalGoodsStore :: where ( 'user_id' , $post [ 'user_id' ])
-> where ( " state " , 2 )
-> where ( " state <> 3 " )
-> count ();
2024-09-16 21:53:19 +08:00
$more = is_more ( $count , $post [ 'page_no' ], $post [ 'page_size' ]); //是否有下一页
return [
'list' => $list ,
'page_no' => $post [ 'page_no' ],
'page_size' => $post [ 'page_size' ],
'count' => $count ,
2024-09-18 11:42:03 +08:00
'all_count' => $all_count ,
'wait_count' => $wait_count ,
'already_count' => $already_count ,
'cancel_count' => $cancel_count ,
2024-09-16 21:53:19 +08:00
'more' => $more ,
];
}
public static function orderList ( $post )
{
$s = " " ;
2024-09-17 00:26:05 +08:00
if ( isset ( $post [ 'state' ])){
2024-09-17 00:15:06 +08:00
$s = " state= " . $post [ 'state' ] . " " ;
2024-09-16 21:53:19 +08:00
}
2024-09-17 00:19:33 +08:00
$list = LocalGoodsStore :: where ( 'lstore_id' , $post [ 'lstore_id' ])
2024-09-16 21:53:19 +08:00
-> where ( $s )
2024-09-16 22:53:23 +08:00
-> where ( " state <> 3 " )
2024-09-16 21:53:19 +08:00
-> page ( $post [ 'page_no' ], $post [ 'page_size' ])
2024-09-16 22:53:23 +08:00
-> order ( 'dtime desc' )
2024-09-16 21:53:19 +08:00
-> select ()
-> toArray ();
foreach ( $list as $key => $value ){
$goods = Db :: name ( " local_store_goods " ) -> where ( " id " , $value [ 'goods_id' ]) -> find ();
2024-09-16 23:37:43 +08:00
$image = Db :: name ( " local_store_goods_pic " ) -> where ( " lsg_id " , $value [ 'goods_id' ]) -> select () -> toArray ();
2024-09-16 23:23:40 +08:00
foreach ( $image as $k => $v ){
2024-09-16 23:37:43 +08:00
$image [ $k ][ 'url' ] = Config :: get ( 'aliyunoss.domain' ) . $v [ 'url' ];
2024-09-16 23:23:40 +08:00
}
2024-09-18 14:27:40 +08:00
$sku_msg = Db :: name ( " local_store_goods_item " )
-> field ( " image,spec_value_str " )
-> where ( " id " , $value [ 'item_id' ])
-> find ();
2024-09-18 14:29:31 +08:00
if ( $sku_msg != null ){
$sku_msg [ 'image' ] = Config :: get ( 'aliyunoss.domain' ) . $sku_msg [ 'image' ];
}
2024-09-18 14:27:40 +08:00
$list [ $key ][ 'sku_msg' ] = $sku_msg ;
2024-09-16 21:53:19 +08:00
$list [ $key ][ 'goods_msg' ] = $goods ;
2024-09-16 23:23:40 +08:00
$list [ $key ][ 'goods_image' ] = $image ;
2024-09-16 22:58:50 +08:00
$local = Db :: name ( " local_store " ) -> field ( " shopName,shopLogo " ) -> where ( " lstore_id " , $value [ 'lstore_id' ]) -> find ();
$list [ $key ][ 'local_msg' ] = $local ;
2024-09-16 21:53:19 +08:00
}
2024-09-17 00:19:33 +08:00
$count = LocalGoodsStore :: where ( 'lstore_id' , $post [ 'lstore_id' ])
2024-09-17 00:10:49 +08:00
-> where ( $s )
2024-09-16 22:55:33 +08:00
-> where ( " state <> 3 " )
-> count ();
2024-09-18 11:42:03 +08:00
$all_count = LocalGoodsStore :: where ( 'lstore_id' , $post [ 'lstore_id' ])
-> where ( " state <> 3 " )
-> count ();
$wait_count = LocalGoodsStore :: where ( 'lstore_id' , $post [ 'lstore_id' ])
-> where ( " state " , 0 )
-> where ( " state <> 3 " )
-> count ();
$already_count = LocalGoodsStore :: where ( 'lstore_id' , $post [ 'lstore_id' ])
-> where ( " state " , 1 )
-> where ( " state <> 3 " )
-> count ();
$cancel_count = LocalGoodsStore :: where ( 'lstore_id' , $post [ 'lstore_id' ])
-> where ( " state " , 2 )
-> where ( " state <> 3 " )
-> count ();
2024-09-16 21:53:19 +08:00
$more = is_more ( $count , $post [ 'page_no' ], $post [ 'page_size' ]); //是否有下一页
return [
'list' => $list ,
'page_no' => $post [ 'page_no' ],
'page_size' => $post [ 'page_size' ],
'count' => $count ,
2024-09-18 11:42:03 +08:00
'all_count' => $all_count ,
'wait_count' => $wait_count ,
'already_count' => $already_count ,
'cancel_count' => $cancel_count ,
2024-09-16 21:53:19 +08:00
'more' => $more ,
];
}
2024-09-29 15:14:04 +08:00
2024-09-29 17:14:43 +08:00
public static function addPostStation ( $post , $user_id )
2024-09-29 15:14:04 +08:00
{
if ( ! $post [ 'province' ] && ! $post [ 'city' ] && ! $post [ 'area' ] && ! $post [ 'address' ]) {
throw new \Exception ( '未选择区域' );
}
2024-09-29 15:22:11 +08:00
if ( ! $post [ 'lat' ] && ! $post [ 'lng' ]) {
2024-09-29 15:14:04 +08:00
throw new \Exception ( '经纬度不能为空' );
}
2024-09-29 17:23:43 +08:00
$post [ 'user_id' ] = $user_id ;
$result = LocalStorePostStation :: insert ( $post );
2024-09-29 15:14:04 +08:00
if ( $result ) {
return true ;
}
throw new \Exception ( '更新失败' );
}
2024-09-29 17:53:43 +08:00
public static function delPostStation ( $post , $user_id )
{
if ( ! $post [ 'id' ]) {
throw new \Exception ( '参数不能为空' );
}
$result = LocalStorePostStation :: where ([
'id' => $post [ 'id' ],
'user_id' => $user_id
]) -> update ([ 'is_delete' => 1 ]);
if ( $result ) {
return true ;
}
throw new \Exception ( '删除失败' );
}
2024-08-10 10:18:04 +08:00
}