GoodsSku.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\common\model;
  13. use cores\BaseModel;
  14. use app\common\library\helper;
  15. /**
  16. * 商品SKU模型
  17. * Class GoodsSku
  18. * @package app\common\model
  19. */
  20. class GoodsSku extends BaseModel
  21. {
  22. // 定义表名
  23. protected $name = 'goods_sku';
  24. // 定义主键
  25. protected $pk = 'id';
  26. /**
  27. * 关联模型:规格图片
  28. * @return \think\model\relation\HasOne
  29. */
  30. public function image(): \think\model\relation\HasOne
  31. {
  32. return $this->hasOne('UploadFile', 'file_id', 'image_id');
  33. }
  34. /**
  35. * 获取器:规格值ID集
  36. * @param $value
  37. * @return array|mixed
  38. */
  39. public function getSpecValueIdsAttr($value)
  40. {
  41. return helper::jsonDecode($value);
  42. }
  43. /**
  44. * 获取器:规格属性
  45. * @param $value
  46. * @return array|mixed
  47. */
  48. public function getGoodsPropsAttr($value)
  49. {
  50. return helper::jsonDecode($value);
  51. }
  52. /**
  53. * 设置器:规格值ID集
  54. * @param $value
  55. * @return string
  56. */
  57. public function setSpecValueIdsAttr($value): string
  58. {
  59. return helper::jsonEncode($value);
  60. }
  61. /**
  62. * 设置器:规格属性
  63. * @param $value
  64. * @return string
  65. */
  66. public function setGoodsPropsAttr($value): string
  67. {
  68. return helper::jsonEncode($value);
  69. }
  70. /**
  71. * 获取sku信息详情
  72. * @param int $goodsId
  73. * @param string $goodsSkuId
  74. * @return array|null|static
  75. */
  76. public static function detail(int $goodsId, string $goodsSkuId)
  77. {
  78. return static::get(['goods_id' => $goodsId, 'goods_sku_id' => $goodsSkuId]);
  79. }
  80. /**
  81. * 获取商品SKU列表
  82. * @param int $goodsId 商品ID
  83. * @return \think\Collection
  84. * @throws \think\db\exception\DataNotFoundException
  85. * @throws \think\db\exception\DbException
  86. * @throws \think\db\exception\ModelNotFoundException
  87. */
  88. public static function getSkuList(int $goodsId): \think\Collection
  89. {
  90. return (new static)->where('goods_id', '=', $goodsId)->select();
  91. }
  92. }