dlh_test_server/app/api/logic/LocalStoreLogic.php
2024-10-16 14:06:14 +08:00

698 lines
25 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\localstore\LocalStoreBind;
use app\common\model\LocalStoreOrder;
use app\common\model\LocalStorePostStation;
use app\common\model\user\User;
use app\common\server\UrlServer;
use think\facade\Config;
use think\facade\Db;
class LocalStoreLogic extends Logic
{
private static $local_menu = array(
1 => [
'id' => 1,
'name' => '商品添加',
'path' => '/bundle_b/pages/local_shop/local_shop_add',
'image' => 'uniapp/bundle_b/local_shop/local_add.png'
],
2 => [
'id' => 2,
'name' => '商品管理',
'path' => '/bundle_b/pages/local_shop/local_shop_manager',
'image' => 'uniapp/bundle_b/local_shop/local_shop.png'
],
3 => [
'id' => 3,
'name' => '订单管理',
'path' => '/bundle_b/pages/local_shop/local_shop_order',
'image' => 'uniapp/bundle_b/local_shop/local_bill.png'
],
4 => [
'id' => 4,
'name' => '站点设置',
'path' => '/bundle_b/pages/local_map/index',
'image' => 'uniapp/bundle_b/local_shop/local_map.png'
]
);
public function getLocalList($data)
{
$model = new LocalStore;
$distance = "";
$s = $s1 = "";
$keyword = $keyword1 = "";
$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];
// }
}
// 搜索商家名称
if (isset($data['keyword']) && $data['keyword'] != '') {
$keyword1 = "shopName like '%" . $data['keyword'] . "%'";
$keyword = "and shopName like '%" . $data['keyword'] . "%'";
}
$count = $model->where('is_delete', 0)
->where($city_where)
->where($s1)
->where($keyword1)
->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 $keyword ORDER BY sort desc 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 getLocalLists($data)
{
// $s = $s1 = $keyword = $keyword1 = "";
$where = [];
if (isset($data['c_name']) && $data['c_name'] != "") {
$where[] = ['address', 'like', '%' . $data['c_name'] . '%'];
// $s1 = "address like '%" . $data['c_name'] . "%'";
// $s = "and address like '%" . $data['c_name'] . "%'";
}
// 搜索商家名称
if (isset($data['keyword']) && $data['keyword'] != '') {
$where[] = ['shopName', 'like', '%' . $data['keyword'] . '%'];
// $keyword1 = "shopName like '%" . $data['keyword'] . "%'";
// $keyword = "and shopName like '%" . $data['keyword'] . "%'";
}
$count = LocalStore::where([
'is_delete' => 0,
'store_state' => 1,
'shopLogo' => 'not null'
])->where($where)->count();
// $size = $data['page_size'];
// $page = ($data['page_no'] - 1) * $data['page_size'];
$lists = LocalStore::where(['is_delete' => 0, 'store_state' => 1, 'shopLogo' => 'not null'])
->where($where)
->order('sort desc')
->page($data['page_no'], $data['page_size'])
->select();
dump($lists);
die;
// $sql = "SELECT * FROM ls_local_store where is_delete = 0 and store_state = 1 and shopLogo is not null $s $keyword ORDER BY sort desc 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'])
];
// 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('经纬度不能为空');
}
if (!$post['phone']) {
throw new \Exception('自提点联系电话不能为空');
}
$post['user_id'] = $user_id;
$result = LocalStorePostStation::insert($post);
if ($result) {
return true;
}
throw new \Exception('更新失败');
}
// 修改自提站点联系电话
public static function editPostStationPhone($post)
{
if (!$post['id']) {
throw new \Exception('参数不能为空');
}
if (!$post['phone']) {
throw new \Exception('自提点联系电话不能为空');
}
LocalStorePostStation::where([
'id' => $post['id'],
])->update(['phone' => $post['phone']]);
}
// 删除自提点
public static function delPostStation($post)
{
if (!$post['id']) {
throw new \Exception('参数不能为空');
}
$result = LocalStorePostStation::where([
'id' => $post['id']
])->update(['is_delete' => 1, 'delete_time' => time()]);
if ($result) {
return true;
}
throw new \Exception('删除失败');
}
public static function searchSn($post)
{
if ($post['keyword']) {
$userInfo = User::where(['sn' => $post['keyword'], 'del' => 0])->field('id, nickname, avatar as abs_avatar')->find();
if ($userInfo) {
$userInfo = $userInfo->toArray();
$userInfo['rule'] = self::$local_menu;
} else {
$userInfo = [];
}
return $userInfo;
}
return false;
}
public static function bindList($post)
{
$list = LocalStoreBind::alias('lsb')
->join('user u', 'lsb.bind_user_id = u.id')
->where(['lstore_id' => $post['lstore_id'], 'is_unbind' => 0])
->field('lsb.*, u.nickname, u.avatar')
->order('id desc')
->page($post['page_no'], $post['page_size'])
->select();
$count = LocalStoreBind::where(['lstore_id' => $post['lstore_id'], 'is_unbind' => 0])->count();
$more = is_more($count, $post['page_no'], $post['page_size']); //是否有下一页
if ($list) {
foreach ($list as &$v) {
$v['avatar'] = UrlServer::getFileUrl($v['avatar']);
}
}
return [
'lists' => $list,
'page_no' => $post['page_no'],
'page_size' => $post['page_size'],
'count' => $count,
'more' => $more,
];
}
public static function bindUser($post)
{
if ($post['user']['id'] <= 0) {
throw new \Exception('缺少用户ID');
}
if (empty($post['user']['rule'])) {
throw new \Exception('请为用户分配权限');
}
$model = new LocalStoreBind();
$in = $model->where(['lstore_id' => $post['lstore_id'], 'bind_user_id' => $post['user']['id'], 'is_unbind' => 0])->find();
if (!empty($in)) {
throw new \Exception('已绑定该用户');
}
$data = array();
$data['lstore_id'] = $post['lstore_id'];
$data['bind_user_id'] = $post['user']['id'];
$data['rule'] = implode(',', $post['user']['rule']);
$data['bind_time'] = time();
$result = $model->insert($data);
if ($result) {
return true;
}
throw new \Exception('添加失败');
}
public static function unbindUser($post)
{
if ($post['id'] <= 0) {
throw new \Exception('缺少ID');
}
$result = LocalStoreBind::where('id', $post['id'])->update([
'is_unbind' => 1,
'unbind_time' => time()
]);
if ($result) {
return true;
}
throw new \Exception('解绑失败');
}
public static function inBindList($user_id, $page, $size)
{
$list = LocalStoreBind::alias('lsb')
->join('local_store ls', 'lsb.lstore_id = ls.lstore_id')
->where(['lsb.bind_user_id' => $user_id, 'lsb.is_unbind' => 0])
->field('lsb.*, ls.shopName, ls.shopLogo')
->page($page, $size)
->select();
$count = LocalStoreBind::where(['bind_user_id' => $user_id, 'is_unbind' => 0])->count();
if ($list) {
foreach ($list as &$v) {
$v['rule'] = self::crate_bind_menu($v['rule']);
}
}
$data = [
'list' => $list,
'page' => $page,
'size' => $size,
'count' => $count,
'more' => is_more($count, $page, $size)
];
return $data;
}
private static function crate_bind_menu($rule)
{
$menu = [];
$r = explode(',', $rule);
sort($r);
foreach ($r as &$v) {
$menu[] = self::$local_menu[$v];
}
return $menu;
}
}