GoodsLists.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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\adminapi\lists\goods;
  20. use app\adminapi\{
  21. lists\BaseAdminDataLists,
  22. logic\goods\GoodsCategoryLogic,
  23. };
  24. use app\common\{enum\GoodsEnum,
  25. lists\ListsExcelInterface,
  26. lists\ListsExtendInterface,
  27. logic\GoodsActivityLogic,
  28. model\Goods};
  29. /**
  30. * 商品列表接口
  31. * Class GoodsLists
  32. * @package app\adminapi\lists\goods
  33. */
  34. class GoodsLists extends BaseAdminDataLists implements ListsExtendInterface,ListsExcelInterface
  35. {
  36. /**
  37. * @notes 搜索条件
  38. * @return array
  39. * @author cjhao
  40. * @date 2021/7/22 10:51
  41. */
  42. public function setSearch(): array
  43. {
  44. return array_intersect(array_keys($this->params),['bar_code', 'type','keyword','category_id','supplier_id','goods_type','activity_type']);
  45. }
  46. /**
  47. * @notes 统计信息
  48. * @return array
  49. * @throws \think\db\exception\DataNotFoundException
  50. * @throws \think\db\exception\DbException
  51. * @throws \think\db\exception\ModelNotFoundException
  52. * @author cjhao
  53. * @date 2021/7/22 10:51
  54. */
  55. public function extend(): array
  56. {
  57. $statistics = (new Goods())
  58. ->withSearch(array_diff(['type'],$this->setSearch()), $this->params)
  59. ->field('count(id) as all_count,
  60. IFNULL(sum(if(status = 1,1,0)),0) as sales_count,
  61. IFNULL(sum(if(status = 1 and stock_warning > 0 and total_stock > 0 and stock_warning > total_stock,1,0)),0) as warning_count,
  62. IFNULL(sum(if(status = 1 and total_stock = 0,1,0)),0) as sellout_count,
  63. IFNULL(sum(if(status = 0,1,0)),0) as storage_count')
  64. ->select()->toArray();
  65. return array_shift($statistics);
  66. }
  67. /**
  68. * @notes 商品列表
  69. * @return array
  70. * @throws \think\db\exception\DataNotFoundException
  71. * @throws \think\db\exception\DbException
  72. * @throws \think\db\exception\ModelNotFoundException
  73. * @author cjhao
  74. * @date 2021/7/21 18:31
  75. */
  76. public function lists(): array
  77. {
  78. $list = (new Goods())
  79. ->withSearch($this->setSearch(), $this->params)
  80. ->with(['goods_category_index'])
  81. ->limit($this->limitOffset, $this->limitLength)
  82. ->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')
  83. ->order('id desc')
  84. ->select()
  85. ->toArray();
  86. //获取参与活动的商品
  87. $goodsIds = array_column($list,'id');
  88. $goodsActivityList = GoodsActivityLogic::activityInfo($goodsIds);
  89. //获取全部商品分类
  90. $categoryList = GoodsCategoryLogic::getCategoryNameLists();
  91. foreach ($list as $goodsKey => $goodsVal) {
  92. $list[$goodsKey]['status_desc'] = GoodsEnum::getStatusDesc($goodsVal['status']);
  93. //商品价格
  94. $list[$goodsKey]['price'] = '¥' . $goodsVal['min_price'];
  95. if ($goodsVal['min_price'] != $goodsVal['max_price']) {
  96. $list[$goodsKey]['price'] = '¥' . $goodsVal['min_price'] . '~' . '¥' . $goodsVal['max_price'];
  97. }
  98. //商品分类
  99. $categoryName = array_map(function ($categoryIndex) use ($categoryList) {
  100. return $categoryList[$categoryIndex['category_id']]['category_name'] ?? '';
  101. }, $goodsVal['goods_category_index']);
  102. $list[$goodsKey]['category_name'] = $categoryName;
  103. unset($list[$goodsKey]['goods_category_index']);
  104. //商品参加了哪些活动
  105. $list[$goodsKey]['goods_activity'] = array_keys($goodsActivityList[$goodsVal['id']] ?? []);
  106. }
  107. return $list;
  108. }
  109. /**
  110. * @notes 商品总数
  111. * @return int
  112. * @author cjhao
  113. * @date 2021/7/21 18:32
  114. */
  115. public function count(): int
  116. {
  117. return (new Goods())
  118. ->withSearch($this->setSearch(), $this->params)
  119. ->count();
  120. }
  121. /**
  122. * @notes 设置excel表名
  123. * @return string
  124. * @author cjhao
  125. * @date 2021/9/23 9:52
  126. */
  127. public function setFileName(): string
  128. {
  129. return '商品列表';
  130. }
  131. /**
  132. * @notes 设置导出字段
  133. * @return array
  134. * @author cjhao
  135. * @date 2021/9/23 9:59
  136. */
  137. public function setExcelFields(): array
  138. {
  139. return [
  140. 'code' => '商品编号',
  141. 'name' => '商品名称',
  142. 'price' => '价格',
  143. 'total_stock' => '库存',
  144. 'sales_num' => '销量',
  145. 'click_num' => '浏览量',
  146. 'status_desc' => '销售状态',
  147. 'sort' => '排序',
  148. 'create_time' => '创建时间',
  149. ];
  150. }
  151. }