ProductAttrResult.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?php
  2. namespace app\model\system;
  3. use library\basic\BaseModel;
  4. use library\traits\ModelTrait;
  5. class ProductAttrResult extends BaseModel
  6. {
  7. /**
  8. * 模型名称
  9. * @var string
  10. */
  11. protected $name = '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. }