/**
 * Copyright 2008-2009. Chongqing Communications Industry Services Co.,Ltd Information Technology Branch. All rights reserved. <a>http://www.crunii.com</a>
 */
package com.crunii.micro.core.service.impl;

import com.crunii.micro.common.dto.PageReq;
import com.crunii.micro.common.dto.PageRsp;
import com.crunii.micro.core.dto.CacheRefresh;
import com.crunii.micro.core.dto.CacheRefreshC;
import com.crunii.micro.core.dto.CacheRefreshQ;
import com.crunii.micro.core.dto.CacheRefreshU;
import com.crunii.micro.core.jdbc.CacheRefreshJdbcDao;
import com.crunii.micro.core.service.CacheRefreshService;
import com.crunii.micro.common.dto.OrderProp;
import com.crunii.micro.domain.entity.main.TCacheRefresh;
import com.crunii.micro.domain.entity.main.TCacheRefreshParam;
import com.crunii.micro.domain.repo.main.TCacheRefreshParamRepo;
import com.crunii.micro.domain.repo.main.TCacheRefreshRepo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * @author 田平 create 2018年10月20日上午11:36:03
 */
@Service
public class CacheRefreshServiceImpl implements CacheRefreshService {

	@Autowired
	private TCacheRefreshRepo tCacheRefreshRepo;

	@Autowired
	private TCacheRefreshParamRepo tCacheRefreshParamRepo;

	@Autowired
	private CacheRefreshJdbcDao cacheRefreshJdbcDao;

	public PageRsp<CacheRefresh> pageCacheRefresh(PageReq<CacheRefreshQ> req) {
		return cacheRefreshJdbcDao.pageCacheRefresh(req);
	}

	public int createCacheRefresh(CacheRefreshC refreshC) {
		TCacheRefresh cacheRefresh = new TCacheRefresh();
		BeanUtils.copyProperties(refreshC, cacheRefresh);
		cacheRefresh.setCreateTime(new Date());
		tCacheRefreshRepo.save(cacheRefresh);
		tCacheRefreshRepo.flush();
		int refreshId = cacheRefresh.getRefreshId();
		refreshC.getParams().forEach(p -> {
			TCacheRefreshParam refreshParam = new TCacheRefreshParam();
			BeanUtils.copyProperties(p, refreshParam);
			refreshParam.setRefreshId(refreshId);
			tCacheRefreshParamRepo.save(refreshParam);
		});
		return refreshId;
	}

	public void updateCacheRefresh(CacheRefreshU refreshU) {
		TCacheRefresh cacheRefresh = tCacheRefreshRepo.getOne(refreshU.getRefreshId());
		BeanUtils.copyProperties(refreshU, cacheRefresh);
		tCacheRefreshRepo.save(cacheRefresh);
		int refreshId = refreshU.getRefreshId();
		tCacheRefreshParamRepo.deleteAll(tCacheRefreshParamRepo.findByProp("refreshId", refreshId));
		refreshU.getParams().forEach(p -> {
			TCacheRefreshParam refreshParam = new TCacheRefreshParam();
			BeanUtils.copyProperties(p, refreshParam);
			refreshParam.setRefreshId(refreshId);
			tCacheRefreshParamRepo.save(refreshParam);
		});
	}

	public void deleteCacheRefresh(int refreshId) {
		TCacheRefresh cacheRefresh = tCacheRefreshRepo.getOne(refreshId);
		if (cacheRefresh != null) {
			tCacheRefreshRepo.delete(cacheRefresh);
		}
		List<TCacheRefreshParam> params = tCacheRefreshParamRepo.findByProp("refreshId", refreshId);
		tCacheRefreshParamRepo.deleteAll(params);
	}

	public TCacheRefresh getTCacheRefresh(int refreshId) {
		return tCacheRefreshRepo.getOne(refreshId);
	}

	public List<TCacheRefreshParam> getTCacheRefreshParams(int refreshId) {
		return tCacheRefreshParamRepo.findByProp("refreshId", refreshId, new OrderProp("paramId"));
	}

}
