package com.shr.serivce.impl;

import com.kingdee.shr.api.Response;
import com.shr.api.CallShrOsfApi;
import com.shr.common.response.RestResponse;
import com.shr.serivce.IToDoTaskService;
import com.shr.transfer.task.ToDoTaskDetailListInVo;
import com.shr.transfer.task.ToDoTaskDetailListOutVo;
import com.shr.transfer.task.ToDoTaskListInVo;
import com.shr.transfer.task.ToDoTaskListOutVo;
import com.shr.utils.HttpClientUtil;
import com.shr.utils.JsonUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.SortedMap;
import java.util.stream.Collectors;

import javax.servlet.jsp.jstl.sql.Result;

/**
 * @author Inori
 */
@Service
public class ToDoTaskServiceImpl implements IToDoTaskService {

    @Autowired
    private CallShrOsfApi callShrOsfApi;


    @Override
    public void toDoTaskList(Result rs) {
        String url = "http://10.10.12.11";
		String json = "{\"userName\":\"erprest\",\"password\":\"erpresthd123\"}";
		String toekn = HttpClientUtil.sendPost(url + "/seeyon/rest/token", json);
		Map<String, Object> temp = JsonUtil.toMap(toekn, String.class, Object.class);
		if (CollectionUtils.isEmpty(temp) && !temp.containsKey("id")) {
			
		    return;
		}

		List<Map<String, Object>> tempList = new ArrayList<>();
		
		for(SortedMap map : rs.getRows())
		{
		    Map<String, Object> reqeust = new HashMap<>();
		    reqeust.put("taskId",map.get("FASSIGNID"));
		    reqeust.put("registerCode", "3001");
		    reqeust.put("title",map.get("FSUBJECT_L1"));
		    reqeust.put("classify", "erp");
		    reqeust.put("contentType", "erp");
		    reqeust.put("thirdSenderId", "");
		    reqeust.put("senderName", "");
		    reqeust.put("thirdReceiverId", "");
		    reqeust.put("noneBindingReceiver", map.get("FUserNumber"));
		    reqeust.put("noneBindingSender",map.get("FUserNumber2"));
		    reqeust.put("creationDate",map.get("FCreatedTime"));
		    reqeust.put("state", "0");
		    reqeust.put("subState", "0");
		    reqeust.put("content", "");
		    reqeust.put("url", "http://10.10.12.31/ShrClient/Default.aspx");
		    reqeust.put("h5url", "");
		    reqeust.put("appParam", "");
		    String result = HttpClientUtil.sendPost(url + "/seeyon/rest/thirdpartyPending/receive?token=" + temp.get("id"),  JsonUtil.toString(reqeust));
		    tempList.add(reqeust);
		}
    }

    @Override
    public void toDoTaskDetailList(Result rs) {
        String url = "http://10.10.12.11";
		String json = "{\"userName\":\"erprest\",\"password\":\"erpresthd123\"}";
		String toekn = HttpClientUtil.sendPost(url + "/seeyon/rest/token", json);
		Map<String, Object> temp = JsonUtil.toMap(toekn, String.class, Object.class);
		if (CollectionUtils.isEmpty(temp) && !temp.containsKey("id")) {
		    return;
		}

		List<Map<String, Object>> tempList = new ArrayList<>();
		for(SortedMap map : rs.getRows())
		{
		    Map<String, Object> reqeust = new HashMap<>();
		    reqeust.put("taskId",map.get("ID"));
		    reqeust.put("registerCode", "3001");
		    reqeust.put("state", "1");
		    reqeust.put("subState", "0");
		    String result = HttpClientUtil.sendPost(url + "/seeyon/rest/thirdpartyPending/updatePendingState?token=" + temp.get("id"),  JsonUtil.toString(reqeust));
		    tempList.add(reqeust);
		}
    }
}
