getPk(); $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]); }); } public function getTypeList() { return ['1' => '书币充值', '2' => 'VIP充值']; } public function getShowTypeList() { return ['0' => '全部用户', '1' => '已充值用户', '2' => '未充值用户', '3' => '新用户', '4' => '老用户']; } public function getTypeTextAttr($value, $data) { $value = $value ? $value : $data['type']; $list = $this->getTypeList(); return isset($list[$value]) ? $list[$value] : ''; } public function getShowTypeTextAttr($value, $data) { $value = $value ? $value : $data['show_type']; $list = $this->getShowTypeList(); return isset($list[$value]) ? $list[$value] : ''; } public function category() { return $this->belongsTo('Category', 'category_id', 'id')->setEagerlyType(0); } /** * 获取分类列表 */ public function getCategoryList() { $category = model('Category'); // 获取商品父类 $goodsCategory = $category->field('id')->where('nickname', '=', 'goods')->findOrFail(); // 充值下的分类 $list = $category ->where('pid', '=', $goodsCategory['id']) ->where('status', '=', 'normal') ->order('weigh desc,id asc') ->select(); return $list; } /** * 获取商品列表 */ public function getList() { $redis = Redis::instance(); $key = CacheConstants::getGoodsAllKey(); if ($redis->exists($key)) { return json_decode($redis->get($key), true); } else { $data = []; $categoryList = $this->getCategoryList(); foreach ($categoryList as $key => $item) { $list = $this ->where('category_id', '=', $item['id']) ->order('weigh desc,id asc') ->select(); $data[] = [ 'category' => $item, //分类 'goods' => $list, //商品 ]; } $redis->setex($key, 300, json_encode($data, JSON_UNESCAPED_UNICODE)); return $data; } } public function getGoodsInfoById($goods_id){ $redis = Redis::instance(); $key = CacheConstants::getGoodsInfoKey($goods_id); if ($redis->exists($key)) { $result = json_decode($redis->get($key), true); }else{ $result = []; if($goods_info = $this->where('id',$goods_id)->find()){ $result = $goods_info->toArray(); } $redis->setex($key, 60, json_encode($result, JSON_UNESCAPED_UNICODE)); } return $result; } public function getGoodsCategoryIds($category_name){ $category_name = (array)$category_name; $category = model('Category'); // 获取商品父类 $goodsCategory = $category->field('id')->where('nickname', '=', 'goods')->findOrFail(); $category->where('pid','=' ,$goodsCategory['id'])->where('status', '=', 'normal'); // 充值下的分类 if(!empty($category_name)){ $category->wherein('nickname',implode(',',$category_name)); } $list = $category->order('weigh desc,id asc')->column('id'); return $list; } /** * @param null $business * @param null $is_test * @param null $is_pay * @return array */ public function getGoodsListForApp($business = null, $is_test = null, $is_pay = null) { $customGoodsList = GoodsService::instance()->getChannelGoodsList(0); $ids = $this->getGoodsCategoryIds('test'); foreach ($customGoodsList as $index=>$item) { $list_line = explode(',', $item['business_line']); if (!in_array($business, $list_line)) { unset($customGoodsList[$index]); continue; } //检查支付 if ($item['show_type'] != '0') { if ($is_pay && $item['show_type'] == '2') { unset($customGoodsList[$index]); continue; } if (!$is_pay && $item['show_type'] == '1') { unset($customGoodsList[$index]); continue; } } //检查测试 if (!$is_test) { if (in_array($item['category_id'], $ids)) { unset($customGoodsList[$index]); continue; } } $customGoodsList[$index]['temp_select'] = !empty($item['second_description']) ? 2 : 1; } return array_values($customGoodsList); } /** * 获取商品列表 * @param string $category * @param int $business * @param int $is_test * @param int $is_pay * @param int $goods_type * @return array */ public function getGoodsList($business = null,$category = null,$goods_type = null,$is_test = null,$is_pay = null){ $redis = Redis::instance(); $key = CacheConstants::getGoodsListKey($business,$category,$goods_type,$is_test); if ($redis->exists($key)) { $result = json_decode($redis->get($key), true); }else{ $goods_category = []; //分类处理 if($category !== null && !empty($category)){ array_push($goods_category,$category); } //测试处理 if($is_test !== null && $is_test){ array_push($goods_category,PayConstants::GOODS_CATEGORY_TEST); } //业务线条件处理 if($business !== null){ $this->where("find_in_set({$business},business_line)"); } //商品分类处理 if($goods_type !== null){ $this->where('type',$goods_type); } //分类条件处理 if(!empty($goods_category)){ $category_map = []; //分类查询map if($ids = $this->getGoodsCategoryIds($goods_category)){ foreach($ids as $category_id){ array_push($category_map,"find_in_set({$category_id},category_id)"); } } //分类不为空时 if(!empty($category_map)){ if(count($category_map) > 1){ $this->where("(".implode(' or ',$category_map).")"); }else{ $this->where(current($category_map)); } } //不是测试时不展示测试商品 if($is_test !== null && !$is_test){ $test_cate_id = current($this->getGoodsCategoryIds([PayConstants::GOODS_CATEGORY_TEST])); $this->where("!find_in_set({$test_cate_id},category_id)"); } } if($result = $this->order('weigh desc,id asc')->select()){ $result = collection($result)->toArray();; } $redis->setex($key, 30, json_encode($result, JSON_UNESCAPED_UNICODE)); } //检查支付 if($is_pay !== null){ foreach($result as $key => $val){ if(intval($is_pay) == 0){ if(intval($val['show_type']) == 1){ unset($result[$key]); } }else{ if(intval($val['show_type']) == 2){ unset($result[$key]); } } } } return array_values($result); } /** * 根据分类nickname获取商品列表 * * @param string $categoryNickname 分类nickname * @return array */ public function getListByCategoryNickname($categoryNickname) { $list = $this->getList(); foreach ($list as $key=>$item){ if($item['category']['nickname'] == $categoryNickname){ return [$item]; } } return []; } /** * 根据分类id获取商品列表 * * @param int $categoryId 分类Id * @return array */ public function getListByCategoryId($categoryId) { $list = $this->getList(); foreach ($list as $key=>$item){ if($item['category']['id'] == $categoryId){ return [$item]; } } return []; } }