package com.palmnest.service.estateserve.impl;

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

import com.palmnest.application.bean.AppResultDto;
import com.palmnest.application.core.util.AppResultUtil;
import com.palmnest.application.core.util.PropertiesKit;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.palmnest.dao.estateserve.VirArchModelDao;
import com.palmnest.dao.estateserve.impl.VirArchModelDaoImpl;
import com.palmnest.model.VirArchModel;
import com.palmnest.model.property.VirtualArchitecture;
import com.palmnest.service.estateserve.VirArchModelManager;
import com.palmnest.service.estateserve.VirtualArchManager;
import com.palmnest.service.impl.GenericManagerImpl;

/**
 * 企业模板配置(入驻、迁出)
 *
 * @author longmeizhen
 */
@Slf4j
@Service("virArchModelManager")
public class VirArchModelManagerImpl extends GenericManagerImpl<VirArchModel, Integer> implements VirArchModelManager {

	private VirArchModelDao VirArchModelDao;

	@Autowired
	public void setVirArchModelDao(VirArchModelDao virArchModelDao) {
		VirArchModelDao = virArchModelDao;
		this.dao = virArchModelDao;
	}

	@Override
	public List<VirArchModel> getVirArchModelByType(int type, String orgId) throws Exception {
		return VirArchModelDao.getVirArchModelByType(type, orgId);
	}

	@Override
	public AppResultDto delFileById(int id) {
		try {
			String prePath = PropertiesKit.readPropertiesValue("attachment.root", "wechat.properties");
			VirArchModel virArchModel = VirArchModelDao.get(id);
			//删除数据库记录
			VirArchModelDao.remove(id);
			//删除服务器或本地文件
			String path = prePath + virArchModel.getFileUrl();
			File file = new File(path);
			if (file.exists()) {
				file.delete();
			}
			return AppResultUtil.createSuccesDto();
		} catch (Exception e) {
			log.error(e.getMessage());
			return AppResultUtil.createErrorDto("100", "删除文件失败");
		}
	}

}
