739 lines
32 KiB
PHP
739 lines
32 KiB
PHP
<?php
|
||
|
||
namespace addon\ali1688\model;
|
||
|
||
use app\model\BaseModel;
|
||
use app\model\goods\Goods as GoodsModel;
|
||
use app\model\NewBaseModel;
|
||
use app\model\system\Cron;
|
||
use app\model\upload\Album;
|
||
use think\Exception;
|
||
|
||
/**
|
||
* 1688供应链商品相关处理
|
||
* Class Goods
|
||
* @package addon\1688\model
|
||
*/
|
||
class Goods extends BaseModel
|
||
{
|
||
|
||
protected $site_id = 0;
|
||
|
||
protected $goods_spec_format = '';// 商品多规格 必须,否则多规格无效
|
||
protected $open_offer_id = 0;// 供应链商品ID
|
||
|
||
public function __construct($siteId)
|
||
{
|
||
$this->site_id = $siteId;
|
||
}
|
||
|
||
/**
|
||
* 取已入库的商品列表
|
||
* @param array $search
|
||
* @return mixed
|
||
*/
|
||
public function getGoodsList($condition, $page, $page_size, $field = 'w.*,g.goods_image,g.brand_name,g.market_price,g.price,g.cost_price,g.goods_stock,g.site_name', $order = 'w.create_time desc')
|
||
{
|
||
$join = [
|
||
['goods g', 'g.goods_id=w.goods_id', 'left']
|
||
];
|
||
$result = model('supply_goods_warehousing')->pageList($condition, $field, $order, $page, $page_size, 'w', $join);
|
||
return $this->success($result);
|
||
}
|
||
|
||
|
||
/***
|
||
* 添加产品
|
||
* @param $data
|
||
* @param $site_id
|
||
* @return array
|
||
*/
|
||
public function addGoods($goodsData, $site_id, $productID, $template_id = 0)
|
||
{
|
||
$follow = (new Choice($this->site_id))->followGoods($productID);
|
||
if ($follow == true) {
|
||
model('goods')->startTrans();
|
||
try {
|
||
$this->site_id = $site_id;
|
||
// 判断:当前商品是否已经入库
|
||
$isHas = (int)model("supply_goods_warehousing")->getValue([['open_offer_id', '=', $productID], ['site_id', '=', $this->site_id]], 'id');
|
||
if ($isHas) throw new Exception('当前商品已经存在,您可以直接编辑商品或者删除重新添加');
|
||
$goodsData['site_id'] = $site_id;
|
||
$goods_image = explode(',', $goodsData['goods_image']);
|
||
$goods_sku_data = json_decode($goodsData['goods_sku_data'], true);
|
||
$sku_image = array_column($goods_sku_data, 'sku_image');
|
||
$goods_images = array_unique(array_merge($goods_image, $sku_image));
|
||
$this->GooodsImgCheck($site_id, $goods_images);//验证图片上传
|
||
$res = (new GoodsModel())->addGoods($goodsData);
|
||
if ($res['data']) {
|
||
model('supply_goods_warehousing')->add([
|
||
'site_id' => $this->site_id,
|
||
'goods_id' => $res['data'],
|
||
'open_offer_id' => $productID,
|
||
'channel_type' => '1688',
|
||
'third_party_name' => '1688精选',
|
||
'template_id' => $template_id,
|
||
'goods_name' => $goodsData['goods_name'],
|
||
'create_time' => time(),
|
||
'update_time' => time(),
|
||
]);
|
||
model('goods')->commit();
|
||
return $this->success('入库成功');
|
||
}
|
||
throw new \Exception($res['message']);
|
||
} catch (\Exception $e) {
|
||
model('goods')->rollback();
|
||
return $this->error('', $e->getMessage());
|
||
}
|
||
} else {
|
||
return $this->error($follow);
|
||
}
|
||
}
|
||
|
||
|
||
/****
|
||
* 检查图片同步
|
||
* @param $goods_images
|
||
* @return void
|
||
*/
|
||
public function GooodsImgCheck($site_id, $goods_images)
|
||
{
|
||
$thumb_value = model('album_pic')->getColumn([['pic_path', 'in', $goods_images]], 'pic_path', 'pic_path');
|
||
$goods_images = array_diff($goods_images, $thumb_value);
|
||
if ($goods_images) {
|
||
$album_id = model('album')->getValue([['site_id', '=', $site_id], ['type', '=', 'img']], 'album_id');
|
||
$album_model = new Album();
|
||
$cron = new Cron();
|
||
foreach ($goods_images as $k => $v) {
|
||
$data = [
|
||
"pic_path" => $v,//图片云存储
|
||
"pic_name" => md5($v),
|
||
"pic_spec" => 'x',
|
||
"update_time" => time(),
|
||
"site_id" => $site_id,
|
||
"size" => 0,
|
||
"album_id" => $album_id,
|
||
"is_thumb" => 0,
|
||
];
|
||
$res = $album_model->addAlbumPic($data);
|
||
if ($res['code'] >= 0) {
|
||
$data["id"] = $res["data"];
|
||
$cron->addCron(1, 1, '图片同步上传', 'AlbumUpload', time(), $res["data"]);
|
||
}
|
||
}
|
||
}
|
||
return $this->success('成功');
|
||
}
|
||
|
||
/***
|
||
* 产品下架
|
||
* @param $goodsData
|
||
* @param $site_id
|
||
* @param $productID
|
||
* @return void
|
||
*/
|
||
public function GooodsDelisting($site_id, $productID, $isDel = 0)
|
||
{
|
||
$where = [
|
||
['site_id', '=', $site_id],
|
||
['open_offer_id', '=', $productID]
|
||
];
|
||
$goods_ids = model('supply_goods_warehousing')->getColumn($where, 'goods_id');
|
||
if ($goods_ids) {
|
||
$goods_model = new GoodsModel();
|
||
$data = [
|
||
'goods_state' => 0
|
||
];
|
||
if ($isDel) {
|
||
$data['is_delete'] = 1;
|
||
$goods_model->modifyIsDelete($goods_ids, 1, $site_id);
|
||
} else {
|
||
$goods_model->modifyGoodsState($goods_ids, 0, $site_id);
|
||
}
|
||
model('supply_goods_warehousing')->update($data, $where);
|
||
}
|
||
return $this->success('下架成功');
|
||
}
|
||
|
||
/**
|
||
* @param $info
|
||
* @param array $goodsDetail 供应链商品信息,不存在则通过api接口获取
|
||
* @return array
|
||
*/
|
||
public function refreshGoodsInfo($info, $goodsDetail = [])
|
||
{
|
||
model('goods')->startTrans();
|
||
try {
|
||
$this->site_id = $info['site_id'] ?? $this->site_id;
|
||
$this->open_offer_id = $info['offer_id'] ?? 0;
|
||
// 判断:当前商品是否已经入库
|
||
$goodsId = (int)model("supply_goods_warehousing")->getValue([['open_offer_id', '=', $this->open_offer_id], ['site_id', '=', $this->site_id]], 'goods_id');
|
||
if ($goodsId <= 0) throw new Exception('商品不存在!');
|
||
// 获取入库商品详情
|
||
if (!$goodsDetail) $goodsDetail = (new Choice($this->site_id))->getGoodsDetail($this->open_offer_id);
|
||
$goodsData = $this->generateGoodsInfo($goodsDetail);
|
||
$goodsData['goods_id'] = $goodsId;
|
||
unset($goodsDetail['create_time']);
|
||
(new GoodsModel())->editGoods($goodsData);
|
||
// 添加入库记录
|
||
model('supply_goods_warehousing')->update([
|
||
'update_time' => time(),
|
||
], [
|
||
['goods_id', '=', $goodsId],
|
||
['site_id', '=', $this->site_id]
|
||
]);
|
||
model('goods')->commit();
|
||
return $this->success('刷新成功');
|
||
} catch (\Exception $e) {
|
||
model('goods')->rollback();
|
||
return $this->error('', $e->getMessage());
|
||
}
|
||
}
|
||
|
||
/**
|
||
*商品入库 - 生成本平台商品信息
|
||
* @param $goodsDetail
|
||
* @return array
|
||
*/
|
||
public function generateGoodsInfo($goodsDetail, $template_id = 0, $saleType = 'normal')
|
||
{
|
||
$time = time();
|
||
$sku = [];
|
||
if (isset($goodsDetail['skuInfos'])) {
|
||
$sku = $goodsDetail['skuInfos'];// 商品规格列表
|
||
}
|
||
$fileList = $goodsDetail['image']['images'] ?? [];// 商品主图列表
|
||
$goods_info = model('supply_goods_warehousing')->getInfo(['open_offer_id' => $goodsDetail['productID']], 'w.goods_id,g.goods_name,g.brand_id,g.price,g.market_price,g.goods_name,g.price,g.goods_image,g.category_id,g.category_json', 'w', [['goods g', 'g.goods_id=w.goods_id', 'left']]);
|
||
$goods_category = [];
|
||
$category_id = '';
|
||
$categoryjson = '';
|
||
$brandId = '';
|
||
$goods_id = '';
|
||
$goods_name = $goodsDetail['subject'];
|
||
if ($goods_info) {
|
||
$categoryjson = $goods_info['category_json'];
|
||
$category_json = json_decode($categoryjson);
|
||
$category_id = $goods_info['category_id'];
|
||
$brandId = $goods_info['category_id'];
|
||
$goods_id = $goods_info['goods_id'];
|
||
$goods_name = $goods_info['goods_name'];
|
||
foreach ($category_json as $k => $v) {
|
||
if (!empty($v)) {
|
||
$category_list = model('goods_category')->getList([['category_id', 'in', $v]], 'category_name', 'level asc');
|
||
$category_name = array_column($category_list, 'category_name');
|
||
$category_name = implode('/', $category_name);
|
||
$goods_category[$k] = [
|
||
'id' => $v,
|
||
'category_name' => $category_name
|
||
];
|
||
}
|
||
}
|
||
$goods_category = $goods_category;
|
||
}
|
||
$price_template = [
|
||
'price_set' => [
|
||
'multiple' => 35,
|
||
'increase' => 0,
|
||
],
|
||
'title_set' => [],
|
||
'sku_set' => [],
|
||
];
|
||
if ($template_id) {
|
||
$template = model('supply_price_template')->getInfo(['id' => $template_id]);
|
||
if ($template) {
|
||
$price_template['price_set'] = json_decode($template['price_set'], true);
|
||
$price_template['title_set'] = json_decode($template['title_set'], true);
|
||
$price_template['sku_set'] = json_decode($template['sku_set'], true);
|
||
}
|
||
} else {
|
||
$template = model('supply_price_template')->getInfo(['site_id' => $this->site_id, 'is_default' => 1]);
|
||
if ($template) {
|
||
$price_template['price_set'] = json_decode($template['price_set'], true);
|
||
$price_template['title_set'] = json_decode($template['title_set'], true);
|
||
$price_template['sku_set'] = json_decode($template['sku_set'], true);
|
||
}
|
||
}
|
||
// 数据处理 - 库存
|
||
$saleInfo = $goodsDetail['saleInfo'];
|
||
$stock = $saleInfo['amountOnSale'];
|
||
$img_url = 'https://cbu01.alicdn.com/';
|
||
// 数据处理 - 主图列表
|
||
$goodsImage = implode(',', array_map(function ($data) use ($img_url) {
|
||
return $img_url . $data;
|
||
}, $fileList));
|
||
$default_img = explode(',', $goodsImage)[0];
|
||
$attributes = array_column($sku, 'attributes');
|
||
$_spec = [];
|
||
foreach ($attributes as $key => $val) {
|
||
foreach ($val as $k => $v) {
|
||
$attributeID = $v['attributeID'];
|
||
$skuImageUrl = $v['skuImageUrl'] ?? '';
|
||
$spec_value_name = $v['attributeValue'];
|
||
if (!isset($_spec[$attributeID])) {
|
||
$_spec[$attributeID] = [
|
||
'spec_id' => $attributeID,
|
||
'spec_name' => $v['attributeName'],
|
||
'value' => [$spec_value_name => [
|
||
'spec_id' => $attributeID,
|
||
'spec_name' => $v['attributeName'],
|
||
'spec_value_name' => $v['attributeValue'],
|
||
'spec_value_id' => $attributeID + $key,
|
||
'image' => $skuImageUrl ? $img_url . $skuImageUrl : ''
|
||
]]
|
||
];
|
||
} else {
|
||
$_spec[$attributeID]['value'][$spec_value_name] = [
|
||
'spec_id' => $attributeID,
|
||
'spec_name' => $v['attributeName'],
|
||
'spec_value_name' => $v['attributeValue'],
|
||
'spec_value_id' => $attributeID + $key,
|
||
'image' => $skuImageUrl ? $img_url . $skuImageUrl : ''
|
||
];
|
||
}
|
||
}
|
||
}
|
||
if (count($sku) > 0) {//多规格
|
||
$skuList = $this->generateGoodsSkuInfo($sku, $goods_id, $saleInfo, $price_template, $default_img, $img_url, $_spec, $goodsDetail['productID'], $saleType);
|
||
$one_sku = $sku[0];
|
||
if ($saleType == 'normal') {
|
||
$minOrderQuantity = 1;
|
||
$consignPrice = $one_sku['retailPrice'];
|
||
$is_free_shipping = 1;
|
||
} else {
|
||
$minOrderQuantity = $saleInfo['minOrderQuantity'];
|
||
$consignPrice = $one_sku['jxhyPfPrice'];
|
||
$is_free_shipping = 0;
|
||
}
|
||
} else { //单规格
|
||
if ($saleType == 'normal') {
|
||
$minOrderQuantity = 1;
|
||
$consignPrice = $saleInfo['retailprice'];
|
||
$is_free_shipping = 1;
|
||
} else {
|
||
$minOrderQuantity = $saleInfo['minOrderQuantity'];
|
||
$consignPrice = $saleInfo['jxhyPfPrice'];
|
||
$is_free_shipping = 0;
|
||
}
|
||
$startQuantity = array_column($goodsDetail['saleInfo']['priceRanges'], 'startQuantity');
|
||
$price = $saleInfo['priceRanges'][array_search(min($startQuantity), $startQuantity)]['price'] ?? $saleInfo['priceRanges'][0]['price'];
|
||
$image = $goodsDetail['image']['images'][0];
|
||
|
||
//单规格单独去拼装
|
||
$skuList = [$this->fictitiousGoodsSkuInfo($goods_id, $goods_name, $price, $consignPrice, $stock, $image, $img_url, $goodsDetail)];
|
||
$one_sku = [
|
||
'retailPrice' => $consignPrice,
|
||
'consignPrice' => $consignPrice,
|
||
'price' => $price
|
||
];
|
||
}
|
||
$_spec = array_map(function ($val) {
|
||
return [
|
||
'spec_id' => $val['spec_id'],
|
||
'spec_name' => $val['spec_name'],
|
||
'value' => array_values($val['value'])
|
||
];
|
||
}, $_spec);
|
||
if ($_spec) {
|
||
$this->goods_spec_format = json_encode(array_values($_spec), JSON_UNESCAPED_UNICODE);
|
||
}
|
||
$goods_attr_format = [];
|
||
if ($goodsDetail['attributes']) {
|
||
foreach ($goodsDetail['attributes'] as $key => $val) {
|
||
$goods_attr_format[] = [
|
||
'attr_class_id' => $key,
|
||
'attr_id' => $val['attributeID'],
|
||
'attr_name' => $val['attributeName'],
|
||
'attr_value_id' => $val['attributeID'],
|
||
'attr_value_name' => $val['value'],
|
||
'sort' => $key,
|
||
];
|
||
}
|
||
}
|
||
$goods_content = $goodsDetail['description'];//商品详情
|
||
$categoryjson = json_encode($category_id);//商品分类
|
||
$mul_inc_price = $price_template['price_set']['multiple'] ?? 0;
|
||
if ($mul_inc_price) {
|
||
$inc_price = ($consignPrice * $mul_inc_price / 100);
|
||
$inc_price = $inc_price < 0.01 ? 0.01 : $inc_price;
|
||
} else {
|
||
$inc_price = ($price_template['price_set']['increase'] ?: $consignPrice * 5 / 100);
|
||
$inc_price = $inc_price < 0.01 ? 0.01 : $inc_price;
|
||
}
|
||
$price = $consignPrice + $inc_price;
|
||
$title_set = $price_template['title_set'] ?? '';
|
||
if ($title_set) {
|
||
if (isset($title_set['delete_keywords']) && $title_set['delete_keywords']) {
|
||
$goods_name = str_replace($title_set['delete_keywords'], '', $goods_name);
|
||
}
|
||
$goods_name = $title_set['prefix'] . $goods_name . $title_set['suffix'];
|
||
}
|
||
// 生成商品数据
|
||
return [
|
||
'goods_id' => $goods_id,// 商品名称
|
||
'goods_name' => $goods_name,// 商品名称
|
||
'goods_class' => 1,// 【固定不变】商品种类1.实物商品2.虚拟商品3.卡券商品4.服务项目5.卡项商品
|
||
'goods_class_name' => '实物商品',// 【固定不变】商品种类
|
||
'goods_attr_class' => 0,// 【固定不变】商品类型id
|
||
'goods_attr_name' => '',// 【固定不变】商品类型名称
|
||
'site_id' => $this->site_id,// 店铺ID
|
||
'goods_image' => $goodsImage,// 商品主图路径 多图以英文逗号隔开
|
||
'introduction' => '',//描述
|
||
'sku_list' => $skuList,//描述
|
||
'goods_content' => $goodsDetail['description'] ?: $goods_content,// 商品详情
|
||
'goods_state' => $goodsDetail['status'] == 'published' ? 1 : 0,// 【固定不变】
|
||
'category_id' => $category_id,// 商品分类id,逗号隔开
|
||
'category_json' => $categoryjson,// 分类json字符串
|
||
'goods_category' => $goods_category,// 分类json字符串
|
||
'brand_id' => $brandId,// 商品品牌id
|
||
'brand_name' => $goodsDetail['brand'] ?? '',// 品牌名称
|
||
|
||
'price' => moneyFormat(round($price, 1)),// 商品价格(取第一个sku) 对应-建议零售价
|
||
'cost_price' => $one_sku['consignPrice'] ?? $one_sku['price'] ?? 0,// 成本价(取第一个sku) 对应-结算价
|
||
'goods_stock' => $stock,// 商品库存(总和)
|
||
'is_free_shipping' => $is_free_shipping,// 是否包邮
|
||
'goods_spec_format' => $this->goods_spec_format,//json_encode($this->goods_spec_format, JSON_UNESCAPED_UNICODE),// 多规格信息
|
||
'create_time' => $time,// 添加时间
|
||
'modify_time' => $time,// 修改时间
|
||
'support_trade_type' => 'express', // 【固定不变】支持的配送方式 仅 快递发货
|
||
'market_price' => moneyFormat(round($price + $price * 35 / 100, 1)),//划线价
|
||
'sale_num' => 0,
|
||
'virtual_sale' => $goodsDetail['bookedCount'] ?? 0,
|
||
'real_stock' => $stock,
|
||
'label_id' => '',
|
||
'timer_on' => 0,
|
||
'timer_off' => '',
|
||
'is_consume_discount' => 0,
|
||
'goods_stock_alarm' => 0,
|
||
'shipping_template' => 0,
|
||
'goods_attr_format' => json_encode($goods_attr_format, JSON_UNESCAPED_UNICODE),
|
||
'keywords' => '',
|
||
'unit' => $saleInfo['unit'],
|
||
'video_url' => $goodsDetail['mainVedio'] ?? '',
|
||
'sort' => 0,
|
||
'sale_show' => 0,
|
||
'stock_show' => 0,
|
||
'market_price_show' => 0,
|
||
'barrage_show' => 0,
|
||
'goods_service_ids' => '',
|
||
'sale_channel' => 'all',
|
||
'sale_store' => 'all',
|
||
'is_limit' => 0,
|
||
'limit_type' => 1,
|
||
'max_buy' => '',
|
||
'min_buy' => $minOrderQuantity,
|
||
'recommend_way' => 0,
|
||
'is_zmxx' => 0,
|
||
'is_unify_pirce' => 1,
|
||
'qr_id' => '',
|
||
'goods_sku_data' => json_encode($skuList, JSON_UNESCAPED_UNICODE),
|
||
];
|
||
}
|
||
|
||
/**
|
||
* Common: 商品入库 - 商品分类处理,查询本平台分类ID;如果不存在则添加
|
||
* @param $cate
|
||
* @return int[]|string[]
|
||
*/
|
||
private function getThisPlatformCateIds($cate)
|
||
{
|
||
$cateNames = array_column($cate, 'name');
|
||
$hasList = model('goods_category')->getColumn([
|
||
['category_name', 'in', $cateNames],
|
||
['site_id', '=', $this->site_id]
|
||
], 'category_name', 'category_id');
|
||
// 数量不对 缺少分类ID
|
||
if (count($hasList) < count($cate)) {
|
||
$createCateData = array_filter(array_map(function ($apiCate) use ($hasList) {
|
||
if (!in_array($apiCate['name'], $hasList)) {
|
||
return [
|
||
'site_id' => $this->site_id,
|
||
'category_name' => $apiCate['name'],
|
||
'image' => imgSeeLink($apiCate['iconPath'] ?? ''),
|
||
'level' => 1,
|
||
'commission_rate' => 0.0,
|
||
];
|
||
}
|
||
return [];
|
||
}, $cate));
|
||
model('goods_category')->addList($createCateData);
|
||
return $this->getThisPlatformCateIds($cate);
|
||
}
|
||
return array_keys($hasList);
|
||
}
|
||
|
||
/**
|
||
* Common: 商品入库 - 商品品牌处理,查询本平台品牌ID;如果不存在则添加
|
||
* @param $brandName
|
||
* @return int
|
||
*/
|
||
private function getThisPlatformBrandId($brandName = '')
|
||
{
|
||
$brandId = 0;
|
||
if (!empty($brandName)) {
|
||
$brandId = (int)model('goods_brand')->getValue([
|
||
['brand_name', 'in', $brandName],
|
||
['site_id', '=', $this->site_id]
|
||
], 'brand_id');
|
||
// 数量不对 缺少分类ID
|
||
if ($brandId <= 0) {
|
||
model('goods_brand')->add([
|
||
'brand_name' => $brandName,
|
||
'site_id' => $this->site_id,
|
||
'create_time' => time()
|
||
]);
|
||
return $this->getThisPlatformBrandId($brandName);
|
||
}
|
||
}
|
||
return $brandId;
|
||
}
|
||
|
||
//1688单规格拼装数据
|
||
private function fictitiousGoodsSkuInfo($goodsId, $goodsName, $price, $retailPrice, $amountOnSale, $image, $img_url, $goodsDetail = [], $saleType = '')
|
||
{
|
||
$offerId = $goodsDetail['productID'];
|
||
return [
|
||
'site_id' => $this->site_id,// 所属店铺id
|
||
'goods_id' => $goodsId,// 商品id
|
||
'sku_id' => '',// 商品id
|
||
'sku_name' => $goodsName,// 商品sku名称
|
||
'sku_no' => '',// 商品sku编码
|
||
'price' => round(moneyFormat($price), 1, PHP_ROUND_HALF_EVEN), // sku单价
|
||
'cost_price' => $retailPrice,//sku成本价
|
||
'stock' => $amountOnSale, // 商品sku库存
|
||
'sku_image' => $img_url . $image,
|
||
'sku_images' => $img_url . $image,
|
||
'goods_class' => 1,
|
||
'goods_class_name' => '实物商品',
|
||
'create_time' => time(),
|
||
'modify_time' => time(),
|
||
'goods_attr_class' => 0,
|
||
'is_default' => 0,
|
||
'spec_name' => '', //
|
||
'real_stock' => $amountOnSale, // 实物库存
|
||
'sku_spec_format' => '',
|
||
'goods_supplier_format' => json_encode([
|
||
'offerId' => $offerId,
|
||
'skuId' => '',
|
||
'spec_id' => '',
|
||
'saleType' => $saleType,
|
||
]),//第三方扩展数据,因为是单规格为空
|
||
// 必须内容
|
||
'market_price' => moneyFormat(round($price + $price * 35 / 100, 1)),
|
||
'discount_price' => moneyFormat(round($price, 1)), // sku单价
|
||
'weight' => 0,
|
||
'volume' => 0,
|
||
'sale_num' => 0,
|
||
'virtual_sale' => 0,
|
||
'fenxiao_price' => 0,
|
||
'stock_alarm' => '',
|
||
];
|
||
}
|
||
|
||
//获取批发价
|
||
private function getRetailPrice($saleInfo)
|
||
{
|
||
//获取最小起订量
|
||
$minOrderQuantity = $saleInfo['minOrderQuantity'];
|
||
$startQuantity_num = array_column($saleInfo['priceRanges'], 'startQuantity');
|
||
$num = array_search($minOrderQuantity, $startQuantity_num);
|
||
return $saleInfo['priceRanges'][$num]['price'] ?? $saleInfo['priceRanges'][0]['price'];
|
||
}
|
||
|
||
/**
|
||
* Common: 商品入库 - 生成本平台商品规格信息
|
||
* @param $goodsDetail
|
||
* @param $goodsId
|
||
* @return array|array[]
|
||
*/
|
||
private function generateGoodsSkuInfo($sku, $goodsId, $saleInfo, $price_template, $defaultSkuImage, $img_url, $_spec, $offerId, $saleType = '')
|
||
{
|
||
// 获取默认主图
|
||
// 处理规格信息 生成本平台规矩信息列表
|
||
$skuList = array_map(function ($skuItem) use ($goodsId, $saleInfo, $defaultSkuImage, $price_template, $img_url, $_spec, $offerId, $saleType) {
|
||
// if (!isset($skuItem['retailPrice'])) {
|
||
// var_dump(555);
|
||
// $skuItem['retailPrice'] = $skuItem['retailPrice'];
|
||
// // $skuItem['retailPrice'] = !isset($skuItem['price']) ? $skuItem['consignPrice'] : $skuItem['price'];
|
||
// }else if(!isset($skuItem['price'])){
|
||
// var_dump(666);
|
||
// $skuItem['price'] = $skuItem['retailPrice'];
|
||
// }
|
||
if ($saleType == 'normal') {
|
||
$consignPrice = $skuItem['retailPrice'];
|
||
} else {
|
||
$consignPrice = $skuItem['jxhyPfPrice'];
|
||
}
|
||
//进一位
|
||
// $retailPrice = ceil($skuItem['retailPrice'] * 10) / 10;
|
||
// if (!isset($skuItem['retailPrice'])) {
|
||
// $skuItem['retailPrice'] = $skuItem['consignPrice'];
|
||
// }
|
||
$mul_inc_price = $price_template['price_set']['multiple'] ?? 0;
|
||
if ($mul_inc_price) {
|
||
$inc_price = ($skuItem['retailPrice'] * $mul_inc_price / 100);
|
||
$inc_price = $inc_price < 0.01 ? 0.01 : $inc_price;
|
||
} else {
|
||
$inc_price = ($price_template['price_set']['increase'] ?: $skuItem['retailPrice'] * 5 / 100);
|
||
$inc_price = $inc_price < 0.01 ? 0.01 : $inc_price;
|
||
}
|
||
$sku_set = $price_template['sku_set'] ?? '';
|
||
$price = $consignPrice + $inc_price;
|
||
// $price = $skuItem['price'] + $inc_price;
|
||
// $saleType=$saleInfo['saleType']??'batch';
|
||
// if($saleType=='batch'){
|
||
// $price = $price / $saleInfo['minOrderQuantity'];
|
||
// $saleInfo['minOrderQuantity']=1;
|
||
// $skuItem['consignPrice']=$skuItem['consignPrice']/$saleInfo['minOrderQuantity'];
|
||
// }
|
||
$price = ceil($price * 10) / 10;
|
||
$attributes = $skuItem['attributes'];
|
||
$attributes = array_map(function ($item) use ($_spec) {
|
||
$spec_value_id = $_spec[$item['attributeID']]['value'][$item['attributeValue']]['spec_value_id'];
|
||
return [
|
||
'spec_id' => $item['attributeID'],
|
||
'spec_name' => $item['attributeName'],
|
||
'spec_value_id' => $spec_value_id,
|
||
'spec_value_name' => $item['attributeValue'],
|
||
'image' => '',
|
||
];
|
||
}, $attributes);
|
||
$sku_name = $skuItem['cargoNumber'];
|
||
if ($sku_set) {
|
||
if (isset($sku_set['delete_keywords']) && $sku_set['delete_keywords']) {
|
||
$sku_name = str_replace($sku_set['delete_keywords'], '', $sku_name);
|
||
}
|
||
$sku_name = $sku_set['prefix'] . $sku_name . $sku_set['suffix'];
|
||
}
|
||
// 规格列表
|
||
return [
|
||
'site_id' => $this->site_id,// 所属店铺id
|
||
'goods_id' => $goodsId,// 商品id
|
||
'sku_id' => '',// 商品id
|
||
'sku_name' => $sku_name,// 商品sku名称
|
||
'sku_no' => $skuItem['skuCode'],// 商品sku编码
|
||
// 'price' => round(moneyFormat($price), 1, PHP_ROUND_HALF_EVEN), // sku单价
|
||
// 'cost_price' => $skuItem['consignPrice'],//sku成本价
|
||
// 'cost_price' => !isset($skuItem['price']) ? $skuItem['consignPrice'] : $skuItem['price'],//sku成本价
|
||
'price' => $price, // sku单价
|
||
'cost_price' => $consignPrice,//sku成本价
|
||
'stock' => $skuItem['amountOnSale'], // 商品sku库存
|
||
'sku_image' => isset($skuItem['attributes'][0]['skuImageUrl']) ? $img_url . $skuItem['attributes'][0]['skuImageUrl'] : $defaultSkuImage,
|
||
'sku_images' => isset($skuItem['attributes'][0]['skuImageUrl']) ? $img_url . $skuItem['attributes'][0]['skuImageUrl'] : $defaultSkuImage,
|
||
'goods_class' => 1,
|
||
'goods_class_name' => '实物商品',
|
||
'create_time' => time(),
|
||
'modify_time' => time(),
|
||
'goods_attr_class' => 0,
|
||
'is_default' => 0,
|
||
'spec_name' => $skuItem['cargoNumber'] ?? '', //
|
||
'real_stock' => $skuItem['amountOnSale'], // 实物库存
|
||
'sku_spec_format' => json_encode($attributes),
|
||
'goods_supplier_format' => json_encode([
|
||
'offerId' => $offerId,
|
||
'skuId' => $skuItem['skuId'],
|
||
'spec_id' => $skuItem['specId'],
|
||
'saleType' => $saleType,
|
||
]),
|
||
// 必须内容
|
||
'market_price' => moneyFormat(round($price + $price * 35 / 100, 1)),
|
||
'discount_price' => moneyFormat(round($price, 1)), // sku单价
|
||
'weight' => 0,
|
||
'volume' => 0,
|
||
'sale_num' => 0,
|
||
'virtual_sale' => 0,
|
||
'fenxiao_price' => 0,
|
||
'stock_alarm' => '',
|
||
];
|
||
}, $sku);
|
||
if ($goodsId) {
|
||
$goods_model = new GoodsModel();
|
||
$goods_sku_list = $goods_model->getGoodsSkuList([['goods_id', '=', $goodsId], ['site_id', '=', $this->site_id]], "sku_id,sku_name,sku_no,sku_spec_format,price,market_price,cost_price,stock,weight,volume,sku_image,sku_images,goods_spec_format,spec_name,stock_alarm,is_default,goods_supplier_format", '')['data'];
|
||
$mpsku_arr = array_column($skuList, null, 'sku_no');
|
||
foreach ($goods_sku_list as $key => &$skuItem) {
|
||
if (isset($mpsku_arr[$skuItem['sku_no']])) {
|
||
$skuItem = array_merge($skuItem, $mpsku_arr[$skuItem['sku_no']]);
|
||
}
|
||
}
|
||
return $goods_sku_list;
|
||
}
|
||
return $skuList ?? [];
|
||
}
|
||
|
||
/**
|
||
* Common: 商品入库 - 刷新规格信息
|
||
* Author: wu-hui
|
||
* Time: 2023/09/04 11:27
|
||
* @param $skuList
|
||
* @param $goodsId
|
||
* @throws \Exception
|
||
*/
|
||
private function refreshGoodsSkuInfo($skuList, $goodsId)
|
||
{
|
||
// 获取已经存在的规格
|
||
$hasList = (array)model('goods_sku')->getColumn([
|
||
['goods_id', '=', $goodsId],
|
||
['site_id', '=', $this->site_id]
|
||
], 'sku_no', 'sku_id');
|
||
$skuIds = array_flip($hasList);// 键值互换 以sku编码为键 id为值
|
||
// 循环处理
|
||
$insertData = array_column($skuList, null, 'sku_no');// 默认添加全部
|
||
$updateData = [];
|
||
$deleteData = $skuIds;// 默认删除全部
|
||
foreach ($skuList as $skuItem) {
|
||
// 判断:如果当前规格已经存在,执行刷新 并且移除删除和添加内容
|
||
if (in_array($skuItem['sku_no'], $hasList)) {
|
||
$skuItem['sku_id'] = $skuIds[$skuItem['sku_no']];
|
||
$updateData[] = $skuItem;
|
||
|
||
unset($deleteData[$skuItem['sku_no']]);
|
||
unset($insertData[$skuItem['sku_no']]);
|
||
}
|
||
}
|
||
// 进行对应的操作 先删除、在修改、最后添加
|
||
model('goods_sku')->delete([
|
||
['goods_id', '=', $goodsId],
|
||
['sku_no', 'in', array_values($deleteData)],
|
||
['site_id', '=', $this->site_id]
|
||
]);
|
||
// 修改
|
||
$goodsSkuModel = (new NewBaseModel(['table_name' => 'goods_sku', 'pk' => 'sku_id']));
|
||
$goodsSkuModel->saveAll($updateData);
|
||
// 添加
|
||
model('goods_sku')->addList(array_values($insertData));
|
||
}
|
||
|
||
public function getDefaultSkuImage()
|
||
{
|
||
$url = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http";
|
||
$url .= "://" . $_SERVER['SERVER_NAME'] . '/public/static/img/default_img/square.png';
|
||
return $url;
|
||
}
|
||
|
||
/***
|
||
* 系统自动入库
|
||
* @return void
|
||
*/
|
||
public function AutoAddGoods($siteId = '')
|
||
{
|
||
return $this->success();
|
||
}
|
||
|
||
/***
|
||
* 删除供应链商品
|
||
* @param $params
|
||
* @return array
|
||
*/
|
||
public function deleteRecycleGoods($params)
|
||
{
|
||
$info = model('supply_goods_warehousing')->getList([['site_id', '=', $params['site_id']], ['goods_id', 'in', $params['goods_ids']]], 'goods_id,open_offer_id');
|
||
if ($info) {
|
||
$follow = (new Choice($this->site_id));
|
||
foreach ($info as $k => $v) {
|
||
$follow->unfollowGoods($v['open_offer_id']); //解除关注
|
||
}
|
||
}
|
||
model('supply_goods_warehousing')->delete([['site_id', '=', $params['site_id']], ['goods_id', 'in', $params['goods_ids']]]);
|
||
return $this->success('删除成功');
|
||
}
|
||
}
|