package com.sa.ibiz.demo.demoapp.controller.demo;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.sql.Timestamp;
import javax.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.Data;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonProperty.Access;
import com.sa.ibiz.demo.ibizutil.domain.ActionResult;
import com.sa.ibiz.demo.demo.service.QJService;
import com.sa.ibiz.demo.ibizutil.domain.AutoCompleteItem;
import com.sa.ibiz.demo.demo.domain.QJ;
import com.sa.ibiz.demo.demo.service.dto.QJSearchFilter;
import com.sa.ibiz.demo.demoapp.controller.demo.vo.*;
import org.springframework.cglib.beans.BeanGenerator;
import org.springframework.cglib.beans.BeanMap;
import org.springframework.util.StringUtils;
import java.io.IOException;
import com.sa.ibiz.demo.ibizutil.errors.BadRequestAlertException;
import org.springframework.validation.annotation.Validated;
import org.springframework.http.ResponseEntity;
import org.springframework.beans.factory.annotation.Autowired;
import com.sa.ibiz.demo.ibizutil.domain.RedirectResult;
import javax.validation.constraints.NotBlank;
import com.sa.ibiz.demo.ibizutil.domain.AutoCompleteItem;
import java.math.BigInteger;
import com.alibaba.fastjson.JSONObject;
import com.sa.ibiz.demo.workflow.WFServiceImpl;
import com.sa.ibiz.demo.ibizutil.security.AuthenticationUser;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import com.alibaba.fastjson.JSONArray;
import org.flowable.bpmn.model.SequenceFlow;

@RestController
@RequestMapping("/demoapp/demo/qj")
public class QJController{


	private String strWorkflowKey = "QJSP" ;

	@Autowired
	WFServiceImpl wfServiceImpl ;

		@GetMapping(value = "wfdiagram")
	public ResponseEntity<byte[]> wfDiagram(@Validated @NotBlank(message = "srfkey不允许为空") @RequestParam("srfkey") String srfkey) {
		InputStream stream = wfServiceImpl.getProcessInstanceDiagram(strWorkflowKey, srfkey) ;

		HttpHeaders responseHeaders = new HttpHeaders();
		responseHeaders.set("Content-Type", "image/png");
		try {
			if(stream!=null)
				return new ResponseEntity<>(org.apache.commons.io.IOUtils.toByteArray(stream), responseHeaders, HttpStatus.OK);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null ;
	}
	
	@GetMapping(value = "his")
	public ResponseEntity<JSONArray> wfHis(@Validated @NotBlank(message = "srfkey不允许为空") @RequestParam("srfkey") String srfkey) {
		JSONArray json = new JSONArray() ;

		return ResponseEntity.ok().body(json);
	}
	
	@GetMapping(value = "wflinks")
	public ResponseEntity<JSONArray> wfLinks(@Validated @NotBlank(message = "srfkey不允许为空") @RequestParam("srfkey") String srfkey) {
		JSONArray jsonArray = new JSONArray() ;
		List<SequenceFlow> flowElements = wfServiceImpl.findTaskLinkByUser(strWorkflowKey, srfkey, AuthenticationUser.getAuthenticationUser().getUserid()) ;
		for (SequenceFlow sequenceFlow : flowElements) {
			JSONObject json = new JSONObject();
			json.put("id", sequenceFlow.getId()) ;
			json.put("name", sequenceFlow.getName());
			if(StringUtils.isEmpty(sequenceFlow.getAttributeValue("http://flowable.org/bpmn", "approvalOperation"))) {
				json.put("type", "BACKEND") ;
			}else {
				json.put("type", "FRONT") ;
				json.put("page", sequenceFlow.getAttributeValue("http://flowable.org/bpmn", "approvalOperation")) ;
				json.put("mobpage", sequenceFlow.getAttributeValue("http://flowable.org/bpmn", "approvalMobOperation")) ;
			}
			
			jsonArray.add(json);	
		}
		return ResponseEntity.ok().body(jsonArray);
	}
	
	@PostMapping(value = "wflink")
	public ResponseEntity<JSONObject> wfLink(@RequestBody JSONObject request) {

		JSONObject ret = new JSONObject() ;
		if(!request.containsKey("args")) {
			ret.put("ret", "902") ;
			ret.put("errorInfo", "处理参数有误") ;
			return ResponseEntity.ok().body(ret);
		}
		JSONObject params = request.getJSONObject("args") ;
		String srfkey = request.getString("srfkey") ;
		String strLink = request.getString("link") ;
		
		if(params==null || StringUtils.isEmpty(srfkey)||StringUtils.isEmpty(strLink)) {
			ret.put("ret", "902") ;
			ret.put("errorInfo", "处理参数有误") ;
			return ResponseEntity.ok().body(ret);
		}
		
    	
    	params.put("ACTION", strLink) ;
    	try {
    		wfServiceImpl.execTask("complete", strWorkflowKey, srfkey, AuthenticationUser.getAuthenticationUser().getUserid(), params);
    		ret.put("ret", "0") ;
    		return ResponseEntity.ok().body(ret);
    	}
		catch (Exception e) {
			ret.put("ret", "903") ;
			ret.put("errorInfo", "处理参数有误") ;
			return ResponseEntity.ok().body(ret);
		}

	}

    @Autowired
	private QJService qjService;
	/**
	 * 获取服务对象
	*/
	protected QJService getService(){
		return this.qjService;
	}
 }