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.TpFollowVo;
import org.dromara.work.domain.bo.TpFollowBo;
import org.dromara.work.service.ITpFollowService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 关注
 *
 * @author Maosw
 * @date 2025-07-23
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/work/follow")
public class TpFollowController extends BaseController {

    private final ITpFollowService tpFollowService;

    /**
     * 查询关注列表
     */
    @SaCheckPermission("work:follow:list")
    @GetMapping("/list")
    public TableDataInfo<TpFollowVo> list(TpFollowBo bo, PageQuery pageQuery) {
        return tpFollowService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出关注列表
     */
    @SaCheckPermission("work:follow:export")
    @Log(title = "关注", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TpFollowBo bo, HttpServletResponse response) {
        List<TpFollowVo> list = tpFollowService.queryList(bo);
        ExcelUtil.exportExcel(list, "关注", TpFollowVo.class, response);
    }

    /**
     * 获取关注详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("work:follow:query")
    @GetMapping("/{id}")
    public R<TpFollowVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(tpFollowService.queryById(id));
    }

    /**
     * 新增关注
     */
    @SaCheckPermission("work:follow:add")
    @Log(title = "关注", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody TpFollowBo bo) {
        return toAjax(tpFollowService.insertByBo(bo));
    }

    /**
     * 修改关注
     */
    @SaCheckPermission("work:follow:edit")
    @Log(title = "关注", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody TpFollowBo bo) {
        return toAjax(tpFollowService.updateByBo(bo));
    }

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