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 cn.hutool.core.lang.Console;

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.stream.Collectors;

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

    @Autowired
    private CallShrOsfApi callShrOsfApi;


    @Override
    public void toDoTaskList(ResultSet rs) {
        try {
			this.toDoTaskSynchronization(rs);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }

    @Override
    public void toDoTaskDetailList(ResultSet rs) {
        try {
			this.toDoTaskSynchronization(rs);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }

    public RestResponse toDoTaskSynchronization(ResultSet rs) throws SQLException {
        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 RestResponse.fail("OA登陆失败");
        }

        List<Map<String, Object>> tempList = new ArrayList<>();
        while (rs.next()) {
            Map<String, Object> reqeust = new HashMap<>(15);
            reqeust.put("taskId",rs.getString("FASSIGNID"));
            reqeust.put("registerCode", "3001");
            reqeust.put("title",rs.getString("FSUBJECT_L1"));
            reqeust.put("classify", "erp");
            reqeust.put("contentType", "erp");
            reqeust.put("thirdSenderId", "");
            reqeust.put("senderName", "");
            reqeust.put("thirdReceiverId", "");
            reqeust.put("noneBindingReceiver", "1881");
            reqeust.put("noneBindingSender", "0009");
            reqeust.put("creationDate", rs.getDate("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", "");
            tempList.add(reqeust);
        }
        
        String para = JsonUtil.toString(tempList);
        Console.log("para:" + para);
        String result = HttpClientUtil.sendPost(url + "/seeyon/rest/thirdpartyPending/receive?token=" + temp.get("id"), para);
        System.out.println("---------------->" + result);
        Map<String, Object> map = JsonUtil.toMap(result, String.class, Object.class);
        if (CollectionUtils.isEmpty(map)) {
            return RestResponse.fail("无返回值");
        }
        if ("false".equals(map.get("success"))) {
            List<Object> errorList = JsonUtil.toList(JsonUtil.toString(map.get("errorMsgs")), Object.class);
            String errorDetail = errorList.stream().map(m -> (String) JsonUtil.toMap(JsonUtil.toString(m), String.class, Object.class).get("errorDetail")).collect(Collectors.joining(", "));
            return RestResponse.fail(errorDetail);
        }

        return RestResponse.success();
    }


}
