package org.cs.mgr.setting.service.impl;

import java.util.Map;

import org.cs.core.service.impl.BaseService;
import org.cs.mgr.setting.dao.IAppVersionDao;
import org.cs.mgr.setting.model.AppVersion;
import org.cs.mgr.setting.service.IAppVersionService;
import org.cs.util.Pager;
import org.cs.util.VersionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;

@Service("appVersionService")
public class AppVersionService extends BaseService<AppVersion> implements  IAppVersionService {

	@Autowired
	private IAppVersionDao appVersionDao;
	
	@Override
	public JSONObject check(String plat, String version) {
		AppVersion check = appVersionDao.check(plat, version);
		if(check==null||version.equals(check.getVersion())){
			return getJson(0, null, "当前版本已为最新版本");
		}else{
			
			boolean compareV = VersionUtil.compareV(version, check.getVersion());
			if(compareV){
				/*if("android".equals(plat)){
					//获取对象存储apk地址
					//String url = CommonsUtil.getWSUrl("download","xiyuan_"+check.getVersion()+".apk",false);
					String url = Constants.APK_PATH+Constants.APP_NAME+"_v_"+check.getVersion()+".apk";
					check.setUrl(url);
				}*/
				
				return getJson(1, check, "版本信息获取成功");
			}else{
				return getJson(0, null, "当前版本已为最新版本");
			}
		}	
	}

	@Cacheable(value="hzycache" ,key="'appVersion'")
	@Transactional(readOnly=false)
	@Override
	public void edit(AppVersion version) {
		appVersionDao.update(version);
	}

//	@Cacheable(value="hzycache" ,key="'appVersion'")
	@Override
	public Pager getVersionList(Pager page) {
		return this.findPageByHql("from AppVersion", page, null);
	}


	@Override
	public Map<String, Object> getVersionBy(String plat) {
		return this.findOne("select * from app_version where plat = '"+ plat +"'");
	}
}
