DataStoreService.java 3.4 KB
Newer Older
苗卫卫 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
package com.starcharge.component.datastore;

import com.ihidea.core.base.CoreService;
import com.ihidea.core.support.cache.CacheSupport;
import com.ihidea.core.support.exception.ServiceException;
import com.ihidea.core.support.orm.mybatis3.util.IbatisServiceUtils;
import com.ihidea.core.util.FileUtilsEx;
import com.starcharge.base.redis.RedisClient;
import com.starcharge.component.datastore.dao.CptDataStoreMapper;
import com.starcharge.component.datastore.dao.model.CptDataStore;
import com.starcharge.component.datastore.dao.model.CptDataStoreCriteria;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("fileStoreService")
public class DataStoreService extends CoreService {

	@Autowired
	private CptDataStoreMapper dao;

	@Autowired
	private JdbcTemplate jdbcTemplate;

	private static Map<String, CptDataStore> dataStoreMap = new HashMap<>();

	/**
	 * 添加
	 * @param record
	 * @return
	 */
	public int insertDataStore(CptDataStore record) {

		if ("2".equals(record.getType())) {
			if (!isExists(record.getPath())) {
				throw new ServiceException("文件路径不存在");
			}

			record.setPath(FileUtilsEx.filterPath(record.getPath()));

			// 备份
			if (!isExists(record.getBakPath())) {
				throw new ServiceException("备份文件路径不存在");
			}

			record.setBakPath(FileUtilsEx.filterPath(record.getBakPath()));
		}

		return dao.insert(record);
	}

	/**
	 * 文件路径是否存在
	 * @param path
	 * @return
	 */
	private boolean isExists(String path) {
		File file = new File(path);
		return file.exists();
	}

	/**
	 * 删除
	 * @param id
	 * @return
	 */
	public int deleteDataStore(String id) {

		// TODO
		// 删除前判断是否还有文件,如果还有,则无法删除

		return dao.deleteByPrimaryKey(id);
	}

	/**
	 * 更新
	 * @param record
	 * @return
	 */
	public int updateDataStore(CptDataStore record) {

		if ("2".equals(record.getType())) {
			record.setPath(FileUtilsEx.filterPath(record.getPath()));
			record.setBakPath(FileUtilsEx.filterPath(record.getBakPath()));
		}

		return dao.updateByPrimaryKey(record);
	}

	/**
	 * 查询
	 * @param record
	 * @return
	 * @throws Exception
	 */
	public List<CptDataStore> selectDataStores(CptDataStore record) throws Exception {
		CptDataStoreCriteria example = new CptDataStoreCriteria();

		CptDataStoreCriteria.Criteria criteria = example.createCriteria();

		IbatisServiceUtils.createCriteriaByEntity(criteria, record);

		example.setOrderByClause("id");

		return dao.selectByExample(example);
	}

	/**
	 * 查询
	 * @param id
	 * @return
	 */
	public CptDataStore selectDataStore(String id) {
		return dao.selectByPrimaryKey(id);
	}

	/**
	 * 通过存储名称得到存储信息
	 * @param name
	 * @return
	 */
	public CptDataStore getInfoByName(String name) {

		CptDataStore result = dataStoreMap.get("DataStoreService.getInfoByName:"+name);

		if (result == null) {
			result = jdbcTemplate.queryForObject("select id, name, type, path, type, bak_path from cpt_datastore where name = ?",
					new Object[] { name }, new BeanPropertyRowMapper<CptDataStore>(CptDataStore.class));
			dataStoreMap.put("DataStoreService.getInfoByName:"+name, result);
		}

		return result;
	}
}