1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081 |
- <?php
- /**
- * 商品基础信息管理
- * Created by PhpStorm.
- * User: XiaoMing
- * Date: 2019/10/30
- * Time: 14:17
- */
-
- namespace JinDouYun\Controller\GoodsManage;
-
- use JinDouYun\Dao\GoodsCategory\DGoodsCategory;
- use JinDouYun\Dao\GoodsManage\DGoodsBrand;
- use JinDouYun\Dao\GoodsManage\DUnits;
- use JinDouYun\Dao\Merchant\DMerchantApply;
- use Mall\Framework\Core\ErrorCode;
- use Mall\Framework\Core\ResultWrapper;
- use Mall\Framework\Core\StatusCode;
-
- use JinDouYun\Model\GoodsManage\MSku;
- use JinDouYun\Model\GoodsManage\MGoodsBasic;
- use JinDouYun\Controller\BaseController;
-
- use Util\Common\ChineseCharacter;
-
- class GoodsBasic extends BaseController
- {
- /**
- * @var MGoodsBasic
- */
- private $objMGoodsBasic;
-
- /**
- * GoodsBasic constructor.
- * @param bool $isCheckAcl
- * @param bool $isMustLogin
- * @throws \Exception
- */
- public function __construct($isCheckAcl = true, $isMustLogin = true)
- {
- parent::__construct($isCheckAcl, $isMustLogin);
- $this->objMGoodsBasic = new MGoodsBasic($this->onlineUserId, $this->onlineEnterpriseId);
- }
-
- /**
- * 添加和编辑商品基础信息的公共字段
- *
- * @return array
- */
- public function commonFieldFilter()
- {
- $params = $this->request->getRawJson();
- if (empty($params)) {
- $this->sendOutput('参数为空', ErrorCode::$paramError);
- }
- $goodsBasicData = [
- 'title' => isset($params['title']) ? $params['title'] : '',
- 'categoryId' => isset($params['categoryId']) ? $params['categoryId'] : 0,
- 'categoryPath' => isset($params['categoryPath']) ? $params['categoryPath'] : '',
- 'images' => isset($params['images']) ? json_encode($params['images']) : [],//商品图册json
- 'unitData' => isset($params['unitData']) ? $params['unitData'] : '',//商品单位
- 'specType' => isset($params['specType']) ? $params['specType'] : '',
- 'isEq' => isset($params['isEq']) ? $params['isEq'] : StatusCode::$delete,
- ];
-
- foreach ($goodsBasicData as $key => $value) {
- if (empty($value) && $value !== 0) {
- $this->sendOutput($key . '参数错误', ErrorCode::$paramError);
- }
- }
-
-
- //商品名称转换搜索条件
- if (isset($goodsBasicData['title'])) {
- $objChineseCharacter = new ChineseCharacter();
- $goodsBasicData['condition'] = $objChineseCharacter->getInitials(trim($goodsBasicData['title']));
- }
-
- if ($goodsBasicData['specType'] == StatusCode::$specType['multiple']) {
- //带属性的
- if (!isset($params['specMultiple']) || empty($params['specMultiple'])) {
- parent::sendOutput('specMultiple参数错误', ErrorCode::$paramError);
- }
- if (!isset($params['specGroup']) || empty($params['specGroup'])) {
- parent::sendOutput('specGroup参数错误', ErrorCode::$paramError);
- }
- $goodsBasicData['specMultiple'] = $params['specMultiple'];//规格明细
- }
- $goodsBasicData['specGroup'] = json_encode($params['specGroup']);//规格组
- $goodsBasicData['brandId'] = isset($params['brandId']) ? $params['brandId'] : 0;
- $goodsBasicData['description'] = isset($params['description']) ? $params['description'] : '';
- $goodsBasicData['describe'] = isset($params['describe']) ? $params['describe'] : '';
- $goodsBasicData['expireTime'] = isset($params['expireTime']) ? $params['expireTime'] : 0;
- $goodsBasicData['tag'] = isset($params['tag']) ? $params['tag'] : '';
- $goodsBasicData['barCode'] = isset($params['barCode']) ? $params['barCode'] : '';//条码
- $goodsBasicData['link'] = isset($params['link']) ? $params['link'] : '';
- $goodsBasicData['noSalesShop'] = isset($params['noSalesShop']) ? $params['noSalesShop'] : ''; //禁止销售店铺
- $goodsBasicData['assistCategoryId'] = isset($params['assistCategoryId']) ? $params['assistCategoryId'] : '';
- $goodsBasicData['assistCategoryPath'] = isset($params['assistCategoryPath']) ? json_encode($params['assistCategoryPath']) : [];
-
- //编辑传参
- $storage = isset($params['storage']) ? $params['storage'] : '';//货架编码
- $goodsBasicData['extends'] = json_encode(['storage' => $storage]);//货架编码
- $goodsBasicData['delUnitIds'] = isset($params['delUnitIds']) ? $params['delUnitIds'] : [];//删除计量单位ids
- $goodsBasicData['delSpecSkuIds'] = isset($params['delSpecSkuIds']) ? $params['delSpecSkuIds'] : [];//删除属性值
- $goodsBasicData['merchantId'] = isset($params['merchantId']) ? $params['merchantId'] : 0;//接收商户id
- !empty($this->supplierId) && $goodsBasicData['supplierId'] = $this->supplierId;
- return $goodsBasicData;
- }
-
- /**
- * 添加商品基础数据
- * @throws \Exception
- */
- public function addGoodsBasic()
- {
- $goodsBasicData = $this->commonFieldFilter();
- unset($goodsBasicData['delUnitIds']);
- unset($goodsBasicData['delSpecSkuIds']);
-
- $result = false;
- switch ($goodsBasicData['specType']) {
- case StatusCode::$specType['single']:
- //没有属性的
- $result = $this->objMGoodsBasic->addGoodsBasic($goodsBasicData);//单规格
- break;
- case StatusCode::$specType['multiple']:
- //有属性的
- $result = $this->objMGoodsBasic->addMultipleSpecGoodsBasic($goodsBasicData);//有属性的
- break;
- }
- if ($result->isSuccess()) {
- parent::sendOutput($result->getData());
- }
- parent::sendOutput($result->getData(), $result->getErrorCode());
- }
-
- /**
- * 获取全部商品基础资料列表
- * @throws \Exception
- */
- public function getAllGoodsBasic()
- {
- $page = $this->request->param('page') ?: 1;
- $pageSize = $this->request->param('pageSize') ?: 10;
- $enableStatus = $this->request->param('enableStatus') ?: '';
- $export = $this->request->param('export') ?: '';
- $merchantId = $this->request->param('merchantId');
- $offset = ($page - 1) * $pageSize;
-
- $selectParams = [
- 'limit' => $pageSize,
- 'offset' => $offset,
- ];
- if (!empty($merchantId)) {
- $selectParams['merchantId'] = $merchantId;
- }
- if (!empty($enableStatus)) $selectParams['enableStatus'] = $enableStatus;
- $is_export = false;
- !empty($export) && $is_export = true;
- !empty($this->shopId) && $selectParams['level'] = StatusCode::$standard;//商户后台只能看到平台商品
- !empty($this->supplierId) && $selectParams['supplierId'] = $this->supplierId;
- $result = $this->objMGoodsBasic->getAllGoodsBasic($selectParams, $is_export);
-
- if ($result->isSuccess()) {
- $returnData = $result->getData();
- $pageData = [
- 'pageIndex' => $page,
- 'pageSize' => $pageSize,
- 'pageTotal' => $returnData['total'],
- ];
- parent::sendOutput($returnData['data'], 0, $pageData);
- } else {
- parent::sendOutput($result->getData(), ErrorCode::$dberror);
- }
- }
-
- /**
- * 搜索基本资料
- * @throws \Exception
- */
- public function search()
- {
- $params = $this->request->getRawJson();
- if (empty($params)) {
- $this->sendOutput('参数为空', ErrorCode::$paramError);
- }
-
- $selectParams = [
- 'merchantId' => isset($params['merchantId']) ? $params['merchantId'] : '',
- 'categoryPath' => isset($params['categoryPath']) ? $params['categoryPath'] : '',
- 'categoryId' => isset($params['categoryId']) ? $params['categoryId'] : '',
- 'brandId' => isset($params['brandId']) ? $params['brandId'] : '',
- 'keyword' => isset($params['keyword']) ? $params['keyword'] : '',
- 'enableStatus' => isset($params['enableStatus']) ? $params['enableStatus'] : '',//销售状态
- 'deleteStatus' => StatusCode::$standard,
- ];
-
- $pageParams = pageToOffset($params['page'] ?: 1, $params['pageSize'] ?: 10);
- $selectParams['limit'] = $pageParams['limit'];
- $selectParams['offset'] = $pageParams['offset'];
- $is_export = false;
- if (isset($params['export']) && $params['export'] == 1) $is_export = true;
-
- !empty($this->shopId) && $selectParams['level'] = StatusCode::$standard;//商户后台只能看到平台商品
- !empty($this->supplierId) && $selectParams['supplierId'] = $this->supplierId;
- $result = $this->objMGoodsBasic->search($selectParams, $is_export);
- if ($result->isSuccess()) {
- $returnData = $result->getData();
- $pageData = [
- 'pageIndex' => $params['page'],
- 'pageSize' => $params['pageSize'],
- 'pageTotal' => $returnData['total'],
- ];
- parent::sendOutput($returnData['data'], 0, $pageData);
- } else {
- parent::sendOutput($result->getData(), $result->getErrorCode());
- }
- }
-
- /**
- * 获取指定id的商品基础资料详情
- * @throws \Exception
- */
- public function getGoodsBasicInfoById()
- {
- $id = $this->request->param('request_id');
- $params = $this->request->getRawJson();
- $isAddGoods = isset($params['isAddGoods']) ? $params['isAddGoods'] : StatusCode::$delete;//添加商品获取详情
-
- if (!$id) {
- $this->sendOutput('参数错误,id', ErrorCode::$paramError);
- }
- $result = $this->objMGoodsBasic->getGoodsBasicInfoById($id, $isAddGoods);
- if ($result->isSuccess()) {
- $resultData = $result->getData();
- $this->sendOutput($resultData);
- }
- $this->sendOutput($result->getData(), $result->getErrorCode());
- }
-
- /**
- * 编辑基础商品数据
- * @throws \Exception
- */
- public function editGoodsBasic()
- {
- $id = $this->request->param('request_id');
- if (empty($id)) {
- $this->sendOutput('参数错误id', ErrorCode::$paramError);
- }
- $goodsBasicData = $this->commonFieldFilter();
- $goodsBasicData['id'] = $id;
- $result = false;
- switch ($goodsBasicData['specType']) {
- case StatusCode::$specType['single']:
- //无属性
- $result = $this->objMGoodsBasic->editGoodsBasic($goodsBasicData);
- break;
- case StatusCode::$specType['multiple']:
- //有属性
- $result = $this->objMGoodsBasic->editMultipleGoodsBasic($goodsBasicData);
- break;
- }
- if ($result->isSuccess()) {
- parent::sendOutput($result->getData());
- }
- parent::sendOutput($result->getData(), $result->getErrorCode());
- }
-
- /**
- * 更新基础商品的启用/禁用
- * @throws \Exception
- */
- public function updateEnableStatus()
- {
- $paramsData = $this->request->getRawJson();
- if (empty($paramsData['id'])) {
- $this->sendOutput('参数错误id', ErrorCode::$paramError);
- }
- $result = $this->objMGoodsBasic->updateEnableStatus($paramsData);
- if ($result->isSuccess()) {
- parent::sendOutput($result->getData());
- }
- parent::sendOutput($result->getData(), $result->getErrorCode());
- }
-
- /**
- * Doc: (des="设置为平台商品")
- * User: XMing
- * Date: 2020/12/7
- * Time: 12:01 下午
- */
- public function pushPlatform()
- {
- $id = $this->request->param('request_id');
- if (!$id) {
- $this->sendOutput('参数错误', ErrorCode::$paramError);
- }
- $result = $this->objMGoodsBasic->pushPlatform($id);
- if ($result->isSuccess()) {
- parent::sendOutput($result->getData());
- }
- parent::sendOutput($result->getData(), $result->getErrorCode());
- }
-
-
- /**
- * 删除基础商品资料
- * @throws \Exception
- */
- public function delGoodsBasic()
- {
- $id = $this->request->param('request_id');
- if (!$id) {
- $this->sendOutput('参数错误', ErrorCode::$paramError);
- }
-
- $result = $this->objMGoodsBasic->delGoodsBasic($id);
- if ($result->isSuccess()) {
- parent::sendOutput($result->getData());
- } else {
- parent::sendOutput($result->getData(), $result->getErrorCode());
- }
- }
-
- /**
- * 调整基础商品资料的分类
- */
- public function updateCategory()
- {
- $params = $this->request->getRawJson();
- $paramsData = [
- 'id' => isset($params['id']) ? $params['id'] : '',
- 'categoryId' => isset($params['categoryId']) ? $params['categoryId'] : '',
- 'categoryPath' => isset($params['categoryPath']) ? $params['categoryPath'] : '',
- ];
- foreach ($paramsData as $key => $value) {
- if (empty($value)) {
- $this->sendOutput($key . '参数错误', ErrorCode::$paramError);
- }
- }
- $result = $this->objMGoodsBasic->updateCategory($paramsData);
- if ($result->isSuccess()) {
- parent::sendOutput($result->getData());
- }
- parent::sendOutput($result->getData(), $result->getErrorCode());
- }
-
- /**
- * 获取商铺id下允许销售的产品
- * @throws \Exception
- */
- public function getGoodsBasicOfShopId()
- {
- $params = $this->request->getRawJson();
- if (empty($params)) {
- $this->sendOutput('参数为空', ErrorCode::$paramError);
- }
-
- $pageParams = pageToOffset($params['page'] ?: 1, $params['pageSize'] ?: 10);
- $selectParams['limit'] = $pageParams['limit'];
- $selectParams['offset'] = $pageParams['offset'];
-
- if (!isset($params['shopId']) || empty($params['shopId'])) {
- $this->sendOutput('shopId参数为空', ErrorCode::$paramError);
- }
- $selectParams['shopId'] = $params['shopId'];
- if (isset($params['keyword']) && !empty($params['keyword'])) {
- $selectParams['title'] = $params['keyword'];
- }
-
- if (isset($params['categoryId']) && !empty($params['categoryId'])) {
- $selectParams['categoryId'] = $params['categoryId'];
- }
-
- $result = $this->objMGoodsBasic->getGoodsBasicOfShopId($selectParams);
-
- if ($result->isSuccess()) {
- $returnData = $result->getData();
- $pageData = [
- 'pageIndex' => $params['page'],
- 'pageSize' => $params['pageSize'],
- 'pageTotal' => $returnData['total'],
- ];
- parent::sendOutput($returnData['data'], 0, $pageData);
- } else {
- $this->sendOutput($result->getData(), $result->getErrorCode());
- }
- }
-
- /**
- * 批量设置不可销售店铺
- */
- public function setNoSalesShop()
- {
- $params = $this->request->getRawJson();
- $paramsData = [
- 'id' => isset($params['id']) ? $params['id'] : '',//ids
- 'noSalesShop' => isset($params['noSalesShop']) ? $params['noSalesShop'] : '',
- ];
- foreach ($paramsData as $key => $value) {
- if (empty($value)) {
- $this->sendOutput($key . '参数错误', ErrorCode::$paramError);
- }
- }
-
- $result = $this->objMGoodsBasic->setNoSalesShop($paramsData);
- if ($result->isSuccess()) {
- parent::sendOutput($result->getData());
- }
- parent::sendOutput($result->getData(), $result->getErrorCode());
- }
-
- /**
- * 批量设置品牌
- */
- public function setBrand()
- {
- $params = $this->request->getRawJson();
- $paramsData = [
- 'id' => isset($params['id']) ? $params['id'] : '',//ids
- 'brandId' => isset($params['brandId']) ? $params['brandId'] : '',
- ];
- foreach ($paramsData as $key => $value) {
- if (empty($value)) {
- $this->sendOutput($key . '参数错误', ErrorCode::$paramError);
- }
- }
-
- $result = $this->objMGoodsBasic->setBrand($paramsData);
- if ($result->isSuccess()) {
- parent::sendOutput($result->getData());
- }
- parent::sendOutput($result->getData(), $result->getErrorCode());
- }
-
- /**
- * 批量设置sku辅助单位
- * @throws \Exception
- */
- public function setSku()
- {
- $params = $this->request->getRawJson();
- $paramsData = [
- 'goodsId' => isset($params['id']) ? $params['id'] : '',//ids
- 'branchData' => isset($params['branchData']) ? $params['branchData'] : '',
- ];
- foreach ($paramsData as $k => $v) {
- if (empty($v) && $v !== 0) {
- $this->sendOutput($k . '参数错误', ErrorCode::$paramError);
- }
- }
- $sheetData = [];
- foreach ($params['branchData'] as $key => $value) {
- $sheetData[$key] = [
- 'conversion' => isset($value['conversion']) ? $value['conversion'] : '',
- 'unitName' => isset($value['unitName']) ? $value['unitName'] : '',
- 'unitId' => isset($value['unitId']) ? $value['unitId'] : '',
- 'isMaster' => StatusCode::$delete,
- ];
- foreach ($sheetData[$key] as $k => $v) {
- if (empty($v) && $v !== 0) {
- $this->sendOutput($k . '参数错误', ErrorCode::$paramError);
- }
- }
- }
- $paramsData['branchData'] = $sheetData;
- $objMSku = new MSku($this->onlineUserId, $this->onlineEnterpriseId);
- $result = $objMSku->setSku($paramsData);
- if ($result->isSuccess()) {
- parent::sendOutput($result->getData());
- }
- parent::sendOutput($result->getData(), $result->getErrorCode());
- }
-
- //修补es数据
- public function fixEsData()
- {
- $params = $this->request->getRawJson();
- if (empty($params)) {
- $this->sendOutput('参数为空', ErrorCode::$paramError);
- }
-
- isset($params['id']) && $selectParams['id'] = $params['id'];
- $pageParams = pageToOffset(1, 4000);
- $selectParams['limit'] = $pageParams['limit'];
- $selectParams['offset'] = $pageParams['offset'];
- $this->objMGoodsBasic->fixEsData($selectParams);
- }
-
- /**
- * 按sku查询的基础商品列表
- */
- public function getAllGoodsBasicBySku()
- {
- $params = $this->request->getRawJson();
- if (empty($params)) {
- $this->sendOutput('参数为空', ErrorCode::$paramError);
- }
- $data = [
- 'isRevealSku' => isset($params['isRevealSku']) ? $params['isRevealSku'] : '',
- ];
- foreach ($data as $key => $val) {
- if (empty($val)) {
- parent::sendOutput($key . '参数错误', ErrorCode::$paramError);
- }
- }
-
- $data['materielId'] = getArrayItem($params, 'materielId', 0);
- $data['keyword'] = isset($params['keyword']) ? $params['keyword'] : '';
- $data['categoryId'] = isset($params['categoryId']) ? $params['categoryId'] : '';
- $pageParams = pageToOffset(isset($params['page']) ? $params['page'] : 1, isset($params['pageSize']) ? $params['pageSize'] : 10);
- $data['limit'] = $pageParams['limit'];
- $data['offset'] = $pageParams['offset'];
- if (isset($this->shopId)) {
- $data['shopId'] = $this->shopId;
- }
- if (isset($params['ifMerchant']) && !empty($params['ifMerchant'])) {
- $data['ifMerchant'] = $params['ifMerchant'];
- }
- if (isset($params['merchantId']) && !empty($params['merchantId'])) {
- $data['merchantId'] = $params['merchantId'];
- }
- $result = $this->objMGoodsBasic->getAllGoodsBasicBySku($data);
- if ($result->isSuccess()) {
- $returnData = $result->getData();
- $pageData = [
- 'pageIndex' => isset($params['page']) ? $params['page'] : 1,
- 'pageSize' => isset($params['pageSize']) ? $params['pageSize'] : 10,
- 'pageTotal' => $returnData['total'],
- ];
- parent::sendOutput($returnData['data'], 0, $pageData);
- }
- parent::sendOutput($result->getData(), $result->getErrorCode());
- }
-
- /**
- * 接收导入数据参数
- * @throws \Exception
- */
- public function getGoodsBasicImportParams()
- {
- //获取数据文件
- $params = $this->request->getRawJson();
-
- //企业id
- $enterpriseId = $this->onlineEnterpriseId;
-
- //商品图片
- $images = [];
-
- //多单位
- $twoUnit = true;
-
- //引用dao
- $objDGoodsCategory = new DGoodsCategory();
- $objDGoodsCategory->setTable('qianniao_goods_category_' . $enterpriseId);
-
- $objDGoodsBrand = new DGoodsBrand();
- $objDGoodsBrand->setTable('qianniao_goods_brand_' . $enterpriseId);
-
- $objDUnits = new DUnits();
- $objDUnits->setTable('qianniao_units_' . $enterpriseId);
-
- $objDMerchantApply = new DMerchantApply();
- //获取数据
- $postArray = [];
- foreach ($params as $value) {
- $goodsArray = $value;
- //商品信息
- $categoryName = isset($goodsArray['categoryName']) ? trim($goodsArray['categoryName']) : '';//分类名称
- $categoryTwo = isset($goodsArray['categoryTwo']) ? trim($goodsArray['categoryTwo']) : '';//二级分类名称
- $categoryThree = isset($goodsArray['categoryThree']) ? trim($goodsArray['categoryThree']) : '';//三级分类名称
- $brandName = isset($goodsArray['brandName']) ? trim($goodsArray['brandName']) : '';//品牌名称
- $goodsName = isset($goodsArray['goodsName']) ? trim($goodsArray['goodsName']) : '';//商品名称
- $merchantName = isset($goodsArray['merchantName']) ? trim($goodsArray['merchantName']) : '';//商户名称
-
- $isEq = isset($goodsArray['isEq']) ? trim($goodsArray['isEq']) : 4;//是否抄码
- $describe = isset($goodsArray['describe']) ? trim($goodsArray['describe']) : '';//商品卖点
- $description = isset($goodsArray['description']) ? '<p>' . trim($goodsArray['description']) . '</p>' : '';//商品详情
-
- //主单位信息
- $skuName = isset($goodsArray['skuName']) ? trim($goodsArray['skuName']) : '无';//单位名称
- $barCode = isset($goodsArray['barCode']) ? trim($goodsArray['barCode']) : '';//主单位条码
- $salePrice = isset($goodsArray['salePrice']) ? trim($goodsArray['salePrice']) : 1;//商品价格
- $salePriceTrue = isset($goodsArray['salePriceTrue']) ? trim($goodsArray['salePriceTrue']) : bcmul($salePrice, 1.2, 2);//市场价格
-
- //辅单位信息
- if ($twoUnit) {
- $skuNameTwo = isset($goodsArray['skuNameTwo']) ? trim($goodsArray['skuNameTwo']) : '';//辅单位
- $barCodeTwo = isset($goodsArray['barCodeTwo']) ? trim($goodsArray['barCodeTwo']) : '';//辅单位条码
- $conversion = isset($goodsArray['conversion']) ? trim($goodsArray['conversion']) : 0;//辅单位换算比例
- $salePriceTwo = isset($goodsArray['salePriceTwo']) ? trim($goodsArray['salePriceTwo']) : 1;//辅单位销售价格
- $salePriceTwoTrue = isset($goodsArray['salePriceTwoTrue']) ? trim($goodsArray['salePriceTwoTrue']) : bcmul($salePriceTwo, 1.2, 2);//辅单位市场价格
- }
-
- if (empty($goodsName)) {
- continue;
- }
-
- /***商户数据***/
- if (!empty($merchantName)) {
- //查询商户是否存在
- $merchantSelect = [
- 'name' => $merchantName,
- ];
- $dbResult = $objDMerchantApply->get($merchantSelect);
- if ($dbResult === false) {
- exit($objDGoodsCategory->error());
- }
- $merchantId = $dbResult['id'];
- unset($dbResult);
- } else {
- $merchantId = 0;
- }
-
- /***首分类数据***/
- $categoryId = 1;
- $categoryPath = 1;
- if (!empty($categoryName)) {
- //查询分类是否存在
- $categorySelect = [
- 'title' => $categoryName,
- ];
- $dbResult = $objDGoodsCategory->get($categorySelect);
- if ($dbResult === false) {
- exit($objDGoodsCategory->error());
- }
- $category = $dbResult;
- unset($dbResult);
- if (empty($category)) {
- $categoryInsert = [
- 'sort' => 0,
- 'title' => $categoryName,
- 'pid' => 0,
- 'enableStatus' => StatusCode::$standard,
- 'deleteStatus' => StatusCode::$standard,
- 'createTime' => time(),
- 'updateTime' => time(),
- ];
- $dbResult = $objDGoodsCategory->insert($categoryInsert);
- if ($dbResult === false) {
- exit($objDGoodsCategory->error());
- }
- $categoryId = $dbResult;
- $categoryPath = $dbResult;
- unset($dbResult);
- } else {
- $categoryId = $category['id'];
- $categoryPath = $category['link'] . ',' . $category['id'];
- }
-
- /***二级分类数据***/
- if (!empty($categoryTwo)) {
- //查询分类是否存在
- $categorySelect = [
- 'title' => $categoryTwo,
- ];
- $dbResult = $objDGoodsCategory->get($categorySelect);
- if ($dbResult === false) {
- exit($objDGoodsCategory->error());
- }
- $category = $dbResult;
- unset($dbResult);
- if (empty($category)) {
- $categoryInsert = [
- 'sort' => 0,
- 'title' => $categoryTwo,
- 'pid' => $categoryId,
- 'link' => $categoryPath,
- 'enableStatus' => StatusCode::$standard,
- 'deleteStatus' => StatusCode::$standard,
- 'createTime' => time(),
- 'updateTime' => time(),
- ];
- $dbResult = $objDGoodsCategory->insert($categoryInsert);
- if ($dbResult === false) {
- exit($objDGoodsCategory->error());
- }
- $categoryId = $dbResult;
- $categoryPath = $categoryId . ',' . $dbResult;
- unset($dbResult);
- } else {
- $categoryId = $category['id'];
- $categoryPath = $category['link'] . ',' . $category['id'];
- }
- /***三级分类数据***/
- if (!empty($categoryThree)) {
- //查询分类是否存在
- $categorySelect = [
- 'title' => $categoryThree,
- ];
- $dbResult = $objDGoodsCategory->get($categorySelect);
- if ($dbResult === false) {
- exit($objDGoodsCategory->error());
- }
- $category = $dbResult;
- unset($dbResult);
- if (empty($category)) {
- $categoryInsert = [
- 'sort' => 0,
- 'title' => $categoryThree,
- 'pid' => $categoryId,
- 'link' => $categoryPath,
- 'enableStatus' => StatusCode::$standard,
- 'deleteStatus' => StatusCode::$standard,
- 'createTime' => time(),
- 'updateTime' => time(),
- ];
- $dbResult = $objDGoodsCategory->insert($categoryInsert);
- if ($dbResult === false) {
- exit($objDGoodsCategory->error());
- }
- $categoryId = $dbResult;
- $categoryPath = $categoryId . ',' . $categoryId . ',' . $dbResult;
- unset($dbResult);
- } else {
- $categoryId = $category['id'];
- $categoryPath = $category['link'] . ',' . $category['id'];
- }
- }
- }
- }
- $categoryPath = trim($categoryPath, ',');
-
- /***品牌数据***/
- if (!empty($brandName)) {
- //查询品牌是否存在
- $brandSelect = [
- 'title' => $brandName
- ];
- $dbResult = $objDGoodsBrand->get($brandSelect);
- if ($dbResult === false) {
- exit($objDGoodsBrand->error());
- }
- $brand = $dbResult;
- unset($dbResult);
- if (empty($brand)) {
- $brandInsert = [
- 'title' => $brandName,
- 'images' => null,
- 'sort' => 0,
- 'enableStatus' => StatusCode::$standard,
- 'createTime' => time(),
- 'updateTime' => time(),
- ];
- $dbResult = $objDGoodsBrand->insert($brandInsert);
- if ($dbResult === false) {
- exit($objDGoodsBrand->error());
- }
- $brandId = $dbResult;
- unset($dbResult);
- } else {
- $brandId = $brand['id'];
- }
- } else {
- $brandId = 1;
- }
-
- /***单位数据***/
- $skuId = NULL;
- if (!empty($skuName)) {
- //查询单位是否存在
- $skuSelect = [
- 'unitName' => $skuName
- ];
- $dbResult = $objDUnits->get($skuSelect);
- if ($dbResult === false) {
- exit($objDUnits->error());
- }
- $sku = $dbResult;
- unset($dbResult);
- if (empty($sku)) {
- $skuInsert = [
- 'unitName' => $skuName,
- 'enableStatus' => StatusCode::$standard,
- 'createTime' => time(),
- 'updateTime' => time(),
- ];
- $dbResult = $objDUnits->insert($skuInsert);
- if ($dbResult === false) {
- exit($objDUnits->error());
- }
- $skuId = $dbResult;
- unset($dbResult);
- } else {
- $skuId = $sku['id'];
- }
- }
- $skuIdTwo = NULL;
- if (!empty($skuNameTwo)) {
- //查询单位是否存在
- $skuSelect = [
- 'unitName' => $skuNameTwo
- ];
- $dbResult = $objDUnits->get($skuSelect);
- if ($dbResult === false) {
- exit($objDUnits->error());
- }
- $sku = $dbResult;
- unset($dbResult);
- if (empty($sku)) {
- $skuInsert = [
- 'unitName' => $skuNameTwo,
- 'enableStatus' => StatusCode::$standard,
- 'createTime' => time(),
- 'updateTime' => time(),
- ];
- $dbResult = $objDUnits->insert($skuInsert);
- if ($dbResult === false) {
- exit($objDUnits->error());
- }
- $skuIdTwo = $dbResult;
- unset($dbResult);
- } else {
- $skuIdTwo = $sku['id'];
- }
- }
-
- if ($twoUnit) {
- $postData = [
- "categoryPath" => $categoryPath,
- "title" => $goodsName,
- "storage" => "",
- "delUnitIds" => [],
- "delSpecSkuIds" => [],
- "specType" => 1,
- "specGroup" => [],
- "specMultiple" => [
- [
- "barCode" => $barCode,
- "weight" => "",
- "isDefault" => 5,
- "unitId" => $skuId,
- "unitName" => $skuName,
- "isMaster" => 5,
- "conversion" => 0,
- "specImage" => "",
- "specGroup" => [],
- "salePrice" => [
- "conversion" => 0,
- "unitName" => $skuName,
- "unitId" => $skuId,
- "isMaster" => 5,
- "deleteStatus" => 5,
- "enabledLadder" => 0,
- "salePriceAreaType" => 1,
- "salePrice" => $salePrice,
- "ladderPrice" => [],
- "marketPrice" => $salePriceTrue,
- "setNum" => 1
- ]
- ],
- [
- "barCode" => $barCodeTwo,
- "isDefault" => 4,
- "unitId" => $skuIdTwo,
- "unitName" => $skuNameTwo,
- "isMaster" => 4,
- "conversion" => $conversion,
- "specImage" => "",
- "specGroup" => [],
- "salePrice" => [
- "conversion" => $conversion,
- "unitName" => $skuNameTwo,
- "unitId" => $skuIdTwo,
- "isMaster" => 4,
- "deleteStatus" => 5,
- "enabledLadder" => 0,
- "salePriceAreaType" => 1,
- "salePrice" => $salePriceTwo,
- "ladderPrice" => [],
- "marketPrice" => $salePriceTwoTrue,
- "setNum" => 1
- ]
- ]
- ],
- "unitData" => [
- [
- "barCode" => "",
- "weight" => "",
- "isMaster" => 5,
- "isDefault" => 5,
- "unitName" => $skuName,
- "unitId" => $skuId
- ],
- [
- "unitName" => $skuNameTwo,
- "isMaster" => 4,
- "unitId" => $skuIdTwo,
- "conversion" => $conversion,
- "isEditSpec" => 5
- ]
- ],
- "describe" => $describe,
- 'merchantId' => $merchantId,//商户
- "code" => "",
- "barCode" => "",
- "weight" => "",
- "categoryId" => $categoryId,
- "expireTime" => "",
- "brandId" => $brandId,
- "tag" => "",
- "description" => $description,
- "noSalesShop" => "",
- "images" => $images,
- "isEq" => $isEq,
- "enableStatus" => 5,
- "deliverySupIds" => "1,2",
- "expressType" => 1,
- "expressFee" => "",
- "showExpress" => 5,
- "ruleId" => "",
- "isStore" => false
- ];
- } else {
- /***插入商品***/
- $specMultiple = [[
- "barCode" => $barCode,//商品条码
- "isDefault" => 5,
- "unitId" => $skuId,
- "unitName" => $skuName,
- "isMaster" => 5,
- "conversion" => 0,
- "specImage" => "",
- "specGroup" => [],
- "salePrice" => [
- "conversion" => 0,
- "unitName" => $skuName,
- "unitId" => $skuId,
- "isMaster" => 5,
- "deleteStatus" => 5,
- "enabledLadder" => 0,
- "salePriceAreaType" => 1,
- "salePrice" => $salePrice,//商品价格
- "ladderPrice" => [],
- "marketPrice" => $salePriceTrue,
- "setNum" => 1//限定数量
- ]
- ]];
-
- $postData = [
- 'categoryPath' => $categoryPath,
- 'title' => $goodsName,
- 'storage' => '',
- 'delUnitIds' => [],
- 'delSpecSkuIds' => [],
- 'specType' => 1,//1:无规格
- 'specGroup' => [],//无规格为空
- 'specMultiple' => $specMultiple,
- 'unitData' =>
- [
- [
- 'barCode' => '',
- 'isMaster' => 5,
- 'isDefault' => 5,
- 'unitName' => $skuName,
- 'unitId' => $skuId,
- ],
- ],
- 'describe' => $describe,//商品卖点
- 'merchantId' => $merchantId,//商户
- 'code' => '',
- 'barCode' => '',
- 'categoryId' => $categoryId,
- 'expireTime' => '',
- 'brandId' => $brandId,
- 'tag' => '',
- 'description' => $description,//商品详情
- 'noSalesShop' => '',
- 'images' => $images,
- 'isEq' => $isEq,
- 'enableStatus' => 5,
- ];
-
- }
- $postArray[] = $postData;
- }
- return $postArray;
- }
-
- /**
- * 导入商品基础数据
- * @throws \Exception
- */
- public function goodsImport()
- {
- //把页面传来的参数对应到符合的字段中全部传递过来;
- $paramsArray = self::getGoodsBasicImportParams();
- if (empty($paramsArray)) {
- $this->sendOutput('参数为空', ErrorCode::$paramError);
- }
- $goodsBasic = [];
- foreach ($paramsArray as $params) {
- //把所有的参数过滤,赋值给对应的字段中
- $goodsBasicData = [
- 'title' => isset($params['title']) ? $params['title'] : null,
- 'categoryId' => isset($params['categoryId']) ? $params['categoryId'] : null,
- 'categoryPath' => isset($params['categoryPath']) ? $params['categoryPath'] : null,
- 'images' => isset($params['images']) ? json_encode($params['images']) : null,//商品图册json
- 'unitData' => isset($params['unitData']) ? $params['unitData'] : null,//商品单位
- 'specType' => isset($params['specType']) ? $params['specType'] : null,
- 'isEq' => isset($params['isEq']) ? $params['isEq'] : 4,
- ];
-
- foreach ($goodsBasicData as $key => $value) {
- if (empty($value)) {
- parent::sendOutput($key . '参数错误', ErrorCode::$paramError);
- }
- }
-
-
- //商品名称转换搜索条件
- if (isset($goodsBasicData['title'])) {
- $objChineseCharacter = new ChineseCharacter();
- $goodsBasicData['condition'] = $objChineseCharacter->getInitials(trim($goodsBasicData['title']));
- }
-
- if ($goodsBasicData['specType'] == StatusCode::$specType['multiple']) {
- //带属性的
- if (!isset($params['specMultiple']) || empty($params['specMultiple'])) {
- parent::sendOutput('specMultiple参数错误', ErrorCode::$paramError);
- }
- if (!isset($params['specGroup']) || empty($params['specGroup'])) {
- parent::sendOutput('specGroup参数错误', ErrorCode::$paramError);
- }
- $goodsBasicData['specGroup'] = json_encode($params['specGroup']);//规格组
- $goodsBasicData['specMultiple'] = $params['specMultiple'];//规格明细
- }
-
- $goodsBasicData['brandId'] = isset($params['brandId']) ? $params['brandId'] : 0;//商品品牌
- $goodsBasicData['description'] = isset($params['description']) ? $params['description'] : '';
- $goodsBasicData['describe'] = isset($params['describe']) ? $params['describe'] : '';
- $goodsBasicData['expireTime'] = isset($params['expireTime']) ? $params['expireTime'] : 0;
- $goodsBasicData['tag'] = isset($params['tag']) ? $params['tag'] : '';
- $goodsBasicData['barCode'] = isset($params['barCode']) ? $params['barCode'] : '';//条码
- $goodsBasicData['link'] = isset($params['link']) ? $params['link'] : '';
- $goodsBasicData['noSalesShop'] = isset($params['noSalesShop']) ? $params['noSalesShop'] : ''; //禁止销售店铺
-
- //编辑传参
- $storage = isset($params['storage']) ? $params['storage'] : '';//货架编码
- $goodsBasicData['extends'] = json_encode(['storage' => $storage]);//货架编码
- $goodsBasicData['delUnitIds'] = isset($params['delUnitIds']) ? $params['delUnitIds'] : [];//删除计量单位ids
- $goodsBasicData['delSpecSkuIds'] = isset($params['delSpecSkuIds']) ? $params['delSpecSkuIds'] : [];//删除属性值
- $goodsBasicData['merchantId'] = isset($params['merchantId']) ? $params['merchantId'] : 0;//接收商户id
- !empty($this->supplierId) && $goodsBasicData['supplierId'] = $this->supplierId;
- $goodsBasic[] = $goodsBasicData;
- }
- unset($goodsBasicData);
- $total = 0;
- $true = 0;
- $false = 0;
- foreach ($goodsBasic as $key => $goodsBasicData) {
-
- unset($goodsBasicData['delUnitIds']);
- unset($goodsBasicData['delSpecSkuIds']);
-
- switch ($goodsBasicData['specType']) {
- case StatusCode::$specType['single']:
- //没有属性的
- $result = $this->objMGoodsBasic->addGoodsBasic($goodsBasicData);//单规格
- break;
- case StatusCode::$specType['multiple']:
- //有属性的
- $result = $this->objMGoodsBasic->addMultipleSpecGoodsBasic($goodsBasicData);//有属性的
- break;
- }
- if (!$result->isSuccess()) {
- $false++;
- } else {
- $true++;
- }
- $total++;
- }
- $return = "共导入'$total'条商品,成功'$true'条商品,失败'$false'条商品";
- parent::sendOutput($return);
- }
- }
|