BasicAndSkuCache.Class.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. namespace JinDouYun\Model\Goods;
  3. /**
  4. * 缓存商品
  5. * Trait BasicAndSkuCache
  6. * @package JinDouYun\Model\Goods
  7. */
  8. trait BasicAndSkuCache
  9. {
  10. /**
  11. * @var bool 是否更新缓存
  12. */
  13. private $isUpdateBasicCache = false;
  14. /**
  15. * @var null|int 更新对象id,基础资料
  16. */
  17. private $objectId = null;
  18. /**
  19. * @var bool sku缓存
  20. */
  21. private $isUpdateSku = false;
  22. /**
  23. * @var array 更新缓存skuIds
  24. */
  25. private $updateSku = [];
  26. /**
  27. * @return bool
  28. */
  29. public function cacheBasic()
  30. {
  31. if (!$this->isUpdateBasicCache) {
  32. return false;
  33. }
  34. $sql = "SELECT c.title as categoryName,b.*,d.title as brandName
  35. FROM qianniao_goods_basic_" . $this->onlineEnterpriseId . " as b
  36. LEFT JOIN qianniao_goods_category_" . $this->onlineEnterpriseId . " as c
  37. ON c.id=b.categoryId
  38. LEFT JOIN qianniao_goods_brand_" . $this->onlineEnterpriseId . " as d
  39. ON b.brandId=d.id WHERE b.id = " . $this->objectId;
  40. $goodsBasicResult = $this->objDGoodsBasic->query($sql);
  41. if ($goodsBasicResult === false) {
  42. file_put_contents('/www/wwwroot/logs/api.junhailan.com/BasicAndSkuCache_error.log', date('Y-m-d H:i:s') . '错误|sql: ' . $this->objDGoodsBasic->error() . PHP_EOL, FILE_APPEND);
  43. return false;
  44. }
  45. if (!empty($goodsBasicResult)) {
  46. $this->objGoodsBasicRelevant->deleteBasicKeyById($this->objectId);
  47. //组装缓存数据
  48. $goodsBasicResult = (array)$goodsBasicResult;
  49. $cacheMap = array_shift($goodsBasicResult);
  50. $cacheResult = $this->objGoodsBasicRelevant->cacheBasicIdRelationName($cacheMap, $this->objectId);
  51. if (!$cacheResult->isSuccess()) {
  52. //缓存写入失败
  53. file_put_contents('/www/wwwroot/logs/api.junhailan.com/BasicAndSkuCache_error.log', date('Y-m-d H:i:s') . '错误|cache缓存失败,基础资料id:' . $this->objectId . PHP_EOL, FILE_APPEND);
  54. return false;
  55. }
  56. }
  57. return true;
  58. }
  59. /**
  60. * @param array $data
  61. * @return array
  62. */
  63. public static function cacheMappers(array $data)
  64. {
  65. $map = [];
  66. if (empty($data)) {
  67. return $map;
  68. }
  69. $map = [
  70. 'id' => $data['id'],
  71. 'title' => $data['title'],
  72. 'images' => $data['images'],
  73. 'condition' => $data['condition'],
  74. 'code' => $data['code'],
  75. 'categoryId' => $data['categoryId'],
  76. 'categoryName' => $data['categoryName'],
  77. 'categoryPath' => $data['categoryPath'],
  78. 'expireTime' => $data['expireTime'],
  79. 'brandId' => $data['brandId'],
  80. 'brandName' => $data['brandName'],
  81. 'describe' => $data['describe'],
  82. 'description' => $data['description'],
  83. 'noSalesShop' => $data['noSalesShop'],
  84. 'extends' => $data['extends'],
  85. 'createTime' => $data['createTime'],
  86. 'addUserId' => $data['addUserId'],
  87. 'salesManId' => $data['salesManId'],
  88. 'specGroup' => $data['specGroup'],
  89. 'specType' => $data['specType']
  90. ];
  91. return $map;
  92. }
  93. public function cacheSku()
  94. {
  95. if (!$this->isUpdateSku) {
  96. return false;
  97. }
  98. $skuResult = $this->objDSku->select(['id' => array_values(array_unique($this->updateSku))]);
  99. if ($skuResult === false) {
  100. file_put_contents('/www/wwwroot/logs/api.junhailan.com/BasicAndSkuCache_error.log', date('Y-m-d H:i:s') . '错误原因|sql: ' . $this->objDSku->error() . PHP_EOL, FILE_APPEND);
  101. return true;
  102. }
  103. foreach ($skuResult as $sku) {
  104. $cacheMap = self::cacheMappersSku($sku);
  105. $this->objSkuCache->delSku($sku['id']);
  106. $cacheRes = $this->objSkuCache->cacheSku($sku['id'], $cacheMap);
  107. if (!$cacheRes->isSuccess()) {
  108. file_put_contents('/www/wwwroot/logs/api.junhailan.com/BasicAndSkuCache_error.log', date('Y-m-d H:i:s') . '缓存失败|error: ' . $sku['id'] . PHP_EOL, FILE_APPEND);
  109. return false;
  110. }
  111. }
  112. return true;
  113. }
  114. /**
  115. * @param array $data
  116. * @return array
  117. */
  118. public static function cacheMappersSku(array $data)
  119. {
  120. $map = [];
  121. if (empty($data)) {
  122. return $map;
  123. }
  124. $map = [
  125. 'id' => $data['id'],
  126. 'unitId' => $data['unitId'],
  127. 'unitName' => $data['unitName'],
  128. 'isMaster' => $data['isMaster'],
  129. 'conversion' => $data['conversion'],
  130. 'specImage' => $data['specImage'],
  131. 'specData' => $data['specData'],
  132. 'specGroupHash' => $data['specGroupHash'],
  133. 'barCode' => $data['barCode'],
  134. 'goodsId' => $data['goodsId']
  135. ];
  136. return $map;
  137. }
  138. }