123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252 |
- <?php
- namespace app\models\system;
- use app\admin\model\store\StoreProductAttrValue;
- use app\admin\model\system\SystemStorePoint;
- use app\admin\model\system\SystemStoreProductStock;
- use app\admin\model\system\SystemStoreProductStockLog;
- use app\models\store\StoreOrderCartInfo;
- use app\models\user\User;
- use crmeb\traits\ModelTrait;
- use crmeb\basic\BaseModel;
- use think\db\exception\DataNotFoundException;
- use think\db\exception\DbException;
- use think\db\exception\ModelNotFoundException;
- /**
- * 门店自提 model
- * Class SystemStore
- * @package app\model\system
- */
- class SystemStore extends BaseModel
- {
- const EARTH_RADIUS = 6371;
- use ModelTrait;
- /**
- * 数据表主键
- * @var string
- */
- protected $pk = 'id';
- /**
- * 模型名称
- * @var string
- */
- protected $name = 'system_store';
- public static function getLatlngAttr($value, $data)
- {
- return $data['latitude'] . ',' . $data['longitude'];
- }
- public static function verificWhere()
- {
- return self::where('is_show', 1)->where('is_del', 0);
- }
- /**
- * 获取门店信息
- * @param int $id
- * @param string $felid
- * @return array|mixed|null|string|\think\Model
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public static function getStoreDispose($id = 0, $felid = '')
- {
- if ($id)
- $storeInfo = self::verificWhere()->where('id', $id)->find();
- else
- $storeInfo = self::verificWhere()->find();
- if ($storeInfo) {
- $storeInfo['latlng'] = self::getLatlngAttr(null, $storeInfo);
- $storeInfo['valid_time'] = $storeInfo['valid_time'] ? explode(' - ', $storeInfo['valid_time']) : [];
- $storeInfo['_valid_time'] = str_replace('-', '/', ($storeInfo['valid_time'][0] ?? '') . ' ~ ' . ($storeInfo['valid_time'][1] ?? ""));
- $storeInfo['day_time'] = $storeInfo['day_time'] ? str_replace(' - ', ' ~ ', $storeInfo['day_time']) : [];
- $storeInfo['_detailed_address'] = $storeInfo['address'] . ' ' . $storeInfo['detailed_address'];
- $storeInfo['address'] = $storeInfo['address'] ? explode(',', $storeInfo['address']) : [];
- if ($felid) return $storeInfo[$felid] ?? '';
- }
- return $storeInfo;
- }
- /**
- * 门店列表
- * @return mixed
- */
- // public static function lst()
- // {
- // $model = new self;
- // $model = $model->where('is_show', 1);
- // $model = $model->where('is_del', 0);
- // $model = $model->order('id DESC');
- // return $model->select();
- // }
- /**
- * 计算某个经纬度的周围某段距离的正方形的四个点
- *
- * @param lng float 经度
- * @param lat float 纬度
- * @param distance float 该点所在圆的半径,该圆与此正方形内切,默认值为2.5千米
- * @return array 正方形的四个点的经纬度坐标
- */
- public static function returnSquarePoint($lng, $lat, $distance = 200)
- {
- $dlng = 2 * asin(sin($distance / (2 * self::EARTH_RADIUS)) / cos(deg2rad($lat)));
- $dlng = rad2deg($dlng);
- $dlat = rad2deg($distance / self::EARTH_RADIUS);
- return [
- 'left_top' => [
- 'lat' => $lat + $dlat,
- 'lng' => $lng - $dlng,
- ],
- 'right_top' => [
- 'lat' => $lat + $dlat,
- 'lng' => $lng + $dlng
- ],
- 'left_bottom' => [
- 'lat' => $lat - $dlat,
- 'lng' => $lng - $dlng
- ],
- 'right_bottom' => [
- 'lat' => $lat - $dlat,
- 'lng' => $lng + $dlng
- ]
- ];
- }
- /*
- 设置where条件
- */
- public static function nearbyWhere($model = null, $latitude = 0, $longitude = 0)
- {
- if (!is_object($model)) {
- $latitude = $model;
- $model = new self();
- $longitude = $latitude;
- }
- $field = "(round(6367000 * 2 * asin(sqrt(pow(sin(((latitude * pi()) / 180 - ({$latitude} * pi()) / 180) / 2), 2) + cos(({$latitude} * pi()) / 180) * cos((latitude * pi()) / 180) * pow(sin(((longitude * pi()) / 180 - ({$longitude} * pi()) / 180) / 2), 2))))) AS distance";
- $model->field($field);
- return $model;
- }
- /**
- * 获取排序sql
- * @param $latitude
- * @param $longitude
- * @return mixed
- */
- public static function distanceSql($latitude, $longitude)
- {
- $field = "(round(6367000 * 2 * asin(sqrt(pow(sin(((latitude * pi()) / 180 - ({$latitude} * pi()) / 180) / 2), 2) + cos(({$latitude} * pi()) / 180) * cos((latitude * pi()) / 180) * pow(sin(((longitude * pi()) / 180 - ({$longitude} * pi()) / 180) / 2), 2))))) AS distance";
- return $field;
- }
- /**
- * 门店列表
- * @return mixed
- */
- public static function lst($latitude, $longitude, $page, $limit, $store_user = false)
- {
- $model = new self();
- $model = $model->where('is_del', 0);
- $model = $model->where('is_show', 1);
- $order = '';
- if ($store_user) {
- $order = "store_user desc";
- }
- if ($latitude && $longitude) {
- $model = $model->field(['*', self::distanceSql($latitude, $longitude)]);
- if ($order)
- $order .= ",distance asc";
- else
- $order .= "distance asc";
- }
- $list = $model->order($order)->page((int)$page, (int)$limit)
- ->select()
- ->hidden(['is_show', 'is_del'])
- ->toArray();
- if ($latitude && $longitude) {
- foreach ($list as &$value) {
- //计算距离
- $value['distance'] = sqrt((pow((($latitude - $value['latitude']) * 111000), 2)) + (pow((($longitude - $value['longitude']) * 111000), 2)));
- //转换单位
- $value['range'] = bcdiv($value['distance'], 1000, 1);
- }
- // $distanceKey = array_column($list, 'distance');
- // array_multisort($distanceKey, SORT_ASC, $list);
- }
- return $list;
- }
- /**
- * 门店列表
- * @return mixed
- */
- public static function point_lst($latitude, $longitude, $page, $limit, $store_id)
- {
- $model = new SystemStorePoint();
- $model = $model->where('is_del', 0);
- $model = $model->where('store_id', $store_id);
- $model = $model->where('is_show', 1);
- if ($latitude && $longitude) {
- $model = $model->field(['*', self::distanceSql($latitude, $longitude)])->order('distance asc');
- }
- $list = $model->page((int)$page, (int)$limit)
- ->select()
- ->hidden(['is_show', 'is_del'])
- ->toArray();
- if ($latitude && $longitude) {
- foreach ($list as &$value) {
- //计算距离
- $value['distance'] = sqrt((pow((($latitude - $value['latitude']) * 111000), 2)) + (pow((($longitude - $value['longitude']) * 111000), 2)));
- //转换单位
- $value['range'] = bcdiv($value['distance'], 1000, 1);
- }
- // $distanceKey = array_column($list, 'distance');
- // array_multisort($distanceKey, SORT_ASC, $list);
- }
- return $list;
- }
- /**
- * @param $orderInfo
- * @return bool
- * @throws DataNotFoundException
- * @throws DbException
- * @throws ModelNotFoundException
- */
- public static function decStock($orderInfo)
- {
- $res = true;
- if (!$orderInfo['store_id']) return $res;
- // var_dump($orderInfo);
- $cartId = is_string($orderInfo['cart_id']) ? json_decode($orderInfo['cart_id'], true) : $orderInfo['cart_id'];
- $cartInfo = StoreOrderCartInfo::whereIn('cart_id', (is_string($cartId) || is_int($cartId)) ? [$cartId] : $cartId)->column('cart_info');
- // dump($cartId);
- // exit;
- $userInfo = User::getUserInfo($orderInfo['uid']);
- foreach ($cartInfo as $value) {
- $product = json_decode($value, true);
- // var_dump($product);
- // exit;
- $cartNum = $product['cart_num'] ?? 0;
- if ($product['exchange_id']) $cartNum = 1;
- if (isset($product['productInfo'])) {
- $stock = SystemStoreProductStock::where(['product_id' => $product['product_id'], 'unique' => $product['product_attr_unique'], 'store_id' => $orderInfo['store_id']])->find();
- $res = $res && SystemStoreProductStockLog::expend($orderInfo['store_id'], $product['product_id'], $product['product_attr_unique'], 'sale', $orderInfo['id'], $cartNum, 0, '用户' . $userInfo['nickname'] . '购买订单完成', 1, $stock ? $stock['price'] : 0);
- }
- }
- // var_dump($res);
- return $res ?: self::setErrorInfo(SystemStoreProductStockLog::getErrorInfo());
- }
- }
|