package com.tashow.cloud.user.controller.admin.user;

import cn.hutool.core.collection.CollUtil;
import com.tashow.cloud.common.pojo.CommonResult;
import com.tashow.cloud.common.pojo.PageResult;
import com.tashow.cloud.user.controller.admin.user.vo.UserLoginPageReqVO;
import com.tashow.cloud.user.controller.admin.user.vo.UserLoginRespVO;
import com.tashow.cloud.user.controller.admin.user.vo.MemberUserUpdateReqVO;
import com.tashow.cloud.user.convert.user.MemberUserConvert;
import com.tashow.cloud.user.dal.dataobject.user.UserLoginDO;
import com.tashow.cloud.user.service.user.UserLoginService;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import static com.tashow.cloud.common.pojo.CommonResult.success;


// 管理后台 - 会员用户
@RestController
@RequestMapping("/user/member")
@Validated
public class UserLoginController {

    @Resource
    private UserLoginService loginUserService;


    // 更新会员用户
    @PutMapping("/update")
    @PreAuthorize("@ss.hasPermission('member:user:update')")
    public CommonResult<Boolean> updateUser(@Valid @RequestBody MemberUserUpdateReqVO updateReqVO) {
        loginUserService.updateUser(updateReqVO);
        return success(true);
    }


    // 获得会员用户
    // id: 编号，必填，示例：1024
    @GetMapping("/get")
    @PreAuthorize("@ss.hasPermission('member:user:query')")
    public CommonResult<UserLoginRespVO> getUser(@RequestParam("id") Long id) {
        UserLoginDO user = loginUserService.getUser(id);
        return success(MemberUserConvert.INSTANCE.convert03(user));
    }

    // 获得会员用户分页
    @GetMapping("/page")
    @PreAuthorize("@ss.hasPermission('member:user:query')")
    public CommonResult<PageResult<UserLoginRespVO>> getUserPage(@Valid UserLoginPageReqVO pageVO) {
        PageResult<UserLoginDO> pageResult = loginUserService.getUserPage(pageVO);
        if (CollUtil.isEmpty(pageResult.getList())) {
            return success(PageResult.empty());
        }
        return success(MemberUserConvert.INSTANCE.convertPage(pageResult));
    }

}
