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.UserGreet;
import com.ruoyi.cai.dto.admin.query.BatchAuditReq;
import com.ruoyi.cai.dto.admin.vo.UserGreetAdminVo;
import com.ruoyi.cai.service.UserGreetService;
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 2024-01-01
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/cai/userGreet")
public class UserGreetController extends BaseController {

    private final UserGreetService userGreetService;

    /**
     * 查询群发审核列表
     */
    @SaCheckPermission("cai:userGreet:list")
    @GetMapping("/list")
    public TableDataInfo<UserGreetAdminVo> list(UserGreetAdminVo bo, PageQuery pageQuery) {
        Page<UserGreetAdminVo> page = userGreetService.pageAdmin(pageQuery, bo);
        return TableDataInfo.build(page);
    }


    /**
     * 获取群发审核详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("cai:userGreet:query")
    @GetMapping("/{id}")
    public R<UserGreet> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Integer id) {
        return R.ok(userGreetService.getById(id));
    }

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


    /**
     * 修改群发审核
     */
    @SaCheckPermission("cai:userGreet:edit")
    @Log(title = "群发审核", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody UserGreet bo) {
        return toAjax(userGreetService.updateById(bo));
    }

    /**
     * 删除群发审核
     *
     * @param ids 主键串
     */
    @SaCheckPermission("cai:userGreet:remove")
    @Log(title = "群发审核", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Integer[] ids) {
        return toAjax(userGreetService.removeBatchByIds(Arrays.asList(ids)));
    }
}
