GoodsSku.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 app\common\enum\goods\Status as GoodsStatusEnum;
  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()
  31. {
  32. return $this->hasOne('UploadFile', 'file_id', 'image_id');
  33. }
  34. public function goods()
  35. {
  36. return $this->belongsTo('Goods');
  37. }
  38. /**
  39. * 获取器:规格值ID集
  40. * @param $value
  41. * @return array
  42. */
  43. public function getSpecValueIdsAttr($value)
  44. {
  45. return helper::jsonDecode($value);
  46. }
  47. /**
  48. * 获取器:规格属性
  49. * @param $value
  50. * @return array
  51. */
  52. public function getGoodsPropsAttr($value)
  53. {
  54. return helper::jsonDecode($value);
  55. }
  56. /**
  57. * 设置器:规格值ID集
  58. * @param $value
  59. * @return string
  60. */
  61. public function setSpecValueIdsAttr($value)
  62. {
  63. return helper::jsonEncode($value);
  64. }
  65. /**
  66. * 设置器:规格属性
  67. * @param $value
  68. * @return string
  69. */
  70. public function setGoodsPropsAttr($value)
  71. {
  72. return helper::jsonEncode($value);
  73. }
  74. /**
  75. * 获取sku信息详情
  76. * @param int $goodsId
  77. * @param string $goodsSkuId
  78. * @return array|null|static
  79. */
  80. public static function detail(int $goodsId, $goodsSkuId)
  81. {
  82. return static::get(['goods_id' => $goodsId, 'goods_sku_id' => $goodsSkuId]);
  83. }
  84. public function getPickerList(array $param = [], int $listRows = 15)
  85. {
  86. // 筛选条件
  87. $query = $this->getQueryFilter($param);
  88. // 执行查询
  89. $list = $query->with(['goods','goods.images.file'])
  90. ->alias($this->name)
  91. ->leftJoin('goods', "goods.goods_id = {$this->name}.goods_id")
  92. ->field($this->getAliasFields($this->name, ['content']))
  93. ->where('goods.is_delete', '=', 0)
  94. ->where('goods.status', '=', GoodsStatusEnum::ON_SALE)
  95. ->order('goods.goods_id', 'asc')
  96. ->paginate($listRows)->each(function(&$item){
  97. $item['preview_url'] = '';
  98. if ($item['goods']['images'] && count($item['goods']['images'])){
  99. $item['preview_url'] = $item['goods']['images'][0]['file']['preview_url'];
  100. }
  101. unset($item['goods']['images']);
  102. });
  103. return $list;
  104. }
  105. /**
  106. * 检索查询条件
  107. * @param array $param
  108. * @return \think\db\BaseQuery
  109. */
  110. private function getQueryFilter(array $param)
  111. {
  112. // 商品列表获取条件
  113. $params = $this->setQueryDefaultValue($param, [
  114. 'goods_name' => null, // 商品名称
  115. 'goods_type'=>10 //普通商品 or赠品
  116. ]);
  117. // 实例化新查询对象
  118. $query = $this->getNewQuery();
  119. // 筛选条件
  120. $filter = [];
  121. // 商品名称
  122. !empty($params['goods_name']) && $filter[] = ['goods.goods_name|goods.goods_no|goods.goods_id', 'like', "%{$params['goods_name']}%"];
  123. //普通商品还是赠品
  124. in_array($params['goods_type'],[10,20]) && $filter[] = ['goods_type','=',$params['goods_type']];
  125. if($params['goods_type']==40)$filter[] = ['goods_type','in',[10,30]];
  126. //商品不包含的数据id
  127. !empty($params['not_goods_id_arr']) && $filter[] = ['goods.goods_id','not in',$params['not_goods_id_arr']];
  128. // 商品SKU
  129. !empty($params['goods_sku_no']) && $filter[] = ['goods_sku.goods_sku_no', 'like', "%{$params['goods_sku_no']}%"];
  130. !empty($params['single']) && $filter[] = ['goods_sku.goods_sku_id', '=', 0];
  131. // 实例化新查询对象
  132. return $query->where($filter);
  133. }
  134. }