MSku.Class.php 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931
  1. <?php
  2. /**
  3. * 规格
  4. * Created by PhpStorm.
  5. * User: XiaoMing
  6. * Date: 2019/11/18
  7. * Time: 18:28
  8. */
  9. namespace JinDouYun\Model\GoodsManage;
  10. use JinDouYun\Cache\SkuCache;
  11. use JinDouYun\Controller\Common\Logger;
  12. use JinDouYun\Model\Goods\BasicAndSkuCache;
  13. use Mall\Framework\Core\ErrorCode;
  14. use Mall\Framework\Core\ResultWrapper;
  15. use Mall\Framework\Core\StatusCode;
  16. use JinDouYun\Cache\GoodsBasicRelevant;
  17. use JinDouYun\Dao\GoodsManage\DSku;
  18. /**
  19. * Class MSku
  20. * @package JinDouYun\Model\GoodsManage
  21. */
  22. class MSku
  23. {
  24. use BasicAndSkuCache;
  25. /**
  26. * @var DSku
  27. */
  28. private $objDSku;
  29. /**
  30. * @var
  31. */
  32. private $onlineUserId;
  33. /**
  34. * @var int
  35. */
  36. private $onlineEnterpriseId;
  37. /**
  38. * @var GoodsBasicRelevant
  39. */
  40. private $objGoodsBasicRelevantCache;
  41. /**
  42. * @var SkuCache
  43. */
  44. private $objSkuCache;
  45. /**
  46. * MSku constructor.
  47. * @param $onlineUserId
  48. * @param $onlineEnterpriseId
  49. * @throws \Exception
  50. */
  51. public function __construct($onlineUserId, $onlineEnterpriseId)
  52. {
  53. $this->onlineUserId = $onlineUserId;
  54. $this->onlineEnterpriseId = $onlineEnterpriseId;
  55. $this->objDSku = new DSku('default');
  56. $this->objDSku->setTable($this->objDSku->get_Table() . '_' . $onlineEnterpriseId);
  57. $this->objSkuCache = new SkuCache($this->onlineEnterpriseId);
  58. $this->objGoodsBasicRelevantCache = new GoodsBasicRelevant($this->onlineEnterpriseId);
  59. }
  60. /**
  61. * 增加规格
  62. * @param $params
  63. * @param bool $multiple
  64. * @return ResultWrapper
  65. */
  66. public function addSku($params, $multiple = false)
  67. {
  68. $last = $this->objDSku->select([], 'id', 'id DESC', 1);//获取最后一条数据的id
  69. $dbResult = $this->objDSku->insert($params, $multiple);
  70. if ($dbResult === false) {
  71. return ResultWrapper::fail($this->objDSku->error(), ErrorCode::$dberror);
  72. }
  73. //写缓存
  74. self::updateCacheSku($last);
  75. $this->isUpdateSku = true;
  76. $this->updateSku = (array) $dbResult;
  77. return ResultWrapper::success($dbResult);
  78. }
  79. /**
  80. * 带有属性的比那集
  81. * @param $specMultiple
  82. * @param $goodsId
  83. * @param $delUnitIds
  84. * @param $delSpecSkuIds
  85. * @param $unitData
  86. * @return ResultWrapper
  87. */
  88. public function editMultipleSku($specMultiple, $goodsId, $delUnitIds, $delSpecSkuIds, $unitData)
  89. {
  90. $dbResult = self::commonMerge($specMultiple, $unitData, ['goodsId' => $goodsId], $goodsId);
  91. if (!$dbResult->isSuccess()) {
  92. return ResultWrapper::fail($dbResult->getData(), $dbResult->getErrorCode());
  93. }
  94. $data = $dbResult->getData();
  95. if (empty($data)) {
  96. $insert = self::commonSpec($specMultiple, $unitData, $goodsId);//组装数据
  97. if (!empty($insert)) {
  98. $dbResult = $this->objDSku->insert($insert, true);
  99. }
  100. $this->updateSku = array_merge($this->updateSku,$dbResult);
  101. unset($insert);
  102. }
  103. if (!empty($data['insert'])) {
  104. $dbResult = $this->objDSku->insert($data['insert'], true);
  105. $this->updateSku = array_merge($this->updateSku,$dbResult);
  106. }
  107. if (!empty($data['update'])) {
  108. foreach ($data['update'] as &$spec) {
  109. $this->updateSku = array_merge($this->updateSku,[$spec['id']]);
  110. unset($spec['hash']);
  111. $dbResult = $this->objDSku->update($spec, ['id' => $spec['id']]);
  112. }
  113. }
  114. if (!empty($data['delete'])) {
  115. $dbResult = $this->objDSku->update(
  116. [
  117. 'deleteStatus' => StatusCode::$delete
  118. ],
  119. [
  120. 'id' => array_column($data['delete'], 'id')
  121. ]);
  122. }
  123. if ($dbResult === false) {
  124. return ResultWrapper::fail($this->objDSku->error(), ErrorCode::$dberror);
  125. }
  126. $this->isUpdateSku = true;
  127. return ResultWrapper::success($dbResult);
  128. }
  129. /**
  130. * 组合spec
  131. * @param $specMultiple
  132. * @param $unitData
  133. * @param $goodsId
  134. * @return array
  135. */
  136. public function commonSpec($specMultiple, $unitData, $goodsId)
  137. {
  138. $isNew = StatusCode::$standard;
  139. $isNewResult = self::getIsNewByGoodsId($goodsId);
  140. if ($isNewResult->isSuccess()){
  141. $isNew = $isNewResult->getData();
  142. }
  143. //属性新增操作
  144. $insert = [];
  145. foreach ($unitData as $unit) {
  146. foreach ($specMultiple as $spec) {
  147. if (!isset($spec['specGroupHash'])) {
  148. $specGroupIds = array_column($spec['specGroup'], 'specValueId');
  149. $hash = md5(implode('|', $specGroupIds));
  150. //属性新增操作
  151. $insert[] = [
  152. 'goodsId' => $goodsId,
  153. 'specData' => json_encode($spec['specGroup']),
  154. 'specImage' => json_encode($spec['specImage']),
  155. 'specType' => StatusCode::$specType['multiple'],
  156. 'unitId' => $unit['unitId'],
  157. 'isMaster' => $unit['isMaster'],
  158. 'unitName' => $unit['unitName'],
  159. 'conversion' => $unit['conversion'],
  160. 'specGroupHash' => $hash,
  161. 'isNew' => $isNew
  162. ];
  163. }
  164. }
  165. }
  166. return $insert;
  167. }
  168. /**
  169. * @param $specMultiple
  170. * @param $unitData
  171. * @param $selectParams
  172. * @param $goodsId
  173. * @return ResultWrapper
  174. */
  175. public function commonMerge($specMultiple, $unitData, $selectParams, $goodsId)
  176. {
  177. // 判断sku是否新版的规则标识
  178. $isNewResult = self::getIsNewByGoodsId($goodsId);
  179. if (!$isNewResult->isSuccess()){
  180. return ResultWrapper::fail($isNewResult->getData(),$isNewResult->getErrorCode());
  181. }
  182. $isNew = $isNewResult->getData();
  183. // 查询当前商品的所有sku数据
  184. $dbResult = self::getSpecQuery($selectParams);
  185. if (!$dbResult->isSuccess()) {
  186. return ResultWrapper::fail($dbResult->getData(), $dbResult->getErrorCode());
  187. }
  188. $oldData = $dbResult->getData();
  189. if (empty($oldData)) {
  190. return ResultWrapper::success([]);
  191. }
  192. $mapping = [];
  193. $allHash = [];
  194. foreach ($oldData as &$value) {
  195. $hash = md5($value['goodsId'] . $value['unitId'] . $value['isMaster'] . $value['specGroupHash']);
  196. $value['hash'] = $hash;
  197. $mapping[$value['hash']] = $value;
  198. $allHash[] = $value['hash'];
  199. }
  200. //比对数据库就数据和新数据
  201. $nowData = [];
  202. foreach ($unitData as $unit) {
  203. foreach ($specMultiple as $spec) {
  204. //生成hash值
  205. $specGroupIds = array_column($spec['specGroup'], 'specValueId');
  206. $specGroupHash = md5(implode('|', $specGroupIds));//属性hash值
  207. $hash = md5($goodsId . $unit['unitId'] . $unit['isMaster'] . $specGroupHash);//对单行数据生成hash值
  208. $nowData[] = [
  209. 'goodsId' => $goodsId,
  210. 'specData' => json_encode($spec['specGroup']),
  211. 'specImage' => json_encode($spec['specImage']),
  212. 'specType' => StatusCode::$specType['multiple'],
  213. 'unitId' => $unit['unitId'],
  214. 'isMaster' => $unit['isMaster'],
  215. 'unitName' => $unit['unitName'],
  216. 'conversion' => $unit['conversion'],
  217. 'specGroupHash' => $specGroupHash,
  218. 'hash' => $hash,
  219. 'barCode' => isset($spec['barCode'][$unit['unitId']]) ? $spec['barCode'][$unit['unitId']] : '',
  220. 'isDefault' => isset($spec['isDefault'][$unit['unitId']]) ? $spec['isDefault'][$unit['unitId']] : StatusCode::$delete,
  221. 'weight' => isset($spec['weight'][$unit['unitId']]) ? $spec['weight'][$unit['unitId']] : 0,
  222. 'isNew' => $isNew
  223. ];//新的数据
  224. }
  225. }
  226. $insert = [];
  227. $update = [];
  228. $delete = [];
  229. foreach ($nowData as &$spec) {
  230. if (in_array($spec['hash'], $allHash)) {
  231. $spec['id'] = $mapping[$spec['hash']]['id'];
  232. $update[] = $spec;
  233. unset($mapping[$spec['hash']]);
  234. } else {
  235. unset($spec['hash']);
  236. $insert[] = $spec;
  237. }
  238. }
  239. $delete = $mapping;
  240. return ResultWrapper::success([
  241. 'insert' => $insert,
  242. 'update' => $update,
  243. 'delete' => array_values($delete),
  244. ]);
  245. }
  246. public function getSpecQuery($selectParams)
  247. {
  248. $selectParams['deleteStatus'] = StatusCode::$standard;
  249. $dbResult = $this->objDSku->select($selectParams);
  250. if ($dbResult === false) {
  251. return ResultWrapper::fail($this->objDSku->error(), ErrorCode::$dberror);
  252. }
  253. return ResultWrapper::success($dbResult);
  254. }
  255. /**
  256. * 修改sku
  257. * @param $unitData
  258. * @param $goodsId
  259. * @param $delUnitIds
  260. * @return ResultWrapper
  261. */
  262. public function editSku($unitData, $goodsId, $delUnitIds, $delSpecSkuIds)
  263. {
  264. $isNewResult = self::getIsNewByGoodsId($goodsId);
  265. if (!$isNewResult->isSuccess()){
  266. return ResultWrapper::fail($isNewResult->getData(),ErrorCode::$dberror);
  267. }
  268. $isNew = $isNewResult->getData();
  269. $objGoodsBasicRelevant = new GoodsBasicRelevant($this->onlineEnterpriseId);
  270. //有删除的
  271. if (!empty($delUnitIds)) {
  272. $dbResult = $this->objDSku->update(
  273. ['deleteStatus' => StatusCode::$delete, 'updateTime' => time()],
  274. [
  275. 'unitId' => $delUnitIds,
  276. 'goodsId' => $goodsId,
  277. ]);
  278. if ($dbResult === false) {
  279. return ResultWrapper::fail($this->objDSku->error(), ErrorCode::$dberror);
  280. }
  281. }
  282. // 删除带属性的sku
  283. if(!empty($delSpecSkuIds)){
  284. $dbResult = $this->objDSku->update(['deleteStatus' => StatusCode::$delete, 'updateTime' => time()], ['specGroupHash'=>$delSpecSkuIds]);
  285. if ($dbResult === false) {
  286. return ResultWrapper::fail($this->objDSku->error(), ErrorCode::$dberror);
  287. }
  288. }
  289. //有更新
  290. if (!empty($unitData)) {
  291. $insert = [];
  292. foreach ($unitData as $unit) {
  293. if (!isset($unit['id'])) {
  294. $insert[] = [
  295. 'conversion' => $unit['conversion'],
  296. 'unitName' => $unit['unitName'],
  297. 'isMaster' => $unit['isMaster'],
  298. 'unitId' => $unit['unitId'],
  299. 'specType' => StatusCode::$specType['single'],//无属性
  300. 'goodsId' => $goodsId,
  301. 'barCode' => isset($unit['barCode']) ? $unit['barCode'] : '',
  302. 'isDefault' => isset($unit['isDefault']) ? $unit['isDefault'] : StatusCode::$delete,
  303. 'weight' => isset($unit['weight']) ? $unit['weight'] : 0,
  304. 'isNew' => $isNew,
  305. ];
  306. } else {
  307. $dbResult = $this->objDSku->update([
  308. 'conversion' => $unit['conversion'],
  309. 'unitName' => $unit['unitName'],
  310. 'isMaster' => $unit['isMaster'],
  311. 'unitId' => $unit['unitId'],
  312. 'barCode' => isset($unit['barCode']) ? $unit['barCode'] : '',
  313. 'isDefault' => isset($unit['isDefault']) ? $unit['isDefault'] : StatusCode::$delete,
  314. 'weight' => isset($unit['weight']) ? $unit['weight'] : 0,
  315. 'isNew' => $isNew
  316. ], $unit['id']);
  317. if ($dbResult === false) {
  318. return ResultWrapper::fail($this->objDSku->error(), ErrorCode::$dberror);
  319. }
  320. $this->updateSku = array_merge($this->updateSku,[$unit['id']]);
  321. $objGoodsBasicRelevant->cacheSkuIdRelationName($unit['unitName'], $unit['id']);
  322. }
  323. }
  324. if (!empty($insert)) {
  325. $last = $this->objDSku->select([], 'id', 'id DESC', 1);//获取最后一条数据的id
  326. $dbResult = $this->objDSku->insert($insert, true);
  327. if ($dbResult === false) {
  328. return ResultWrapper::fail($this->objDSku->error(), ErrorCode::$dberror);
  329. }
  330. $this->updateSku = array_merge($this->updateSku,$dbResult);
  331. if (isset($last)) {
  332. self::updateCacheSku($last);//写缓存
  333. }
  334. }
  335. }
  336. $this->isUpdateSku = true;
  337. return ResultWrapper::success(true);
  338. }
  339. /**
  340. * Doc: (des="获取最新单位的isNew")
  341. * User: XMing
  342. * Date: 2020/10/23
  343. * Time: 11:46 上午
  344. */
  345. public function getIsNewByGoodsId($goodsId): ResultWrapper
  346. {
  347. $result = $this->objDSku->get(['goodsId' => $goodsId,'isMaster' => StatusCode::$standard],'isNew');
  348. if ($result === false){
  349. return ResultWrapper::fail($this->objDSku->error(),ErrorCode::$dberror);
  350. }
  351. return ResultWrapper::success(isset($result['isNew']) ? $result['isNew'] : StatusCode::$standard);
  352. }
  353. /**
  354. * 批量设置sku
  355. * @param $params
  356. * @return ResultWrapper
  357. */
  358. public function setSku($params)
  359. {
  360. if (!is_array($params['goodsId'])) return ResultWrapper::fail('参数格式错误', ErrorCode::$paramError);
  361. $last = $this->objDSku->select([], 'id', 'id DESC', 1);//获取最后一条数据的id
  362. if (is_array($params['goodsId'])) {
  363. $skuData = [];
  364. foreach ($params['goodsId'] as $key => $val) {
  365. foreach ($params['branchData'] as $sku) {
  366. $skuData[] = [
  367. 'id' => '',
  368. 'goodsId' => $val,
  369. 'conversion' => $sku['conversion'],
  370. 'unitName' => $sku['unitName'],
  371. 'unitId' => $sku['unitId'],
  372. 'isMaster' => $sku['isMaster'],
  373. ];
  374. }
  375. }
  376. $dbResult = $this->objDSku->replace($skuData, true);
  377. if ($dbResult === false) {
  378. return ResultWrapper::fail($this->objDSku->error(), ErrorCode::$dberror);
  379. }
  380. self::updateCacheSku($last);
  381. return ResultWrapper::success($dbResult);
  382. }
  383. }
  384. /**
  385. * @param $sql
  386. * @return ResultWrapper
  387. */
  388. public function getQuery($sql)
  389. {
  390. $dbResult = $this->objDSku->query($sql);
  391. if ($dbResult === false) {
  392. return ResultWrapper::fail($this->objDSku->error(), ErrorCode::$dberror);
  393. }
  394. return ResultWrapper::success($dbResult);
  395. }
  396. /**
  397. * 根据goodsId获取sku信息 根据主单位辅单位区分
  398. * @param $params
  399. * @param string $fields
  400. * @param bool $group
  401. * @return ResultWrapper
  402. */
  403. public function getSkuInfo($params, $fields = 'id,goodsId,conversion,unitName,unitId,isMaster', $group = true)
  404. {
  405. $params['deleteStatus'] = StatusCode::$standard;
  406. $dbResult = $this->objDSku->select($params, $fields);
  407. if ($dbResult === false) {
  408. return ResultWrapper::fail($this->objDSku->error(), ErrorCode::$dberror);
  409. }
  410. if ($group) {
  411. foreach ($dbResult as $key => &$val) {
  412. $isMaster = $val['isMaster'];
  413. $goodsId = $val['goodsId'];
  414. //unset($val['isMaster']);
  415. unset($val['goodsId']);
  416. if ($isMaster == StatusCode::$standard) {
  417. $data[$goodsId]['masterUnit'] = $val;
  418. } else {
  419. $data[$goodsId]['branchUnit'][] = $val;
  420. }
  421. }
  422. } else {
  423. $data = $dbResult;
  424. }
  425. return ResultWrapper::success(isset($data) ? $data : []);
  426. }
  427. /**
  428. * 根据ids数组获取name
  429. * @param $ids
  430. * @param bool $type
  431. * @return array|ResultWrapper
  432. */
  433. public function getNameByIds($ids, $type = false)
  434. {
  435. $dbResult = $this->objDSku->select($ids, '*');
  436. if ($dbResult === false) {
  437. return ResultWrapper::fail($this->objDSku->error(), ErrorCode::$dberror);
  438. }
  439. if (empty($dbResult)) {
  440. return ResultWrapper::fail('skuIds错误', ErrorCode::$dberror);
  441. }
  442. $returnData = [];
  443. foreach ($dbResult as $key => $value) {
  444. if ($type) {
  445. $returnData[$value['id']] = $value;
  446. } else {
  447. $returnData[$value['id']]['id'] = $value['id'];
  448. $returnData[$value['id']]['unitName'] = $value['unitName'];
  449. }
  450. }
  451. return ResultWrapper::success($returnData);
  452. }
  453. /**
  454. * 根据ids数组获取name
  455. * @param $GoodsIds
  456. * @param bool $isMaster
  457. * @return array|ResultWrapper
  458. */
  459. public function getSkuDataByGoodsIds($GoodsIds, $isMaster = false)
  460. {
  461. $skuData = [];
  462. $falseResult = [];
  463. foreach ($GoodsIds as $value){
  464. $cacheResult = $this->objSkuCache->getGoodsBasicSku($value);
  465. if($cacheResult) {
  466. $skuData[$value] = $cacheResult;
  467. }else{
  468. $falseResult[] = $value;
  469. }
  470. }
  471. if(empty($falseResult)){
  472. if($isMaster){
  473. $return = [];
  474. foreach($skuData as $goodsId => $sku){
  475. foreach($sku as $skuId => $v){
  476. if($v['isMaster'] == StatusCode::$standard){
  477. $return[$goodsId][$skuId] = $v;
  478. }
  479. }
  480. }
  481. return ResultWrapper::success($return);
  482. }
  483. return ResultWrapper::success($skuData);
  484. }
  485. $where = ['goodsId' => $falseResult];
  486. $where['deleteStatus'] = StatusCode::$standard;
  487. $dbResult = $this->objDSku->select($where);
  488. if ($dbResult === false) {
  489. return ResultWrapper::fail($this->objDSku->error(), ErrorCode::$dberror);
  490. }
  491. $data = $dbResult;
  492. unset($dbResult);
  493. $format = [];
  494. foreach($data as $value){
  495. $format[$value['goodsId']][$value['id']] = $value;
  496. }
  497. foreach($format as $goodsId => $value){
  498. $this->objSkuCache->addGoodsBasicSku($goodsId,$value);
  499. $skuData[$goodsId] = $value;
  500. }
  501. if($isMaster){
  502. $return = [];
  503. foreach($skuData as $goodsId => $sku){
  504. foreach($sku as $skuId => $v){
  505. if($v['isMaster'] == StatusCode::$standard){
  506. $return[$goodsId][$skuId] = $v;
  507. }
  508. }
  509. }
  510. return ResultWrapper::success($return);
  511. }
  512. return ResultWrapper::success($skuData);
  513. }
  514. /**
  515. * @param $skuIds
  516. * @return array
  517. */
  518. public function getSku($skuIds)
  519. {
  520. $return = [];
  521. if (!$skuIds) {
  522. return $return;
  523. }
  524. $dbResult = $this->objDSku->select($skuIds, '*');
  525. if ($dbResult === false) {
  526. return $return;
  527. }
  528. foreach ($dbResult as $sku) {
  529. $return[$sku['id']] = $sku;
  530. }
  531. return $return;
  532. }
  533. /**
  534. * 获取单个sku数据
  535. * @param $where
  536. * @return array
  537. */
  538. public function getSkuData($where = [])
  539. {
  540. $dbResult = $this->objDSku->get($where);
  541. if ($dbResult === false) {
  542. return [];
  543. }
  544. return $dbResult;
  545. }
  546. /**
  547. * @param $selectParams
  548. * @return ResultWrapper
  549. */
  550. public function getMaterSkuId($selectParams)
  551. {
  552. $dbResult = $this->objDSku->select($selectParams, 'id,goodsId');
  553. if ($dbResult === false) {
  554. return ResultWrapper::fail($this->objDSku->error(), ErrorCode::$dberror);
  555. }
  556. $mapping = [];
  557. foreach ($dbResult as $val) {
  558. $mapping[$val['goodsId']] = $val['id'];
  559. }
  560. return ResultWrapper::success($mapping);
  561. }
  562. /**
  563. * todo(这个缓存方法废弃了,但是好像很多地方用,暂时没删除)
  564. * @param $last
  565. * @param string $action
  566. * @return bool
  567. */
  568. public function updateCacheSku($last)
  569. {
  570. $lastId = isset($last[0]['id']) ? $last[0]['id'] : 0;
  571. $objGoodsBasicRelevant = new GoodsBasicRelevant($this->onlineEnterpriseId);
  572. $dbResult = $this->objDSku->select("id > {$lastId}", 'id,unitName');
  573. if ($dbResult == false) {
  574. return false;
  575. }
  576. foreach ($dbResult as $key => $val) {
  577. $objGoodsBasicRelevant->cacheSkuIdRelationName($val['unitName'], $val['id']);
  578. }
  579. return true;
  580. }
  581. /**
  582. * @param $selectParams
  583. * @return ResultWrapper
  584. */
  585. public function getSkuByGoodsBasicId($selectParams)
  586. {
  587. $selectParams['deleteStatus'] = StatusCode::$standard;
  588. $dbResult = $this->objDSku->select($selectParams, 'id,unitName,specData,unitId,specImage,conversion,isMaster,specType');
  589. if ($dbResult === false) {
  590. return ResultWrapper::fail($this->objDSku->error(), ErrorCode::$dberror);
  591. }
  592. return ResultWrapper::success($dbResult);
  593. }
  594. /**
  595. * 获取sku数据
  596. * @param $skuIds
  597. * @return ResultWrapper
  598. * @throws \Exception
  599. */
  600. public function getSkuDataBySkuIds($skuIds)
  601. {
  602. if (empty($skuIds)) return ResultWrapper::success([]);
  603. $skuData = [];
  604. $falseResult = [];
  605. if(!is_array($skuIds)){
  606. $skuIds = [$skuIds];
  607. }
  608. foreach ($skuIds as $value){
  609. $cacheResult = $this->objSkuCache->getSku($value,true);
  610. if($cacheResult && isset($cacheResult['specType'])) {
  611. $skuData[] = $cacheResult;
  612. }else{
  613. $falseResult[] = $value;
  614. }
  615. }
  616. if(!empty($falseResult)){
  617. $where = ['id' => $falseResult];
  618. $dbResult = $this->objDSku->select($where);
  619. if ($dbResult === false) {
  620. return ResultWrapper::fail($this->objDSku->error(), ErrorCode::$dberror);
  621. }
  622. if(!empty($dbResult)){
  623. foreach ($dbResult as $value){
  624. $skuData[] = $value;
  625. $cacheMap = self::cacheMappersSku($value);
  626. $this->objSkuCache->delSku($value['id']);
  627. $cacheResult = $this->objSkuCache->cacheSku($value['id'], $cacheMap);
  628. if(!$cacheResult->isSuccess()){
  629. return ResultWrapper::fail($cacheResult->getData(), $cacheResult->getErrorCode());
  630. }
  631. }
  632. }
  633. }
  634. $returnData = [];
  635. foreach ($skuData as $value) {
  636. //单规格
  637. $returnData[$value['id']]['unitId'] = $value['unitId'];
  638. $returnData[$value['id']]['unitName'] = $value['unitName'];
  639. $returnData[$value['id']]['barCode'] = $value['barCode'];
  640. $returnData[$value['id']]['isMaster'] = $value['isMaster'];
  641. $returnData[$value['id']]['skuName'] = '';
  642. $returnData[$value['id']]['conversion'] = $value['conversion'];
  643. $returnData[$value['id']]['specData'] = [];
  644. if ($value['specType'] == StatusCode::$specType['multiple']) {
  645. //多规格
  646. $returnData[$value['id']]['skuName'] = '';
  647. $specData = json_decode($value['specData'], true);
  648. foreach ($specData as $vv) {
  649. $returnData[$value['id']]['specData'][$vv['specId']]['specName'] = $vv['specName'];
  650. $returnData[$value['id']]['specData'][$vv['specId']]['specValue'][$vv['specValueId']] = $vv['specValueName'];
  651. $returnData[$value['id']]['skuName'] = empty($returnData[$value['id']]['skuName']) ? $vv['specValueName'] : $returnData[$value['id']]['skuName'].';'. $vv['specValueName'];
  652. }
  653. }
  654. }
  655. return ResultWrapper::success($returnData);
  656. }
  657. /**
  658. * 通过skuIds获取规格名
  659. * @param $skuIds
  660. * @return ResultWrapper
  661. */
  662. public function getSpecNameBySkuId($skuIds)
  663. {
  664. if (empty($skuIds)){
  665. return ResultWrapper::success([]);
  666. }
  667. $dbResult = $this->objDSku->select(['id' => $skuIds]);
  668. if ($dbResult === false) {
  669. return ResultWrapper::fail($this->objDSku->error(), ErrorCode::$dberror);
  670. }
  671. if (empty($dbResult)) {
  672. return ResultWrapper::success([]);
  673. }
  674. $mapping = [];
  675. foreach ($dbResult as $row) {
  676. $unitName = $row['unitName'];
  677. $specData = [];
  678. $specName = '';
  679. if ($row['specType'] == StatusCode::$specType['multiple']) {
  680. $specData = json_decode($row['specData'], true);
  681. $arrSpecName = array_column($specData, 'specValueName');
  682. $specName = rtrim(implode('_', $arrSpecName), '_');
  683. }
  684. if (isset($specName) && empty($specName)) {
  685. $unitName .= ' ' . $specName;
  686. }
  687. $mapping[$row['id']] = [
  688. 'specGroup' => empty($specData) ? [] : $specData,//属性组
  689. 'unitName' => $row['unitName'],//单位名称
  690. 'nameFields' => $unitName,//拼接好的名字
  691. 'isMaster' => $row['isMaster'],//是否是主单位
  692. 'conversion' => $row['conversion'],//换算比率
  693. 'specImages' => empty($row['specImage']) ? [] : json_decode($row['specImage'], true),
  694. 'barCode' => empty($row['barCode']) ? '' : $row['barCode'],
  695. 'weight' => empty($row['weight']) ? 0 : $row['weight'],
  696. 'isNew' => $row['isNew']
  697. ];
  698. }
  699. return ResultWrapper::success($mapping);
  700. }
  701. /**
  702. * 传入skuIds 获取sku对应主单位skuId及换算比例
  703. * @param $skuIds
  704. * @return ResultWrapper
  705. */
  706. public function getConversion($skuIds)
  707. {
  708. if (empty($skuIds)){
  709. return ResultWrapper::success([]);
  710. }
  711. $dbResult = $this->objDSku->select(['id' => $skuIds], 'id,isMaster,unitName,goodsId,specType,conversion,specGroupHash,isNew');
  712. if ($dbResult === false) {
  713. return ResultWrapper::fail($this->objDSku->error(), ErrorCode::$dberror);
  714. }
  715. if (empty($dbResult)) {
  716. return ResultWrapper::success([]);
  717. }
  718. $mapping = [];
  719. foreach ($dbResult as $item) {
  720. if ($item['isMaster'] == StatusCode::$standard) {
  721. $mapping[$item['id']] = [
  722. 'masterSkuId' => $item['id'],
  723. 'conversion' => 0,
  724. 'isMaster' => StatusCode::$standard,
  725. 'deleteStatus' => StatusCode::$standard,
  726. 'masterUnitName' => $item['unitName'],
  727. 'conversionStr' => $item['unitName'].'(主)',
  728. 'isNew' => $item['isNew']
  729. ];
  730. } else {
  731. //获取当前属性主单位skuID
  732. $masterSku = $this->objDSku->get(
  733. [
  734. 'isMaster' => StatusCode::$standard,
  735. 'goodsId' => $item['goodsId'],
  736. 'specGroupHash' => $item['specGroupHash'],
  737. 'deleteStatus' => StatusCode::$standard
  738. ],'id,unitName');
  739. $mapping[$item['id']] = [
  740. 'masterSkuId' => $masterSku['id'],
  741. 'conversion' => $item['conversion'],
  742. 'isMaster' => StatusCode::$delete,
  743. 'isNew' => $item['isNew'],
  744. 'conversionStr' => '1'.$item['unitName'].'='.floatval($item['conversion']).$masterSku['unitName'],
  745. 'masterUnitName' => $masterSku['unitName'],
  746. ];
  747. }
  748. }
  749. return ResultWrapper::success($mapping);
  750. }
  751. /**
  752. * Doc: (des="删除基础资料下sku缓存")
  753. * User: XMing
  754. * Date: 2020/10/21
  755. * Time: 7:42 下午
  756. * @param $goodsBasicId
  757. * @return ResultWrapper
  758. */
  759. public function delSkuCache($goodsBasicId): ResultWrapper
  760. {
  761. $result = $this->objSkuCache->delGoodsBasicSku($goodsBasicId);
  762. return ResultWrapper::success($result);
  763. }
  764. /**
  765. * @param array $select
  766. * @return ResultWrapper
  767. */
  768. public function getSkuByParams(array $select)
  769. {
  770. $dbResult = $this->objDSku->select($select);
  771. if ($dbResult === false) {
  772. return ResultWrapper::fail($this->objDSku->error(), ErrorCode::$dberror);
  773. }
  774. return ResultWrapper::success($dbResult);
  775. }
  776. /**
  777. * Doc: (des="")
  778. * User: XMing
  779. * Date: 2020/12/19
  780. * Time: 9:51 上午
  781. * @param $ids
  782. * @return ResultWrapper
  783. */
  784. public function getSkuMapByIds($ids): ResultWrapper
  785. {
  786. if (empty($ids)){
  787. return ResultWrapper::success([]);
  788. }
  789. $lists = $this->objDSku->select(['id' => $ids],'id,unitName,specData as specGroup');
  790. if ($lists === false) {
  791. return ResultWrapper::fail($this->objDSku->error(), ErrorCode::$dberror);
  792. }
  793. $map = [];
  794. foreach ($lists as $list){
  795. $list['specGroup'] = !empty($list['specGroup']) ? json_decode($list['specGroup'],true) : [];
  796. $map[$list['id']] = $list;
  797. }
  798. return ResultWrapper::success($map);
  799. }
  800. /**
  801. * Doc: (des="")
  802. * User: XMing
  803. * Date: 2021/3/4
  804. * Time: 4:29 下午
  805. * @param array $ids
  806. * @return ResultWrapper
  807. */
  808. public function getMasterUnitNameByMaterielIds(array $ids): ResultWrapper
  809. {
  810. if (empty($ids)){
  811. return ResultWrapper::success([]);
  812. }
  813. $lists = $this->objDSku->select([
  814. 'goodsId' => $ids,
  815. 'deleteStatus' => StatusCode::$standard,
  816. 'isMaster' => StatusCode::$standard
  817. ],'unitName,goodsId');
  818. if ($lists === false){
  819. Logger::logs(E_USER_ERROR,'sql error',__CLASS__,__LINE__,$this->objDSku->error());
  820. return ResultWrapper::fail($this->objDSku->error(),ErrorCode::$dberror);
  821. }
  822. $map = [];
  823. foreach ($lists as $value){
  824. if (!isset($map[$value['goodsId']]) || empty($map[$value['goodsId']])){
  825. $map[$value['goodsId']] = $value['unitName'];
  826. }
  827. }
  828. return ResultWrapper::success($map);
  829. }
  830. /**
  831. * Doc: (des="")
  832. * User: XMing
  833. * Date: 2021/3/13
  834. * Time: 4:14 下午
  835. * @param int $id
  836. * @return ResultWrapper
  837. */
  838. public function getMasterById(int $id): ResultWrapper
  839. {
  840. $result = $this->objDSku->get(['goodsId' => $id,'deleteStatus' => StatusCode::$standard,'isMaster' => StatusCode::$standard]);
  841. if ($result === false){
  842. return ResultWrapper::fail($this->objDSku->error(),ErrorCode::$dberror);
  843. }
  844. return ResultWrapper::success($result);
  845. }
  846. /**
  847. * @After
  848. */
  849. public function __destruct()
  850. {
  851. // TODO: Implement __destruct() method.
  852. self::cacheSku();
  853. }
  854. }