package me.zhengjie.modules.front.rest;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import me.zhengjie.annotation.Log;
import me.zhengjie.modules.front.domain.dto.LoginVo;
import me.zhengjie.modules.front.domain.dto.MyDeviceCriteria;
import me.zhengjie.modules.security.service.dto.JwtUserDto;
import me.zhengjie.modules.system.domain.BusDevice;
import me.zhengjie.modules.system.domain.BusUser;
import me.zhengjie.modules.system.domain.dto.BusDeviceQueryCriteria;
import me.zhengjie.modules.system.service.BusDeviceService;
import me.zhengjie.modules.system.service.BusUserService;
import me.zhengjie.utils.PageResult;
import me.zhengjie.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;


@Slf4j
@RestController("UserController")
@RequestMapping("api/front/user")
@Api(tags = "微信：用户管理")
public class BusUserController {

    @Autowired private BusUserService busUserService;
    @Autowired private BusDeviceService busDeviceService;

    @Log("我的设备")
    @GetMapping(value = "myDevice")
    @ApiOperation("我的设备")
    public ResponseEntity<PageResult<BusDevice>> myDeviceList(MyDeviceCriteria criteria) {
        JwtUserDto jwtUser = (JwtUserDto) SecurityUtils.getCurrentUser();
        Page<Object> page = new Page<>(criteria.getPage(), criteria.getSize());
        return new ResponseEntity<>(busDeviceService.myDeviceList(jwtUser.getUser().getId(), page), HttpStatus.OK);
    }

    @Log("用户信息")
    @ApiOperation("获取用户信息")
    @GetMapping(value = "/info")
    public ResponseEntity<LoginVo> getLoginInfo() {
        JwtUserDto jwtUser = (JwtUserDto) SecurityUtils.getCurrentUser();
        LoginVo loginVo = new LoginVo();
        loginVo.setType("login");
        loginVo.setUid(jwtUser.getUser().getId());
        loginVo.setNikeName(jwtUser.getUser().getNickName());
        loginVo.setAvatar(jwtUser.getUser().getAvatarPath());
        loginVo.setPhone(jwtUser.getUser().getPhone());
        return new ResponseEntity<>(loginVo, HttpStatus.OK);
    }

    @Log("信息修改")
    @ApiOperation("修改用户信息")
    @PostMapping(value = "/update")
    public ResponseEntity<String> updateInfo() {
        JwtUserDto jwtUser = (JwtUserDto) SecurityUtils.getCurrentUser();
        BusUser upd = new BusUser();
        upd.setId(jwtUser.getUser().getId());
        upd.setAvatar(jwtUser.getUser().getAvatarPath());
        upd.setNickname(jwtUser.getUser().getNickName());
        upd.setPhone(jwtUser.getUser().getPhone());
        busUserService.updateById(upd);
        return new ResponseEntity<>("修改成功", HttpStatus.OK);
    }

}



