package com.ssj.service.conch.conch.service.impl;

import com.ssj.bean.conch.conch.domain.SconOtaManager;
import com.ssj.dao.conch.conch.dao.SconOtaManagerDao;
import com.ssj.framework.core.common.service.BaseServiceImpl;
import com.ssj.framework.core.persistence.DynamicSpecifications;
import com.ssj.framework.core.persistence.SearchFilter;
import com.ssj.framework.core.util.ResponseEntity;
import com.ssj.framework.core.util.SystemResourceLocator;
import com.ssj.service.conch.conch.request.OtaVersionReq;
import com.ssj.service.conch.conch.service.SconOtaManagerService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author yangcan
 * 类描述: ota系统管理
 * 创建时间:2019/6/28 13:23
 */
@Service
@Transactional
public class SconOtaManagerServiceImpl extends BaseServiceImpl<SconOtaManager, String> implements SconOtaManagerService {

	private   Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	private SconOtaManagerDao sconOtaManagerDao;

	@Override
	public PagingAndSortingRepository<SconOtaManager, String> getDao() {
		return sconOtaManagerDao;
	}

	@Override
	public SconOtaManager findByCurrentVersion(String currentVersion) {
		return sconOtaManagerDao.findByCurrentVersion(currentVersion);
	}

	@Override
	public ResponseEntity getOtaVersion(OtaVersionReq request) {
		ResponseEntity responseEntity = new ResponseEntity();
		//查询ota版本信息
		SconOtaManager sconOtaManager = sconOtaManagerDao.findByCurrentVersion(request.getCurrentVersion());
		if(sconOtaManager == null) {
			responseEntity.failure("未查询到需要升级的ota版本信息");
			return responseEntity;
		}
		Map<String, Object> resultMap = new HashMap<>();
		resultMap.put("updatePackName", sconOtaManager.getUpdatePackName());
		resultMap.put("currentVersion", sconOtaManager.getCurrentVersion());
		resultMap.put("updateVersion", sconOtaManager.getUpdateVersion());
		String sysEnv = (String) SystemResourceLocator.getValue("SYS_ENV");
		if("1".equals(sysEnv)) {
			//正式环境
			resultMap.put("fileKey","https://img.sharingschool.com"+sconOtaManager.getFileKey());
		}else {
			//测试环境
			resultMap.put("fileKey","https://xt.sharingschool.com/upload"+sconOtaManager.getFileKey());
		}
		resultMap.put("content", sconOtaManager.getContent());
		resultMap.put("isTest", sconOtaManager.getIsTest());
		resultMap.put("testImei", sconOtaManager.getTestImei());
		resultMap.put("isUsing", sconOtaManager.getIsUsing());
		resultMap.put("createTime", sconOtaManager.getCreateTime());
		resultMap.put("packName",sconOtaManager.getFileKey().substring(14));
		resultMap.put("md5",sconOtaManager.getMd5());
		String testImei = sconOtaManager.getTestImei();
		int isTest=sconOtaManager.getIsTest();
		if(1==isTest){
			if(!this.isContains(testImei,request.getImei())){
				responseEntity.failure("imei号错误");
				return  responseEntity;
			}
		}
		responseEntity.success(resultMap, "成功");
		return responseEntity;
	}

	@Override
	public Page<SconOtaManager> findByPage(Map<String, Object> params, Pageable sp) {
		Map<String, SearchFilter> filters = SearchFilter.parse(params);
		Specification<SconOtaManager> specification = DynamicSpecifications.bySearchFilter(filters.values(), SconOtaManager.class);
		return sconOtaManagerDao.findAll(specification,sp);
	}

	private boolean isContains(String str,String str1){
		String  sr[] =str.split(",");
		List<String>list=Arrays.asList(sr).stream().filter(string->!string.isEmpty()).collect(Collectors.toList());
		if(list.contains(str1)){
			return true;
		}else {
			return false;
		}
	}
}
