StoreProductAttrResult.php 1.4 KB

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