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

import com.hz.pm.api.gov.manage.GovProjectCollectionManage;
import com.hz.pm.api.gov.model.dto.GovBizProjectSaveDTO;
import com.hz.pm.api.gov.model.dto.GovOperationProjectSaveDTO;
import com.hz.pm.api.gov.model.vo.GovBizProjectDetailVO;
import com.hz.pm.api.gov.model.vo.GovBizProjectListVO;
import com.hz.pm.api.gov.model.vo.GovOperationProjectDetailVO;
import com.hz.pm.api.gov.model.vo.GovProjectDictionaryVO;
import com.hz.pm.api.projectlib.model.req.ProjectListReq;
import com.hz.pm.api.scheduler.task.SynProjectCollectionTask;
import com.hz.pm.api.scheduler.task.SynProvinceOrgTask;
import com.ningdatech.basic.model.PageVo;
import com.ningdatech.log.annotation.WebLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.net.UnknownHostException;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zpf
 * @since 2023-08-21
 */
@Slf4j
@Validated
@RestController
@RequiredArgsConstructor
@Api(value = "GovProjectCollectionController", tags = "新项目归集")
@RequestMapping("/api/v1/gov-project-collection")
public class GovProjectCollectionController {

    private final GovProjectCollectionManage collectionManage;

    private final SynProjectCollectionTask collectionTask;

    private final SynProvinceOrgTask synProvinceOrgTask;

    @GetMapping("/dictionary")
    @ApiOperation("字典")
    public List<GovProjectDictionaryVO> dictionary(@RequestParam(value = "type", required = false) String type) {
        return collectionManage.dictionary(type);
    }

    @GetMapping("/list")
    @ApiOperation("项目归集列表")
    public PageVo<GovBizProjectListVO> list(ProjectListReq req) {
        return collectionManage.list(req);
    }

    @GetMapping("/operation/list")
    @ApiOperation("运维项目项目归集列表")
    public PageVo<GovBizProjectListVO> operationList(ProjectListReq req) {
        return collectionManage.operationList(req);
    }

    @GetMapping("/detail/{projId}")
    @ApiOperation("项目归集详情")
    public GovBizProjectDetailVO detail(@PathVariable String projId) {
        return collectionManage.detail(projId);
    }

    @GetMapping("/operation/detail/{projId}")
    @ApiOperation("运维项目归集详情")
    public GovOperationProjectDetailVO operationDetail(@PathVariable String projId) {
        return collectionManage.operationDetail(projId);
    }

    @PostMapping("/save")
    @ApiOperation("项目归集保存")
    @WebLog("项目归集保存")
    public String save(@Valid @RequestBody GovBizProjectSaveDTO saveDTO) {
        return collectionManage.save(saveDTO,null);
    }

    @PostMapping("/operation/save")
    @ApiOperation("运维项目归集保存")
    @WebLog("运维项目归集保存")
    public String operationSave(@Valid @RequestBody GovOperationProjectSaveDTO saveDTO) {
        return collectionManage.operationSave(saveDTO,null);
    }

    @PostMapping("/remove/{projId}")
    @ApiOperation("项目归集删除")
    @WebLog("项目归集删除")
    public String remove(@PathVariable String projId) {
        return collectionManage.remove(projId);
    }

    @PostMapping("/operation/remove/{projId}")
    @ApiOperation("运维项目归集删除")
    @WebLog("运维项目归集删除")
    public String operationRemove(@PathVariable String projId) {
        return collectionManage.operationRemove(projId);
    }

    @GetMapping("/draft-list")
    @ApiOperation("项目归集草稿箱列表")
    public PageVo<GovBizProjectListVO> draftList(ProjectListReq req) {
        return collectionManage.draftList(req);
    }

    @GetMapping("/operation/draft-list")
    @ApiOperation("运维项目归集草稿箱列表")
    public PageVo<GovBizProjectListVO> operationDraftList(ProjectListReq req) {
        return collectionManage.operationDraftList(req);
    }

    @GetMapping("/draft-detail/{draftId}")
    @ApiOperation("项目归集草稿箱详情")
    public GovBizProjectDetailVO draftDetail(@PathVariable Long draftId) {
        return collectionManage.draftDetail(draftId);
    }

    @GetMapping("/operation/draft-detail/{draftId}")
    @ApiOperation("运维项目归集草稿箱详情")
    public GovOperationProjectDetailVO operationDraftDetail(@PathVariable Long draftId) {
        return collectionManage.operationDraftDetail(draftId);
    }

    @PostMapping("/draft")
    @ApiOperation("项目归集暂存草稿箱")
    @WebLog("项目归集暂存草稿箱")
    public String draft(@Valid @RequestBody GovBizProjectSaveDTO saveDTO) {
        return collectionManage.draft(saveDTO,null);
    }

    @PostMapping("/operation/draft")
    @ApiOperation("运维项目归集暂存草稿箱")
    @WebLog("运维项目归集暂存草稿箱")
    public String operationDraft(@Valid @RequestBody GovOperationProjectSaveDTO saveDTO) {
        return collectionManage.operationDraft(saveDTO,null);
    }

    @PostMapping("/draft-remove/{draftId}")
    @ApiOperation("项目归集草稿箱删除")
    @WebLog("项目归集草稿箱删除")
    public String draftRemove(@PathVariable Long draftId) {
        return collectionManage.draftRemove(draftId);
    }

    @PostMapping("/operation/draft-remove/{draftId}")
    @ApiOperation("运维项目归集草稿箱删除")
    @WebLog("运维项目归集草稿箱删除")
    public String operationDraftRemove(@PathVariable Long draftId) {
        return collectionManage.operationDraftRemove(draftId);
    }

    @PostMapping("/push-task")
    @ApiOperation("主动调用定时器方法")
    @WebLog("主动调用定时器方法")
    public void pushTask() throws UnknownHostException {
        collectionTask.doTask();
    }

    @PostMapping("/push-task-single")
    @ApiOperation("主动调用定时器方法")
    @WebLog("主动调用定时器方法")
    public void pushTaskSingle(@RequestBody List<String> projectCodes) {
        collectionTask.doTaskSingle(projectCodes);
    }

    @PostMapping("/staging-convert-pdf")
    @ApiOperation("把库存的项目归集的文件 都转换成PDF文件存入字段")
    @WebLog("主动转换库存的文件都为PDF文件")
    public void stagingConvertPdf(@RequestParam(required = false) String projId) {
        collectionTask.stagingConvertPdf(projId);
    }

    @PostMapping("/staging-convert-pdf2")
    @ApiOperation("把库存的项目归集的文件approval_file重新转换")
    @WebLog("把库存的项目归集的文件approval_file重新转换")
    public void stagingConvertPdf2(@RequestParam(required = false) String projId) {
        collectionTask.stagingConvertPdf2(projId);
    }

    @PostMapping("/staging-convert-pdf3")
    @ApiOperation("把库存的项目归集的文件 都转换成PDF文件存入字段 采购")
    @WebLog("主动转换库存的文件都为PDF文件 采购")
    public void stagingConvertPdf3(@RequestParam(required = false) String projId) {
        collectionTask.stagingConvertPdf3(projId);
    }

    @PostMapping("/rebuild-project-code")
    @ApiOperation("重新生成项目编号")
    @WebLog("重新生成项目编号")
    public String stagingConvertPdf3(@RequestBody List<String> projIds) {
        return collectionManage.rebuildProjectCode(projIds);
    }

    @PostMapping("/remove-project")
    @ApiOperation("删除申报项目")
    @WebLog("删除申报项目")
    public String removeProject(@RequestBody List<String> projectCodes){
        return collectionManage.removeProject(projectCodes);
    }

    @GetMapping("/syn-province")
    @ApiOperation("同步省级单位")
    @WebLog("同步省级单位")
    public String synProvince() throws UnknownHostException {
        synProvinceOrgTask.doTask();
        return "同步成功";
    }
}
