package org.fouram.core.plugin.weixin.cp.util;

import static org.fouram.core.plugin.weixin.cp.core.WXCpOaApprovalBase.oaApprovalService;
import static org.fouram.core.plugin.weixin.cp.core.WXCpOaBase.oaService;

import java.util.Date;
import java.util.List;

import org.fouram.core.util.DateUtil;
import org.fouram.entity.SapApprovalInfo;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;

import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.cp.bean.oa.WxCpApprovalDetailResult;
import me.chanjar.weixin.cp.bean.oa.WxCpApprovalInfo;
import me.chanjar.weixin.cp.bean.oa.WxCpApprovalInfoQueryFilter;
import me.chanjar.weixin.cp.bean.oa.WxCpApprovalInfoQueryFilter.KEY;
import me.chanjar.weixin.cp.bean.oa.WxCpCheckinData;
import me.chanjar.weixin.cp.bean.oa.WxCpCheckinOption;
import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder;

/**
 * 企业微信用户工具类
 * 
 * @author Liup
 */
public class WXCpOaUtil {

	public static List<WxCpCheckinData> getCheckinData(Date startTime, Date endTime, List<String> userIdList) {
		// 打卡类型。1：上下班打卡；2：外出打卡；3：全部打卡
		try {
			return oaService.getCheckinData(3, startTime, endTime, userIdList);
		} catch (WxErrorException e) {
			e.printStackTrace();
			return Lists.newArrayList();
		}
	}

	public static List<WxCpCheckinOption> getCheckinOption(List<Date> datetimes, List<String> userIdList) {
		try {
			List<WxCpCheckinOption> result = Lists.newArrayList();
			for (Date datetime : datetimes) {
				oaService.getCheckinOption(datetime, userIdList).stream().forEach(option -> result.add(option));
			}
			return result;
		} catch (WxErrorException e) {
			e.printStackTrace();
			return Lists.newArrayList();
		}
	}

	public static List<WxCpApprovalInfo> getAllApprovalInfo(Date startTime, Date endTime) throws WxErrorException {
		List<WxCpApprovalInfo> result = Lists.newArrayList();
		Integer cursor = 0;
		while (true) {
			WxCpApprovalInfo approvalInfo = getApprovalInfo(startTime, endTime, cursor);
			result.add(approvalInfo);
			if (approvalInfo.getNextCursor() != null) {
				cursor = approvalInfo.getNextCursor();
			} else {
				return result;
			}
		}
	}

	public static List<SapApprovalInfo> getApprovalDetailList(Date startTime, Date endTime) throws WxErrorException {
		List<SapApprovalInfo> result = Lists.newArrayList();
		for (WxCpApprovalInfo approvalInfo : getAllApprovalInfo(startTime, endTime)) {
			for (String spNo : approvalInfo.getSpNoList()) {
				String responseContent = oaApprovalService.getApprovalDetailToString(spNo);
				if (responseContent.contains("\"time\"")) {
					String str = responseContent.substring(responseContent.indexOf("\"time\""));
					String time = JSONObject.parseObject("{" + str.substring(0, str.indexOf("}")) + "}").get("time")
							.toString();
					String userId = WxCpGsonBuilder.create().fromJson(responseContent, WxCpApprovalDetailResult.class)
							.getInfo().getApplier().getUserId();
					SapApprovalInfo info = SapApprovalInfo.builder().userId(userId).time(time)
							.day(DateUtil.getDay(new Date(Long.valueOf(time) * 1000))).build();
					result.add(info);
				}
			}
		}
		return result;
	}

	public static WxCpApprovalInfo getApprovalInfo(Date startTime, Date endTime, Integer cursor)
			throws WxErrorException {
		List<WxCpApprovalInfoQueryFilter> filters = Lists.newArrayList();
		WxCpApprovalInfoQueryFilter filter1 = new WxCpApprovalInfoQueryFilter();
		filter1.setKey(KEY.TEMPLATE_ID);
		filter1.setValue("Bs5GudP8BZj4AuSmR8qHopZXhkYKXZ2mU3rP4jeQB");
		filters.add(filter1);
		WxCpApprovalInfoQueryFilter filter2 = new WxCpApprovalInfoQueryFilter();
		filter2.setKey(KEY.SP_STATUS);
		filter2.setValue("2"); // 已通过
		filters.add(filter2);
		return oaApprovalService.getApprovalInfo(startTime, endTime, cursor, 100, filters);
	}

	public static void main(String[] args) throws Exception {
		System.out.println(getApprovalDetailList(DateUtil.sdfTime.parse("2020-04-01 00:00:00"),
				DateUtil.sdfTime.parse("2020-04-01 23:59:59")));
	}
}
