package com.platform.controller;

import com.platform.annotation.SysLog;
import com.platform.entity.Result;
import com.platform.entity.SysMacroEntity;
import com.platform.service.SysMacroService;
import com.platform.utils.PageUtils;
import com.platform.utils.Query;

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

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

/**
 * 通用字典表Controller
 *
 * @author lipengjun
 * @email 939961241@qq.com
 * @gitee https://gitee.com/fuyang_lipengjun/platform
 * @date 2017-08-22 11:48:16
 */
@RestController
@RequestMapping("sys/macro")
public class SysMacroController {
    @Autowired
    private SysMacroService sysMacroService;

    /**
     * 所有字典列表
     *
     * @param params 请求参数
     * @return R
     */
    @RequestMapping("/list")
    
    public Result list(@RequestBody Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);

        List<SysMacroEntity> sysMacroList = sysMacroService.queryList(query);
        int total = sysMacroService.queryTotal(query);

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

        return Result.success(pageUtil);
    }

    /**
     * 根据主键获取字典信息
     *
     * @param macroId 主键
     * @return R
     */
    @RequestMapping("/info")
    
    public Result info(@RequestParam("macroId") Long macroId) {
        SysMacroEntity sysMacro = sysMacroService.queryObject(macroId);

        return Result.success(sysMacro);
    }

    /**
     * 新增字典
     *
     * @param sysMacro 字典
     * @return R
     */
    @SysLog("新增字典")
    @RequestMapping("/save")
    
    public Result save(@RequestBody SysMacroEntity sysMacro) {
        sysMacroService.save(sysMacro);

        return Result.success();
    }

    /**
     * 修改字典
     *
     * @param sysMacro 字典
     * @return R
     */
    @SysLog("修改字典")
    @RequestMapping("/update")
    
    public Result update(@RequestBody SysMacroEntity sysMacro) {
        sysMacroService.update(sysMacro);

        return Result.success();
    }

    /**
     * 删除字典
     *
     * @param macroIds 主键集
     * @return R
     */
    @SysLog("删除字典")
    @RequestMapping("/delete")
    
    public Result delete(@RequestBody Long[] macroIds) {
        sysMacroService.deleteBatch(macroIds);

        return Result.success();
    }

    /**
     * 查看字典列表
     *
     * @param params 请求参数
     * @return R
     */
    @RequestMapping("/queryAll")
    public Result queryAll(@RequestBody Map<String, Object> params) {

        List<SysMacroEntity> list = sysMacroService.queryList(params);

        return Result.success( list);
    }

    /**
     * 查看字典目录列表
     *
     * @param params 请求参数
     * @return R
     */
    @RequestMapping("/queryAllParent")
    public Result queryAllParent(@RequestBody Map<String, Object> params) {

        List<SysMacroEntity> list = sysMacroService.queryAllParent(params);

        return Result.success(list);
    }

    /**
     * 根据value查询数据字典
     *
     * @param value value
     * @return R
     */
    @RequestMapping("/queryMacrosByValue")
    public Result queryMacrosByValue(@RequestParam String value) {

        List<SysMacroEntity> list = sysMacroService.queryMacrosByValue(value);

        return Result.success(list);
    }
}
