package com.platform.controller;

import com.platform.annotation.CanUserUpdateRecord;
import com.platform.entity.CategoryEntity;
import com.platform.entity.Result;
import com.platform.service.CategoryService;
import com.platform.utils.PageUtils;
import com.platform.utils.Query;

import com.platform.utils.TreeUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Controller
 *
 * @author lipengjun
 * @email 939961241@qq.com
 * @date 2017-08-21 15:32:31
 */
@RestController
@RequestMapping("category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 查看列表
     */
    @RequestMapping("/list")
    public Result list(@RequestBody Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);

        List<CategoryEntity> categoryList = categoryService.queryList(query);
        int total = categoryService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(categoryList, total, query.getLimit(), query.getPage());

        return Result.success(pageUtil);
    }

    /**
     * 查看信息
     */
    @RequestMapping("/info/{id}")
    public Result info(@PathVariable("id") Integer id) {
        CategoryEntity category = categoryService.queryObject(id);

        return Result.success( category);
    }

    /**
     * 保存
     */
    @CanUserUpdateRecord
    @RequestMapping("/save")
    public Result save(@RequestBody CategoryEntity category) {
        return categoryService.save(category);
    }

    /**
     * 修改
     */
    @CanUserUpdateRecord
    @RequestMapping("/update")
    public Result update(@RequestBody CategoryEntity category) {
        return categoryService.update(category);
    }

    /**
     * 删除
     */
    @CanUserUpdateRecord
    @RequestMapping("/delete")
    public Result delete(@RequestBody Integer[] ids) {
        categoryService.deleteBatch(ids);

        return Result.success();
    }

    /**
     * 查看所有列表
     */
    @RequestMapping("/queryAll")
    public Result queryAll(@RequestBody Map<String, Object> params) {

        List<CategoryEntity> list = categoryService.queryList(params);
        //添加顶级菜单
//        CategoryEntity root = new CategoryEntity();
//        root.setId(0);
//        root.setName("一级分类");
//        root.setParentId(-1);
//        root.setOpen(true);
//        list.add(0,root);
        return Result.success(list);
    }

    /**
     * 查看信息(全部加载页面渲染太慢！)
     */
    @RequestMapping("/getAreaTree")
    public Result getAreaTree() {
        List<CategoryEntity> list = categoryService.queryList(new HashMap<String, Object>());
        for (CategoryEntity sysRegionEntity : list) {
            sysRegionEntity.setValue(sysRegionEntity.getId() + "");
            sysRegionEntity.setLabel(sysRegionEntity.getName());
        }
        List<CategoryEntity> node = TreeUtils.factorTree(list);

        return Result.success( node);
    }

    /**
     * 查询
     *
     * @return
     */
    @RequestMapping("/getCategorySelect")
    public Result getCategorySelect() {
        Map<String, Object> map = new HashMap<>();
        map.put("parentId", "0");
        List<CategoryEntity> list = categoryService.queryList(map);
        return Result.success(list);
    }
}
