package com.crm.manager.controller;

import com.crm.manager.service.AuthorityService;
import com.crm.manager.service.UserRoleService;
import com.crm.rely.backend.core.dto.base.BaseResultDto;
import com.crm.rely.backend.core.dto.user.role.UserNodeDisplayDto;
import com.crm.rely.backend.core.entity.system.action.SysActionAddEntity;
import com.crm.rely.backend.core.entity.system.action.SysActionDeleteEntity;
import com.crm.rely.backend.core.entity.system.action.SysActionUpdateEntity;
import com.crm.rely.backend.core.entity.system.node.SysNodeAddEntity;
import com.crm.rely.backend.core.entity.system.node.SysNodeDeleteEntity;
import com.crm.rely.backend.core.entity.system.node.SysNodeUpdateEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
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 java.util.List;

/**
 * 权限
 *
 * @author houn
 */
@RestController
@RequestMapping("/authority")
public class AuthorityController {

    @Autowired
    private AuthorityService authorityService;

    @Autowired
    private UserRoleService roleInfoService;

    /**
     * 添加节点
     *
     * @param entity
     * @return
     * @throws Exception
     */
    @PostMapping("/node/add")
    public BaseResultDto nodeAdd(@RequestBody @Validated SysNodeAddEntity entity) throws Exception {
        authorityService.add(entity);
        return BaseResultDto.success();
    }

    /**
     * 删除节点
     *
     * @param entity
     * @return
     * @throws Exception
     */
    @PostMapping("/node/delete")
    public BaseResultDto nodeDelete(@RequestBody @Validated SysNodeDeleteEntity entity) throws Exception {
        authorityService.delete(entity);
        return BaseResultDto.success();
    }

    /**
     * 修改节点
     *
     * @param entity
     * @return
     * @throws Exception
     */
    @PostMapping("/node/update")
    public BaseResultDto nodeUpdate(@RequestBody @Validated SysNodeUpdateEntity entity) throws Exception {
        authorityService.update(entity);
        return BaseResultDto.success();
    }

    /**
     * 添加按钮
     *
     * @param entity
     * @return
     * @throws Exception
     */
    @PostMapping("/action/add")
    public BaseResultDto actionAdd(@RequestBody @Validated SysActionAddEntity entity) throws Exception {
        authorityService.add(entity);
        return BaseResultDto.success();
    }

    /**
     * 删除按钮
     *
     * @param entity
     * @return
     * @throws Exception
     */
    @PostMapping("/action/delete")
    public BaseResultDto actionDelete(@RequestBody @Validated SysActionDeleteEntity entity) throws Exception {
        authorityService.delete(entity);
        return BaseResultDto.success();
    }

    /**
     * 修改按钮
     *
     * @param entity
     * @return
     * @throws Exception
     */
    @PostMapping("/action/update")
    public BaseResultDto actionUpdate(@RequestBody @Validated SysActionUpdateEntity entity) throws Exception {
        authorityService.update(entity);
        return BaseResultDto.success();
    }

    @PostMapping("/info")
    public BaseResultDto info() throws Exception {
        List<UserNodeDisplayDto> userNodeDisplayDtos = roleInfoService.getUserNodeDisplay(0L);
        return BaseResultDto.success(userNodeDisplayDtos);
    }
}
