TRUE /** * QuickGoods constructor. * @param bool $isCheckAcl * @param bool $isMustLogin * @throws \Exception */ public function __construct($isCheckAcl = true, $isMustLogin = true) { parent::__construct($isCheckAcl, $isMustLogin); $this->objMQuickGoods = new MQuickGoods($this->onlineEnterpriseId,$this->onlineUserId); } /** * 但店铺,查询默认店铺id,name */ public function _initShop() { if ($this->isStore){ $objMShop = new MShop($this->onlineEnterpriseId,$this->onlineUserId); $shopName = $objMShop->getShopNameById($this->shopId); $this->shopName = $shopName; }else{ $shopResult = $this->objMQuickGoods->getShopData(); if (!$shopResult->isSuccess()){ parent::sendOutput($shopResult->getData(), ErrorCode::$dberror); } $shopData = $shopResult->getData(); if (empty($shopData)){ parent::sendOutput('请先创建店铺',ErrorCode::$paramError); } $this->shopId = isset($shopData['id']) ? $shopData['id'] : 0; $this->shopName = isset($shopData['name']) ? $shopData['name'] : ''; return $shopData; } } /** * 公共字段 * * @return array */ public function commonFieldFilter() { $params = $this->request->getRawJson(); if (empty($params)) { $this->sendOutput('参数为空', ErrorCode::$paramError); } ( isset($params['isStore']) && $params['isStore'] == true ) && $this->isStore = true; self::_initShop(); $data = [ '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, 'specMultiple' => isset($params['specMultiple']) ? $params['specMultiple'] : null, 'shopId' => $this->shopId,//商铺id 'shopName' => $this->shopName,//商铺名称 ]; foreach ($data as $key => $value) { if (empty($value)) { parent::sendOutput($key . '参数错误', ErrorCode::$paramError); } } foreach ($data['specMultiple'] as $item){ if ($item['isMaster']==StatusCode::$delete){ if (empty((int) $item['conversion'])){ parent::sendOutput('辅单位换算比例有误', ErrorCode::$paramError); } } } //商品名称转换搜索条件 if(isset($data['title'])){ $objChineseCharacter = new ChineseCharacter(); $data['condition'] = $objChineseCharacter->getInitials(trim($data['title'])); } $data['isStore'] = $this->isStore; $data['specGroup'] = isset($params['specGroup']) ? json_encode($params['specGroup']) : [];//规格组 $data['brandId'] = isset($params['brandId']) ? $params['brandId'] : 0; $data['description'] = isset($params['description']) ? $params['description'] : ''; $data['describe'] = isset($params['describe']) ? $params['describe'] : ''; $data['expireTime'] = isset($params['expireTime']) ? $params['expireTime'] : 0; $data['tag'] = isset($params['tag']) ? $params['tag'] : ''; $data['barCode'] = isset($params['barCode']) ? $params['barCode'] : '';//条码 $data['link'] = isset($params['link']) ? $params['link'] : ''; $data['noSalesShop'] = isset($params['noSalesShop']) ? $params['noSalesShop'] : ''; //禁止销售店铺 $data['assistCategoryId'] = isset($params['assistCategoryId']) ? $params['assistCategoryId'] : ''; $data['assistCategoryPath'] = isset($params['assistCategoryPath']) ? json_encode($params['assistCategoryPath']) : []; //商品 $data['sort'] = isset($params['sort']) ? $params['sort'] : 9999;//商品排序 $data['enableStatus'] = isset($params['enableStatus']) ? $params['enableStatus'] : StatusCode::$delete; $data['createUserName'] = isset($params['createUserName']) ? $params['createUserName'] : ''; $storage = isset($params['storage']) ? $params['storage'] : '';//货架编码 $data['extends'] = json_encode(['storage'=>$storage]);//货架编码 isset($params['deliverySupIds']) && $data['deliverySupIds'] = $params['deliverySupIds']; isset($params['expressType']) && $data['expressType'] = $params['expressType']; isset($params['ruleId']) && $data['ruleId'] = $params['ruleId']; isset($params['expressFee']) && $data['expressFee'] = $params['expressFee']; isset($params['showExpress']) && $data['showExpress'] = $params['showExpress']; (isset($this->supplierId) && !empty($this->supplierId)) && $data['supplierId'] = $this->supplierId; $data['isEq'] = isset($params['isEq']) ? $params['isEq'] : null; $data['merchantId'] = getArrayItem($params, 'merchantId', 0); $data['isShield'] = getArrayItem($params,'isShield',StatusCode::$delete); $data['notArea'] = json_encode(getArrayItem($params,'notArea',null)); $data['notCustomerType'] = getArrayItem($params,'notCustomerType',''); $data['notCustomer'] = getArrayItem($params,'notCustomer',''); $data['isDistribution'] = getArrayItem($params,'isDistribution',StatusCode::$delete); $data['createUserName'] = getArrayItem($params,'createUserName',''); return $data; } /** * 添加商品基础资料 * @throws \Exception */ public function addBasicAndPublishGoods() { $data = self::commonFieldFilter(); //添加商品基础资料 $result = $this->objMQuickGoods->addBasicAndPublishGoods($data); if ($result->isSuccess()) { parent::sendOutput($result->getData()); } parent::sendOutput($result->getData(), $result->getErrorCode()); } /** * 商品及商品基础资料详情 * @throws \Exception */ public function getQuickGoodsInfo() { $goodsId = $this->request->param('request_id'); if (!$goodsId) { $this->sendOutput('参数错误', ErrorCode::$paramError); } $result = $this->objMQuickGoods->getQuickGoodsInfo($goodsId); if ($result->isSuccess()) { $resultData = $result->getData(); $this->sendOutput($resultData); } $this->sendOutput($result->getData(), $result->getErrorCode()); } /** * 编辑 * @throws \Exception */ public function editQuickGoods() { $data = self::commonFieldFilter(); $params = $this->request->getRawJson(); if (!isset($params['id']) || empty($params['id'])){ parent::sendOutput('id参数错误', ErrorCode::$paramError); } if (!isset($params['basicGoodsId']) || empty($params['basicGoodsId'])){ parent::sendOutput('basicGoodsId参数错误', ErrorCode::$paramError); } $result = $this->objMQuickGoods->editQuickGoods($data,$params['id'],$params['basicGoodsId']); if ($result->isSuccess()) { parent::sendOutput($result->getData()); } parent::sendOutput($result->getData(), $result->getErrorCode()); } /** * 接收导入数据参数 * @throws \Exception */ public function getGoodsQuickImportParams() { //获取数据文件 $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; //商品信息 $goodsName = isset($goodsArray['goodsName']) ? trim($goodsArray['goodsName']) : '';//商品名称 $describe = isset($goodsArray['describe']) ? trim($goodsArray['describe']) : '';//商品卖点 $categoryName = isset($goodsArray['categoryName']) ? trim($goodsArray['categoryName']) : ''; //分类名称 $categoryTwo = isset($goodsArray['categoryTwo']) ? trim($goodsArray['categoryTwo']) : '';//二级分类名称 $categoryThree = isset($goodsArray['categoryThree']) ? trim($goodsArray['categoryThree']) : '';//三级分类名称 // //商户名称 $merchantName = isset($goodsArray['merchantName']) ? trim($goodsArray['merchantName']) : '';//商户名称 // $brandName = isset($goodsArray['brandName']) ? trim($goodsArray['brandName']) : '';//品牌名称 $isEq = isset($goodsArray['isEq']) ? trim($goodsArray['isEq']) : 4;//是否抄码 $description = isset($goodsArray['description']) ? '

' . trim($goodsArray['description']) . '

' : '';//商品详情 //主单位信息 $skuName = isset($goodsArray['skuName']) ? trim($goodsArray['skuName']) : '无';//单位名称 $salePrice = isset($goodsArray['salePrice']) ? trim($goodsArray['salePrice']) : 1;//销售价 $memberPrice = isset($goodsArray['memberPrice']) ? trim($goodsArray['memberPrice']) : 1;//会员价 $barCode = isset($goodsArray['barCode']) ? trim($goodsArray['barCode']) : '';//主单位条码 $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']) : '';//辅单位条码 $salePriceTwo = isset($goodsArray['salePriceTwo']) ? trim($goodsArray['salePriceTwo']) : 1;//辅单位销售价格 $conversion = isset($goodsArray['conversion']) ? trim($goodsArray['conversion']) : 0;//辅单位换算比例 $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 = !empty($dbResult['id']) ? empty($dbResult['id']) : 0 ; 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 = ['id']; if(!empty($category['link'])){ $categoryPath = $category['link'] . ',' . $category['id']; }else{ $categoryPath = $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']; if(!empty($category['link'])){ $categoryPath = $category['link'] . ',' . $category['id']; }else{ $categoryPath = $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']; if(!empty($category['link'])){ $categoryPath = $category['link'] . ',' . $category['id']; }else{ $categoryPath = $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']; } } $postData = [ 'merchantId' => $merchantId, 'categoryPath' => $categoryPath, 'categoryId' => $categoryId, 'assistCategoryPath' =>[],//商品辅助的单位 'assistCategoryId' => '',//商品辅助的单位id 'title' => $goodsName, 'storage' => '', 'delUnitIds' =>[], 'delSpecSkuIds' =>[], 'specType' => 1, 'createUserName' => '', 'specGroup' =>[], 'specMultiple' => [ [ 'barCode' => $barCode, 'weight' => '', 'isDefault' => 5, 'unitId' =>$skuIdTwo, 'unitName' => $skuName, 'isMaster' => 5, 'conversion' => $conversion, 'specImage' => '', 'specGroup' =>[], 'salePrice' => [ 'conversion' => $conversion, 'unitName' => $skuName, 'unitId' => $skuId, 'isMaster' => 5, 'deleteStatus' => 4, 'enabledLadder' => 0, 'salePriceAreaType' => 1, 'salePrice' => $salePrice, 'memberPrice'=> $memberPrice, 'ladderPrice' =>[], 'marketPrice' =>$salePriceTrue, 'setNum' => 1, ], 'customerTypePrice' =>[], 'customerPrice' =>[], ], ], 'unitData' => [ [ 'barCode' => $barCode, 'weight' => '', 'isMaster' => 5, 'isDefault' => 5, 'unitName' => $skuName, 'unitId' => $skuId, ], ], 'describe' => $describe, 'code' => '', 'barCode' => $barCode, 'weight' => '', 'expireTime' => '', 'brandId' => $brandId, 'tag' => '', 'description' => $description, 'noSalesShop' => '', 'images' =>$images, 'enableStatus' => 5, 'isEq' => $isEq, 'isDistribution' => 4, 'isShield' => 4, 'deliverySupIds' => '', 'expressType' => 1, 'expressFee' => '', 'showExpress' => '', 'ruleId' => '', 'notArea' =>[], 'notCustomerType' => '', 'notCustomer' => '', 'isStore' => '', ]; $postArray[] = $postData; } return $postArray; } /** * 单商铺导入商品基础数据 * @throws \Exception */ public function goodsQuickImport() { //把页面传来的参数对应到符合的字段中全部传递过来; $paramsArray = self::getGoodsQuickImportParams(); if (empty($paramsArray)) { $this->sendOutput('参数为空', ErrorCode::$paramError); } $goodsBasic = []; foreach ($paramsArray as $params) { ( isset($params['isStore']) && $params['isStore'] == true ) && $this->isStore = true; self::_initShop(); $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, 'specMultiple' => isset($params['specMultiple']) ? $params['specMultiple'] : null, 'shopId' => $this->shopId,//商铺id 'shopName' => $this->shopName,//商铺名称 ]; foreach ($goodsBasicData as $key => $value) { if (empty($value)) { parent::sendOutput($key . '参数错误', ErrorCode::$paramError); } } foreach ($goodsBasicData['specMultiple'] as $item){ if ($item['isMaster']==StatusCode::$delete){ if (empty((int) $item['conversion'])){ parent::sendOutput('辅单位换算比例有误', ErrorCode::$paramError); } } } //商品名称转换搜索条件 if(isset($goodsBasicData['title'])){ $objChineseCharacter = new ChineseCharacter(); $goodsBasicData['condition'] = $objChineseCharacter->getInitials(trim($goodsBasicData['title'])); } $goodsBasicData['isStore'] = $this->isStore; $goodsBasicData['specGroup'] = isset($params['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['memberPrice'] = isset($params['memberPrice']) ? $params['memberPrice'] : 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']) : []; //商品 $goodsBasicData['sort'] = isset($params['sort']) ? $params['sort'] : 9999;//商品排序 $goodsBasicData['enableStatus'] = isset($params['enableStatus']) ? $params['enableStatus'] : StatusCode::$delete; $goodsBasicData['createUserName'] = isset($params['createUserName']) ? $params['createUserName'] : ''; $storage = isset($params['storage']) ? $params['storage'] : '';//货架编码 $goodsBasicData['extends'] = json_encode(['storage'=>$storage]);//货架编码 isset($params['deliverySupIds']) && $goodsBasicData['deliverySupIds'] = $params['deliverySupIds']; isset($params['expressType']) && $goodsBasicData['expressType'] = $params['expressType']; isset($params['ruleId']) && $goodsBasicData['ruleId'] = $params['ruleId']; isset($params['expressFee']) && $goodsBasicData['expressFee'] = $params['expressFee']; isset($params['showExpress']) && $goodsBasicData['showExpress'] = $params['showExpress']; (isset($this->supplierId) && !empty($this->supplierId)) && $goodsBasicData['supplierId'] = $this->supplierId; $goodsBasicData['isEq'] = isset($params['isEq']) ? $params['isEq'] : null; $goodsBasicData['merchantId'] = getArrayItem($params, 'merchantId', 0); $goodsBasicData['isShield'] = getArrayItem($params,'isShield',StatusCode::$delete); $goodsBasicData['notArea'] = json_encode(getArrayItem($params,'notArea',null)); $goodsBasicData['notCustomerType'] = getArrayItem($params,'notCustomerType',''); $goodsBasicData['notCustomer'] = getArrayItem($params,'notCustomer',''); $goodsBasicData['isDistribution'] = getArrayItem($params,'isDistribution',StatusCode::$delete); $goodsBasicData['createUserName'] = getArrayItem($params,'createUserName',''); $goodsBasic[] = $goodsBasicData; } unset($goodsBasicData); $total = 0; $true = 0; $false = 0; foreach ($goodsBasic as $key => $goodsBasicData) { $result = $this->objMQuickGoods->addBasicAndPublishGoods($goodsBasicData); if (!$result->isSuccess()) { $false++; } else { $true++; } $total++; } $return = "共导入'$total'条商品,成功'$true'条商品,失败'$false'条商品"; parent::sendOutput($return); } }