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.TpWorksVo;
import org.dromara.work.domain.bo.TpWorksBo;
import org.dromara.work.service.ITpWorksService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 作品收藏
 *
 * @author Maosw
 * @date 2025-07-23
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/work/works")
public class TpWorksController extends BaseController {

    private final ITpWorksService tpWorksService;

    /**
     * 查询作品收藏列表
     */
    @SaCheckPermission("work:works:list")
    @GetMapping("/list")
    public TableDataInfo<TpWorksVo> list(TpWorksBo bo, PageQuery pageQuery) {
        return tpWorksService.queryPageList(bo, pageQuery);
    }

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

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

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

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

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