package com.duanledexianxianxian.demo.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.duanledexianxianxian.demo.common.util.BeanUtils;
import com.duanledexianxianxian.demo.domain.dto.UserDTO;
import com.duanledexianxianxian.demo.domain.model.query.UserForm;
import com.duanledexianxianxian.demo.domain.model.query.UserQuery;
import com.duanledexianxianxian.demo.domain.model.vo.UserDetailVO;
import com.duanledexianxianxian.demo.domain.model.vo.UserVO;
import com.duanledexianxianxian.demo.service.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

/**
 * 用户接口
 *
 * @author duanledexianxianxian
 * @since 2020 -03-19
 */
@RestController
@RequestMapping("/api/v1/users")
@Slf4j
public class UserController {
    /**
     * The Menu service.
     */
    @Autowired
    private IUserService userService;

    /**
     * 用户分页列表查询
     *
     * @param query the query
     * @return the long
     */
    @GetMapping
    public IPage<UserVO> getUserListPage(UserQuery query) {
        IPage page = userService.getUserListPage(query);
        if (page != null && !CollectionUtils.isEmpty(page.getRecords())) {
            page.setRecords(BeanUtils.copyList(page.getRecords(), UserVO.class));
        }
        return page;
    }


    /**
     * 获取用户详情
     *
     * @param userId the user id
     * @return the user menu list
     */
    @GetMapping("/{userId}")
    public UserDetailVO getUserMenuList(@PathVariable("userId")Long userId) {
        UserDTO userDTO = userService.getUserDetail(userId);
        return BeanUtils.copyObject(userDTO, UserDetailVO.class);
    }

    /**
     * 添加用户
     *
     * @param form 用户表单
     * @return the long
     */
    @PostMapping
    public Long addUser(@RequestBody UserForm form) {
        return userService.addUser(form);
    }


    /**
     * 编辑用户
     *
     * @param form 用户表单
     * @return the long
     */
    @PutMapping
    public Boolean editUser(@RequestBody UserForm form) {
        return userService.editUser(form);
    }


    /**
     * 删除用户
     *
     * @param userId 用户编号
     * @return the long
     */
    @DeleteMapping("/{userId}")
    public Boolean deleteUser(@PathVariable("userId") Long userId) {
        return userService.deleteUser(userId);
    }
}
