package org.dromara.progress.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
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.progress.domain.dto.progressplan.PgsProgressPlanCreateReq;
import org.dromara.progress.domain.dto.progressplan.PgsProgressPlanQueryReq;
import org.dromara.progress.domain.vo.progressplan.PgsProgressPlanVo;
import org.dromara.progress.service.IPgsProgressPlanService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 进度计划
 *
 * @author lcj
 * @date 2025-05-27
 */
@Validated
@RestController
@RequestMapping("/progress/progressPlan")
public class PgsProgressPlanController extends BaseController {

    @Resource
    private IPgsProgressPlanService pgsProgressPlanService;

    /**
     * 查询进度计划列表
     */
    @SaCheckPermission("progress:progressPlan:list")
    @GetMapping("/list")
    public TableDataInfo<PgsProgressPlanVo> list(PgsProgressPlanQueryReq req, PageQuery pageQuery) {
        return pgsProgressPlanService.queryPageList(req, pageQuery);
    }

    /**
     * 导出进度计划列表
     */
    @SaCheckPermission("progress:progressPlan:export")
    @Log(title = "进度计划", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(PgsProgressPlanQueryReq req, HttpServletResponse response) {
        List<PgsProgressPlanVo> list = pgsProgressPlanService.queryList(req);
        ExcelUtil.exportExcel(list, "进度计划", PgsProgressPlanVo.class, response);
    }

    /**
     * 获取进度计划详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("progress:progressPlan:query")
    @GetMapping("/{id}")
    public R<PgsProgressPlanVo> getInfo(@NotNull(message = "主键不能为空")
                                        @PathVariable Long id) {
        return R.ok(pgsProgressPlanService.queryById(id));
    }

    /**
     * 新增进度计划
     */
    @SaCheckPermission("progress:progressPlan:add")
    @Log(title = "进度计划", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Long> add(@Validated(AddGroup.class) @RequestBody PgsProgressPlanCreateReq req) {
        return R.ok(pgsProgressPlanService.insertByBo(req));
    }

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