package com.yihu.hos.tenant.service; import com.yihu.hos.config.MongoConfig; import com.yihu.hos.core.encrypt.DES; import com.yihu.hos.tenant.dao.TenantDao; import com.yihu.hos.tenant.model.TenantModel; import com.yihu.hos.web.framework.model.Result; import com.yihu.hos.web.framework.util.GridFSUtil; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.io.OutputStream; import java.util.Date; import java.util.List; import java.util.Map; import java.util.UUID; /** * @author HZY * @vsrsion 1.0 * Created at 2016/12/2. */ @Service("TenantService") public class TenantService { public static final String BEAN_ID = "TenantService"; @Autowired private TenantDao tenantDao; @Autowired private MongoConfig mongoConfig; @Value("${spring.data.mongodb.gridFsDatabase}") private String dbName; public TenantModel findTenantByName(String name) throws Exception { List list = tenantDao.getTenantList(name); if (list!=null && !list.isEmpty()){ return list.get(0); } return null; } public Result getTenantList(Map params) throws Exception { return tenantDao.getTenantList(params); } public TenantModel getTenantById(Long id) throws Exception { return tenantDao.getEntity(TenantModel.class,id); } public Result addTenant(TenantModel obj) throws Exception { String code = UUID.randomUUID().toString(); obj.setCode(code); obj.setCreated(new Date()); obj.setCreatedUnix(0); obj.setUpdated(new Date()); obj.setUpdatedUnix(0); tenantDao.saveEntity(obj); return Result.success("保存成功"); } @Transactional public Result updateTenant(TenantModel obj) throws Exception { TenantModel tenant = tenantDao.getEntity(TenantModel.class, obj.getId()); tenant.setName(obj.getName()); tenant.setLoginName(obj.getLoginName()); tenant.setSchema(obj.getSchema()); tenant.setPassword(obj.getPassword()); tenant.setValid(obj.getValid()); tenant.setUpdated(new Date()); tenant.setUpdatedUnix(1); tenantDao.updateEntity(tenant); return Result.success("更新成功"); } @Transactional public Result deleteTenant(Long id) throws Exception { TenantModel systemApp = tenantDao.getEntity(TenantModel.class, id); tenantDao.deleteEntity(systemApp); return Result.success("删除成功"); } public Result readFile(OutputStream os, String fileName) { try { fileName = DES.decrypt(fileName, DES.COMMON_PASSWORD); GridFSUtil.readFile(mongoConfig.mongoClient().getDatabase(dbName), os, fileName); return Result.success("读取成功"); } catch (Exception e) { e.printStackTrace(); } return Result.error("读取失败"); } }