package com.keao.edu.user.service.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.keao.edu.common.dal.BaseDAO;
import com.keao.edu.common.exception.BizException;
import com.keao.edu.common.service.impl.BaseServiceImpl;
import com.keao.edu.user.dao.AppVersionInfoDao;
import com.keao.edu.user.entity.AppVersionInfo;
import com.keao.edu.user.service.AppVersionInfoService;

@Service
public class AppVersionInfoServiceImpl extends BaseServiceImpl<Integer, AppVersionInfo>  implements AppVersionInfoService {
	
	@Autowired
	private AppVersionInfoDao appVersionInfoDao;

	@Override
	public BaseDAO<Integer, AppVersionInfo> getDAO() {
		return appVersionInfoDao;
	}

	@Override
	public List<AppVersionInfo> queryNewestByPlatform(String platform) {
		return appVersionInfoDao.queryNewestByPlatform(platform);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void add(AppVersionInfo appVersionInfo) {
		if(StringUtils.isEmpty(appVersionInfo.getPlatform())){
			throw new BizException("参数校验异常");
		}
		if (StringUtils.equals(appVersionInfo.getStatus(), "newest")) {
			//修改其他的为历史版本
			appVersionInfoDao.batchUpdateStatus(appVersionInfo.getPlatform());
		}
		appVersionInfoDao.insert(appVersionInfo);
	}

	@Override
	public void updateVersion(AppVersionInfo appVersionInfo) {
		if(StringUtils.isEmpty(appVersionInfo.getPlatform())){
			throw new BizException("参数校验异常");
		}
		if (StringUtils.equals(appVersionInfo.getStatus(), "newest")) {
			appVersionInfoDao.batchUpdateStatus(appVersionInfo.getPlatform());
		}
		appVersionInfo.setUpdateTime(new Date());
		appVersionInfoDao.update(appVersionInfo);
	}

}