package com.palmnest.service.lingling.service.impl;

import java.lang.reflect.InvocationTargetException;
import java.util.*;

import com.palmnest.application.bean.AppResultDto;
import com.palmnest.application.core.util.AppResultUtil;
import com.palmnest.application.core.util.BeanUtils;
import com.palmnest.application.core.util.LingLingUtil;
import com.palmnest.application.core.util.StringUtil;
import com.palmnest.application.dto.input.lingling.CurrentsetDto;
import com.palmnest.model.lingling.DevicePermGroup;
import com.palmnest.model.lingling.LingLingCurrentset;
import com.palmnest.model.lingling.LingLingDeveloper;
import com.palmnest.service.lingling.manager.LingLingCurrentsetManager;
import com.palmnest.service.lingling.manager.LingLingManager;
import com.palmnest.service.lingling.service.LingLingService;

import lombok.extern.slf4j.Slf4j;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

/**
 * @Author HIAPAD
 * @Description TODO
 * @Date 2018/12/7 12:01
 **/
@Slf4j
@Service(value = "lingLingService")
public class LingLingServiceImpl implements LingLingService {
	

	private @Autowired LingLingManager lingLingManager;
	private @Autowired
	LingLingCurrentsetManager lingLingCurrentsetManager;
	
	@Override
	public String saveUserPerm(Integer groupId,String lingLingIds,Integer orgId){
		//拿项目对应的开发者信息
		long beginTime=System.currentTimeMillis();
		LingLingDeveloper developer=lingLingManager.findByOrgId(orgId);
		String deviceIds=lingLingManager.getDeviceIdsByGroupId(groupId);
		Map param=new HashMap();
		param.put("cardUids", lingLingIds.split(","));
		param.put("deviceIds", deviceIds.split(","));
		param.put("endTime", LingLingUtil.endTime);
		param.put("cardType", 2);
		param.put("cardFlag", 0);
		String res=LingLingUtil.addOpenDoorCard(param,developer.getUrl(),developer.getSignature(),developer.getToken(),developer.getOpenToken(),developer.getStrKey(),2);
		long endTime=System.currentTimeMillis();
		log.info("批量设置令令权限时间22222:"+(endTime-beginTime)+";;;;;;res:"+res);
		return res;
	}

	@Override
//	@Lazy(value=true)
	@Async
	public void updateUserPerm(DevicePermGroup group) {
		int methedType=lingLingManager.getMethedType(group.getOrgId().toString());
		if(2==methedType){
			Integer groupId=Integer.parseInt(group.getId().toString());
			String lingLingIds=lingLingManager.getLinglingIdsByGroupId(group.getId());
			if(StringUtils.isBlank(lingLingIds)){
				return;
			}
			List<String> list=StringUtil.indexStr(lingLingIds);
			if(CollectionUtils.isEmpty(list)){
				return;
			}
			long beginTime=System.currentTimeMillis();
			List<String> sucList=new ArrayList<String>();
			for(int i=0;i<list.size();i++){
				String lings=list.get(i);
				String res=saveUserPerm(groupId, lings,group.getOrgId());
				if(StringUtils.isNotBlank(res)){
					continue;
				}
				lings="'"+lings.replace(",", "','")+"'";
				sucList.add(lings);
			}
			long endTime=System.currentTimeMillis();
			log.info("批量设置令令权限时间:"+(endTime-beginTime));
			if(!CollectionUtils.isEmpty(sucList)){
				for(String lings:sucList){
					log.info("manager-updatePermGroupUser-lingLingIds:"+lingLingIds);
					int updateCount=lingLingManager.updatePermGroupUser(group.getId(),lings,3);
					log.info("manager-updatePermGroupUser-updateCount:"+updateCount);
				}
			}
		}
		
	}

	@Override
	public AppResultDto getCurrentsetInfo(Integer orgId) {
		LingLingCurrentset info = lingLingCurrentsetManager.getByOrgId(orgId);
		return AppResultUtil.createSuccesDto(info);
	}

	@Override
	public AppResultDto saveCurrentset(CurrentsetDto currentsetDto) {

		try {
			if(null != currentsetDto.getId()){
				LingLingCurrentset info = lingLingCurrentsetManager.get(currentsetDto.getId());
				if(info.getOrgId().intValue() != currentsetDto.getOrgId().intValue()){
					return AppResultUtil.createErrorDto("500","不能修改项目id");
				}
				BeanUtils.copyProperties(info, currentsetDto);
				info.setModifier(currentsetDto.getOperator());
				info.setModifyTime(new Date());
				lingLingCurrentsetManager.updateModel(info);
				return AppResultUtil.createSuccesDto(info);
			}else{
				LingLingCurrentset info = lingLingCurrentsetManager.getByOrgId(currentsetDto.getOrgId());
				if(null != info){
					BeanUtils.copyProperties(info, currentsetDto);
					info.setModifier(currentsetDto.getOperator());
					info.setModifyTime(new Date());
					lingLingCurrentsetManager.updateModel(info);
					return AppResultUtil.createSuccesDto(info);
				}else{
					info = new LingLingCurrentset();
					BeanUtils.copyProperties(info, currentsetDto);
					info.setIsEnabled(1);
					info.setCreator(currentsetDto.getOperator());
					info.setCreateTime(new Date());
					lingLingCurrentsetManager.saveModel(info);
					return AppResultUtil.createSuccesDto(info);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return AppResultUtil.createSuccesDto();
	}
}
