package com.unis.flowable.server.controller;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.fastjson.JSONObject;
import com.unis.common.utils.CurrentUtil;
import com.unis.common.utils.MapUtil;
import com.unis.common.utils.ResponseWrapper;
import com.unis.flowable.common.vo.DeploymentVO;
import com.unis.flowable.common.vo.ItemFilterVO;
import com.unis.flowable.server.service.FlowTaskService;
import com.unis.flowable.server.service.HomePageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

@RestController
@RequestMapping("/hometodo")
@Slf4j
@Api(value = "系统首页待办查询", tags = "系统首页待办查询")
@SuppressWarnings("all")
public class HomePageTodoController {

	@Autowired
	private HomePageService homeService;
	
	@Autowired
	private FlowTaskService flowTaskService;

	
	/**
	 * 【粮食首页】查询待办事项
	 * @param page
	 * @param size
	 * @return ResponseWrapper
	 * @author liyb
	 * @date 2019-05-10 11:01:30
	 * @version v1.0
	 */
	@ApiOperation(value = "【粮食首页】查询待办事项", notes = "参数：起始页，每页展示行数")
	@PostMapping(value = "/findWaitItemForGrain/{page}/{size}")
	@ApiImplicitParams({ @ApiImplicitParam(name = "page", value = "起始页", required = true),
			@ApiImplicitParam(name = "size", value = "每页展示行数", required = true) })
	public ResponseWrapper findWaitItemForGrain(@PathVariable("page") Integer page, @PathVariable("size") Integer size, @RequestBody ItemFilterVO itemFilterVO) {
		try {
			Map<String, Object> list = homeService.findWaitItemForGrain(page, size, itemFilterVO);
			return ResponseWrapper.success(200, "查询成功", list);
		} catch (Exception e) {
			e.printStackTrace();
			return ResponseWrapper.error("【粮食首页】待办事项查询失败");
		}
	}
	
	
	 /**
     * 查询待办事项
     * @return ResponseWrapper
     * @author liyb
     * @date 2018-12-12 14:30:55
     * @version v1.0
     */
    @ApiOperation(value = "查询待办事项-----待办事项", notes = "主要参数：用户登录账号")
    @PostMapping(value = "/queryToDoItems/{page}/{size}")
    public ResponseWrapper queryToDoItems(@PathVariable("page") Integer page, @PathVariable("size") Integer size) {
        try {
        List<Map>  mapList=new ArrayList();
          String creditCode = CurrentUtil.getCreditCode();
          List<DeploymentVO> deploymentListList= flowTaskService.getUserTaskNoPage(creditCode);
  		   ResponseWrapper responseWrapperFlowAble = ResponseWrapper.success(200, "查询成功", deploymentListList);
          if(responseWrapperFlowAble.getInfoCode().intValue()==200) {
              Object obj= responseWrapperFlowAble.getData();
              List parseObject = JSONObject.parseObject(JSONObject.toJSONString(obj), List.class);
              List<Map> resultDate = JSONObject.parseArray(JSONObject.toJSONString(parseObject), Map.class);
              for(Map flow:resultDate){
                  Map map = new HashMap();
                  map.put("actId","");
                  map.put("taskId",flow.get("taskId"));
                  map.put("procInstId", flow.get("procInsId"));
                  map.put("procdefKey", flow.get("procdefKey"));
                  map.put("actName", flow.get("taskName"));
                  map.put("procName",flow.get("procdefName"));
                  map.put("starter", "");
                  map.put("desc", "");
                  map.put("creditCode", creditCode);
                  map.put("code","");
                  map.put("startTime",flow.get("startTime"));
                  map.put("type", 6);
                  map.put("userCreditCode",flow.get("userCreditCode"));
                  mapList.add(map);
              }
          }

            Collections.sort(mapList, new Comparator() {
                @Override
                public int compare(Object o1, Object o2) {
                    if (o1 instanceof Map && o2 instanceof Map) {
                        Map e1 = (Map) o1;
                        Map e2 = (Map) o2;
                        if (!"".equals(e1.get("startTime")) && null != e1.get("startTime")
                                && !"".equals(e2.get("startTime")) && null != e2.get("startTime")) {
                            if (e1.get("startTime").toString().compareTo(e2.get("startTime").toString()) > 0) {
                                return -1;
                            }
                            if (e1.get("startTime").toString().compareTo(e2.get("startTime").toString()) == 0) {
                                return 0;
                            }
                            if (e1.get("startTime").toString().compareTo(e2.get("startTime").toString()) < 0) {
                                return 1;
                            }
                        } else {
                            return -1;
                        }
                    }
                    throw new ClassCastException("不能转换为JSONObject类型");
                }
            });
            return ResponseWrapper.success(200, "查询待办事项成功", MapUtil.getMapForPage(mapList,page,size));
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseWrapper.error("查询待办事项失败");
        }
    }
    
    
    
    
	
	
	
}
