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

import com.jkcredit.invoice.hub.controller.base.WebResource;
import com.jkcredit.invoice.hub.controller.web.userRolePermission.fb.UserRolePermissionFB;
import com.jkcredit.invoice.hub.model.dto.userRolePermission.UserRolePermissionDto;
import com.jkcredit.invoice.hub.service.userRolePermission.UserRolePermissionService;
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.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * @description:
 * @author: xusonglin
 * @create: 2020/1/13 17:40
 * @version: V1.0
 **/
@Slf4j
@RestController
public class UserRolePermissionResource extends WebResource {
    @Autowired
    UserRolePermissionService userRolePermissionService;

    @PreAuthorize("hasPermission('userRolePermission','edit')")
    @PostMapping("/userRolePermission")
    public ResponseData saveUserRolePermission(@RequestBody UserRolePermissionFB fb) {
        try {
            validate(fb);
            UserRolePermissionDto userDto = mapper.map(fb, UserRolePermissionDto.class);
            userRolePermissionService.saveUserRolePermission(userDto);

            return ResponseData.success("用户分配权限成功");
        } catch (ServiceException e) {
            log.error(e.getMessage());
            return ResponseData.failed("用户分配权限失败，失败原因" + e.getMessage());
        }

    }
}
