dlh_test_server/app/api/logic/LocalStoreLogic.php
2024-10-03 19:12:31 +08:00

452 lines
17 KiB
PHP

<?php
namespace app\api\logic;
use app\common\basics\Logic;
use app\common\model\LocalStore;
use app\common\model\localstore\LocalGoodsStore;
use app\common\model\LocalStoreOrder;
use app\common\model\LocalStorePostStation;
use think\facade\Db;
use think\facade\Config;
class LocalStoreLogic extends Logic
{
public function getLocalList($data)
{
$model = new LocalStore;
$distance = "";
$s = $s1 = "";
$city_where = [];
if (isset($data['c_name'])) {
if ($data['c_name'] != "") {
$s1 = "address like '%" . $data['c_name'] . "%'";
$s = "and address like '%" . $data['c_name'] . "%'";
}
if ($data['c_name'] == '嘉兴市') {
$city_where[] = ['lstore_id', '=', 138];
}
}
$count = $model->where('is_delete', 0)
->where($city_where)
->where($s1)
->where("store_state", 1)
->where("shopLogo is not null")
->count();
// $lists = $model->where('is_delete',0)
// ->where($s)
// ->page($data['page'], $data['size'])
// ->order('lstore_id desc')
// ->select();
$size = $data['page_size'];
$page = ($data['page_no'] - 1) * $data['page_size'];
// $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";
if (isset($data['c_name']) && $data['c_name'] == '嘉兴市') {
$sql = "SELECT * FROM ls_local_store where lstore_id = 138 and is_delete = 0 and store_state = 1 and shopLogo is not null $s ORDER BY lstore_id asc limit " . $page . "," . $size;
} else {
$sql = "SELECT * FROM ls_local_store where is_delete = 0 and store_state = 1 and shopLogo is not null $s ORDER BY lstore_id asc limit " . $page . "," . $size;
}
// var_dump($sql);
$lists = Db::query($sql);
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"];
}
$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;
}
public function getStreetlList($data)
{
$model = new LocalStore;
$distance = "";
$s = $s1 = "";
$city_where = [];
if (isset($data['c_name'])) {
if ($data['c_name'] != "") {
$s1 = "address like '%" . $data['c_name'] . "%'";
$s = "and address like '%" . $data['c_name'] . "%'";
}
}
$count = $model->where('is_delete', 0)
->where($city_where)
->where($s1)
->where("store_state", 1)
->where("shopLogo is not null")
->count();
$size = $data['page_size'];
$page = ($data['page_no'] - 1) * $data['page_size'];
$sql = "SELECT * FROM ls_local_store where is_delete = 0 and store_state = 1 and shopLogo is not null $s ORDER BY lstore_id asc limit " . $page . "," . $size;
$lists = Db::query($sql);
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"];
}
$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;
}
public function getLocalOrderList($data)
{
$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();
// $lists = $model->where('is_delete',0)
// ->where($s)
// ->page($data['page'], $data['size'])
// ->order('lstore_id desc')
// ->select();
// $size = $data['page_size'];
// $page = ($data['page_no']-1)*$data['page_size'];
// $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";
// $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')
->where(['lso.state' => 1, 'lso.pay_shop_type' => 1, 'lso.lstore_id' => $data['lstore_id']])
->order('lso.create_time desc')
->page($data['page_no'], $data['page_size'])
->select();
$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;
}
public function getLocalUserOrderList($data)
{
$count = Db::name("local_store_order")
->where("user_id", $data['user_id'])
->where("pay_shop_type", 1)
->where('state', 1)
->count();
$size = $data['page_size'];
$page = ($data['page_no'] - 1) * $data['page_size'];
$sql = "SELECT * FROM ls_local_store_order where state = 1 and
pay_shop_type = 1 and user_id = " . $data['user_id'] . "
ORDER BY lstore_id desc limit " . $page . "," . $size;
$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;
}
function distance($lat1, $lon1, $lat2, $lon2)
{
$earthRadius = 6371000; // 地球平均半径,单位为米
// 将经纬度转换为弧度
$lat1 = deg2rad($lat1);
$lon1 = deg2rad($lon1);
$lat2 = deg2rad($lat2);
$lon2 = deg2rad($lon2);
// 使用haversine公式计算距离
$diffLat = $lat2 - $lat1;
$diffLon = $lon2 - $lon1;
$a = sin($diffLat / 2) * sin($diffLat / 2) + cos($lat1) * cos($lat2) * sin($diffLon / 2) * sin($diffLon / 2);
$c = 2 * atan2(sqrt($a), sqrt(1 - $a));
$distance = $earthRadius * $c;
return $distance;
}
public static function record($page, $size, $user_id)
{
$list = LocalStore::where(['user_id' => $user_id, 'is_delete' => 0])->page($page, $size)->order('lstore_id desc')->select()->toArray();
$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,
];
}
public static function submitOrder($post,$user_id){
$goods_msg = Db::name("local_store_goods")->where("id",$post['goods_id'])->find();
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);
}
}else{
$data['goods_total_price'] = round($goods_msg['price']*$post['goods_num'],2);
}
$data['order_no'] = createSn('local_goods_order', 'order_no');
$data['lstore_id'] = $post['lstore_id'];
$data['item_id'] = $post['item_id'];
$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;
}
public static function orderDetails($post){
$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;
$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;
$item = Db::name("local_store_goods_item")->where("id",$result['item_id'])->find();
if($item != null){
$item['image'] = Config::get('aliyunoss.domain').$item['image'];
}
$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;
}
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;
}
public static function myOrderList($post)
{
$s = "";
if(isset($post['state'])){
$s = "state= ".$post['state']."";
}
$list = LocalGoodsStore::where('user_id',$post['user_id'])
->where($s)
->where("state <> 3")
->page($post['page_no'], $post['page_size'])
->order('dtime desc')
->select()
->toArray();
foreach($list as $key=>$value){
$goods = Db::name("local_store_goods")->where("id",$value['goods_id'])->find();
$image = Db::name("local_store_goods_pic")->where("lsg_id",$value['goods_id'])->select()->toArray();
foreach($image as $k=>$v){
$image[$k]['url'] = Config::get('aliyunoss.domain').$v['url'];
}
$sku_msg = Db::name("local_store_goods_item")
->field("image,spec_value_str")
->where("id",$value['item_id'])
->find();
if($sku_msg != null){
$sku_msg['image'] = Config::get('aliyunoss.domain').$sku_msg['image'];
}
$list[$key]['sku_msg'] = $sku_msg;
$list[$key]['goods_msg'] = $goods;
$list[$key]['goods_image'] = $image;
$local = Db::name("local_store")->field("shopName,shopLogo")->where("lstore_id",$value['lstore_id'])->find();
$post_station = Db::name('local_store_post_station')->where('id',$value['post_station_id'])->find();
$list[$key]['post_station'] = $post_station;
$list[$key]['local_msg'] = $local;
$list[$key]['post_station_time'] = date("m-d", $value['post_station_time']);
}
$count = LocalGoodsStore::where('user_id',$post['user_id'])
->where($s)
->where("state <> 3")
->count();
$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();
$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,
'all_count'=>$all_count,
'wait_count' => $wait_count,
'already_count' => $already_count,
'cancel_count' => $cancel_count,
'more' => $more,
];
}
public static function orderList($post)
{
$s = "";
if(isset($post['state'])){
$s = "state= ".$post['state']."";
}
$list = LocalGoodsStore::where('lstore_id',$post['lstore_id'])
->where($s)
->where("state <> 3")
->page($post['page_no'], $post['page_size'])
->order('dtime desc')
->select()
->toArray();
foreach($list as $key=>$value){
$goods = Db::name("local_store_goods")->where("id",$value['goods_id'])->find();
$image = Db::name("local_store_goods_pic")->where("lsg_id",$value['goods_id'])->select()->toArray();
foreach($image as $k=>$v){
$image[$k]['url'] = Config::get('aliyunoss.domain').$v['url'];
}
$sku_msg = Db::name("local_store_goods_item")
->field("image,spec_value_str")
->where("id",$value['item_id'])
->find();
if($sku_msg != null){
$sku_msg['image'] = Config::get('aliyunoss.domain').$sku_msg['image'];
}
$list[$key]['sku_msg'] = $sku_msg;
$list[$key]['goods_msg'] = $goods;
$list[$key]['goods_image'] = $image;
$local = Db::name("local_store")->field("shopName,shopLogo")->where("lstore_id",$value['lstore_id'])->find();
$list[$key]['local_msg'] = $local;
}
$count = LocalGoodsStore::where('lstore_id',$post['lstore_id'])
->where($s)
->where("state <> 3")
->count();
$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();
$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,
'all_count'=>$all_count,
'wait_count' => $wait_count,
'already_count' => $already_count,
'cancel_count' => $cancel_count,
'more' => $more,
];
}
public static function addPostStation($post, $user_id)
{
if (!$post['province'] && !$post['city'] && !$post['area'] && !$post['address']) {
throw new \Exception('未选择区域');
}
if (!$post['lat'] && !$post['lng']) {
throw new \Exception('经纬度不能为空');
}
$post['user_id'] = $user_id;
$result = LocalStorePostStation::insert($post);
if ($result) {
return true;
}
throw new \Exception('更新失败');
}
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('删除失败');
}
}