package com.ruoyi.cai.controller.admin;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.cai.domain.Dynamic;
import com.ruoyi.cai.domain.UserCameraAudit;
import com.ruoyi.cai.dto.admin.query.BatchAuditReq;
import com.ruoyi.cai.dto.admin.vo.UserCameraAuditAdminVo;
import com.ruoyi.cai.service.UserCameraAuditService;
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 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 2023-12-30
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/cai/userCameraAudit")
public class UserCameraAuditController extends BaseController {

    private final UserCameraAuditService userCameraAuditService;

    /**
     * 查询自拍认证列表
     */
    @SaCheckPermission("cai:userCameraAudit:list")
    @GetMapping("/list")
    public TableDataInfo<UserCameraAuditAdminVo> list(UserCameraAuditAdminVo bo, PageQuery pageQuery) {
        Page<UserCameraAuditAdminVo> page = userCameraAuditService.pageAdmin(pageQuery, bo);
        return TableDataInfo.build(page);
    }

    /**
     * 获取自拍认证详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("cai:userCameraAudit:query")
    @GetMapping("/{id}")
    public R<UserCameraAudit> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Integer id) {
        return R.ok(userCameraAuditService.getById(id));
    }

    /**
     * 新增自拍认证
     */
    @SaCheckPermission("cai:userCameraAudit:add")
    @Log(title = "自拍认证", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody UserCameraAudit bo) {
        return toAjax(userCameraAuditService.save(bo));
    }

    /**
     * 修改自拍认证
     */
    @SaCheckPermission("cai:userCameraAudit:edit")
    @Log(title = "自拍认证", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody UserCameraAudit bo) {
        return toAjax(userCameraAuditService.updateById(bo));
    }

    @SaCheckPermission("cai:userCameraAudit:edit")
    @RepeatSubmit()
    @PostMapping("/batch/audit")
    public R<Void> edit(@RequestBody BatchAuditReq bo) {
        userCameraAuditService.update(Wrappers.lambdaUpdate(UserCameraAudit.class)
                .in(UserCameraAudit::getId,bo.getIds())
                .set(UserCameraAudit::getAuditStatus,bo.getAuditStatus())
                .eq(UserCameraAudit::getAuditStatus,1));
        return R.ok();
    }

    /**
     * 删除自拍认证
     *
     * @param ids 主键串
     */
    @SaCheckPermission("cai:userCameraAudit:remove")
    @Log(title = "自拍认证", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Integer[] ids) {
        return toAjax(userCameraAuditService.removeBatchByIds(Arrays.asList(ids), true));
    }
}
