GoodsLists.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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\businessapi\lists;
  20. use app\common\{enum\GoodsEnum,
  21. lists\ListsExtendInterface,
  22. model\Goods};
  23. /**
  24. * 商品列表接口
  25. * Class GoodsLists
  26. * @package app\adminapi\lists\goods
  27. */
  28. class GoodsLists extends BaseBusinesseDataLists implements ListsExtendInterface
  29. {
  30. /**
  31. * @notes 搜索条件
  32. * @return array
  33. * @author cjhao
  34. * @date 2021/7/22 10:51
  35. */
  36. public function setSearch(): array
  37. {
  38. return array_intersect(array_keys($this->params),['keyword','type']);
  39. }
  40. /**
  41. * @notes 统计信息
  42. * @return array
  43. * @throws \think\db\exception\DataNotFoundException
  44. * @throws \think\db\exception\DbException
  45. * @throws \think\db\exception\ModelNotFoundException
  46. * @author cjhao
  47. * @date 2021/7/22 10:51
  48. */
  49. public function extend(): array
  50. {
  51. $statistics = (new Goods())
  52. ->withSearch(array_diff(['type'],$this->setSearch()), $this->params)
  53. ->field('
  54. IFNULL(sum(if(status = 1,1,0)),0) as sales_count,
  55. IFNULL(sum(if(status = 1 and stock_warning > 0 and total_stock > 0 and stock_warning > total_stock,1,0)),0) as warning_count,
  56. IFNULL(sum(if(status = 1 and total_stock = 0,1,0)),0) as sellout_count,
  57. IFNULL(sum(if(status = 0,1,0)),0) as storage_count')
  58. ->select()->toArray();
  59. return array_shift($statistics);
  60. }
  61. /**
  62. * @notes 商品列表
  63. * @return array
  64. * @throws \think\db\exception\DataNotFoundException
  65. * @throws \think\db\exception\DbException
  66. * @throws \think\db\exception\ModelNotFoundException
  67. * @author cjhao
  68. * @date 2021/7/21 18:31
  69. */
  70. public function lists(): array
  71. {
  72. $list = (new Goods())
  73. ->withSearch($this->setSearch(), $this->params)
  74. ->limit($this->limitOffset, $this->limitLength)
  75. ->field('id,name,code,image,min_price,max_price,total_stock,virtual_sales_num+sales_num as sales_num,virtual_click_num+click_num as click_num,status,sort,spec_type,create_time')
  76. ->order('id desc')
  77. ->select()
  78. ->toArray();
  79. foreach ($list as $goodsKey => $goodsVal) {
  80. $list[$goodsKey]['status_desc'] = GoodsEnum::getStatusDesc($goodsVal['status']);
  81. //商品价格
  82. $list[$goodsKey]['price'] = '¥' . $goodsVal['min_price'];
  83. if ($goodsVal['min_price'] != $goodsVal['max_price']) {
  84. $list[$goodsKey]['price'] = '¥' . $goodsVal['min_price'] . '~' . '¥' . $goodsVal['max_price'];
  85. }
  86. }
  87. return $list;
  88. }
  89. /**
  90. * @notes 商品总数
  91. * @return int
  92. * @author cjhao
  93. * @date 2021/7/21 18:32
  94. */
  95. public function count(): int
  96. {
  97. return (new Goods())
  98. ->withSearch($this->setSearch(), $this->params)
  99. ->count();
  100. }
  101. }