StoreProductAttrResult.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. namespace app\admin\model\store;
  3. use crmeb\basic\BaseModel;
  4. use crmeb\traits\ModelTrait;
  5. class StoreProductAttrResult extends BaseModel
  6. {
  7. /**
  8. * 模型名称
  9. * @var string
  10. */
  11. protected $name = 'store_product_attr_result';
  12. use ModelTrait;
  13. protected $insert = ['change_time'];
  14. protected static function setChangeTimeAttr($value)
  15. {
  16. return time();
  17. }
  18. protected static function setResultAttr($value)
  19. {
  20. return is_array($value) ? json_encode($value) : $value;
  21. }
  22. public static function setResult($result, $product_id, $type = 0)
  23. {
  24. $result = self::setResultAttr($result);
  25. $change_time = self::setChangeTimeAttr(0);
  26. $count = self::where('product_id', $product_id)->where('type', $type)->count();
  27. $res = true;
  28. if ($count) $res = self::where('product_id', $product_id)->where('type', $type)->delete();
  29. if ($res) return self::insert(compact('product_id', 'result', 'change_time', 'type'), true);
  30. return $res;
  31. }
  32. public static function getResult($productId, int $type = 0)
  33. {
  34. return json_decode(self::where('product_id', $productId)->where('type', $type)->value('result'), true) ?: ['value' => []];
  35. }
  36. public static function clearResult($productId)
  37. {
  38. return self::where('product_id', $productId)->delete();
  39. }
  40. }