GoodsBasic.Class.php 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081
  1. <?php
  2. /**
  3. * 商品基础信息管理
  4. * Created by PhpStorm.
  5. * User: XiaoMing
  6. * Date: 2019/10/30
  7. * Time: 14:17
  8. */
  9. namespace JinDouYun\Controller\GoodsManage;
  10. use JinDouYun\Dao\GoodsCategory\DGoodsCategory;
  11. use JinDouYun\Dao\GoodsManage\DGoodsBrand;
  12. use JinDouYun\Dao\GoodsManage\DUnits;
  13. use JinDouYun\Dao\Merchant\DMerchantApply;
  14. use Mall\Framework\Core\ErrorCode;
  15. use Mall\Framework\Core\ResultWrapper;
  16. use Mall\Framework\Core\StatusCode;
  17. use JinDouYun\Model\GoodsManage\MSku;
  18. use JinDouYun\Model\GoodsManage\MGoodsBasic;
  19. use JinDouYun\Controller\BaseController;
  20. use Util\Common\ChineseCharacter;
  21. class GoodsBasic extends BaseController
  22. {
  23. /**
  24. * @var MGoodsBasic
  25. */
  26. private $objMGoodsBasic;
  27. /**
  28. * GoodsBasic constructor.
  29. * @param bool $isCheckAcl
  30. * @param bool $isMustLogin
  31. * @throws \Exception
  32. */
  33. public function __construct($isCheckAcl = true, $isMustLogin = true)
  34. {
  35. parent::__construct($isCheckAcl, $isMustLogin);
  36. $this->objMGoodsBasic = new MGoodsBasic($this->onlineUserId, $this->onlineEnterpriseId);
  37. }
  38. /**
  39. * 添加和编辑商品基础信息的公共字段
  40. *
  41. * @return array
  42. */
  43. public function commonFieldFilter()
  44. {
  45. $params = $this->request->getRawJson();
  46. if (empty($params)) {
  47. $this->sendOutput('参数为空', ErrorCode::$paramError);
  48. }
  49. $goodsBasicData = [
  50. 'title' => isset($params['title']) ? $params['title'] : '',
  51. 'categoryId' => isset($params['categoryId']) ? $params['categoryId'] : 0,
  52. 'categoryPath' => isset($params['categoryPath']) ? $params['categoryPath'] : '',
  53. 'images' => isset($params['images']) ? json_encode($params['images']) : [],//商品图册json
  54. 'unitData' => isset($params['unitData']) ? $params['unitData'] : '',//商品单位
  55. 'specType' => isset($params['specType']) ? $params['specType'] : '',
  56. 'isEq' => isset($params['isEq']) ? $params['isEq'] : StatusCode::$delete,
  57. ];
  58. foreach ($goodsBasicData as $key => $value) {
  59. if (empty($value) && $value !== 0) {
  60. $this->sendOutput($key . '参数错误', ErrorCode::$paramError);
  61. }
  62. }
  63. //商品名称转换搜索条件
  64. if (isset($goodsBasicData['title'])) {
  65. $objChineseCharacter = new ChineseCharacter();
  66. $goodsBasicData['condition'] = $objChineseCharacter->getInitials(trim($goodsBasicData['title']));
  67. }
  68. if ($goodsBasicData['specType'] == StatusCode::$specType['multiple']) {
  69. //带属性的
  70. if (!isset($params['specMultiple']) || empty($params['specMultiple'])) {
  71. parent::sendOutput('specMultiple参数错误', ErrorCode::$paramError);
  72. }
  73. if (!isset($params['specGroup']) || empty($params['specGroup'])) {
  74. parent::sendOutput('specGroup参数错误', ErrorCode::$paramError);
  75. }
  76. $goodsBasicData['specMultiple'] = $params['specMultiple'];//规格明细
  77. }
  78. $goodsBasicData['specGroup'] = json_encode($params['specGroup']);//规格组
  79. $goodsBasicData['brandId'] = isset($params['brandId']) ? $params['brandId'] : 0;
  80. $goodsBasicData['description'] = isset($params['description']) ? $params['description'] : '';
  81. $goodsBasicData['describe'] = isset($params['describe']) ? $params['describe'] : '';
  82. $goodsBasicData['expireTime'] = isset($params['expireTime']) ? $params['expireTime'] : 0;
  83. $goodsBasicData['tag'] = isset($params['tag']) ? $params['tag'] : '';
  84. $goodsBasicData['barCode'] = isset($params['barCode']) ? $params['barCode'] : '';//条码
  85. $goodsBasicData['link'] = isset($params['link']) ? $params['link'] : '';
  86. $goodsBasicData['noSalesShop'] = isset($params['noSalesShop']) ? $params['noSalesShop'] : ''; //禁止销售店铺
  87. $goodsBasicData['assistCategoryId'] = isset($params['assistCategoryId']) ? $params['assistCategoryId'] : '';
  88. $goodsBasicData['assistCategoryPath'] = isset($params['assistCategoryPath']) ? json_encode($params['assistCategoryPath']) : [];
  89. //编辑传参
  90. $storage = isset($params['storage']) ? $params['storage'] : '';//货架编码
  91. $goodsBasicData['extends'] = json_encode(['storage' => $storage]);//货架编码
  92. $goodsBasicData['delUnitIds'] = isset($params['delUnitIds']) ? $params['delUnitIds'] : [];//删除计量单位ids
  93. $goodsBasicData['delSpecSkuIds'] = isset($params['delSpecSkuIds']) ? $params['delSpecSkuIds'] : [];//删除属性值
  94. $goodsBasicData['merchantId'] = isset($params['merchantId']) ? $params['merchantId'] : 0;//接收商户id
  95. !empty($this->supplierId) && $goodsBasicData['supplierId'] = $this->supplierId;
  96. return $goodsBasicData;
  97. }
  98. /**
  99. * 添加商品基础数据
  100. * @throws \Exception
  101. */
  102. public function addGoodsBasic()
  103. {
  104. $goodsBasicData = $this->commonFieldFilter();
  105. unset($goodsBasicData['delUnitIds']);
  106. unset($goodsBasicData['delSpecSkuIds']);
  107. $result = false;
  108. switch ($goodsBasicData['specType']) {
  109. case StatusCode::$specType['single']:
  110. //没有属性的
  111. $result = $this->objMGoodsBasic->addGoodsBasic($goodsBasicData);//单规格
  112. break;
  113. case StatusCode::$specType['multiple']:
  114. //有属性的
  115. $result = $this->objMGoodsBasic->addMultipleSpecGoodsBasic($goodsBasicData);//有属性的
  116. break;
  117. }
  118. if ($result->isSuccess()) {
  119. parent::sendOutput($result->getData());
  120. }
  121. parent::sendOutput($result->getData(), $result->getErrorCode());
  122. }
  123. /**
  124. * 获取全部商品基础资料列表
  125. * @throws \Exception
  126. */
  127. public function getAllGoodsBasic()
  128. {
  129. $page = $this->request->param('page') ?: 1;
  130. $pageSize = $this->request->param('pageSize') ?: 10;
  131. $enableStatus = $this->request->param('enableStatus') ?: '';
  132. $export = $this->request->param('export') ?: '';
  133. $merchantId = $this->request->param('merchantId');
  134. $offset = ($page - 1) * $pageSize;
  135. $selectParams = [
  136. 'limit' => $pageSize,
  137. 'offset' => $offset,
  138. ];
  139. if (!empty($merchantId)) {
  140. $selectParams['merchantId'] = $merchantId;
  141. }
  142. if (!empty($enableStatus)) $selectParams['enableStatus'] = $enableStatus;
  143. $is_export = false;
  144. !empty($export) && $is_export = true;
  145. !empty($this->shopId) && $selectParams['level'] = StatusCode::$standard;//商户后台只能看到平台商品
  146. !empty($this->supplierId) && $selectParams['supplierId'] = $this->supplierId;
  147. $result = $this->objMGoodsBasic->getAllGoodsBasic($selectParams, $is_export);
  148. if ($result->isSuccess()) {
  149. $returnData = $result->getData();
  150. $pageData = [
  151. 'pageIndex' => $page,
  152. 'pageSize' => $pageSize,
  153. 'pageTotal' => $returnData['total'],
  154. ];
  155. parent::sendOutput($returnData['data'], 0, $pageData);
  156. } else {
  157. parent::sendOutput($result->getData(), ErrorCode::$dberror);
  158. }
  159. }
  160. /**
  161. * 搜索基本资料
  162. * @throws \Exception
  163. */
  164. public function search()
  165. {
  166. $params = $this->request->getRawJson();
  167. if (empty($params)) {
  168. $this->sendOutput('参数为空', ErrorCode::$paramError);
  169. }
  170. $selectParams = [
  171. 'merchantId' => isset($params['merchantId']) ? $params['merchantId'] : '',
  172. 'categoryPath' => isset($params['categoryPath']) ? $params['categoryPath'] : '',
  173. 'categoryId' => isset($params['categoryId']) ? $params['categoryId'] : '',
  174. 'brandId' => isset($params['brandId']) ? $params['brandId'] : '',
  175. 'keyword' => isset($params['keyword']) ? $params['keyword'] : '',
  176. 'enableStatus' => isset($params['enableStatus']) ? $params['enableStatus'] : '',//销售状态
  177. 'deleteStatus' => StatusCode::$standard,
  178. ];
  179. $pageParams = pageToOffset($params['page'] ?: 1, $params['pageSize'] ?: 10);
  180. $selectParams['limit'] = $pageParams['limit'];
  181. $selectParams['offset'] = $pageParams['offset'];
  182. $is_export = false;
  183. if (isset($params['export']) && $params['export'] == 1) $is_export = true;
  184. !empty($this->shopId) && $selectParams['level'] = StatusCode::$standard;//商户后台只能看到平台商品
  185. !empty($this->supplierId) && $selectParams['supplierId'] = $this->supplierId;
  186. $result = $this->objMGoodsBasic->search($selectParams, $is_export);
  187. if ($result->isSuccess()) {
  188. $returnData = $result->getData();
  189. $pageData = [
  190. 'pageIndex' => $params['page'],
  191. 'pageSize' => $params['pageSize'],
  192. 'pageTotal' => $returnData['total'],
  193. ];
  194. parent::sendOutput($returnData['data'], 0, $pageData);
  195. } else {
  196. parent::sendOutput($result->getData(), $result->getErrorCode());
  197. }
  198. }
  199. /**
  200. * 获取指定id的商品基础资料详情
  201. * @throws \Exception
  202. */
  203. public function getGoodsBasicInfoById()
  204. {
  205. $id = $this->request->param('request_id');
  206. $params = $this->request->getRawJson();
  207. $isAddGoods = isset($params['isAddGoods']) ? $params['isAddGoods'] : StatusCode::$delete;//添加商品获取详情
  208. if (!$id) {
  209. $this->sendOutput('参数错误,id', ErrorCode::$paramError);
  210. }
  211. $result = $this->objMGoodsBasic->getGoodsBasicInfoById($id, $isAddGoods);
  212. if ($result->isSuccess()) {
  213. $resultData = $result->getData();
  214. $this->sendOutput($resultData);
  215. }
  216. $this->sendOutput($result->getData(), $result->getErrorCode());
  217. }
  218. /**
  219. * 编辑基础商品数据
  220. * @throws \Exception
  221. */
  222. public function editGoodsBasic()
  223. {
  224. $id = $this->request->param('request_id');
  225. if (empty($id)) {
  226. $this->sendOutput('参数错误id', ErrorCode::$paramError);
  227. }
  228. $goodsBasicData = $this->commonFieldFilter();
  229. $goodsBasicData['id'] = $id;
  230. $result = false;
  231. switch ($goodsBasicData['specType']) {
  232. case StatusCode::$specType['single']:
  233. //无属性
  234. $result = $this->objMGoodsBasic->editGoodsBasic($goodsBasicData);
  235. break;
  236. case StatusCode::$specType['multiple']:
  237. //有属性
  238. $result = $this->objMGoodsBasic->editMultipleGoodsBasic($goodsBasicData);
  239. break;
  240. }
  241. if ($result->isSuccess()) {
  242. parent::sendOutput($result->getData());
  243. }
  244. parent::sendOutput($result->getData(), $result->getErrorCode());
  245. }
  246. /**
  247. * 更新基础商品的启用/禁用
  248. * @throws \Exception
  249. */
  250. public function updateEnableStatus()
  251. {
  252. $paramsData = $this->request->getRawJson();
  253. if (empty($paramsData['id'])) {
  254. $this->sendOutput('参数错误id', ErrorCode::$paramError);
  255. }
  256. $result = $this->objMGoodsBasic->updateEnableStatus($paramsData);
  257. if ($result->isSuccess()) {
  258. parent::sendOutput($result->getData());
  259. }
  260. parent::sendOutput($result->getData(), $result->getErrorCode());
  261. }
  262. /**
  263. * Doc: (des="设置为平台商品")
  264. * User: XMing
  265. * Date: 2020/12/7
  266. * Time: 12:01 下午
  267. */
  268. public function pushPlatform()
  269. {
  270. $id = $this->request->param('request_id');
  271. if (!$id) {
  272. $this->sendOutput('参数错误', ErrorCode::$paramError);
  273. }
  274. $result = $this->objMGoodsBasic->pushPlatform($id);
  275. if ($result->isSuccess()) {
  276. parent::sendOutput($result->getData());
  277. }
  278. parent::sendOutput($result->getData(), $result->getErrorCode());
  279. }
  280. /**
  281. * 删除基础商品资料
  282. * @throws \Exception
  283. */
  284. public function delGoodsBasic()
  285. {
  286. $id = $this->request->param('request_id');
  287. if (!$id) {
  288. $this->sendOutput('参数错误', ErrorCode::$paramError);
  289. }
  290. $result = $this->objMGoodsBasic->delGoodsBasic($id);
  291. if ($result->isSuccess()) {
  292. parent::sendOutput($result->getData());
  293. } else {
  294. parent::sendOutput($result->getData(), $result->getErrorCode());
  295. }
  296. }
  297. /**
  298. * 调整基础商品资料的分类
  299. */
  300. public function updateCategory()
  301. {
  302. $params = $this->request->getRawJson();
  303. $paramsData = [
  304. 'id' => isset($params['id']) ? $params['id'] : '',
  305. 'categoryId' => isset($params['categoryId']) ? $params['categoryId'] : '',
  306. 'categoryPath' => isset($params['categoryPath']) ? $params['categoryPath'] : '',
  307. ];
  308. foreach ($paramsData as $key => $value) {
  309. if (empty($value)) {
  310. $this->sendOutput($key . '参数错误', ErrorCode::$paramError);
  311. }
  312. }
  313. $result = $this->objMGoodsBasic->updateCategory($paramsData);
  314. if ($result->isSuccess()) {
  315. parent::sendOutput($result->getData());
  316. }
  317. parent::sendOutput($result->getData(), $result->getErrorCode());
  318. }
  319. /**
  320. * 获取商铺id下允许销售的产品
  321. * @throws \Exception
  322. */
  323. public function getGoodsBasicOfShopId()
  324. {
  325. $params = $this->request->getRawJson();
  326. if (empty($params)) {
  327. $this->sendOutput('参数为空', ErrorCode::$paramError);
  328. }
  329. $pageParams = pageToOffset($params['page'] ?: 1, $params['pageSize'] ?: 10);
  330. $selectParams['limit'] = $pageParams['limit'];
  331. $selectParams['offset'] = $pageParams['offset'];
  332. if (!isset($params['shopId']) || empty($params['shopId'])) {
  333. $this->sendOutput('shopId参数为空', ErrorCode::$paramError);
  334. }
  335. $selectParams['shopId'] = $params['shopId'];
  336. if (isset($params['keyword']) && !empty($params['keyword'])) {
  337. $selectParams['title'] = $params['keyword'];
  338. }
  339. if (isset($params['categoryId']) && !empty($params['categoryId'])) {
  340. $selectParams['categoryId'] = $params['categoryId'];
  341. }
  342. $result = $this->objMGoodsBasic->getGoodsBasicOfShopId($selectParams);
  343. if ($result->isSuccess()) {
  344. $returnData = $result->getData();
  345. $pageData = [
  346. 'pageIndex' => $params['page'],
  347. 'pageSize' => $params['pageSize'],
  348. 'pageTotal' => $returnData['total'],
  349. ];
  350. parent::sendOutput($returnData['data'], 0, $pageData);
  351. } else {
  352. $this->sendOutput($result->getData(), $result->getErrorCode());
  353. }
  354. }
  355. /**
  356. * 批量设置不可销售店铺
  357. */
  358. public function setNoSalesShop()
  359. {
  360. $params = $this->request->getRawJson();
  361. $paramsData = [
  362. 'id' => isset($params['id']) ? $params['id'] : '',//ids
  363. 'noSalesShop' => isset($params['noSalesShop']) ? $params['noSalesShop'] : '',
  364. ];
  365. foreach ($paramsData as $key => $value) {
  366. if (empty($value)) {
  367. $this->sendOutput($key . '参数错误', ErrorCode::$paramError);
  368. }
  369. }
  370. $result = $this->objMGoodsBasic->setNoSalesShop($paramsData);
  371. if ($result->isSuccess()) {
  372. parent::sendOutput($result->getData());
  373. }
  374. parent::sendOutput($result->getData(), $result->getErrorCode());
  375. }
  376. /**
  377. * 批量设置品牌
  378. */
  379. public function setBrand()
  380. {
  381. $params = $this->request->getRawJson();
  382. $paramsData = [
  383. 'id' => isset($params['id']) ? $params['id'] : '',//ids
  384. 'brandId' => isset($params['brandId']) ? $params['brandId'] : '',
  385. ];
  386. foreach ($paramsData as $key => $value) {
  387. if (empty($value)) {
  388. $this->sendOutput($key . '参数错误', ErrorCode::$paramError);
  389. }
  390. }
  391. $result = $this->objMGoodsBasic->setBrand($paramsData);
  392. if ($result->isSuccess()) {
  393. parent::sendOutput($result->getData());
  394. }
  395. parent::sendOutput($result->getData(), $result->getErrorCode());
  396. }
  397. /**
  398. * 批量设置sku辅助单位
  399. * @throws \Exception
  400. */
  401. public function setSku()
  402. {
  403. $params = $this->request->getRawJson();
  404. $paramsData = [
  405. 'goodsId' => isset($params['id']) ? $params['id'] : '',//ids
  406. 'branchData' => isset($params['branchData']) ? $params['branchData'] : '',
  407. ];
  408. foreach ($paramsData as $k => $v) {
  409. if (empty($v) && $v !== 0) {
  410. $this->sendOutput($k . '参数错误', ErrorCode::$paramError);
  411. }
  412. }
  413. $sheetData = [];
  414. foreach ($params['branchData'] as $key => $value) {
  415. $sheetData[$key] = [
  416. 'conversion' => isset($value['conversion']) ? $value['conversion'] : '',
  417. 'unitName' => isset($value['unitName']) ? $value['unitName'] : '',
  418. 'unitId' => isset($value['unitId']) ? $value['unitId'] : '',
  419. 'isMaster' => StatusCode::$delete,
  420. ];
  421. foreach ($sheetData[$key] as $k => $v) {
  422. if (empty($v) && $v !== 0) {
  423. $this->sendOutput($k . '参数错误', ErrorCode::$paramError);
  424. }
  425. }
  426. }
  427. $paramsData['branchData'] = $sheetData;
  428. $objMSku = new MSku($this->onlineUserId, $this->onlineEnterpriseId);
  429. $result = $objMSku->setSku($paramsData);
  430. if ($result->isSuccess()) {
  431. parent::sendOutput($result->getData());
  432. }
  433. parent::sendOutput($result->getData(), $result->getErrorCode());
  434. }
  435. //修补es数据
  436. public function fixEsData()
  437. {
  438. $params = $this->request->getRawJson();
  439. if (empty($params)) {
  440. $this->sendOutput('参数为空', ErrorCode::$paramError);
  441. }
  442. isset($params['id']) && $selectParams['id'] = $params['id'];
  443. $pageParams = pageToOffset(1, 4000);
  444. $selectParams['limit'] = $pageParams['limit'];
  445. $selectParams['offset'] = $pageParams['offset'];
  446. $this->objMGoodsBasic->fixEsData($selectParams);
  447. }
  448. /**
  449. * 按sku查询的基础商品列表
  450. */
  451. public function getAllGoodsBasicBySku()
  452. {
  453. $params = $this->request->getRawJson();
  454. if (empty($params)) {
  455. $this->sendOutput('参数为空', ErrorCode::$paramError);
  456. }
  457. $data = [
  458. 'isRevealSku' => isset($params['isRevealSku']) ? $params['isRevealSku'] : '',
  459. ];
  460. foreach ($data as $key => $val) {
  461. if (empty($val)) {
  462. parent::sendOutput($key . '参数错误', ErrorCode::$paramError);
  463. }
  464. }
  465. $data['materielId'] = getArrayItem($params, 'materielId', 0);
  466. $data['keyword'] = isset($params['keyword']) ? $params['keyword'] : '';
  467. $data['categoryId'] = isset($params['categoryId']) ? $params['categoryId'] : '';
  468. $pageParams = pageToOffset(isset($params['page']) ? $params['page'] : 1, isset($params['pageSize']) ? $params['pageSize'] : 10);
  469. $data['limit'] = $pageParams['limit'];
  470. $data['offset'] = $pageParams['offset'];
  471. if (isset($this->shopId)) {
  472. $data['shopId'] = $this->shopId;
  473. }
  474. if (isset($params['ifMerchant']) && !empty($params['ifMerchant'])) {
  475. $data['ifMerchant'] = $params['ifMerchant'];
  476. }
  477. if (isset($params['merchantId']) && !empty($params['merchantId'])) {
  478. $data['merchantId'] = $params['merchantId'];
  479. }
  480. $result = $this->objMGoodsBasic->getAllGoodsBasicBySku($data);
  481. if ($result->isSuccess()) {
  482. $returnData = $result->getData();
  483. $pageData = [
  484. 'pageIndex' => isset($params['page']) ? $params['page'] : 1,
  485. 'pageSize' => isset($params['pageSize']) ? $params['pageSize'] : 10,
  486. 'pageTotal' => $returnData['total'],
  487. ];
  488. parent::sendOutput($returnData['data'], 0, $pageData);
  489. }
  490. parent::sendOutput($result->getData(), $result->getErrorCode());
  491. }
  492. /**
  493. * 接收导入数据参数
  494. * @throws \Exception
  495. */
  496. public function getGoodsBasicImportParams()
  497. {
  498. //获取数据文件
  499. $params = $this->request->getRawJson();
  500. //企业id
  501. $enterpriseId = $this->onlineEnterpriseId;
  502. //商品图片
  503. $images = [];
  504. //多单位
  505. $twoUnit = true;
  506. //引用dao
  507. $objDGoodsCategory = new DGoodsCategory();
  508. $objDGoodsCategory->setTable('qianniao_goods_category_' . $enterpriseId);
  509. $objDGoodsBrand = new DGoodsBrand();
  510. $objDGoodsBrand->setTable('qianniao_goods_brand_' . $enterpriseId);
  511. $objDUnits = new DUnits();
  512. $objDUnits->setTable('qianniao_units_' . $enterpriseId);
  513. $objDMerchantApply = new DMerchantApply();
  514. //获取数据
  515. $postArray = [];
  516. foreach ($params as $value) {
  517. $goodsArray = $value;
  518. //商品信息
  519. $categoryName = isset($goodsArray['categoryName']) ? trim($goodsArray['categoryName']) : '';//分类名称
  520. $categoryTwo = isset($goodsArray['categoryTwo']) ? trim($goodsArray['categoryTwo']) : '';//二级分类名称
  521. $categoryThree = isset($goodsArray['categoryThree']) ? trim($goodsArray['categoryThree']) : '';//三级分类名称
  522. $brandName = isset($goodsArray['brandName']) ? trim($goodsArray['brandName']) : '';//品牌名称
  523. $goodsName = isset($goodsArray['goodsName']) ? trim($goodsArray['goodsName']) : '';//商品名称
  524. $merchantName = isset($goodsArray['merchantName']) ? trim($goodsArray['merchantName']) : '';//商户名称
  525. $isEq = isset($goodsArray['isEq']) ? trim($goodsArray['isEq']) : 4;//是否抄码
  526. $describe = isset($goodsArray['describe']) ? trim($goodsArray['describe']) : '';//商品卖点
  527. $description = isset($goodsArray['description']) ? '<p>' . trim($goodsArray['description']) . '</p>' : '';//商品详情
  528. //主单位信息
  529. $skuName = isset($goodsArray['skuName']) ? trim($goodsArray['skuName']) : '无';//单位名称
  530. $barCode = isset($goodsArray['barCode']) ? trim($goodsArray['barCode']) : '';//主单位条码
  531. $salePrice = isset($goodsArray['salePrice']) ? trim($goodsArray['salePrice']) : 1;//商品价格
  532. $salePriceTrue = isset($goodsArray['salePriceTrue']) ? trim($goodsArray['salePriceTrue']) : bcmul($salePrice, 1.2, 2);//市场价格
  533. //辅单位信息
  534. if ($twoUnit) {
  535. $skuNameTwo = isset($goodsArray['skuNameTwo']) ? trim($goodsArray['skuNameTwo']) : '';//辅单位
  536. $barCodeTwo = isset($goodsArray['barCodeTwo']) ? trim($goodsArray['barCodeTwo']) : '';//辅单位条码
  537. $conversion = isset($goodsArray['conversion']) ? trim($goodsArray['conversion']) : 0;//辅单位换算比例
  538. $salePriceTwo = isset($goodsArray['salePriceTwo']) ? trim($goodsArray['salePriceTwo']) : 1;//辅单位销售价格
  539. $salePriceTwoTrue = isset($goodsArray['salePriceTwoTrue']) ? trim($goodsArray['salePriceTwoTrue']) : bcmul($salePriceTwo, 1.2, 2);//辅单位市场价格
  540. }
  541. if (empty($goodsName)) {
  542. continue;
  543. }
  544. /***商户数据***/
  545. if (!empty($merchantName)) {
  546. //查询商户是否存在
  547. $merchantSelect = [
  548. 'name' => $merchantName,
  549. ];
  550. $dbResult = $objDMerchantApply->get($merchantSelect);
  551. if ($dbResult === false) {
  552. exit($objDGoodsCategory->error());
  553. }
  554. $merchantId = $dbResult['id'];
  555. unset($dbResult);
  556. } else {
  557. $merchantId = 0;
  558. }
  559. /***首分类数据***/
  560. $categoryId = 1;
  561. $categoryPath = 1;
  562. if (!empty($categoryName)) {
  563. //查询分类是否存在
  564. $categorySelect = [
  565. 'title' => $categoryName,
  566. ];
  567. $dbResult = $objDGoodsCategory->get($categorySelect);
  568. if ($dbResult === false) {
  569. exit($objDGoodsCategory->error());
  570. }
  571. $category = $dbResult;
  572. unset($dbResult);
  573. if (empty($category)) {
  574. $categoryInsert = [
  575. 'sort' => 0,
  576. 'title' => $categoryName,
  577. 'pid' => 0,
  578. 'enableStatus' => StatusCode::$standard,
  579. 'deleteStatus' => StatusCode::$standard,
  580. 'createTime' => time(),
  581. 'updateTime' => time(),
  582. ];
  583. $dbResult = $objDGoodsCategory->insert($categoryInsert);
  584. if ($dbResult === false) {
  585. exit($objDGoodsCategory->error());
  586. }
  587. $categoryId = $dbResult;
  588. $categoryPath = $dbResult;
  589. unset($dbResult);
  590. } else {
  591. $categoryId = $category['id'];
  592. $categoryPath = $category['link'] . ',' . $category['id'];
  593. }
  594. /***二级分类数据***/
  595. if (!empty($categoryTwo)) {
  596. //查询分类是否存在
  597. $categorySelect = [
  598. 'title' => $categoryTwo,
  599. ];
  600. $dbResult = $objDGoodsCategory->get($categorySelect);
  601. if ($dbResult === false) {
  602. exit($objDGoodsCategory->error());
  603. }
  604. $category = $dbResult;
  605. unset($dbResult);
  606. if (empty($category)) {
  607. $categoryInsert = [
  608. 'sort' => 0,
  609. 'title' => $categoryTwo,
  610. 'pid' => $categoryId,
  611. 'link' => $categoryPath,
  612. 'enableStatus' => StatusCode::$standard,
  613. 'deleteStatus' => StatusCode::$standard,
  614. 'createTime' => time(),
  615. 'updateTime' => time(),
  616. ];
  617. $dbResult = $objDGoodsCategory->insert($categoryInsert);
  618. if ($dbResult === false) {
  619. exit($objDGoodsCategory->error());
  620. }
  621. $categoryId = $dbResult;
  622. $categoryPath = $categoryId . ',' . $dbResult;
  623. unset($dbResult);
  624. } else {
  625. $categoryId = $category['id'];
  626. $categoryPath = $category['link'] . ',' . $category['id'];
  627. }
  628. /***三级分类数据***/
  629. if (!empty($categoryThree)) {
  630. //查询分类是否存在
  631. $categorySelect = [
  632. 'title' => $categoryThree,
  633. ];
  634. $dbResult = $objDGoodsCategory->get($categorySelect);
  635. if ($dbResult === false) {
  636. exit($objDGoodsCategory->error());
  637. }
  638. $category = $dbResult;
  639. unset($dbResult);
  640. if (empty($category)) {
  641. $categoryInsert = [
  642. 'sort' => 0,
  643. 'title' => $categoryThree,
  644. 'pid' => $categoryId,
  645. 'link' => $categoryPath,
  646. 'enableStatus' => StatusCode::$standard,
  647. 'deleteStatus' => StatusCode::$standard,
  648. 'createTime' => time(),
  649. 'updateTime' => time(),
  650. ];
  651. $dbResult = $objDGoodsCategory->insert($categoryInsert);
  652. if ($dbResult === false) {
  653. exit($objDGoodsCategory->error());
  654. }
  655. $categoryId = $dbResult;
  656. $categoryPath = $categoryId . ',' . $categoryId . ',' . $dbResult;
  657. unset($dbResult);
  658. } else {
  659. $categoryId = $category['id'];
  660. $categoryPath = $category['link'] . ',' . $category['id'];
  661. }
  662. }
  663. }
  664. }
  665. $categoryPath = trim($categoryPath, ',');
  666. /***品牌数据***/
  667. if (!empty($brandName)) {
  668. //查询品牌是否存在
  669. $brandSelect = [
  670. 'title' => $brandName
  671. ];
  672. $dbResult = $objDGoodsBrand->get($brandSelect);
  673. if ($dbResult === false) {
  674. exit($objDGoodsBrand->error());
  675. }
  676. $brand = $dbResult;
  677. unset($dbResult);
  678. if (empty($brand)) {
  679. $brandInsert = [
  680. 'title' => $brandName,
  681. 'images' => null,
  682. 'sort' => 0,
  683. 'enableStatus' => StatusCode::$standard,
  684. 'createTime' => time(),
  685. 'updateTime' => time(),
  686. ];
  687. $dbResult = $objDGoodsBrand->insert($brandInsert);
  688. if ($dbResult === false) {
  689. exit($objDGoodsBrand->error());
  690. }
  691. $brandId = $dbResult;
  692. unset($dbResult);
  693. } else {
  694. $brandId = $brand['id'];
  695. }
  696. } else {
  697. $brandId = 1;
  698. }
  699. /***单位数据***/
  700. $skuId = NULL;
  701. if (!empty($skuName)) {
  702. //查询单位是否存在
  703. $skuSelect = [
  704. 'unitName' => $skuName
  705. ];
  706. $dbResult = $objDUnits->get($skuSelect);
  707. if ($dbResult === false) {
  708. exit($objDUnits->error());
  709. }
  710. $sku = $dbResult;
  711. unset($dbResult);
  712. if (empty($sku)) {
  713. $skuInsert = [
  714. 'unitName' => $skuName,
  715. 'enableStatus' => StatusCode::$standard,
  716. 'createTime' => time(),
  717. 'updateTime' => time(),
  718. ];
  719. $dbResult = $objDUnits->insert($skuInsert);
  720. if ($dbResult === false) {
  721. exit($objDUnits->error());
  722. }
  723. $skuId = $dbResult;
  724. unset($dbResult);
  725. } else {
  726. $skuId = $sku['id'];
  727. }
  728. }
  729. $skuIdTwo = NULL;
  730. if (!empty($skuNameTwo)) {
  731. //查询单位是否存在
  732. $skuSelect = [
  733. 'unitName' => $skuNameTwo
  734. ];
  735. $dbResult = $objDUnits->get($skuSelect);
  736. if ($dbResult === false) {
  737. exit($objDUnits->error());
  738. }
  739. $sku = $dbResult;
  740. unset($dbResult);
  741. if (empty($sku)) {
  742. $skuInsert = [
  743. 'unitName' => $skuNameTwo,
  744. 'enableStatus' => StatusCode::$standard,
  745. 'createTime' => time(),
  746. 'updateTime' => time(),
  747. ];
  748. $dbResult = $objDUnits->insert($skuInsert);
  749. if ($dbResult === false) {
  750. exit($objDUnits->error());
  751. }
  752. $skuIdTwo = $dbResult;
  753. unset($dbResult);
  754. } else {
  755. $skuIdTwo = $sku['id'];
  756. }
  757. }
  758. if ($twoUnit) {
  759. $postData = [
  760. "categoryPath" => $categoryPath,
  761. "title" => $goodsName,
  762. "storage" => "",
  763. "delUnitIds" => [],
  764. "delSpecSkuIds" => [],
  765. "specType" => 1,
  766. "specGroup" => [],
  767. "specMultiple" => [
  768. [
  769. "barCode" => $barCode,
  770. "weight" => "",
  771. "isDefault" => 5,
  772. "unitId" => $skuId,
  773. "unitName" => $skuName,
  774. "isMaster" => 5,
  775. "conversion" => 0,
  776. "specImage" => "",
  777. "specGroup" => [],
  778. "salePrice" => [
  779. "conversion" => 0,
  780. "unitName" => $skuName,
  781. "unitId" => $skuId,
  782. "isMaster" => 5,
  783. "deleteStatus" => 5,
  784. "enabledLadder" => 0,
  785. "salePriceAreaType" => 1,
  786. "salePrice" => $salePrice,
  787. "ladderPrice" => [],
  788. "marketPrice" => $salePriceTrue,
  789. "setNum" => 1
  790. ]
  791. ],
  792. [
  793. "barCode" => $barCodeTwo,
  794. "isDefault" => 4,
  795. "unitId" => $skuIdTwo,
  796. "unitName" => $skuNameTwo,
  797. "isMaster" => 4,
  798. "conversion" => $conversion,
  799. "specImage" => "",
  800. "specGroup" => [],
  801. "salePrice" => [
  802. "conversion" => $conversion,
  803. "unitName" => $skuNameTwo,
  804. "unitId" => $skuIdTwo,
  805. "isMaster" => 4,
  806. "deleteStatus" => 5,
  807. "enabledLadder" => 0,
  808. "salePriceAreaType" => 1,
  809. "salePrice" => $salePriceTwo,
  810. "ladderPrice" => [],
  811. "marketPrice" => $salePriceTwoTrue,
  812. "setNum" => 1
  813. ]
  814. ]
  815. ],
  816. "unitData" => [
  817. [
  818. "barCode" => "",
  819. "weight" => "",
  820. "isMaster" => 5,
  821. "isDefault" => 5,
  822. "unitName" => $skuName,
  823. "unitId" => $skuId
  824. ],
  825. [
  826. "unitName" => $skuNameTwo,
  827. "isMaster" => 4,
  828. "unitId" => $skuIdTwo,
  829. "conversion" => $conversion,
  830. "isEditSpec" => 5
  831. ]
  832. ],
  833. "describe" => $describe,
  834. 'merchantId' => $merchantId,//商户
  835. "code" => "",
  836. "barCode" => "",
  837. "weight" => "",
  838. "categoryId" => $categoryId,
  839. "expireTime" => "",
  840. "brandId" => $brandId,
  841. "tag" => "",
  842. "description" => $description,
  843. "noSalesShop" => "",
  844. "images" => $images,
  845. "isEq" => $isEq,
  846. "enableStatus" => 5,
  847. "deliverySupIds" => "1,2",
  848. "expressType" => 1,
  849. "expressFee" => "",
  850. "showExpress" => 5,
  851. "ruleId" => "",
  852. "isStore" => false
  853. ];
  854. } else {
  855. /***插入商品***/
  856. $specMultiple = [[
  857. "barCode" => $barCode,//商品条码
  858. "isDefault" => 5,
  859. "unitId" => $skuId,
  860. "unitName" => $skuName,
  861. "isMaster" => 5,
  862. "conversion" => 0,
  863. "specImage" => "",
  864. "specGroup" => [],
  865. "salePrice" => [
  866. "conversion" => 0,
  867. "unitName" => $skuName,
  868. "unitId" => $skuId,
  869. "isMaster" => 5,
  870. "deleteStatus" => 5,
  871. "enabledLadder" => 0,
  872. "salePriceAreaType" => 1,
  873. "salePrice" => $salePrice,//商品价格
  874. "ladderPrice" => [],
  875. "marketPrice" => $salePriceTrue,
  876. "setNum" => 1//限定数量
  877. ]
  878. ]];
  879. $postData = [
  880. 'categoryPath' => $categoryPath,
  881. 'title' => $goodsName,
  882. 'storage' => '',
  883. 'delUnitIds' => [],
  884. 'delSpecSkuIds' => [],
  885. 'specType' => 1,//1:无规格
  886. 'specGroup' => [],//无规格为空
  887. 'specMultiple' => $specMultiple,
  888. 'unitData' =>
  889. [
  890. [
  891. 'barCode' => '',
  892. 'isMaster' => 5,
  893. 'isDefault' => 5,
  894. 'unitName' => $skuName,
  895. 'unitId' => $skuId,
  896. ],
  897. ],
  898. 'describe' => $describe,//商品卖点
  899. 'merchantId' => $merchantId,//商户
  900. 'code' => '',
  901. 'barCode' => '',
  902. 'categoryId' => $categoryId,
  903. 'expireTime' => '',
  904. 'brandId' => $brandId,
  905. 'tag' => '',
  906. 'description' => $description,//商品详情
  907. 'noSalesShop' => '',
  908. 'images' => $images,
  909. 'isEq' => $isEq,
  910. 'enableStatus' => 5,
  911. ];
  912. }
  913. $postArray[] = $postData;
  914. }
  915. return $postArray;
  916. }
  917. /**
  918. * 导入商品基础数据
  919. * @throws \Exception
  920. */
  921. public function goodsImport()
  922. {
  923. //把页面传来的参数对应到符合的字段中全部传递过来;
  924. $paramsArray = self::getGoodsBasicImportParams();
  925. if (empty($paramsArray)) {
  926. $this->sendOutput('参数为空', ErrorCode::$paramError);
  927. }
  928. $goodsBasic = [];
  929. foreach ($paramsArray as $params) {
  930. //把所有的参数过滤,赋值给对应的字段中
  931. $goodsBasicData = [
  932. 'title' => isset($params['title']) ? $params['title'] : null,
  933. 'categoryId' => isset($params['categoryId']) ? $params['categoryId'] : null,
  934. 'categoryPath' => isset($params['categoryPath']) ? $params['categoryPath'] : null,
  935. 'images' => isset($params['images']) ? json_encode($params['images']) : null,//商品图册json
  936. 'unitData' => isset($params['unitData']) ? $params['unitData'] : null,//商品单位
  937. 'specType' => isset($params['specType']) ? $params['specType'] : null,
  938. 'isEq' => isset($params['isEq']) ? $params['isEq'] : 4,
  939. ];
  940. foreach ($goodsBasicData as $key => $value) {
  941. if (empty($value)) {
  942. parent::sendOutput($key . '参数错误', ErrorCode::$paramError);
  943. }
  944. }
  945. //商品名称转换搜索条件
  946. if (isset($goodsBasicData['title'])) {
  947. $objChineseCharacter = new ChineseCharacter();
  948. $goodsBasicData['condition'] = $objChineseCharacter->getInitials(trim($goodsBasicData['title']));
  949. }
  950. if ($goodsBasicData['specType'] == StatusCode::$specType['multiple']) {
  951. //带属性的
  952. if (!isset($params['specMultiple']) || empty($params['specMultiple'])) {
  953. parent::sendOutput('specMultiple参数错误', ErrorCode::$paramError);
  954. }
  955. if (!isset($params['specGroup']) || empty($params['specGroup'])) {
  956. parent::sendOutput('specGroup参数错误', ErrorCode::$paramError);
  957. }
  958. $goodsBasicData['specGroup'] = json_encode($params['specGroup']);//规格组
  959. $goodsBasicData['specMultiple'] = $params['specMultiple'];//规格明细
  960. }
  961. $goodsBasicData['brandId'] = isset($params['brandId']) ? $params['brandId'] : 0;//商品品牌
  962. $goodsBasicData['description'] = isset($params['description']) ? $params['description'] : '';
  963. $goodsBasicData['describe'] = isset($params['describe']) ? $params['describe'] : '';
  964. $goodsBasicData['expireTime'] = isset($params['expireTime']) ? $params['expireTime'] : 0;
  965. $goodsBasicData['tag'] = isset($params['tag']) ? $params['tag'] : '';
  966. $goodsBasicData['barCode'] = isset($params['barCode']) ? $params['barCode'] : '';//条码
  967. $goodsBasicData['link'] = isset($params['link']) ? $params['link'] : '';
  968. $goodsBasicData['noSalesShop'] = isset($params['noSalesShop']) ? $params['noSalesShop'] : ''; //禁止销售店铺
  969. //编辑传参
  970. $storage = isset($params['storage']) ? $params['storage'] : '';//货架编码
  971. $goodsBasicData['extends'] = json_encode(['storage' => $storage]);//货架编码
  972. $goodsBasicData['delUnitIds'] = isset($params['delUnitIds']) ? $params['delUnitIds'] : [];//删除计量单位ids
  973. $goodsBasicData['delSpecSkuIds'] = isset($params['delSpecSkuIds']) ? $params['delSpecSkuIds'] : [];//删除属性值
  974. $goodsBasicData['merchantId'] = isset($params['merchantId']) ? $params['merchantId'] : 0;//接收商户id
  975. !empty($this->supplierId) && $goodsBasicData['supplierId'] = $this->supplierId;
  976. $goodsBasic[] = $goodsBasicData;
  977. }
  978. unset($goodsBasicData);
  979. $total = 0;
  980. $true = 0;
  981. $false = 0;
  982. foreach ($goodsBasic as $key => $goodsBasicData) {
  983. unset($goodsBasicData['delUnitIds']);
  984. unset($goodsBasicData['delSpecSkuIds']);
  985. switch ($goodsBasicData['specType']) {
  986. case StatusCode::$specType['single']:
  987. //没有属性的
  988. $result = $this->objMGoodsBasic->addGoodsBasic($goodsBasicData);//单规格
  989. break;
  990. case StatusCode::$specType['multiple']:
  991. //有属性的
  992. $result = $this->objMGoodsBasic->addMultipleSpecGoodsBasic($goodsBasicData);//有属性的
  993. break;
  994. }
  995. if (!$result->isSuccess()) {
  996. $false++;
  997. } else {
  998. $true++;
  999. }
  1000. $total++;
  1001. }
  1002. $return = "共导入'$total'条商品,成功'$true'条商品,失败'$false'条商品";
  1003. parent::sendOutput($return);
  1004. }
  1005. }