package com.palmnest.service.appVersion.impl;

import cn.jiguang.common.utils.StringUtils;
import com.palmnest.application.core.util.CollectionUtils;
import com.palmnest.application.core.util.DateHelper;
import com.palmnest.application.dto.input.AppUseVersionDto;
import com.palmnest.application.dto.output.AppUseVersionOutPutDto;
import com.palmnest.model.estateserve.AppUseVersion;
import com.palmnest.service.appVersion.AppUseVersionService;
import com.palmnest.service.appVersion.manager.AppUseVersionManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * app版本管理service实现类
 *
 * @author longmeizhen
 */
@Service("appUseVersionService")
public class AppUseVersionServiceImpl implements AppUseVersionService {

	@Autowired
	private AppUseVersionManager appUseVersionManager;


	@Override
	public String saveAppVersion(AppUseVersionDto dto, String userId) throws Exception {
		String res = "FAIL";
		try {
			if (StringUtils.isNotEmpty(userId)) {
				//首先根据传过来的类型获取该类型下所有的app版本列表
				appUseVersionManager.updateRecodeByType(dto.getUsePlatform(), dto.getUseCode());
				//修改历史的记录之后新增最新的记录
				AppUseVersion auv = new AppUseVersion();
				auv.setCreateTime(new Date());
				auv.setCreator(userId);
				auv.setIsLast("1");
				auv.setStatus("1");
				auv.setCustomer(dto.getCustomer());
				auv.setMinimumVersion(dto.getMinimumVersion());
				auv.setRemark(dto.getRemark());
				auv.setStrMessage(dto.getStrMessage());
				if (StringUtils.isNotEmpty(dto.getUpdateFlag())) {
					auv.setUpdateFlag(dto.getUpdateFlag());
				} else {
					auv.setUpdateFlag("2");
				}
				auv.setUpdateURL(dto.getUpdateURL());
				auv.setUseCode(dto.getUseCode());
				auv.setUseName(dto.getUseName());
				auv.setUsePlatform(dto.getUsePlatform());
				auv.setVersionCode(dto.getVersionCode());
				auv.setVersionName(dto.getVersionName());
				if (StringUtils.isNotEmpty(dto.getClientId())) {
					auv.setClientId(Long.parseLong(dto.getClientId()));
				}
				if (StringUtils.isNotEmpty(dto.getUseCodeId())) {
					auv.setUseCodeId(Long.parseLong(dto.getUseCodeId()));
				}
				appUseVersionManager.save(auv);
				res = "SUCCESS";
			}
		} catch (Exception e) {
			res = "操作失败：" + e.getMessage();
			e.printStackTrace();
			throw new Exception(e);
		}
		return res;
	}


	@Override
	public AppUseVersionOutPutDto getLatestRecode(String useCode, String type)
			throws Exception {
		AppUseVersion auv = appUseVersionManager.getLatestRecode(useCode, type);
		if (auv != null) {
			AppUseVersionOutPutDto dto = new AppUseVersionOutPutDto(String.valueOf(auv.getId()), auv.getUseCode(), auv.getUseName()
					, auv.getUsePlatform(), auv.getVersionName(), auv.getCustomer(), auv.getStrMessage(), auv.getStatus()
					, auv.getUpdateURL(), auv.getUpdateFlag(), auv.getRemark(), auv.getMinimumVersion());
			dto.setCreateTime(DateHelper.formatDate(auv.getCreateTime(), DateHelper.DATE_STRING));
			if ("1".equals(auv.getUsePlatform())) {//安卓
				dto.setVersionCode(Integer.parseInt(auv.getVersionCode()));
				dto.setLatestVersion("");
			} else if ("2".equals(auv.getUsePlatform())) {//IOS
				dto.setVersionCode(0);
				dto.setLatestVersion(auv.getVersionCode());
			} else {
				dto.setVersionCode(0);
				dto.setLatestVersion("");
			}
			return dto;
		} else {
			return null;
		}

	}

	@Override
	public List<AppUseVersionOutPutDto>  getByRecode(String useCode, String type) {
		List<AppUseVersionOutPutDto> dtoList = new ArrayList<>();
		List<AppUseVersion> auvs = appUseVersionManager.getByRecode(useCode, type);
		if (null != auvs && auvs.size()>0) {
			for (AppUseVersion auv:
				 auvs ) {
				AppUseVersionOutPutDto dto = new AppUseVersionOutPutDto(String.valueOf(auv.getId()), auv.getUseCode(), auv.getUseName()
						, auv.getUsePlatform(), auv.getVersionName(), auv.getCustomer(), auv.getStrMessage(), auv.getStatus()
						, auv.getUpdateURL(), auv.getUpdateFlag(), auv.getRemark(), auv.getMinimumVersion());
				dto.setCreateTime(DateHelper.formatDate(auv.getCreateTime(), DateHelper.DATE_STRING));
				if ("1".equals(auv.getUsePlatform())) {//安卓
					dto.setVersionCode(StringUtils.isEmpty(auv.getVersionCode()) ? 0 : Integer.parseInt(auv.getVersionCode()));
					dto.setLatestVersion("");
				} else if ("2".equals(auv.getUsePlatform())) {//IOS
					dto.setVersionCode(0);
					dto.setLatestVersion(StringUtils.isEmpty(auv.getVersionCode()) ? "" : auv.getVersionCode());
				} else {
					dto.setVersionCode(0);
					dto.setLatestVersion("");
				}
				dtoList.add(dto);
			}

			return dtoList;
		} else {
			return null;
		}

	}

}
