package com.jkcredit.invoice.hub.controller.web.role;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jkcredit.invoice.hub.controller.base.WebResource;
import com.jkcredit.invoice.hub.controller.web.role.fb.RoleFB;
import com.jkcredit.invoice.hub.controller.web.role.fb.RoleUpdateFB;
import com.jkcredit.invoice.hub.model.dto.role.RoleDto;
import com.jkcredit.invoice.hub.model.vo.role.RoleVo;
import com.jkcredit.invoice.hub.service.role.RoleService;
import com.jkcredit.invoice.hub.spi.lang.exception.ServiceException;
import com.jkcredit.invoice.hub.spi.web.data.ResponseData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * @description:
 * @author: xusonglin
 * @create: 2020/1/13 12:46
 * @version: V1.0
 **/
@Slf4j
@RestController
public class RoleResource extends WebResource {
    @Autowired
    RoleService roleService;

    @PreAuthorize("hasPermission('role', 'read')")
    @GetMapping("/page/roles")
    public ResponseData getRoles(Page page, RoleFB roleFB) {
        try {
//            validate(roleFB);
            RoleDto roleDto = mapper.map(roleFB, RoleDto.class);

            IPage<RoleVo> roles = roleService.getRoles(page, roleDto);
            return ResponseData.success(roles);
        } catch (ServiceException exp) {
            return ResponseData.failed("获取角色列表失败");
        }
    }

    @PreAuthorize("hasPermission('role','edit')")
    @PostMapping("/role")
    public ResponseData addRole(@RequestBody RoleFB roleFB) {
        try {
            validate(roleFB);
            RoleDto roleDto = mapper.map(roleFB, RoleDto.class);
            roleService.addRole(roleDto);
            return ResponseData.success("新增角色成功");
        } catch (ServiceException exp) {
            log.error(exp.getMessage());
            return ResponseData.failed("新增角色失败，失败原因" + exp.getMessage());
        }
    }

    @PreAuthorize("hasPermission('role','read')")
    @GetMapping("/roles")
    public ResponseData getRoles() {
        try {
            return ResponseData.success(roleService.getRoles());
        } catch (ServiceException exp) {
            return ResponseData.failed("获取角色列表失败");
        }
    }

    @PreAuthorize("hasPermission('role','edit')")
    @PutMapping("/role/{id}")
    public ResponseData deleteRole(@PathVariable("id") Long id) {
        try {
            roleService.deleteRole(id);
            return ResponseData.success("删除角色成功");
        } catch (ServiceException e) {
            log.error(e.getMessage());
            return ResponseData.failed("删除角色失败，失败原因" + e.getMessage());
        }
    }

    @PreAuthorize("hasPermission('role','edit')")
    @PutMapping("/role")
    public ResponseData deleteRole(@RequestBody RoleUpdateFB roleUpdateFB) {
        try {
            validate(roleUpdateFB);
            RoleDto roleDto = mapper.map(roleUpdateFB, RoleDto.class);
            roleService.updateRole(roleDto);
            return ResponseData.success("修改角色成功");
        } catch (ServiceException e) {
            log.error(e.getMessage());
            return ResponseData.failed("修改角色失败，失败原因" + e.getMessage());
        }
    }
}
