package com.ruoyi.scientific.controller;

import java.util.List;
import java.util.Arrays;

import com.ruoyi.scientific.domain.bo.ProjectPlanBo;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.scientific.domain.vo.ProjectPlanVo;
import com.ruoyi.scientific.service.IProjectPlanService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 项目申报v2
 *
 * @author ruoyi
 * @date 2024-07-23
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/scientific/plan")
public class ProjectPlanController extends BaseController {

    private final IProjectPlanService iProjectHandbookService;

    /**
     * 查询项目申报v2列表
     */
    @SaCheckPermission("scientific:project_application_plan:list")
    @GetMapping("/list")
    public TableDataInfo<ProjectPlanVo> list(ProjectPlanBo bo, PageQuery pageQuery) {
        return iProjectHandbookService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出项目申报计划列表
     */
    @SaCheckPermission("scientific:plan:export")
    @Log(title = "项目申报v2", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ProjectPlanBo bo, HttpServletResponse response) {
        List<ProjectPlanVo> list = iProjectHandbookService.queryList(bo);
        ExcelUtil.exportExcel(list, "项目申报v2", ProjectPlanVo.class, response);
    }

    /**
     * 获取项目申报计划详细信息
     *
     * @param planId 主键
     */
    @SaCheckPermission("scientific:plan:query")
    @GetMapping("/{planId}")
    public R<ProjectPlanVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long planId) {
        return R.ok(iProjectHandbookService.queryById(planId));
    }

    /**
     * 新增项目申报计划
     */
    @SaCheckPermission("scientific:plan:add")
    @Log(title = "项目申报计划", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ProjectPlanBo bo) {
        return toAjax(iProjectHandbookService.insertByBo(bo));
    }

    /**
     * 修改项目申报计划
     */
    @SaCheckPermission("scientific:plan:edit")
    @Log(title = "项目申报计划", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ProjectPlanBo bo) {
        return toAjax(iProjectHandbookService.updateByBo(bo));
    }

    /**
     * 删除项目申报计划
     *
     * @param planIds 主键串
     */
    @SaCheckPermission("scientific:plan:remove")
    @Log(title = "项目申报计划", businessType = BusinessType.DELETE)
    @DeleteMapping("/{planIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] planIds) {
        return toAjax(iProjectHandbookService.deleteWithValidByIds(Arrays.asList(planIds), true));
    }
}
