package org.dromara.ygpassport.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
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.ygpassport.domain.bo.GoalBo;
import org.dromara.ygpassport.domain.vo.GoalVo;
import org.dromara.ygpassport.service.IGoalService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 目标信息
 *
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/ygpassport/goal")
public class GoalController extends BaseController {

    private final IGoalService goalService;

    /**
     * 查询目标信息列表
     */
    @SaCheckPermission("ygpassport:goal:list")
    @GetMapping("/list")
    public TableDataInfo<GoalVo> list(GoalBo bo, PageQuery pageQuery) {
        return goalService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出目标信息列表
     */
    @SaCheckPermission("ygpassport:goal:export")
    @Log(title = "目标信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(GoalBo bo, HttpServletResponse response) {
        List<GoalVo> list = goalService.queryList(bo);
        ExcelUtil.exportExcel(list, "目标信息", GoalVo.class, response);
    }

    /**
     * 获取目标信息详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("ygpassport:goal:query")
    @GetMapping("/{id}")
    public R<GoalVo> getInfo(@NotNull(message = "主键不能为空")
                             @PathVariable Long id) {
        return R.ok(goalService.queryById(id));
    }

    /**
     * 新增目标信息
     */
    @SaCheckPermission("ygpassport:goal:add")
    @Log(title = "目标信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody GoalBo bo) {
        return toAjax(goalService.insertByBo(bo));
    }

    /**
     * 修改目标信息
     */
    @SaCheckPermission("ygpassport:goal:edit")
    @Log(title = "目标信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody GoalBo bo) {
        return toAjax(goalService.updateByBo(bo));
    }

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