123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187 |
- <?php
- namespace Shop\Lib;
- use Dever;
- class Info
- {
- # 获取店铺基本信息
- public function getOne($shop_id, $lng, $lat)
- {
- $shop = $this->fetch($shop_id, false, $lng, $lat);
- return $shop;
- }
- # 获取店铺基本信息
- public function get($city, $lng, $lat, $name = '', $method = 'fetch', $shop_id = false)
- {
- if (!$city) {
- Dever::alert('请传入城市');
- }
- if (!$lng || !$lat) {
- Dever::alert('请传入用户坐标');
- }
- $data = $this->fetch(false, $city, $lng, $lat, 1, $name, $method);
- # 验证本城市内有没有店
- if (!$data) {
- $data = $this->fetch(false, $city, $lng, $lat, 2, $name, $method);
- }
- if ($data) {
- if ($method == 'fetch') {
- $data = $this->getInfo($data);
- } else {
- foreach ($data as $k => $v) {
- $data[$k] = $this->getInfo($data[$k]);
- if ($shop_id == $v['id']) {
- $data[$k]['cur'] = 1;
- } else {
- $data[$k]['cur'] = 2;
- }
- }
- }
- }
- return $data;
- }
- # 获取店铺的商品列表
- public function getGoods($shop, $column = false)
- {
- $table = 'shop/goods';
- $where['shop_id'] = isset($shop['id']) ? $shop['id'] : $shop;
- if ($column) {
- $where['column'] = $column;
- $method = 'getData';
- } else {
- $method = 'getDataPage';
- }
-
- $name = Dever::input('name');
- if ($name) {
- $where['name'] = $name;
- }
-
- $data = Dever::db($table)->$method($where);
- if ($data) {
- foreach ($data as $k => $v) {
- $data[$k] = $this->getGoodsInfo($where['shop_id'], $v);
- }
- }
- return $data;
- }
- # 获取详细信息
- private function getInfo($data)
- {
- if ($data['worktime']) {
- $time = date('Hi');
- $worktime = str_replace(':', '', $data['worktime']);
- $temp = explode('~', $worktime);
- if ($time < $temp[0] || $time > $temp[1]) {
- $data['open'] = 2;
- }
- }
- return $data;
- }
- # 获取距离
- private function fetch($id, $city, $lng, $lat, $type = 1, $name = '', $method = 'fetch')
- {
- $page = array();
- if ($method == 'fetchAll') {
- $page['template'] = 'list';
- $page['num'] = 10;
- } else {
- $page = false;
- }
- $where = 'type = '.$type.' and status = 1 and state = 1';
- $county = Dever::db('area/county')->find($city);
- if ($county) {
- $city = $county['id'];
- }
- if ($type == 1 && $city) {
- $where .= ' and city = ' . $city;
- }
- if ($name) {
- $where .= ' and name like("%'.$name.'%")';
- }
- if ($id) {
- $where .= ' and id = ' . $id;
- }
- $sql = 'select id,name,`desc`,truename,mobile,lng,lat,address,open,worktime,method,gotime,pdesc,round((st_distance(point(lng, lat), point('.$lng.', '.$lat.'))*111195)/1000, 2) as distance from {table} where '.$where.' order by distance asc';
- $data = Dever::db('shop/info')->$method($sql, array(), $page);
- return $data;
- }
- # 获取库存
- public function getGoodsInfo($shop_id, $info, $sku_id = false, $attr = true)
- {
- $where['shop_id'] = $shop_id;
- $where['goods_id'] = isset($info['goods_id']) ? $info['goods_id'] : $info;
- $other = Dever::db('shop/goods_sku')->getData($where);
- $data = Dever::load('goods/lib/info')->getInfo($info, $attr, array($other, array('total')));
- if($data) {
- $sku_id = $sku_id ? $sku_id : $data['sku_id'];
- $data['total'] = 0;
- if ($data['price_type'] == 4) {
- if (isset($data['goods']) && is_array($data['goods'])) {
- foreach ($data['goods'] as $k => $v) {
- $data['goods'][$k]['total'] = $this->getTotal($other, -1);
- if ($data['total'] > $data['goods'][$k]['total']) {
- $data['total'] = $data['goods'][$k]['total'];
- }
- }
- }
- } else {
- $data['total'] = $this->getTotal($other, $sku_id);
- }
- }
- return $data;
- }
- # 验证库存
- public function checkTotal(&$num, $goods_id, $shop_id, $sku_id, $state = 1)
- {
- $info = $this->getGoodsInfo($shop_id, $goods_id, $sku_id, false);
- if (!$info) {
- Dever::alert('商品不存在');
- }
- $total = $info['total'];
- if ($num > $total) {
- if ($state == 2) {
- Dever::alert('库存不足');
- }
- $num = $total;
- }
- return $total;
- }
- # 获取库存
- public function getTotal($other, $sku_id)
- {
- if (isset($other[$sku_id])) {
- return $other[$sku_id]['total'];
- } else {
- return 0;
- }
- }
- }
|