package cn.com.countrygarden.bi.main.controller;

import cn.com.countrygarden.bi.main.core.Result;
import cn.com.countrygarden.bi.main.model.sys.Function;
import cn.com.countrygarden.bi.main.service.FunctionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * package: cn.com.countrygarden.bi.main.controller
 * describe:
 * creat_user: lsl
 * creat_date: 2018/6/9
 * creat_time: 22:00
 **/
@RestController("FunctionsController")
@RequestMapping("/api/functions")
public class FunctionsController extends BaseController {

    @Autowired
    private FunctionService functionService;


    @GetMapping(value = "/list", produces = "application/json;charset=utf-8")
    public Result list(Function functions,
                       @RequestParam(value = "size", required = false, defaultValue = "10") int size,
                       @RequestParam(value = "page", required = false, defaultValue = "1") int page,
                       HttpServletRequest request){
        return functionService.queryPageList(functions,page,size);
    }

    @GetMapping(value = "/one", produces = "application/json;charset=utf-8")
    public Result queryById(@RequestParam(value = "id", required = true)Integer id,
                            HttpServletRequest request){
        return functionService.queryById(id);
    }

    @PostMapping(value = "/add")
    public Result add(Function functions,
                      HttpServletRequest request){
        return functionService.add(functions,getAccessToken(request));
    }

    @PutMapping(value = "/edit")
    public Result edit(Function functions, HttpServletRequest request){
        return functionService.edit(functions,getAccessToken(request));
    }

    @DeleteMapping(value="/del")
    public Result del(@RequestParam(value = "id", required = true)Integer id){
        return functionService.del(id);
    }




    @GetMapping(value = "/checkList", produces = "application/json;charset=utf-8")
    public Result list(String roleCode, HttpServletRequest request){

        return functionService.queryFunctionsByRole(roleCode);
    }

    @GetMapping(value = "/transfer", produces = "application/json;charset=utf-8")
    public Result transfer(String roleCode, HttpServletRequest request){
        return functionService.queryTransferListByRole(roleCode);
    }

    @PutMapping(value = "editRolesFunctions", produces = "application/json;charset=utf-8")
    public @ResponseBody
    Result editRolesFunctions(
            @RequestParam(value = "rCode",required = true)String roleCode,
            @RequestParam(value = "funCodes",required = true)String funCodes,
            HttpServletRequest request){
        return functionService.editRolesFunctions(roleCode,funCodes, getAccessToken(request).getUserCode());
    }
}
