364 行
13 KiB
PHP
可执行文件
364 行
13 KiB
PHP
可执行文件
<?php
|
|
|
|
namespace CashierApi\Controller;
|
|
|
|
use Common\Business\GoodsClassify;
|
|
use Common\Model\RetailGoodsModel;
|
|
use Common\Model\RetailManagerModel;
|
|
use Common\Model\RetailSkuGoodsModel;
|
|
use Common\Model\RetailStoreModel;
|
|
use Common\Model\SSaleMGoodsModel;
|
|
use Common\Model\StoreModel;
|
|
use LittleGift\Business\Goods;
|
|
use Think\Controller;
|
|
|
|
//[1.8.72]
|
|
|
|
/**
|
|
* 商品接口
|
|
* Class RetailGoodsController
|
|
* [
|
|
* inveGoodsList 库存查询
|
|
* getGoodsClass 获取商品分类列表
|
|
* otherStoreGoodsDetails 获取其他门店的商品库存详情
|
|
* ]
|
|
* @package CashierApi\Controller
|
|
*/
|
|
class RetailGoodsController extends BaseController
|
|
{
|
|
|
|
/**
|
|
* 库存查询
|
|
* @param search_type 搜索类型 1商品名称 2货号 3条码
|
|
* @param search_text 搜索内容
|
|
* @param order_type 排序方式 1库存升序 2库存倒序
|
|
* @param goods_type 商品状态 1上架 2下架
|
|
* @param first_class 一级分类
|
|
* @param second_class 二级分类
|
|
* @param third_class 三级分类
|
|
* @param page 页码
|
|
* @param limit 页长
|
|
*/
|
|
public function inveGoodsList()
|
|
{
|
|
$SSaleMGoodsModel = new SSaleMGoodsModel();
|
|
$store_id = self::$store_id;
|
|
$merchant_id = self::$merchant_id;
|
|
|
|
$param = I('post.');
|
|
|
|
$page = $param['page'] ? $param['page'] : 1;
|
|
$limit = $param['limit'] ? $param['limit'] : 10;
|
|
$goods_type = $param['goods_type'];
|
|
$search_type = $param['search_type'];
|
|
$search_text = $param['search_text'];
|
|
$order_type = $param['order_type'];
|
|
|
|
$where['merchant_id'] = $merchant_id;
|
|
$where['store_id'] = $store_id;
|
|
|
|
// 内容模糊搜索
|
|
if ($search_text) {
|
|
switch ($search_type) {
|
|
case 1:
|
|
// 商品名称
|
|
$where['goods_name'] = ['like', "%" . $search_text . "%"];
|
|
break;
|
|
case 2:
|
|
// 货号
|
|
$where['goods_number|sku_goods_number'] = ['like', "%" . $search_text . "%"];
|
|
break;
|
|
case 3:
|
|
// 条码
|
|
$where['goods_code|sku_code'] = ['like', "%" . $search_text . "%"];
|
|
break;
|
|
default:
|
|
$where['goods_number|goods_code|goods_name'] = ['like', "%" . $search_text . "%"];
|
|
break;
|
|
}
|
|
}
|
|
// 处理分类
|
|
$goods_class_id = getGoodsClass($param['first_class'], $param['second_class'], $param['third_class']);
|
|
if ($goods_class_id) {
|
|
$where['goods_class_id'] = $goods_class_id;
|
|
}
|
|
// 上下架
|
|
switch ($goods_type) {
|
|
case 1:
|
|
$where['status'] = 0;
|
|
break;
|
|
case 2:
|
|
$where['status'] = 1;
|
|
break;
|
|
default:
|
|
$where['status'] = ['in', [0, 1]];
|
|
break;
|
|
}
|
|
// 排序方式
|
|
switch ($order_type) {
|
|
case 1:
|
|
$order = 'goods_num asc';
|
|
break;
|
|
case 2:
|
|
$order = 'goods_num desc';
|
|
break;
|
|
default:
|
|
$order = 'add_time desc';
|
|
break;
|
|
}
|
|
|
|
|
|
$field = [
|
|
'id',
|
|
'goods_id',
|
|
'sku_id',
|
|
'goods_name',
|
|
'goods_number',
|
|
'goods_code',
|
|
'goods_num',
|
|
'specifications',
|
|
'attribute_state',
|
|
'sku_goods_number',
|
|
'sku_code',
|
|
'sku_name',
|
|
'status',
|
|
];
|
|
if (self::$merchant['platform_type'] == 2) {
|
|
//第三方平台
|
|
$field[] = 'third_goods_code';
|
|
}
|
|
$goodsList = $SSaleMGoodsModel->getSelect($where, $page, $limit, $field, $order);
|
|
|
|
if ($goodsList) {
|
|
$StoreModel = new RetailStoreModel();
|
|
$sMap = [
|
|
'merchantid' => self::$merchant_id,
|
|
'id' => ['neq', self::$store_id],
|
|
'state' => 0,
|
|
'store_status' => 0,
|
|
];
|
|
$store_ids = $StoreModel->getFindField($sMap, 'id', true);
|
|
if (self::$merchant['platform_type'] == 2) {
|
|
//第三方平台
|
|
$code_arr = [];
|
|
}
|
|
$SkuGoodsModel = new RetailSkuGoodsModel();
|
|
$GoodsModel = new RetailGoodsModel();
|
|
foreach ($goodsList as &$val) {
|
|
if (!empty($val['sku_id'])) {
|
|
$val['retail_price'] = $SkuGoodsModel->getFindField(['id' => $val['sku_id']], 'retail_price');
|
|
} else {
|
|
$val['retail_price'] = $GoodsModel->getFindField(['id' => $val['goods_id']], 'retail_price');
|
|
}
|
|
if (self::$merchant['platform_type'] == 2) {
|
|
$code_arr[$val['id']] = $val['third_goods_code'];
|
|
}
|
|
$val['goods_num'] = round($val['goods_num'], 3);
|
|
if ($val['attribute_state'] == 2) {
|
|
$val['goods_number'] = $val['sku_goods_number'];
|
|
$val['goods_code'] = $val['sku_code'];
|
|
$val['specifications'] = $val['sku_name'];
|
|
}
|
|
switch ($val['status']) {
|
|
case 0:
|
|
$val['status_name'] = "上架";
|
|
break;
|
|
case 1:
|
|
$val['status_name'] = "下架";
|
|
break;
|
|
}
|
|
if (self::$cashier['other_store_goods'] == 1 && !empty($store_ids) && self::$merchant['platform_type'] == 1) {
|
|
//有权限查看其他门店库存
|
|
$gMap = [
|
|
'id' => ['neq', $val['id']],
|
|
'merchant_id' => self::$merchant_id,
|
|
'store_id' => ['in', $store_ids],
|
|
'status' => 0,
|
|
];
|
|
if ($val['attribute_state'] == 1) {
|
|
//货号判断
|
|
$gMap['goods_number'] = $val['goods_number'];
|
|
//条码判断
|
|
$gMap[] = [
|
|
['goods_code' => $val['goods_code']],
|
|
['goods_code' => ''],
|
|
['goods_code' => ['EXP', 'IS NULL']],
|
|
'_logic' => 'OR',
|
|
];
|
|
} else {
|
|
//货号判断
|
|
$gMap['sku_goods_number'] = $val['sku_goods_number'];
|
|
//条码判断
|
|
$gMap[] = [
|
|
['sku_code' => $val['sku_code']],
|
|
['sku_code' => ''],
|
|
['sku_code' => ['EXP', 'IS NULL']],
|
|
'_logic' => 'OR',
|
|
];
|
|
}
|
|
$val['other_store_goods_num'] = $SSaleMGoodsModel->getSum($gMap, 'goods_num');
|
|
$val['other_store_goods_num'] = round($val['other_store_goods_num'], 3);
|
|
} else {
|
|
$val['other_store_goods_num'] = 0;
|
|
}
|
|
}
|
|
if (self::$merchant['platform_type'] == 2 && !empty($code_arr)) {
|
|
$ThirdGoodsClass = new Goods();
|
|
$thirdResult = $ThirdGoodsClass->goodsNumQuery([
|
|
'merchant_id' => self::$merchant_id,
|
|
'type' => 2,
|
|
'code_arr' => $code_arr,
|
|
'Customer_Codes' => self::$store['third_code']
|
|
]);
|
|
$goods_stock = $thirdResult['data'];
|
|
foreach ($goodsList as &$val) {
|
|
$val['goods_num'] = $goods_stock[$val['id']] ? $goods_stock[$val['id']] : 0;
|
|
}
|
|
}
|
|
}
|
|
$result = [
|
|
'status' => 1,
|
|
'msg' => $goodsList ? '获取成功' : '暂无数据',
|
|
'count' => $SSaleMGoodsModel->getCount($where),
|
|
'data' => $goodsList ? $goodsList : [],
|
|
];
|
|
$this->ajaxReturn($result);
|
|
}
|
|
|
|
/**
|
|
* 获取商品分类列表
|
|
* @param pid 上级分类id
|
|
*/
|
|
public function getGoodsClass()
|
|
{
|
|
$ClassApi = new GoodsClassify();
|
|
$param = [
|
|
'pid' => I('post.pid'),
|
|
'merchant_id' => self::$merchant_id,
|
|
];
|
|
$data = $ClassApi->getAbsolutelyGoodsClass($param);
|
|
$this->ajaxReturn([
|
|
'status' => 1,
|
|
'msg' => $data ? '获取成功' : '暂无分类',
|
|
'data' => $data ? $data : [],
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* 获取其他门店的商品库存详情
|
|
*/
|
|
public function otherStoreGoodsDetails()
|
|
{
|
|
if (self::$cashier['other_store_goods'] != 1) {
|
|
$this->ajaxReturn(callBack(statusCode()['error'], '您无权查看其他门店商品库存'));
|
|
}
|
|
$id = I('post.id');
|
|
if (empty($id)) {
|
|
$this->ajaxReturn(callBack(statusCode()['error'], '缺少参数'));
|
|
}
|
|
$order_type = I('post.order_type');//排序方式 1库存降序 2距离升序
|
|
//判断商品是否存在
|
|
$SSaleMGoodsModel = new SSaleMGoodsModel();
|
|
$gMap = [
|
|
'id' => $id,
|
|
'status' => ['in', '0,1'],
|
|
'merchant_id' => self::$merchant_id,
|
|
'store_id' => self::$store_id,
|
|
];
|
|
$goods = $SSaleMGoodsModel->getFind($gMap, 'attribute_state,goods_number,goods_code,sku_goods_number,sku_code');
|
|
if (empty($goods)) {
|
|
$this->ajaxReturn(callBack(statusCode()['error'], '商品不存在'));
|
|
}
|
|
|
|
//获取门店ID列表
|
|
$map = [
|
|
'id' => ['neq', $id],
|
|
'status' => 0,
|
|
'merchant_id' => self::$merchant_id,
|
|
'goods_num' => ['gt', 0],
|
|
];
|
|
//非规格商品
|
|
if ($goods['attribute_state'] == 1) {
|
|
//货号判断
|
|
$map['goods_number'] = $goods['goods_number'];
|
|
//条码判断
|
|
$map[] = [
|
|
['goods_code' => $goods['goods_code']],
|
|
['goods_code' => ''],
|
|
['goods_code' => ['EXP', 'IS NULL']],
|
|
'_logic' => 'OR',
|
|
];
|
|
} else {
|
|
//货号判断
|
|
$map['sku_goods_number'] = $goods['sku_goods_number'];
|
|
//条码判断
|
|
$map[] = [
|
|
['sku_code' => $goods['sku_code']],
|
|
['sku_code' => ''],
|
|
['sku_code' => ['EXP', 'IS NULL']],
|
|
'_logic' => 'OR',
|
|
];
|
|
}
|
|
|
|
$store_ids = $SSaleMGoodsModel->getFindField($map, 'store_id', true);
|
|
if (empty($store_ids)) {
|
|
$this->ajaxReturn(callBack(statusCode()['error'], '暂无门店存在该商品'));
|
|
}
|
|
|
|
//查询门店及对应库存
|
|
$StoreModel = new RetailStoreModel();
|
|
$sMap = [
|
|
'merchantid' => self::$merchant_id,
|
|
'id' => ['in', $store_ids],
|
|
'state' => 0,
|
|
'store_status' => 0,
|
|
];
|
|
$fields = [
|
|
'id',
|
|
'storetag as store_name',
|
|
'province',
|
|
'city',
|
|
'area',
|
|
'address',
|
|
'latitude',
|
|
'longitude',
|
|
];
|
|
$storeList = $StoreModel->getSelect($sMap, $fields);
|
|
if (empty($storeList)) {
|
|
$this->ajaxReturn(callBack(statusCode()['error'], '暂无门店存在该商品'));
|
|
}
|
|
if (self::$store['longitude'] === null || self::$store['longitude'] === '') self::$store['longitude'] = 0;
|
|
if (self::$store['latitude'] === null || self::$store['latitude'] === '') self::$store['latitude'] = 0;
|
|
$ManagerModel = new RetailManagerModel();
|
|
foreach ($storeList as &$val) {
|
|
if ($val['longitude'] === null || $val['longitude'] === '') $val['longitude'] = self::$store['longitude'];
|
|
if ($val['latitude'] === null || $val['latitude'] === '') $val['latitude'] = self::$store['longitude'];
|
|
$val['distance'] = get_distance(self::$store['longitude'], self::$store['latitude'], $val['longitude'], $val['latitude']); //距离
|
|
$map['store_id'] = $val['id'];
|
|
$val['address'] = $val['province'] . $val['city'] . $val['area'] . $val['address'];
|
|
unset($val['province']);
|
|
unset($val['city']);
|
|
unset($val['area']);
|
|
unset($val['longitude']);
|
|
unset($val['latitude']);
|
|
$val['other_store_goods_num'] = $SSaleMGoodsModel->getSum($map, 'goods_num');
|
|
$val['other_store_goods_num'] = round($val['other_store_goods_num'], 3);
|
|
|
|
$mMap = [
|
|
'storeid' => $val['id'],
|
|
'status' => 1,
|
|
];
|
|
$val['managers'] = $ManagerModel->getSelect($mMap, 'name,mobile');
|
|
$val['managers'] = $val['managers'] ? $val['managers'] : [];
|
|
}
|
|
if (count($storeList) > 1) {
|
|
if ($order_type == 2) {
|
|
$storeList = array_sequence($storeList, 'distance');
|
|
} else {
|
|
$storeList = array_sequence($storeList, 'other_store_goods_num', 'SORT_DESC');
|
|
}
|
|
}
|
|
$this->ajaxReturn(callBack(statusCode()['success'], '获取成功', $storeList));
|
|
}
|
|
}
|