package com.ruoyi.project.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.project.domain.vo.ProjectApplicationVo;
import com.ruoyi.project.domain.bo.ProjectApplicationBo;
import com.ruoyi.project.service.IProjectApplicationService;
import com.ruoyi.common.core.page.TableDataInfo;

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

    private final IProjectApplicationService iProjectApplicationService;

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

    /**
     * 导出项目申报列表
     */
    @SaCheckPermission("project: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("project:application:query")
    @GetMapping("/{projectId}")
    public R<ProjectApplicationVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long projectId) {
        return R.ok(iProjectApplicationService.queryById(projectId));
    }

    /**
     * 新增项目申报
     */
    @SaCheckPermission("project: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("project:application:edit")
    @Log(title = "项目申报", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ProjectApplicationBo bo) {
        return toAjax(iProjectApplicationService.updateByBo(bo));
    }

    /**
     * 删除项目申报
     *
     * @param projectIds 主键串
     */
    @SaCheckPermission("project: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));
    }
}
