package com.ningdatech.pmapi.projectdeclared.controller;

import java.util.Iterator;

import javax.servlet.http.HttpServletResponse;

import com.ningdatech.log.annotation.WebLog;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ningdatech.basic.model.PageVo;
import com.ningdatech.pmapi.common.util.ExcelDownUtil;
import com.ningdatech.pmapi.projectdeclared.contants.DeclaredProjectContant;
import com.ningdatech.pmapi.projectdeclared.manage.DeclaredProjectManage;
import com.ningdatech.pmapi.projectdeclared.manage.ProjectAdjustmentManage;
import com.ningdatech.pmapi.projectdeclared.model.dto.DeclaredProjectListParamDTO;
import com.ningdatech.pmapi.projectdeclared.model.dto.DefaultDeclaredDTO;
import com.ningdatech.pmapi.projectdeclared.model.dto.ProjectDraftSaveDTO;
import com.ningdatech.pmapi.projectdeclared.model.vo.ProjectDraftVO;
import com.ningdatech.pmapi.projectlib.model.req.ProjectListReq;
import com.ningdatech.pmapi.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;

    @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) {
        JSONObject jsonObject = JSONObject.parseObject(DeclaredProjectContant.Biz.CORE_BIZ);
        if(StringUtils.isNotBlank(businessName)){
            JSONArray dataArray = JSONArray.parseArray(jsonObject.getString("data"));
            Iterator iter = dataArray.stream().iterator();
            JSONArray dataArrayRes = new JSONArray();
            while (iter.hasNext()) {
                JSONObject value = (JSONObject) iter.next();
                String businessValue = value.getString("matterName");
                if(StringUtils.isNotBlank(businessValue) && businessValue.contains(businessName)){
                    dataArrayRes.add(value);
                }
            }
            jsonObject.put("data",dataArrayRes);
        }
        return jsonObject;
    }

    @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.draftDatail(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);
    }
}
