package com.ruoyi.scientific.controller;

import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

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.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.scientific.domain.vo.ProjectApplicationVo;
import com.ruoyi.scientific.domain.bo.ProjectApplicationBo;
import com.ruoyi.scientific.service.IProjectApplicationService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 项目申报
 *
 * @author ruoyi
 * @date 2024-07-18
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/scientific/application")
public class ProjectApplicationController extends BaseController {

    private final IProjectApplicationService iProjectApplicationService;

    /**
     * 查询项目申报列表
     */
    @SaCheckPermission("scientific:application:list")
    @GetMapping("/list")
    public TableDataInfo<ProjectApplicationVo> list(ProjectApplicationBo bo, PageQuery pageQuery) {
        return iProjectApplicationService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出项目申报列表
     */
    @SaCheckPermission("scientific:application:export")
    @Log(title = "项目申报", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ProjectApplicationBo bo, HttpServletResponse response) {
        List<ProjectApplicationVo> list = iProjectApplicationService.queryList(bo);
        ExcelUtil.exportExcel(list, "项目申报", ProjectApplicationVo.class, response);
    }

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

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

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


    /**
     * 修改项项目预算审批
     */
    @SaCheckPermission("scientific:application:edit")
    @RepeatSubmit()
    @PostMapping("/updateBudgetProc")
    public R<Void> updateBudgetProc(@Validated(EditGroup.class) @RequestBody ProjectApplicationBo bo) {
        return toAjax(iProjectApplicationService.updateBudgetProc(bo));
    }


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