params['bar_code'])) { $goodsIds = GoodsItem::where('bar_code', $this->params['bar_code'])->column('goods_id'); if ($goodsIds) { $searchWhere[] = ['g.id', 'in', $goodsIds]; } else { $searchWhere[] = ['g.id', '=', 0]; // 没有匹配的商品 } } // 关键词搜索 if (!empty($this->params['keyword'])) { $searchWhere[] = ['g.code|g.name', 'like', '%' . $this->params['keyword'] . '%']; } // 分类搜索 - 使用GoodsCategoryIndex表 if (!empty($this->params['category_id'])) { $goodsCategory = GoodsCategory::find($this->params['category_id']); $level = $goodsCategory['level'] ?? ''; $categoryIds = []; switch ($level) { case 1: $categoryIds = GoodsCategory::alias('A') ->join('goods_category B', 'A.id = B.pid') ->where(['A.pid' => $this->params['category_id']]) ->field('A.id as aid,B.id as bid') ->select()->toArray(); $categoryIds = array_merge(array_column($categoryIds, 'aid'), array_column($categoryIds, 'bid')); break; case 2: $categoryIds = GoodsCategory::where(['pid' => $this->params['category_id']]) ->column('id'); break; } $categoryIds = array_merge([(int)$this->params['category_id']], $categoryIds); $goodsIds = GoodsCategoryIndex::where([['category_id', 'in', $categoryIds]])->column('goods_id'); if ($goodsIds) { $searchWhere[] = ['g.id', 'in', $goodsIds]; } else { $searchWhere[] = ['g.id', '=', 0]; // 没有匹配的商品 } } // 供应商搜索 if (!empty($this->params['supplier_id'])) { $searchWhere[] = ['g.supplier_id', '=', $this->params['supplier_id']]; } // 商品类型搜索 if (!empty($this->params['goods_type'])) { $searchWhere[] = ['g.goods_type', '=', $this->params['goods_type']]; } // 状态搜索 if (isset($this->params['status']) && $this->params['status'] !== '') { $searchWhere[] = ['g.status', '=', $this->params['status']]; } return $searchWhere; } /** * @notes 获取商品规格价格列表 * @return array * @author * @date 2024/01/01 00:00 */ public function lists(): array { $searchWhere = $this->setSearch(); $lists = Goods::alias('g') ->leftJoin('goods_item gi', 'g.id = gi.goods_id') ->where($searchWhere) ->where('g.delete_time', null) ->field([ 'g.id as goods_id', 'g.code as goods_code', 'g.name as goods_name', 'gi.id as item_id', 'gi.spec_value_str', 'gi.sell_price', 'gi.lineation_price', 'gi.cost_price', 'gi.stock', 'gi.weight', 'gi.volume' ]) ->limit($this->limitOffset, $this->limitLength) ->order('g.id desc, gi.id asc') ->select() ->toArray(); return $lists; } /** * @notes 获取数量 * @return int * @author * @date 2024/01/01 00:00 */ public function count(): int { $searchWhere = $this->setSearch(); return Goods::alias('g') ->leftJoin('goods_item gi', 'g.id = gi.goods_id') ->where($searchWhere) ->where('g.delete_time', null) ->count(); } /** * @notes 设置导出字段 * @return array * @author * @date 2024/01/01 00:00 */ public function setExcelFields(): array { return [ 'goods_id' => '商品ID', 'goods_code' => '商品编码', 'goods_name' => '商品名称', 'item_id' => '规格ID', 'spec_value_str' => '规格名称', 'sell_price' => '销售价格', 'lineation_price' => '划线价格', 'cost_price' => '成本价格', 'stock' => '库存数量', 'weight' => '重量(kg)', 'volume' => '体积(m³)' ]; } /** * @notes 设置文件名 * @return string * @author * @date 2024/01/01 00:00 */ public function setFileName(): string { return '商品规格价格'; } }