<?php

namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use JiGuang\JSMS as JSMS;
use Illuminate\Support\Facades\Redis;
use Dingo\Api\Http\Response;
use Validator;
use DB;
use App\Models\User;
use App\Models\Suggestion;
use App\Services\CategoryService;
use App\Models\Category;
use App\Models\Product;

/**
 * 
* Copyright: Copyright (c) 2019 LanRu-Caifu
* 
* @Description: 分类控制器
* @version: v1.0.0
* @author: fengyichao
* @date: 2019年8月5日 下午6:52:40 
*
* Modification History:
* Date         Author          Version            Description
*---------------------------------------------------------*
* 2019年8月5日     fengyichao           v1.0.0               修改原因
 */
class CategoryController extends ApiController {
	public function __construct(CategoryService $categoryService) {
		$this->categoryService = $categoryService;
	}
	
	/**
	 *
	 * @api {post} category/list  商品分类（检测前需要选择疾病分类）
	 * @apiName category/list
	 * @apiGroup category
	 * @apiSuccess {List[Map]} data 商品分类
	 * @apiSuccess (data) {int}  id 分类id
	 * @apiSuccess (data) {int}  name 分类名称
	 * @apiSuccess {List[Map]} products 本类相关商品
	 * @apiSuccess (data) {int}  id 商品id
	 * @apiSuccess (data) {string}  title 短标题
	 * @apiSuccess (data) {string}  long_title 长标题
	 * @apiParamExample {curl} Example usage:
	 * http://59.110.137.245:8080/api/category/list
	 */
	function list(Request $request){
		$categorys = $this->categoryService->getCategoryTree ();
		$firstCategory=Category::orderBy('id')->first();
		$products=Product::select(['id','title','long_title'])->where('category_id',$firstCategory->id)->get();
		
		$resData ['data'] = $categorys;
		$resData ['products'] = $products;
		return $this->success ( $resData );
	}
}
