CustomBlockGoods.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. declare (strict_types=1);
  3. namespace app\common\model;
  4. use app\common\library\helper;
  5. /**
  6. * 首页自定义模块商品
  7. * Class CustomBlockGoods
  8. * @package app\common\model
  9. * Author: zhangs
  10. * DateTime: 2021/9/24 16:45
  11. */
  12. class CustomBlockGoods extends BaseModel
  13. {
  14. protected $name = 'custom_block_goods';
  15. // 定义主键
  16. protected $pk = 'id';
  17. public function goods()
  18. {
  19. return $this->hasOne('Goods','goods_id','goods_id')->with(['images' => ['file'], 'skuList' => ['image']]);
  20. }
  21. public function customBlocks()
  22. {
  23. return $this->belongsTo('CustomBlocks', 'block_id');
  24. }
  25. public function getList(array $param = [])
  26. {
  27. // 检索查询条件
  28. $query = $this->setQueryFilter($param);
  29. // 排序条件
  30. $sort = $this->setQuerySort($param);
  31. // 查询列表数据
  32. return $query->with(['goods'])
  33. ->alias($this->name)
  34. // ->field(["$this->name.*,custom_blocks.title as block_title,provider.provider_name as provider_name"])
  35. ->field(["$this->name.*,provider.provider_name as provider_name"])
  36. ->join('goods', "goods.goods_id = {$this->name}.goods_id")
  37. // ->join('custom_blocks', "custom_blocks.id = {$this->name}.block_id")
  38. ->join('provider', "provider.provider_id = goods.provider_id")
  39. ->order($sort)
  40. ->paginate(15)
  41. ->each(function ($item) {
  42. if (!empty($item['goods'])) {
  43. $goodsInfo = $item['goods'];
  44. // 商品图片列表
  45. $goodsInfo['goods_images'] = helper::getArrayColumn($goodsInfo['images'], 'file');
  46. // 商品主图
  47. $goodsInfo['goods_image'] = current($goodsInfo['goods_images'])['preview_url'];
  48. $goods = [
  49. 'goods_id' => $goodsInfo['goods_id'],
  50. 'goods_name' => $goodsInfo['goods_name'],
  51. 'spec_type' => $goodsInfo['spec_type'],
  52. 'goods_price_min' => $goodsInfo['goods_price_min'],
  53. 'goods_price_max' => $goodsInfo['goods_price_max'],
  54. 'line_price_min' => $goodsInfo['line_price_min'],
  55. 'line_price_max' => $goodsInfo['line_price_max'],
  56. 'goods_images' => $goodsInfo['goods_images'],
  57. 'goods_image' => $goodsInfo['goods_image'],
  58. 'stock_total' => $goodsInfo['stock_total'],
  59. 'goods_no' => $goodsInfo['goods_no'],
  60. 'status' => $goodsInfo['status'],
  61. ];
  62. unset($item['goods']);
  63. $item['goods'] = $goods;
  64. }
  65. });
  66. // return $query->alias($this->name)
  67. // ->field(["$this->name.*"])
  68. // ->join('goods', "goods.goods_id = {$this->name}.goods_id")
  69. // ->order(["{$this->name}.sort" => 'asc', "{$this->name}.create_time" => 'desc'])
  70. // ->paginate(15);
  71. }
  72. public function setQuerySort($param = [])
  73. {
  74. $params = $this->setQueryDefaultValue($param, [
  75. 'sortField' => "create_time", // 排序字段 block_title-版块标题 create_time-创建时间 sort-排序
  76. 'sortOrder' => 'descend' // 排序方式 descend-倒序 ascend-顺序
  77. ]);
  78. $sort = ["block_title" => 'asc'];
  79. if (in_array($params['sortOrder'], ['descend','ascend']) && in_array($params['sortField'], ['block_title','create_time','sort'])) {
  80. $sortField = "{$this->name}.".$params['sortField'];
  81. if ($params['sortField'] == 'block_title') {
  82. $sortField = "block_title";
  83. }
  84. $sort = [$sortField => str_replace(['descend','ascend'],['desc','asc'], $params['sortOrder'])];
  85. }
  86. return array_merge($sort, [$this->name.".".$this->getPk() => 'desc']);
  87. }
  88. /**
  89. * 检索查询条件
  90. * @param array $param
  91. * @return \think\db\BaseQuery
  92. */
  93. private function setQueryFilter(array $param)
  94. {
  95. // 实例化查询对象
  96. $query = $this->getNewQuery();
  97. // 查询参数
  98. $params = $this->setQueryDefaultValue($param, [
  99. 'block_id' => 0, // 版块id
  100. 'goods_name' => '', // 商品名称/编码
  101. 'provider_name' => '', // 商品名称/编码
  102. ]);
  103. // 版块id
  104. // !empty($params['block_id']) && $query->where('block_id', $params['block_id']);
  105. // 商品名称/编码
  106. !empty($params['goods_name']) && $query->where('goods.goods_name|goods.goods_no', 'like', "%{$params['goods_name']}%");
  107. !empty($params['provider_name']) && $query->where('provider.provider_name', 'like', "%{$params['provider_name']}%");
  108. return $query;
  109. }
  110. /**
  111. * 详情
  112. * @param int $id
  113. * @return null|static
  114. */
  115. public static function detail(int $id)
  116. {
  117. return self::get($id);
  118. }
  119. public static function getByGoodsIdBlockId(int $blockId, int $goodsId)
  120. {
  121. return self::where(['goods_id'=>$goodsId,'block_id'=>$blockId])->find();
  122. }
  123. /**
  124. * 添加新记录
  125. * @param $data
  126. * @return false|int
  127. */
  128. public function add($data)
  129. {
  130. if (empty($data)) {
  131. throwError('参数无效');
  132. }
  133. // 先删除
  134. $this->where(['goods_id'=>$data['goods_id'],'block_id'=>$data['block_id']])->delete();
  135. // 新增数据
  136. $this->save([
  137. 'goods_id' => $data['goods_id'],
  138. 'block_id' => $data['block_id'],
  139. 'sort' => $data['sort'],
  140. ]);
  141. return true;
  142. }
  143. /**
  144. * 编辑记录
  145. * @param $data
  146. * @return mixed
  147. */
  148. public function edit($data)
  149. {
  150. return $this->save($data) !== false;
  151. }
  152. }