CustomBlocks.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. declare (strict_types=1);
  3. namespace app\common\model;
  4. use app\common\library\helper;
  5. /**
  6. * 首页自定义模块
  7. * Class CustomBlocks
  8. * @package app\common\model
  9. * Author: zhangs
  10. * DateTime: 2021/9/24 16:45
  11. */
  12. class CustomBlocks extends BaseModel
  13. {
  14. // 商品图展示模式
  15. const IMG_MODE = [0 => '小图模式', 1 => '中图模式', 2 => '大图模式'];
  16. const SORT_TYPE = [0 => '新品', 1 => '销量', 2 => '综合'];
  17. protected $name = 'custom_blocks';
  18. // 定义主键
  19. protected $pk = 'id';
  20. protected $append = ['img_mode_text', 'sort_type_text'];
  21. /**
  22. * Notes:关联自定义模块商品数据
  23. * Author: zhangs
  24. * DateTime: 2021/9/24 16:57
  25. * @return \think\model\relation\HasMany
  26. */
  27. public function customBlockGoods()
  28. {
  29. return $this->hasMany(CustomBlockGoods::class, 'block_id')->order(['sort' => 'asc', 'create_time' => 'desc']);
  30. }
  31. public function getImgModeTextAttr($value, $data)
  32. {
  33. return self::IMG_MODE[$data['img_mode']] ?? '';
  34. }
  35. public function getSortTypeTextAttr($value, $data)
  36. {
  37. return self::SORT_TYPE[$data['sort_type']] ?? '';
  38. }
  39. /**
  40. * 获取全部记录
  41. * @param array $param
  42. * @return \think\Collection
  43. * @throws \think\db\exception\DataNotFoundException
  44. * @throws \think\db\exception\DbException
  45. * @throws \think\db\exception\ModelNotFoundException
  46. */
  47. public function getAll($param = [])
  48. {
  49. // 检索查询条件
  50. $filter = $this->getFilter($param);
  51. // 查询列表数据
  52. return $this->where($filter)->order(['sort', $this->getPk()])->select();
  53. }
  54. /**
  55. * 获取列表
  56. * @param array $param
  57. * @return \think\Paginator
  58. * @throws \think\db\exception\DbException
  59. */
  60. public function getList($param = [])
  61. {
  62. // 检索查询调价你
  63. $filter = $this->getFilter($param);
  64. // 查询列表数据
  65. return $this->where($filter)->order(['sort', 'id'])->paginate(15)
  66. ->each(function ($item) {
  67. $item->goods_count = CustomBlockGoods::alias('cbg')
  68. ->leftJoin('goods', 'goods.goods_id=cbg.goods_id')
  69. ->where('goods.is_delete', 0)
  70. ->where('goods.status', 10)
  71. ->where('cbg.block_id', $item->id)
  72. ->count();
  73. });
  74. }
  75. /**
  76. * 检索查询条件
  77. * @param array $param
  78. * @return array
  79. */
  80. private function getFilter($param = [])
  81. {
  82. // 默认查询条件
  83. $params = $this->setQueryDefaultValue($param, ['title' => '']);
  84. // 检索查询条件
  85. $filter = [];
  86. !empty($params['title']) && $filter[] = ['title', 'like', "%{$params['title']}%"];
  87. return $filter;
  88. }
  89. /**
  90. * 详情
  91. * @param int $id
  92. * @return null|static
  93. */
  94. public static function detail(int $id)
  95. {
  96. return self::get($id);
  97. }
  98. /**
  99. * 添加新记录
  100. * @param $data
  101. * @return false|int
  102. */
  103. public function add($data)
  104. {
  105. return $this->save($data);
  106. }
  107. /**
  108. * 编辑记录
  109. * @param $data
  110. * @return mixed
  111. */
  112. public function edit($data)
  113. {
  114. return $this->save($data) !== false;
  115. }
  116. }