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.dto.admin.query.BatchAuditReq;
import com.ruoyi.cai.dto.admin.vo.DynamicAdminVo;
import com.ruoyi.cai.service.DynamicService;
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-23
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/cai/dynamic")
public class DynamicController extends BaseController {

    private final DynamicService dynamicService;

    /**
     * 查询主播动态列表
     */
    @SaCheckPermission("cai:dynamic:list")
    @GetMapping("/list")
    public TableDataInfo<DynamicAdminVo> list(DynamicAdminVo bo, PageQuery pageQuery) {
        Page<DynamicAdminVo> page = dynamicService.pageAdmin(pageQuery, bo);
        return TableDataInfo.build(page);
    }

    /**
     * 获取主播动态详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("cai:dynamic:query")
    @GetMapping("/{id}")
    public R<Dynamic> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(dynamicService.getById(id));
    }

    /**
     * 新增主播动态
     */
    @SaCheckPermission("cai:dynamic:add")
    @Log(title = "主播动态", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody Dynamic bo) {
        return toAjax(dynamicService.save(bo));
    }

    /**
     * 修改主播动态
     */
    @SaCheckPermission("cai:dynamic:edit")
    @Log(title = "主播动态", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody Dynamic bo) {
        return toAjax(dynamicService.updateById(bo));
    }

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

    /**
     * 删除主播动态
     *
     * @param ids 主键串
     */
    @SaCheckPermission("cai:dynamic:remove")
    @Log(title = "主播动态", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(dynamicService.removeBatchByIds(Arrays.asList(ids), true));
    }
}
