123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- <?php
- namespace JinDouYun\Model\Goods;
- /**
- * 缓存商品
- * Trait BasicAndSkuCache
- * @package JinDouYun\Model\Goods
- */
- trait BasicAndSkuCache
- {
- /**
- * @var bool 是否更新缓存
- */
- private $isUpdateBasicCache = false;
- /**
- * @var null|int 更新对象id,基础资料
- */
- private $objectId = null;
- /**
- * @var bool sku缓存
- */
- private $isUpdateSku = false;
- /**
- * @var array 更新缓存skuIds
- */
- private $updateSku = [];
- /**
- * @return bool
- */
- public function cacheBasic()
- {
- if (!$this->isUpdateBasicCache) {
- return false;
- }
- $sql = "SELECT c.title as categoryName,b.*,d.title as brandName
- FROM qianniao_goods_basic_" . $this->onlineEnterpriseId . " as b
- LEFT JOIN qianniao_goods_category_" . $this->onlineEnterpriseId . " as c
- ON c.id=b.categoryId
- LEFT JOIN qianniao_goods_brand_" . $this->onlineEnterpriseId . " as d
- ON b.brandId=d.id WHERE b.id = " . $this->objectId;
- $goodsBasicResult = $this->objDGoodsBasic->query($sql);
- if ($goodsBasicResult === false) {
- 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);
- return false;
- }
- if (!empty($goodsBasicResult)) {
- $this->objGoodsBasicRelevant->deleteBasicKeyById($this->objectId);
- //组装缓存数据
- $goodsBasicResult = (array)$goodsBasicResult;
- $cacheMap = array_shift($goodsBasicResult);
- $cacheResult = $this->objGoodsBasicRelevant->cacheBasicIdRelationName($cacheMap, $this->objectId);
- if (!$cacheResult->isSuccess()) {
- //缓存写入失败
- 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);
- return false;
- }
- }
- return true;
- }
- /**
- * @param array $data
- * @return array
- */
- public static function cacheMappers(array $data)
- {
- $map = [];
- if (empty($data)) {
- return $map;
- }
- $map = [
- 'id' => $data['id'],
- 'title' => $data['title'],
- 'images' => $data['images'],
- 'condition' => $data['condition'],
- 'code' => $data['code'],
- 'categoryId' => $data['categoryId'],
- 'categoryName' => $data['categoryName'],
- 'categoryPath' => $data['categoryPath'],
- 'expireTime' => $data['expireTime'],
- 'brandId' => $data['brandId'],
- 'brandName' => $data['brandName'],
- 'describe' => $data['describe'],
- 'description' => $data['description'],
- 'noSalesShop' => $data['noSalesShop'],
- 'extends' => $data['extends'],
- 'createTime' => $data['createTime'],
- 'addUserId' => $data['addUserId'],
- 'salesManId' => $data['salesManId'],
- 'specGroup' => $data['specGroup'],
- 'specType' => $data['specType']
- ];
- return $map;
- }
- public function cacheSku()
- {
- if (!$this->isUpdateSku) {
- return false;
- }
- $skuResult = $this->objDSku->select(['id' => array_values(array_unique($this->updateSku))]);
- if ($skuResult === false) {
- 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);
- return true;
- }
- foreach ($skuResult as $sku) {
- $cacheMap = self::cacheMappersSku($sku);
- $this->objSkuCache->delSku($sku['id']);
- $cacheRes = $this->objSkuCache->cacheSku($sku['id'], $cacheMap);
- if (!$cacheRes->isSuccess()) {
- 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);
- return false;
- }
- }
- return true;
- }
- /**
- * @param array $data
- * @return array
- */
- public static function cacheMappersSku(array $data)
- {
- $map = [];
- if (empty($data)) {
- return $map;
- }
- $map = [
- 'id' => $data['id'],
- 'unitId' => $data['unitId'],
- 'unitName' => $data['unitName'],
- 'isMaster' => $data['isMaster'],
- 'conversion' => $data['conversion'],
- 'specImage' => $data['specImage'],
- 'specData' => $data['specData'],
- 'specGroupHash' => $data['specGroupHash'],
- 'barCode' => $data['barCode'],
- 'goodsId' => $data['goodsId']
- ];
- return $map;
- }
- }
|