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

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jkcredit.invoice.hub.constant.CommonConstant;
import com.jkcredit.invoice.hub.constant.ResponseStatus;
import com.jkcredit.invoice.hub.controller.base.WebResource;
import com.jkcredit.invoice.hub.controller.web.user.fb.UpdateUserFB;
import com.jkcredit.invoice.hub.controller.web.user.fb.UserFB;
import com.jkcredit.invoice.hub.controller.web.user.fb.UserQueryFB;
import com.jkcredit.invoice.hub.model.dto.user.UserDto;
import com.jkcredit.invoice.hub.model.vo.common.AppResponseVo;
import com.jkcredit.invoice.hub.model.vo.user.UserVO;
import com.jkcredit.invoice.hub.service.user.UserService;
import com.jkcredit.invoice.hub.spi.lang.exception.ServiceException;
import com.jkcredit.invoice.hub.spi.web.data.ResponseData;
import com.jkcredit.invoice.hub.util.BeanUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * @description:
 * @author: xusonglin
 * @create: 2020/1/2 10:13
 * @version: V1.0
 **/
@Slf4j
@RestController
public class UserResource extends WebResource {
    @Autowired
    UserService userService;

//    @PreAuthorize("hasPermission('user','read')")
//    @GetMapping("/user/{id}")
//    public ResponseEntity<AppResponseVo> getUserById(@PathVariable("id") Long id) {
//        try {
//            UserVO userVo = userService.getUserById(id);
//            return ResponseEntity.ok(new AppResponseVo(ResponseStatus.SUCCESS, CommonConstant.MESSAGE_SUCCESS, userVo));
//        } catch (ServiceException serviceException) {
//            return ResponseEntity.ok(new AppResponseVo(ResponseStatus.FAILED, serviceException.getMessage(), null));
//        }
//    }

    @PreAuthorize("hasPermission('user','read')")
    @GetMapping("/page/users")
    public ResponseData getUsers(Page page, UserQueryFB userQueryFB) {
        try {
            validate(userQueryFB);
            UserDto userDto = mapper.map(userQueryFB, UserDto.class);

            IPage<UserVO> users = userService.getAllUsers(page, userDto);
            return ResponseData.success(users);
        } catch (ServiceException exp) {
            return ResponseData.failed("获取用户列表失败");
        }
    }

    @PreAuthorize("hasPermission('user','edit')")
    @PostMapping("/user")
    public ResponseData addUser(@RequestBody UserFB userFB) {
        try {
            validate(userFB);

            UserDto userDto = mapper.map(userFB, UserDto.class);
            userService.saveUser(userDto);

            return ResponseData.success("新增用户成功");
        } catch (ServiceException exp) {
            log.error(exp.getMessage());
            return ResponseData.failed("新增用户失败，失败原因" + exp.getMessage());
        }
    }

    @PreAuthorize("hasPermission('user','edit')")
    @PutMapping("/user")
    public ResponseData updateUser(@RequestBody UpdateUserFB userFB) {
        try {
            validate(userFB);
            UserDto userDto = mapper.map(userFB, UserDto.class);
            Boolean result = userService.updateUser(userDto);
            return ResponseData.success(result);
        } catch (ServiceException e) {
            return ResponseData.failed("更新用户失败，失败原因" + e.getMessage());
        }
    }

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