package com.ruoyi.xq.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.xq.domain.UserInfo;
import com.ruoyi.xq.dto.admin.user.UserInfoListAdminVo;
import com.ruoyi.xq.dto.admin.user.req.UpdateUserInfoReq;
import com.ruoyi.xq.dto.admin.user.req.UpdateUserinfoFilterReq;
import com.ruoyi.xq.service.UserInfoService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;

/**
 * 用户信息
 *
 * @author 77
 * @date 2024-03-04
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/xq/userInfo")
public class UserInfoController extends BaseController {

    private final UserInfoService userInfoService;

    /**
     * 查询用户信息列表
     */
    @SaCheckPermission("xq:userInfo:list")
    @GetMapping("/list")
    public TableDataInfo<UserInfoListAdminVo> list(UserInfoListAdminVo bo, PageQuery pageQuery) {
        Page<UserInfoListAdminVo> page = userInfoService.pageAdmin(pageQuery, bo);
        return TableDataInfo.build(page);
    }

    /**
     * 获取用户信息详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("xq:userInfo:query")
    @GetMapping("/{id}")
    public R<UserInfo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(userInfoService.getById(id));
    }

    @SaCheckPermission("xq:userInfo:query")
    @GetMapping("/userInfoUpdate")
    public R<UpdateUserInfoReq> userInfoUpdate(Long userId) {
        UpdateUserInfoReq res = userInfoService.getUserInfoUpdate(userId);
        return R.ok(res);
    }

    /**
     * 修改用户信息
     */
    @SaCheckPermission("xq:userInfo:edit")
    @Log(title = "用户信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@RequestBody UpdateUserInfoReq bo) {
        userInfoService.updateInfo(bo);
        return R.ok();
    }


    @SaCheckPermission("xq:userInfo:edit")
    @Log(title = "用户信息择偶修改", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping("/filter")
    public R<Void> edit(@RequestBody UpdateUserinfoFilterReq bo) {
        userInfoService.updateInfoFilter(bo);
        return R.ok();
    }

    /**
     * 删除用户信息
     *
     * @param ids 主键串
     */
    @SaCheckPermission("xq:userInfo:remove")
    @Log(title = "用户信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(userInfoService.removeBatchByIds(Arrays.asList(ids)));
    }
}
