package org.fouram.service;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.fouram.core.base.service.BaseService;
import org.fouram.core.util.DateUtil;
import org.springframework.stereotype.Service;

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

@Service
public class SapCheckReportService extends BaseService {

	public JSONObject findUserReportPage(String startDate, String endDate, String departId, String name, String state,
			Integer pageSize, Integer pageNumber) throws Exception {
		List<Map<String, Object>> list = findUserReports(startDate, endDate, departId, name, state, pageSize,
				pageNumber);
		Long total = findUserReportTotal(startDate, endDate, departId, name, state);
		return getPageResult(list, total);
	}
	
	public static JSONObject getPageResult(List<? extends Object> list, Object total) {
		JSONObject object = new JSONObject();
		object.put("total", total);
		object.put("data", list);
		return object;
	}

	@SuppressWarnings("unchecked")
	public List<Map<String, Object>> findUserReports(String startDate, String endDate, String departId, String name,
			String state, Integer pageSize, Integer pageNumber) throws Exception {
		if (StringUtils.isNotBlank(departId) && departId.endsWith(",")) {
			departId = departId.substring(0, departId.length() - 1);
		}
		if (StringUtils.isNotBlank(state)) {
			if ("1".equals(state)) {
				state = "正常";
			} else {
				state = "异常";
			}
		}
		Map<String, String> pd = Maps.newHashMap();
		pd.put("startDate", startDate);
		pd.put("endDate", endDate);
		pd.put("departId", departId);
		pd.put("name", name);
		pd.put("state", state);
		if (pageSize != null) {
			pd.put("pageCurrent", String.valueOf(pageSize * (pageNumber - 1)));
			pd.put("pageSize", String.valueOf(pageSize));
		}
		return (List<Map<String, Object>>) findList("SapCheckReportMapper.findUserReports", pd);
	}

	@SuppressWarnings("unchecked")
	public List<Map<String, Object>> selectReportDayList(String startDate, String endDate, String departId, String name)
			throws Exception {
		if (StringUtils.isNotBlank(departId) && departId.endsWith(",")) {
			departId = departId.substring(0, departId.length() - 1);
		}
		Map<String, String> pd = Maps.newHashMap();
		pd.put("startDate", startDate);
		pd.put("endDate", endDate);
		pd.put("departId", departId);
		pd.put("name", name);
		pd.put("reportDaySql", getReportDaySql(startDate, endDate));

//		公司 部门 工号	姓名5月25日	5月26日	5月27日	5月28日	5月29日
		List<String> days = DateUtil.getSubDateList(startDate, endDate);
		Map<String, Object> headerMap = Maps.newLinkedHashMap();
		headerMap.put("firstDepartName", "公司");
		headerMap.put("secondDepartName", "部门");
		headerMap.put("userId", "工号");
		headerMap.put("name", "姓名");
		for (String day : days) {
			headerMap.put(day, day);
		}
		List<Map<String, Object>> result = Lists.newArrayList(headerMap);
		List<Map<String, Object>> list = (List<Map<String, Object>>) findList("SapCheckReportMapper.selectReportDayList", pd);
		list.stream().forEach(check -> result.add(check));
		return result;
	}

	private String getReportDaySql(String startDate, String endDate) throws Exception {
		String baseSql = "replace(group_concat(case when a.checkinDay = '%s' then a.result else '' end ),',','') as '%s',";
		List<String> days = DateUtil.getSubDateList(startDate, endDate);
		String reportDaySql = "";
		for (String day : days) {
			reportDaySql += String.format(baseSql, day, day);
		}
		reportDaySql = reportDaySql.substring(0, reportDaySql.length() - 1);
		return reportDaySql;
	}

	public Long findUserReportTotal(String startDate, String endDate, String departId, String name, String state)
			throws Exception {
		if (StringUtils.isNotBlank(departId) && departId.endsWith(",")) {
			departId = departId.substring(0, departId.length() - 1);
		}
		if (StringUtils.isNotBlank(state)) {
			if ("1".equals(state)) {
				state = "正常";
			} else {
				state = "异常";
			}
		}
		Map<String, String> pd = Maps.newHashMap();
		pd.put("startDate", startDate);
		pd.put("endDate", endDate);
		pd.put("departId", departId);
		pd.put("name", name);
		pd.put("state", state);
		return (Long) findObject("SapCheckReportMapper.findUserReportTotal", pd);
	}
}