package com.zhiqiyun.open.mvc.manager.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhiqiyun.open.annotation.Permission;
import com.zhiqiyun.open.core.models.user.RoleInfo;
import com.zhiqiyun.open.core.service.RoleInfoService;
import com.zhiqiyun.open.mvc.Result;
import com.zhiqiyun.open.mvc.manager.params.QueryRoleParam;
import com.zhiqiyun.open.mvc.manager.params.SaveRoleParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 * @author jtoms
 */
@Slf4j
@RestController
@RequestMapping("/user/role")
public class RoleInfoController {

    @Autowired
    private RoleInfoService roleInfoService;

    @Permission(value = "oauth.role.find", tags = "查询系统角色")
    @PostMapping("/findPage")
    public Result findRolePage(@RequestBody QueryRoleParam params) {
        Page<RoleInfo> page = params.getPage();
        page.addOrder(OrderItem.desc("id"));
        QueryWrapper<RoleInfo> queryWrapper = new QueryWrapper<>();

        Page<RoleInfo> resultData = this.roleInfoService.page(page, queryWrapper);

        for (RoleInfo roleInfo : resultData.getRecords()) {
            roleInfo.setListAuthorities(this.roleInfoService.findAuthorities(roleInfo.getId()));
        }

        return Result.instance(Result.Code.SUCCESS).setData(resultData);
    }

    @Permission(value = "oauth.role.find", tags = "查询所有系统角色")
    @PostMapping("/findAll")
    public Result findAll() {
        List<RoleInfo> listData = this.roleInfoService.list();
        return Result.instance(Result.Code.SUCCESS).setData(listData);
    }

    @Permission(value = "oauth.role.add", tags = "添加系统角色")
    @PostMapping("/save")
    public Result save(@Valid @RequestBody SaveRoleParam param) {
        RoleInfo roleInfo = new RoleInfo();
        BeanUtils.copyProperties(param, roleInfo);
        this.roleInfoService.insert(roleInfo, param.getListAuthorities());
        return Result.instance(Result.Code.MESSAGE_SUCCESS);
    }

    @Permission(value = "oauth.role.edit", tags = "更新系统角色")
    @PostMapping("/updateById")
    public Result updateById(Long id, @Valid @RequestBody SaveRoleParam param) {
        if (id == null || id <= 0) {
            return Result.instance("数据异常，请刷新页面后再试");
        }
        RoleInfo roleInfo = new RoleInfo();
        BeanUtils.copyProperties(param, roleInfo);
        roleInfo.setId(id);
        this.roleInfoService.updateById(roleInfo, param.getListAuthorities());
        return Result.instance(Result.Code.MESSAGE_SUCCESS);
    }

    @Permission(value = "oauth.role.delete", tags = "删除系统角色")
    @PostMapping("/deleteByIds")
    public Result deleteByIds(@RequestBody List<Long> ids) throws Exception {
        if (ids.contains(1000L)) {
            return Result.instance(Result.Code.MESSAGE_ERROR, "系统管理员不能删除");
        }
        this.roleInfoService.deleteByIds(ids);
        return Result.instance(Result.Code.MESSAGE_SUCCESS);
    }

//    @Permission(value = "oauth.role.authority", tags = "保存用户的权限值")
//    @PostMapping("/saveAuthorities")
//    public Result saveAuthorities(Long roleId, @RequestBody Long[] authorities) throws Exception {
//        this.roleInfoService.saveAuthorities(roleId, authorities);
//        return Result.instance(Result.Code.MESSAGE_SUCCESS);
//
//    }
}
