package org.dromara.workflow.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
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.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.workflow.domain.bo.TActivityBo;
import org.dromara.workflow.domain.vo.TActivityVo;
import org.dromara.workflow.service.ITActivityService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 活动
 *
 * @author Lion Li
 * @date 2024-07-19
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/workflow/activity")
public class TActivityController extends BaseController {

    private final ITActivityService tActivityService;

    /**
     * 查询活动列表
     */
    @SaCheckPermission("workflow:activity:list")
    @GetMapping("/list")
    public TableDataInfo<TActivityVo> list(TActivityBo bo, PageQuery pageQuery) {
        return tActivityService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出活动列表
     */
    @SaCheckPermission("workflow:activity:export")
    @Log(title = "活动", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TActivityBo bo, HttpServletResponse response) {
        List<TActivityVo> list = tActivityService.queryList(bo);
        ExcelUtil.exportExcel(list, "活动", TActivityVo.class, response);
    }

    /**
     * 获取活动详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("workflow:activity:query")
    @GetMapping("/{id}")
    public R<TActivityVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(tActivityService.queryById(id));
    }

    /**
     * 新增活动
     */
    @SaCheckPermission("workflow:activity:add")
    @Log(title = "活动", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody TActivityBo bo) {
        return toAjax(tActivityService.insertByBo(bo));
    }

    /**
     * 修改活动
     */
    @SaCheckPermission("workflow:activity:edit")
    @Log(title = "活动", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody TActivityBo bo) {
        return toAjax(tActivityService.updateByBo(bo));
    }

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