
package com.yami.shop.api.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yami.shop.bean.app.dto.CategoryDto;
import com.yami.shop.bean.model.Category;
import com.yami.shop.bean.vo.ListCategoryForUserVO;
import com.yami.shop.service.CategoryService;
import com.yami.shop.service.IShopCategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import ma.glasnost.orika.MapperFacade;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 商品分类信息管理实现
 *
 * @author lgh
 * @version 1.0.0
 * @since 2018-11-26
 */
@RestController
@RequestMapping("/category")
@Api(tags = "分类接口")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private IShopCategoryService shopCategoryService;

    @Autowired
    private MapperFacade mapperFacade;

    /**
     * 分类信息列表接口
     */
    @GetMapping("/categoryInfo")
    @ApiOperation(value = "分类信息列表", notes = "获取所有的产品分类信息，顶级分类的parentId为0,默认为顶级分类")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "parentId", value = "分类ID", required = false, dataType = "Long"),
            @ApiImplicitParam(name = "shopId", value = "店铺id", required = false, dataType = "Long")
    })

    public ResponseEntity<IPage<CategoryDto>> categoryInfo(@RequestParam(value = "parentId", defaultValue = "0") Long parentId,
                                                           @RequestParam(value = "shopId",required = false) Long shopId,
                                                           @RequestParam(value = "current", defaultValue = "1") Integer current,
                                                           @RequestParam(value = "size", defaultValue = "10") Integer size) {
        IPage<Category> categories = shopCategoryService.listByParentIdAndShopId(parentId,shopId, current, size);
        List<Category> records = categories.getRecords();
        List<CategoryDto> categoryDtos = new ArrayList<>(records.size());
        for (Category category : records) {
            CategoryDto categoryDto = new CategoryDto();
            BeanUtil.copyProperties(category, categoryDto);
            categoryDtos.add(categoryDto);
        }
        Page res = new Page();
        BeanUtils.copyProperties(categories, res);
        res.setRecords(categoryDtos);
        return ResponseEntity.ok(res);
    }

    /**
     * 用户端根据商户ID查询所有分类信息
     * */
    @GetMapping("/listCategoryForUser")
    @ApiOperation(value = "用户端根据商户ID查询所有分类信息")
    public ResponseEntity<List<ListCategoryForUserVO>> listCategoryForUser(@RequestParam Long channelId,@RequestParam Long shopId ){
        return ResponseEntity.ok(categoryService.listCategoryForUser(channelId,shopId));
    }

}
