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.ScoreplanBo;
import org.dromara.ygpassport.domain.vo.ScoreplanVo;
import org.dromara.ygpassport.service.IScoreplanService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 个人目标与成长计划
 *
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/scoreplan")
public class ScoreplanController extends BaseController {

    private final IScoreplanService scoreplanService;

    /**
     * 查询个人目标与成长计划列表
     */
    @SaCheckPermission("system:scoreplan:list")
    @GetMapping("/list")
    public TableDataInfo<ScoreplanVo> list(ScoreplanBo bo, PageQuery pageQuery) {
        return scoreplanService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出个人目标与成长计划列表
     */
    @SaCheckPermission("system:scoreplan:export")
    @Log(title = "个人目标与成长计划", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ScoreplanBo bo, HttpServletResponse response) {
        List<ScoreplanVo> list = scoreplanService.queryList(bo);
        ExcelUtil.exportExcel(list, "个人目标与成长计划", ScoreplanVo.class, response);
    }

    /**
     * 获取个人目标与成长计划详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("system:scoreplan:query")
    @GetMapping("/{id}")
    public R<ScoreplanVo> getInfo(@NotNull(message = "主键不能为空")
                                  @PathVariable Long id) {
        return R.ok(scoreplanService.queryById(id));
    }

    /**
     * 新增个人目标与成长计划
     */
    @SaCheckPermission("system:scoreplan:add")
    @Log(title = "个人目标与成长计划", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ScoreplanBo bo) {
        return toAjax(scoreplanService.insertByBo(bo));
    }

    /**
     * 修改个人目标与成长计划
     */
    @SaCheckPermission("system:scoreplan:edit")
    @Log(title = "个人目标与成长计划", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ScoreplanBo bo) {
        return toAjax(scoreplanService.updateByBo(bo));
    }

    /**
     * 删除个人目标与成长计划
     *
     * @param ids 主键串
     */
    @SaCheckPermission("system:scoreplan:remove")
    @Log(title = "个人目标与成长计划", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(scoreplanService.deleteWithValidByIds(List.of(ids), true));
    }
}
