jh-admin/addon/supply/v1/Product.php

315 lines
13 KiB
PHP

<?php
namespace addon\supply\v1;
use addon\supply\model\goods\Goods as GoodsModel;
use addon\supply\model\goods\GoodsWarehousing;
use addon\supply\model\goods\Goods;
use addon\supply\model\CloudApi;
class Product extends ApiBase
{
/***
* 产品列表
* @return array|mixed
*/
public function lists()
{
$cloud_id = input('cloud_id', 'own');
$keyword = input('keyword');
$categoryId = input('categoryId');
$page_index = input('page', 1);
$page_size = input('pageSize', 10);
$priceStart = input('priceStart');
$priceEnd = input('priceEnd');
$info = [];
$goodsModel = new Goods();
if ($cloud_id == 'ag' && $this->AgConfig['is_supply'] == 1) {
$param = [
'pageNum' => $page_index,
'pageSize' => $page_size,
];
if ($keyword) {
$param['keyword'] = $keyword;
}
if ($categoryId) {
$param['categoryId'] = $categoryId;
}
if ($priceStart) {
$param['priceStart'] = $priceStart;
}
if ($priceEnd) {
$param['priceEnd'] = $priceEnd;
}
$info = $this->Choice->productlist($param)['result']['result'] ?? [];
} else if ($cloud_id && $cloud_id == 'own') { //平台自营供应链
$where = [
['goods_state', '=', 1]
];
if ($keyword) {
$where[] = ['goods_name', 'like', '%' . $keyword . '%'];
}
if ($priceStart) {
$where[] = ['min_price', '>=', $priceStart];
}
if ($priceEnd) {
$where[] = ['max_price', '<=', $priceEnd];
}
$field = 'goods_id,goods_name as title,min_price as minPrice,max_price as maxPrice,goods_image as imgUrl,channel_type';
$goodsList = $goodsModel->getGoodsPageList($where, $page_index, $page_size, 'create_time desc', $field);
$info = $goodsList['data'];
foreach ($info['list'] as $k => &$v) {
$v = [
'goods_id' => $v['goods_id'],
'title' => $v['title'],
'minPrice' => $v['minPrice'],
'maxPrice' => $v['maxPrice'],
'imgUrl' => img($v['imgUrl']),
'salesCnt90d' => 0, //计算90天销量
'cloud_type' => 'own', //计算90天销量
'serviceList' => [],
];
}
} else if ($cloud_id && is_numeric($cloud_id)) {//其他第三方云仓库
$cloudAip = new CloudApi($cloud_id);
$data = [
'cloud_id' => $cloud_id,
'page' => $page_index,
'pageSize' => $page_size,
];
// $data['cloud_id'] = 'own';
if ($keyword) {
$data['keyword'] = $keyword;
}
if ($categoryId) {
$data['categoryId'] = $categoryId;
}
if ($priceStart) {
$data['priceStart'] = $priceStart;
}
if ($priceEnd) {
$data['priceEnd'] = $priceEnd;
}
$info = $cloudAip->getGoodsList($data);
}
return $info;
}
/***
* 关注商品与取消关注
* @return mixed
*/
public function follow()
{
$itemId = input('itemId');
$isFollow = input('isFollow', 1);
$cloud_id = input('cloud_id', 'own');
if ($itemId) {
$follow = new GoodsWarehousing($this->SupplyInfo);
$where = [
'appid' => $this->appInfo['app_id'],
'site_id' => $this->SiteInfo['site_id'],
'cloud_id' => $cloud_id,
'open_offer_id' => $itemId,
];
$followInfo = $follow->getInfo($where);
$goodsModel = new Goods();
switch ($cloud_id) {
case 'ag':
$goods_id = model('supply_goods')->getValue([['site_id', '=', $this->SupplyInfo['supplier_site_id']], ['extend_goods_id', '=', $itemId]], 'goods_id');
$product1688Info = $this->Choice->productInfoGet($itemId)['result']['result'][0]['productInfo'];
$product1688Info['goods_id'] = $goods_id;
$GoodsInfo = $follow->Generate1688GoodsInfo($product1688Info, $this->AgConfig['template_id'], 'normal');
if ($isFollow) {
$result = $this->Choice->followGoods($itemId);
if ($result) {
if (empty($goods_id)) {
$goods_id = $goodsModel->addGoods($GoodsInfo)['data'];
} else {
$goods_id = $goodsModel->editGoods($GoodsInfo)['data'];
}
if (empty($followInfo)) {
$this->addGoodsWarehousing($follow, $goods_id, $itemId, $GoodsInfo, $cloud_id, '1688', '1688精选', $this->AgConfig['template_id'], 1);
}
} else {
return error(-1, '关注失败');
}
} else {//取消关注
$follow->deleteGoodsWarehousing($where);
}
break;
case 'own': //自营
$where = [
'goods_id' => $itemId,
];
$GoodsInfo = model('supply_goods')->getInfo($where, 'goods_id,goods_name,goods_image');
if ($isFollow && $GoodsInfo) {
if (empty($followInfo)) {
$this->addGoodsWarehousing($follow, $itemId, $itemId, $GoodsInfo, $cloud_id);
}
} else {
$follow->deleteGoodsWarehousing($where);
}
break;
}
return success(0, 'ok', ['isFollow' => $isFollow, 'cloud_id' => $cloud_id, 'itemId' => $itemId]);
} else {
return error(-1, '参数错误');
}
}
/***
* 添加关注
* @param $follow
* @param $itemId
* @param $GoodsInfo
* @param $channel_type
* @param $third_party_name
* @param $template_id
* @param $is_supply
* @return null
*/
private function addGoodsWarehousing($follow, $goods_id, $itemId, $GoodsInfo, $cloud_id = 'own', $channel_type = 'own', $third_party_name = '平台自营', $template_id = 0, $is_supply = 0)
{
$data = [
'appid' => $this->appInfo['app_id'],
'goods_id' => $goods_id,
'sku_image' => explode(',', $GoodsInfo['goods_image'])[0],
'site_id' => $this->SiteInfo['site_id'],
'ag_site_id' => $this->ag_site_id,
'cloud_id' => $cloud_id,
'open_offer_id' => $itemId,
'template_id' => $template_id,
'is_supply' => $is_supply,
'channel_type' => $channel_type,
'third_party_name' => $third_party_name,
'goods_name' => $GoodsInfo['goods_name'],
'create_time' => time(),
'update_time' => time(),
];
$res = $follow->addGoodsWarehousing($data);
if ($res) {
return success();
} else {
return error();
}
}
/***
* 获取我的关注列表
* @return array
*/
public function followlist()
{
$page_index = input('page', 1);
$page_size = input('pageSize', 20);
$keyword = input('keyword');
$follow = new GoodsWarehousing();
$where = [
['site_id', '=', $this->SiteInfo['site_id']],
['appid', '=', $this->appInfo['app_id']],
];
if ($keyword) {
$where[] = ['goods_name', 'like', "%{$keyword}%"];
}
$field = 'goods_id as itemId,goods_name as title,sku_image as imgUrl,channel_type,open_offer_id,cloud_id';
$result = $follow->getPageList($where, $field, '', $page_index, $page_size);
foreach ($result['list'] as $k => &$v) {
$v['isfollow'] = 1;
if ($v['channel_type'] == '1688' && $v['itemId'] == 0) {
$v['itemId'] = $v['open_offer_id'];
}
}
return $result;
}
public function details()
{
$itemIds = input('itemIds');
$cloud_id = input('cloud_id');
$ids = explode(',', $itemIds);
if (count($ids) > 5) return error(-1, '最多只能获取5个商品');
$data_list = model('supply_goods')->getList(
[
['goods_id', 'in', $ids]
],
'goods_id as itemId,site_name,category_id,goods_class,category_name,goods_name,keywords,introduction,video_url,unit,goods_image,goods_content,is_free_shipping,shipping_template,min_num,market_price,price,min_price,max_price,goods_state,goods_stock,is_virtual,channel_type,goods_spec_format,goods_attr_format',
);
$goods_model = new GoodsModel();
foreach ($data_list as $k => &$v) {
$category_id = explode(',', $v['category_id']);
$v['category_id'] = end($category_id);
$v['goods_image'] = $this->ImagesURL($v['goods_image']);
$goods_sku_list = $goods_model->getGoodsSkuList([['goods_id', '=', $v['itemId']]], "sku_id,goods_class,sku_name,sku_no,sku_spec_format,price_json,min_num,market_price,stock,weight,volume,sku_image,sku_images");
$goods_sku_list = $goods_sku_list['data'];
foreach ($goods_sku_list as &$vv) {
$vv['sku_image'] = $this->ImagesURL($vv['sku_image']);
$vv['sku_images'] = $this->ImagesURL($vv['sku_images']);
// $vv['price_json'] = json_decode($vv['price_json'], true);
// $vv['sku_spec_format'] = json_decode($vv['sku_spec_format'], true);
// if ($vv['extend_sku_id'] == '') {
// $vv['extend_sku_id'] = $vv['sku_id'];
// }
}
$v['cloud_type'] = 'own';
$v['sku_list'] = $goods_sku_list;
unset($v['channel_type']);
ksort($v);
}
// if ($this->AgConfig['is_supply'] == 1) {
// $item_arr = [];
// if ($data_list) {
// $item_arr = array_column($data_list, 'itemId');
// }
// $temp_arr = array_diff($ids, $item_arr);
// var_dump($ids, $item_arr);die;
// if ($temp_arr && $cloud_id == 'ag') {
// $follow = new GoodsWarehousing($this->SupplyInfo);
// $info = $this->Choice->productInfoGet(array_values($temp_arr));
// if (!isset($info['error_code']) && $info['result']['result']) {
// $result = $info['result']['result'];
// foreach ($result as $key => $val) {
// $val['goods_id'] = 0;
// $GoodsInfo = $follow->Generate1688GoodsInfo($val['productInfo'], $this->AgConfig['template_id'], 'normal');
// $GoodsInfo['itemId'] = $GoodsInfo['extend_goods_id'];
// $GoodsInfo['cloud_id'] = 'ag';
// $GoodsInfo['goods_spec_format'] = json_decode($GoodsInfo['goods_spec_format'], true);
// $GoodsInfo['goods_attr_format'] = json_decode($GoodsInfo['goods_attr_format'], true);
// $goods_sku_data = json_decode($GoodsInfo['goods_sku_data'], true);
// $category_id = array_unique(explode(',', $GoodsInfo['category_id']));
// $GoodsInfo['category_id'] = end($category_id);
// foreach ($goods_sku_data as $k1 => $v1) {
// $goods_sku_data[$k1]['price_json'] = json_decode($v1['price_json'], true);
// unset($goods_sku_data[$k1]['cost_price'], $goods_sku_data[$k1]['goods_supplier_format']);
// unset($goods_sku_data[$k1]['site_id'], $goods_sku_data[$k1]['goods_id'], $goods_sku_data[$k1]['sku_id']);
// }
// $GoodsInfo['sku_list'] = $goods_sku_data;
// unset($GoodsInfo['goods_id'], $GoodsInfo['extend_goods_id'], $GoodsInfo['cost_price'], $GoodsInfo['channel_type'], $GoodsInfo['channel_type']);
// unset($GoodsInfo['goods_sku_data'], $GoodsInfo['website_id'], $GoodsInfo['category_array'], $GoodsInfo['category_json']);
// unset($GoodsInfo['goods_stock_alarm'], $GoodsInfo['site_id'], $GoodsInfo['verify_state']);
// ksort($GoodsInfo);
// $data_list[] = $GoodsInfo;
// }
// }
// }
// }
if (empty($data_list)) {
return error(-1, '商品不存在');
} else {
return $data_list;
}
}
public function ImagesURL($images)
{
$images = explode(',', $images);
$data_list = [];
foreach ($images as $k => $v) {
$data_list[] = img($v);
}
return implode(',', $data_list);
}
}