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

import cn.com.countrygarden.bi.main.core.Result;
import cn.com.countrygarden.bi.main.exception.ServiceException;
import cn.com.countrygarden.bi.main.model.AccessToken;
import cn.com.countrygarden.bi.main.model.sys.Role;
import cn.com.countrygarden.bi.main.service.MenuService;
import cn.com.countrygarden.bi.main.service.RoleService;
import cn.com.countrygarden.bi.main.service.UserFunctionService;
import cn.com.countrygarden.bi.main.service.UserRoleService;
import cn.com.countrygarden.bi.main.service.erp.impl.ExcelArrearageServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * package: cn.com.countrygarden.bi.main.controller
 * describe:
 * creat_user: lsl
 * creat_date: 2018/6/7
 * creat_time: 21:53
 **/
@RestController("RoleController")
@RequestMapping("/api/role")
public class RoleController extends BaseController {

    @Autowired
    private MenuService menuService;

    @Autowired
    private UserRoleService userRoleService;

    @Autowired
    private UserFunctionService userFunctionService;

    @Autowired
    private RoleService roleService;

    private final org.slf4j.Logger logger = LoggerFactory.getLogger(ExcelArrearageServiceImpl.class);

    @GetMapping(value = "/listRole", produces = "application/json;charset=utf-8")
    public Result list(Role role,
                       @RequestParam(value = "size", required = false, defaultValue = "10") int size,
                       @RequestParam(value = "page", required = false, defaultValue = "1") int page,
                       HttpServletRequest request){
    	AccessToken token = this.getAccessToken();
        return roleService.queryRoleInfo(role,token.getUserCode(),page,size);
    }

    @PostMapping(value = "add")
    public Result add(Role role,
                      @RequestParam(value = "menusIds", required = true)String menusIds,
                      @RequestParam(value = "functionIds", required = true)String functionIds,
                      HttpServletRequest request){
        if(StringUtils.isEmpty(role.getCode())){
            throw new ServiceException("接口异常，角色编号不能为空！");
        }
        boolean existFlag=roleService.checkRoleCodeExist(role.getCode());
//      添加角色
        if(existFlag){
           throw new ServiceException("接口异常，角色编号不能重复");
        }
        AccessToken token = getAccessToken(request);
        roleService.checkIsAdmin(token.getUserCode());
        role.setCreatedBy(token.getUserCode());
        role.setCreatedDate(new Date());
        role.setEnable(true);
        return roleService.addRoleOrMenusAndFunctions(role,menusIds,functionIds);
    }



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

    @PutMapping(value = "/edit")
    public Result edit(Role role,
                       @RequestParam(value = "menusIds", required = true)String menusIds,
                       @RequestParam(value = "functionIds", required = true)String functionIds,
                       HttpServletRequest request){
        AccessToken token = getAccessToken(request);
        roleService.checkIsAdmin(token.getUserCode());
        role.setUpdatedBy(token.getUserCode());
        role.setUpdatedDate(new Date());
        return roleService.edit(role,menusIds,functionIds);
    }

    @DeleteMapping(value="/del")
    public Result del(@RequestParam(value = "id", required = true)Integer id){
    	AccessToken token = this.getAccessToken();
    	roleService.checkIsAdmin(token.getUserCode());
        return roleService.del(id);
    }

}
