package com.persagy.calendar.service.impl;

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

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

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.Lists;
import com.persagy.calendar.client.RwdObjectClient;
import com.persagy.calendar.client.model.BaseModel;
import com.persagy.calendar.client.model.BaseQueryModel;
import com.persagy.calendar.client.model.RwdObjectModel;
import com.persagy.calendar.pojo.dto.WorkCalendar;
import com.persagy.calendar.pojo.dto.WorkCalendarObject;
import com.persagy.calendar.service.IWorkCalendarObjectService;
import com.persagy.calendar.service.IWorkCalendarService;
import com.persagy.calendar.service.RwdObjectService;
import com.persagy.common.constant.DMPConstant;
import com.persagy.common.enums.ResponseCode;
import com.persagy.common.utils.ResponseResult;
import com.persagy.common.utils.ResponseUtil;
import com.persagy.common.utils.StringUtil;

import cn.hutool.core.collection.CollectionUtil;

/**
 * @version 
 * @description 
 * @company persagy
 * @author zhangqiankun
 * @since 2021年3月2日:	下午3:15:57
 */
@Service
public class RwdObjectServiceImpl implements RwdObjectService {

	@Autowired
	private RwdObjectClient rwdObjectClient;

	@Autowired
	private IWorkCalendarService workCalendarService;
	
	@Autowired
	private IWorkCalendarObjectService workCalendarObjectService;
	
	@Override
	public ResponseResult queryRwdObjects(BaseModel baseModel, RwdObjectModel objectModel) {
		BaseQueryModel<RwdObjectModel> object = new BaseQueryModel<RwdObjectModel>();
		object.setCriteria(objectModel);
		String objects = this.rwdObjectClient.queryProjectObjects(baseModel, object);
		return this.parseDMPResult(objects, baseModel.getGroupCode());
	}
	
	/**
	 * 解析数据中台响应数据
	 * 
	 * @param dmpResult
	 * @return
	 */
	public ResponseResult parseDMPResult(String dmpResult, String groupCode) {
		if (StringUtil.isBlank(dmpResult) || dmpResult.indexOf(DMPConstant.DMP_RESULT_KEY) < 0) {
			return ResponseUtil.errorResult(ResponseCode.C0110.getCode(), "DMP服务响应异常");
		}
		JSONObject result = JSONObject.parseObject(dmpResult);
		if (StringUtil.isNotBlank(result.getString(DMPConstant.DMP_FAIL_MSG))) {
			return ResponseUtil.errorResult(ResponseCode.C0110.getCode(), result.getString(DMPConstant.DMP_FAIL_MSG));
		}
		if (!DMPConstant.DMP_RESULT_VAL.equals(result.getString(DMPConstant.DMP_RESULT_KEY))) {
			return ResponseUtil.errorResult(ResponseCode.C0110.getCode(), ResponseCode.C0110.getDesc());
		}
		JSONArray array = result.getJSONArray(DMPConstant.DMP_DATA_KEY);
		if (array == null || array.size() == 0) {
			return ResponseUtil.successResult(Lists.newArrayList(), 0L);
		}
		// 获取本地存储的对象关联信息
		QueryWrapper<WorkCalendarObject> queryWrapper = new WorkCalendarObject.Builder().createQueryWrapper()
				.groupCodeEq(groupCode).builderQueryWrapper();
		List<WorkCalendarObject> objects = this.workCalendarObjectService.list(queryWrapper);
		this.setCalendarId(array, groupCode, objects);
		return ResponseUtil.successResult(array, (long)array.size());  
	}
	
	/**
	 * 添加是否具有工作历的标识
	 * @param array
	 * @return
	 */
	public void setCalendarId(JSONArray array, String groupCode, List<WorkCalendarObject> objects) {
		// key-objId，value-calendarId
		Map<String, String> objToCalendar = new HashMap<String, String>();
		// key-calendarId，value-calendarName
		Map<String, String> calendarName = new HashMap<String, String>();
		if (CollectionUtil.isNotEmpty(objects)) {
			for (WorkCalendarObject object : objects) {
				objToCalendar.put(object.getObjectId(), object.getCalendarId());
				if (!calendarName.containsKey(object.getCalendarId())) {
					WorkCalendar calendar = this.workCalendarService.getById(object.getCalendarId());
					if (calendar != null) {
						calendarName.put(object.getCalendarId(), calendar.getCalendarName());
					}
				}
			}
		}
		int size = array.size();
		for(int i = 0; i < size; i++) {
			JSONObject jsonObject = array.getJSONObject(i);
			String calendarId = objToCalendar.get(jsonObject.getString("id"));
			if (StringUtil.isBlank(calendarId)) {
				jsonObject.put("isRelated", false);	//是否存在有工作历
			} else {
				jsonObject.put("calendarId", calendarId);
				jsonObject.put("calendarName", calendarName.get(calendarId));
				jsonObject.put("isRelated", true);
			}
		}
	}
	
}
