StoreDescription.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: 吴昊天
  5. * Date: 2020-03-16
  6. * Time: 12:35
  7. */
  8. namespace app\models\store;
  9. use crmeb\basic\BaseModel;
  10. use crmeb\traits\ModelTrait;
  11. class StoreDescription extends BaseModel
  12. {
  13. /**
  14. * 模型名称
  15. * @var string
  16. */
  17. protected $name = 'store_product_description';
  18. use ModelTrait;
  19. /**
  20. * 获取详情
  21. * @param $product_id
  22. * @param int $type
  23. * @return mixed
  24. */
  25. public static function getDescription($product_id, $type = 0)
  26. {
  27. $description = self::where('product_id', $product_id)->where('type', $type)->value('description');
  28. return htmlspecialchars_decode($description);
  29. }
  30. /**
  31. * 添加或者修改详情
  32. * @param string $description
  33. * @param int $product_id
  34. * @param int $type
  35. * @return bool|\think\Model|static
  36. * @throws \think\db\exception\DataNotFoundException
  37. * @throws \think\db\exception\DbException
  38. * @throws \think\db\exception\ModelNotFoundException
  39. */
  40. public static function saveDescription(string $description = '', int $product_id = 0, int $type = 0)
  41. {
  42. $description = htmlspecialchars($description);
  43. if ($product_id) {
  44. $info = self::where(['product_id' => $product_id, 'type' => $type])->find();
  45. if ($info) {
  46. $info->description = $description;
  47. return $info->save();
  48. }
  49. }
  50. return self::create(compact('description', 'product_id', 'type'));
  51. }
  52. }