package org.dromara.work.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.work.domain.vo.TpWorksNumVo;
import org.dromara.work.domain.bo.TpWorksNumBo;
import org.dromara.work.service.ITpWorksNumService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 作品点赞收藏
 *
 * @author Maosw
 * @date 2025-04-11
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/work/worksNum")
public class TpWorksNumController extends BaseController {

    private final ITpWorksNumService tpWorksNumService;

    /**
     * 查询作品点赞收藏列表
     */
    @SaCheckPermission("work:worksNum:list")
    @GetMapping("/list")
    public TableDataInfo<TpWorksNumVo> list(TpWorksNumBo bo, PageQuery pageQuery) {
        return tpWorksNumService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出作品点赞收藏列表
     */
    @SaCheckPermission("work:worksNum:export")
    @Log(title = "作品点赞收藏", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TpWorksNumBo bo, HttpServletResponse response) {
        List<TpWorksNumVo> list = tpWorksNumService.queryList(bo);
        ExcelUtil.exportExcel(list, "作品点赞收藏", TpWorksNumVo.class, response);
    }

    /**
     * 获取作品点赞收藏详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("work:worksNum:query")
    @GetMapping("/{id}")
    public R<TpWorksNumVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(tpWorksNumService.queryById(id));
    }

    /**
     * 新增作品点赞收藏
     */
    @SaCheckPermission("work:worksNum:add")
    @Log(title = "作品点赞收藏", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody TpWorksNumBo bo) {
        return toAjax(tpWorksNumService.insertByBo(bo));
    }

    /**
     * 修改作品点赞收藏
     */
    @SaCheckPermission("work:worksNum:edit")
    @Log(title = "作品点赞收藏", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody TpWorksNumBo bo) {
        return toAjax(tpWorksNumService.updateByBo(bo));
    }

    /**
     * 删除作品点赞收藏
     *
     * @param ids 主键串
     */
    @SaCheckPermission("work:worksNum:remove")
    @Log(title = "作品点赞收藏", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(tpWorksNumService.deleteWithValidByIds(List.of(ids), true));
    }
}
