package com.hz.pm.api.projectdeclared.controller;

import javax.servlet.http.HttpServletResponse;

import com.ningdatech.log.annotation.WebLog;
import com.hz.pm.api.gov.manage.GovProjectCollectionManage;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.alibaba.fastjson.JSONObject;
import com.ningdatech.basic.model.PageVo;
import com.hz.pm.api.common.util.ExcelDownUtil;
import com.hz.pm.api.projectdeclared.manage.DeclaredProjectManage;
import com.hz.pm.api.projectdeclared.manage.ProjectAdjustmentManage;
import com.hz.pm.api.projectdeclared.model.dto.DeclaredProjectListParamDTO;
import com.hz.pm.api.projectdeclared.model.dto.DefaultDeclaredDTO;
import com.hz.pm.api.projectdeclared.model.dto.ProjectDraftSaveDTO;
import com.hz.pm.api.projectdeclared.model.vo.ProjectDraftVO;
import com.hz.pm.api.projectlib.model.req.ProjectListReq;
import com.hz.pm.api.projectlib.model.vo.ProjectLibListItemVO;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * @Classname DeclaredProjectController
 * @Description 申报项目
 * @Date 2023/1/31 11:29
 * @Author PoffyZhang
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/api/v1/declared")
@Api(value = "DeclaredProject", tags = "申报管理-申报项目")
@RequiredArgsConstructor
public class DeclaredProjectController {
    private final DeclaredProjectManage declaredProjectManage;

    private final ProjectAdjustmentManage projectAdjustmentManage;

    private final GovProjectCollectionManage collectionManage;

    @ApiOperation(value = "申报项目已申报列表", notes = "申报项目已申报列表")
    @GetMapping("/list")
    public PageVo<ProjectLibListItemVO> list(@ModelAttribute ProjectListReq req) {
        return declaredProjectManage.projectLibList(req);
    }

    @ApiOperation(value = "核心业务列表", notes = "核心业务列表")
    @GetMapping("/core-biz")
    public JSONObject bizList(@RequestParam(required = false) String businessName,
                              @RequestParam(required = false) Integer pageSize,
                              @RequestParam(required = false) Integer pageNumber,
                              @RequestParam(required = false) String orgCode) {
        return declaredProjectManage.bizList(businessName,pageSize,pageNumber,orgCode);
    }

    @ApiOperation(value = "申报项目草稿箱列表", notes = "申报项目草稿箱列表")
    @GetMapping("/draft")
    public PageVo<ProjectDraftVO> draft(@ModelAttribute DeclaredProjectListParamDTO params) {
        return declaredProjectManage.pageDraft(params);
    }

    @ApiOperation(value = "申报项目草稿箱详情", notes = "申报项目草稿箱详情")
    @GetMapping("/draft/{id}")
    public ProjectDraftVO draft(@PathVariable(value = "id") Long id) {
        return declaredProjectManage.draftDetail(id);
    }

    @ApiOperation(value = "申报项目保存至草稿箱", notes = "申报项目保存至草稿箱")
    @WebLog("申报项目保存至草稿箱")
    @PostMapping("/save-to-draft")
    public Object saveToDraft(@Validated @RequestBody ProjectDraftSaveDTO dto) {
        return declaredProjectManage.saveToDraft(dto);
    }

    @ApiOperation(value = "申报项目", notes = "申报项目")
    @WebLog("申报项目")
    @PostMapping("/start")
    public String startTheProcess(@Validated @RequestBody DefaultDeclaredDTO dto) {
        String instanceId = declaredProjectManage.startTheProcess(dto);
        return "启动流程实例 【" + instanceId + "】 成功";
    }

    @ApiOperation(value = "重新申报项目", notes = "重新申报项目")
    @WebLog("重新申报项目")
    @PostMapping("/restart")
    public String reStartTheProcess(@Validated @RequestBody DefaultDeclaredDTO dto) {
        return projectAdjustmentManage.adjustment(dto);
    }

    @GetMapping("/export")
    @ApiOperation("申报项目列表导出")
    @WebLog("申报项目列表导出")
    public void exportList(ProjectListReq req, HttpServletResponse response){
        ExcelDownUtil.downXls(response,req,declaredProjectManage::exportList);
    }

    @PostMapping("/remove/{projectCode}")
    @ApiOperation("申报项目删除-预审失败")
    @WebLog("申报项目删除-预审失败")
    public String remove(@PathVariable String projectCode){
        return declaredProjectManage.removeProject(projectCode);
    }
}
