BargainLists.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeshop100%开源免费商用商城系统
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | 商业版本务必购买商业授权,以免引起法律纠纷
  8. // | 禁止对系统程序代码以任何目的,任何形式的再发布
  9. // | gitee下载:https://gitee.com/likeshop_gitee
  10. // | github下载:https://github.com/likeshop-github
  11. // | 访问官网:https://www.likeshop.cn
  12. // | 访问社区:https://home.likeshop.cn
  13. // | 访问手册:http://doc.likeshop.cn
  14. // | 微信公众号:likeshop技术社区
  15. // | likeshop团队 版权所有 拥有最终解释权
  16. // +----------------------------------------------------------------------
  17. // | author: likeshopTeam
  18. // +----------------------------------------------------------------------
  19. namespace app\shopapi\lists;
  20. use app\common\enum\BargainEnum;
  21. use app\common\enum\GoodsEnum;
  22. use app\common\lists\BaseDataLists;
  23. use app\common\lists\ListsExtendInterface;
  24. use app\common\model\BargainGoods;
  25. use app\common\model\BargainInitiate;
  26. use app\common\service\FileService;
  27. /**
  28. * 砍价活动列表
  29. * Class BargainLists
  30. * @package app\shopapi\lists
  31. */
  32. class BargainLists extends BaseDataLists implements ListsExtendInterface
  33. {
  34. /**
  35. * @notes 统计数据
  36. * @return mixed|void
  37. * @author Tab
  38. * @date 2021/8/28 15:14
  39. */
  40. public function extend()
  41. {
  42. // 砍价成功数量
  43. $success = BargainInitiate::where('status', BargainEnum::STATUS_SUCCESS)->count();
  44. return [
  45. 'success' => $success
  46. ];
  47. }
  48. /**
  49. * @notes 设置搜索
  50. * @author Tab
  51. * @date 2021/8/28 15:13
  52. */
  53. public function setSearch()
  54. {
  55. $this->searchWhere = [
  56. // 进行中状态
  57. ['ba.status', '=', BargainEnum::ACTIVITY_STATUS_ING],
  58. // 已到活动开始时间
  59. ['ba.start_time', '<=', time()],
  60. // 未到活动结束时间
  61. ['ba.end_time', '>', time()],
  62. // 商品状态须为已上架
  63. ['g.status', '=', GoodsEnum::STATUS_SELL],
  64. ];
  65. // 越临近结束的砍价越靠前
  66. $this->sortOrder = ['ba.end_time' => 'asc'];
  67. }
  68. /**
  69. * @notes 列表
  70. * @return array
  71. * @author Tab
  72. * @date 2021/8/28 15:13
  73. */
  74. public function lists(): array
  75. {
  76. // 初始化搜索
  77. $this->setSearch();
  78. $field = [
  79. 'g.image' => 'goods_image',
  80. 'g.name' => 'goods_name',
  81. 'g.max_price' => 'goods_max_price',
  82. 'ba.end_time',
  83. 'bg.activity_id' => 'bargain_min_price',
  84. 'bg.activity_id',
  85. 'bg.goods_id',
  86. ];
  87. $lists = BargainGoods::alias('bg')
  88. ->leftJoin('goods g', 'g.id = bg.goods_id')
  89. ->leftJoin('bargain_activity ba', 'ba.id = bg.activity_id')
  90. ->distinct(true)
  91. ->field($field)
  92. ->where($this->searchWhere)
  93. ->order($this->sortOrder)
  94. ->limit($this->limitOffset, $this->limitLength)
  95. ->select()
  96. ->toArray();
  97. foreach ($lists as &$item) {
  98. $item['goods_image'] = FileService::getFileUrl($item['goods_image']);
  99. }
  100. return $lists;
  101. }
  102. /**
  103. * @notes 记录数
  104. * @return int
  105. * @author Tab
  106. * @date 2021/8/28 15:13
  107. */
  108. public function count(): int
  109. {
  110. // 初始化搜索
  111. $this->setSearch();
  112. $field = [
  113. 'g.image' => 'goods_image',
  114. 'g.name' => 'goods_name',
  115. 'g.max_price' => 'goods_max_price',
  116. 'ba.end_time',
  117. 'bg.activity_id' => 'bargain_min_price',
  118. ];
  119. $listsNoPage = BargainGoods::alias('bg')
  120. ->leftJoin('goods g', 'g.id = bg.goods_id')
  121. ->leftJoin('bargain_activity ba', 'ba.id = bg.activity_id')
  122. ->distinct(true)
  123. ->field($field)
  124. ->where($this->searchWhere)
  125. ->order($this->sortOrder)
  126. ->select()
  127. ->toArray();
  128. return count($listsNoPage);
  129. }
  130. }