GoodsSpecPriceLists.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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\lists\BaseAdminDataLists;
  21. use app\common\lists\ListsExcelInterface;
  22. use app\common\lists\ListsSearchInterface;
  23. use app\common\model\Goods;
  24. use app\common\model\GoodsCategory;
  25. use app\common\model\GoodsCategoryIndex;
  26. use app\common\model\GoodsItem;
  27. /**
  28. * 商品规格价格列表
  29. * Class GoodsSpecPriceLists
  30. * @package app\adminapi\lists\goods
  31. */
  32. class GoodsSpecPriceLists extends BaseAdminDataLists implements ListsSearchInterface, ListsExcelInterface
  33. {
  34. /**
  35. * @notes 搜索条件
  36. * @return array
  37. * @author
  38. * @date 2024/01/01 00:00
  39. */
  40. public function setSearch(): array
  41. {
  42. $searchWhere = [];
  43. // 条形码搜索
  44. if (!empty($this->params['bar_code'])) {
  45. $goodsIds = GoodsItem::where('bar_code', $this->params['bar_code'])->column('goods_id');
  46. if ($goodsIds) {
  47. $searchWhere[] = ['g.id', 'in', $goodsIds];
  48. } else {
  49. $searchWhere[] = ['g.id', '=', 0]; // 没有匹配的商品
  50. }
  51. }
  52. // 关键词搜索
  53. if (!empty($this->params['keyword'])) {
  54. $searchWhere[] = ['g.code|g.name', 'like', '%' . $this->params['keyword'] . '%'];
  55. }
  56. // 分类搜索 - 使用GoodsCategoryIndex表
  57. if (!empty($this->params['category_id'])) {
  58. $goodsCategory = GoodsCategory::find($this->params['category_id']);
  59. $level = $goodsCategory['level'] ?? '';
  60. $categoryIds = [];
  61. switch ($level) {
  62. case 1:
  63. $categoryIds = GoodsCategory::alias('A')
  64. ->join('goods_category B', 'A.id = B.pid')
  65. ->where(['A.pid' => $this->params['category_id']])
  66. ->field('A.id as aid,B.id as bid')
  67. ->select()->toArray();
  68. $categoryIds = array_merge(array_column($categoryIds, 'aid'), array_column($categoryIds, 'bid'));
  69. break;
  70. case 2:
  71. $categoryIds = GoodsCategory::where(['pid' => $this->params['category_id']])
  72. ->column('id');
  73. break;
  74. }
  75. $categoryIds = array_merge([(int)$this->params['category_id']], $categoryIds);
  76. $goodsIds = GoodsCategoryIndex::where([['category_id', 'in', $categoryIds]])->column('goods_id');
  77. if ($goodsIds) {
  78. $searchWhere[] = ['g.id', 'in', $goodsIds];
  79. } else {
  80. $searchWhere[] = ['g.id', '=', 0]; // 没有匹配的商品
  81. }
  82. }
  83. // 供应商搜索
  84. if (!empty($this->params['supplier_id'])) {
  85. $searchWhere[] = ['g.supplier_id', '=', $this->params['supplier_id']];
  86. }
  87. // 商品类型搜索
  88. if (!empty($this->params['goods_type'])) {
  89. $searchWhere[] = ['g.goods_type', '=', $this->params['goods_type']];
  90. }
  91. // 状态搜索
  92. if (isset($this->params['status']) && $this->params['status'] !== '') {
  93. $searchWhere[] = ['g.status', '=', $this->params['status']];
  94. }
  95. return $searchWhere;
  96. }
  97. /**
  98. * @notes 获取商品规格价格列表
  99. * @return array
  100. * @author
  101. * @date 2024/01/01 00:00
  102. */
  103. public function lists(): array
  104. {
  105. $searchWhere = $this->setSearch();
  106. $lists = Goods::alias('g')
  107. ->leftJoin('goods_item gi', 'g.id = gi.goods_id')
  108. ->where($searchWhere)
  109. ->where('g.delete_time', null)
  110. ->field([
  111. 'g.id as goods_id',
  112. 'g.code as goods_code',
  113. 'g.name as goods_name',
  114. 'gi.id as item_id',
  115. 'gi.spec_value_str',
  116. 'gi.sell_price',
  117. 'gi.lineation_price',
  118. 'gi.cost_price',
  119. 'gi.stock',
  120. 'gi.weight',
  121. 'gi.volume'
  122. ])
  123. ->limit($this->limitOffset, $this->limitLength)
  124. ->order('g.id desc, gi.id asc')
  125. ->select()
  126. ->toArray();
  127. return $lists;
  128. }
  129. /**
  130. * @notes 获取数量
  131. * @return int
  132. * @author
  133. * @date 2024/01/01 00:00
  134. */
  135. public function count(): int
  136. {
  137. $searchWhere = $this->setSearch();
  138. return Goods::alias('g')
  139. ->leftJoin('goods_item gi', 'g.id = gi.goods_id')
  140. ->where($searchWhere)
  141. ->where('g.delete_time', null)
  142. ->count();
  143. }
  144. /**
  145. * @notes 设置导出字段
  146. * @return array
  147. * @author
  148. * @date 2024/01/01 00:00
  149. */
  150. public function setExcelFields(): array
  151. {
  152. return [
  153. 'goods_id' => '商品ID',
  154. 'goods_code' => '商品编码',
  155. 'goods_name' => '商品名称',
  156. 'item_id' => '规格ID',
  157. 'spec_value_str' => '规格名称',
  158. 'sell_price' => '销售价格',
  159. 'lineation_price' => '划线价格',
  160. 'cost_price' => '成本价格',
  161. 'stock' => '库存数量',
  162. 'weight' => '重量(kg)',
  163. 'volume' => '体积(m³)'
  164. ];
  165. }
  166. /**
  167. * @notes 设置文件名
  168. * @return string
  169. * @author
  170. * @date 2024/01/01 00:00
  171. */
  172. public function setFileName(): string
  173. {
  174. return '商品规格价格';
  175. }
  176. }