Browse Source

新增Service实现

zh.sqy 9 years ago
parent
commit
e5c599fcc5

+ 28 - 0
hsweb-web-service-impl-common/pom.xml

@@ -12,5 +12,33 @@
     <artifactId>hsweb-web-service-impl-common</artifactId>
     <version>${parent.version}</version>
 
+    <dependencies>
 
+
+        <dependency>
+            <groupId>org.hsweb</groupId>
+            <artifactId>hsweb-web-core</artifactId>
+            <version>${parent.version}</version>
+        </dependency>
+
+        <dependency>
+            <groupId>org.hsweb</groupId>
+            <artifactId>hsweb-web-dao-interface</artifactId>
+            <version>${parent.version}</version>
+        </dependency>
+
+        <dependency>
+            <groupId>org.hsweb</groupId>
+            <artifactId>hsweb-web-service-interface</artifactId>
+            <version>${parent.version}</version>
+        </dependency>
+
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-jdbc</artifactId>
+            <version>${spring.boot.version}</version>
+        </dependency>
+
+
+    </dependencies>
 </project>

+ 101 - 0
hsweb-web-service-impl-common/src/main/java/org/hsweb/web/service/impl/AbstractServiceImpl.java

@@ -0,0 +1,101 @@
+package org.hsweb.web.service.impl;
+
+import org.hsweb.web.bean.common.PagerResult;
+import org.hsweb.web.bean.common.QueryParam;
+import org.hsweb.web.bean.po.GenericPo;
+import org.hsweb.web.bean.valid.ValidResults;
+import org.hsweb.web.dao.GenericMapper;
+import org.hsweb.web.service.GenericService;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.transaction.annotation.Transactional;
+
+import javax.validation.ValidationException;
+import java.util.List;
+
+/**
+ * Created by 浩 on 2016-01-22 0022.
+ */
+public abstract class AbstractServiceImpl<Po, PK> implements GenericService<Po, PK> {
+    protected Logger logger = LoggerFactory.getLogger(this.getClass());
+
+    protected abstract GenericMapper<Po, PK> getMapper();
+
+    @Override
+    @Transactional(readOnly = true)
+    public PagerResult<Po> selectPager(QueryParam param) throws Exception {
+        PagerResult<Po> pagerResult = new PagerResult<>();
+        int total = getMapper().total(param);
+        pagerResult.setTotal(total);
+        //根据实际记录数量重新指定分页参数
+        param.rePaging(total);
+        pagerResult.setData(getMapper().select(param));
+        return pagerResult;
+    }
+
+    @Override
+    @Transactional(rollbackFor = Throwable.class)
+    public PK insert(Po data) throws Exception {
+        getMapper().insert(data);
+        if (data instanceof GenericPo) {
+            return (PK) ((GenericPo) data).getU_id();
+        }
+        return null;
+    }
+
+    @Override
+    @Transactional(rollbackFor = Throwable.class)
+    public int delete(PK pk) throws Exception {
+        return getMapper().delete(pk);
+    }
+
+    @Override
+    @Transactional(rollbackFor = Throwable.class)
+    public int update(Po data) throws Exception {
+        return getMapper().update(data);
+    }
+
+    @Override
+    @Transactional(rollbackFor = Throwable.class)
+    public int update(List<Po> data) throws Exception {
+        int i = 0;
+        for (Po po : data) {
+            i += getMapper().update(po);
+        }
+        return i;
+    }
+
+    @Override
+    @Transactional(readOnly = true)
+    public List<Po> select(QueryParam param) throws Exception {
+        return this.getMapper().select(param);
+    }
+
+    @Transactional(readOnly = true)
+    public List<Po> select() throws Exception {
+        return this.getMapper().select(new QueryParam());
+    }
+
+    @Override
+    @Transactional(readOnly = true)
+    public int total(QueryParam param) throws Exception {
+        return this.getMapper().total(param);
+    }
+
+    @Override
+    @Transactional(readOnly = true)
+    public Po selectByPk(PK pk) throws Exception {
+        return this.getMapper().selectByPk(pk);
+    }
+
+    protected void tryValidPo(Po data) {
+        ValidResults results;
+        if (data instanceof GenericPo) {
+            results = ((GenericPo) data).valid();
+        } else {
+            results = GenericPo.valid(data);
+        }
+        if (!results.isSuccess())
+            throw new ValidationException(results.toString());
+    }
+}

+ 30 - 0
hsweb-web-service-impl-common/src/main/java/org/hsweb/web/service/impl/basic/SqlExecutorService.java

@@ -0,0 +1,30 @@
+package org.hsweb.web.service.impl.basic;
+
+import org.springframework.jdbc.datasource.DataSourceUtils;
+import org.springframework.stereotype.Service;
+import org.webbuilder.sql.support.executor.AbstractJdbcSqlExecutor;
+
+import javax.annotation.Resource;
+import javax.sql.DataSource;
+import java.sql.Connection;
+
+/**
+ * SQL执行服务类,用于执行原生sql
+ * Created by 浩 on 2015-10-09 0009.
+ */
+@Service
+public class SqlExecutorService extends AbstractJdbcSqlExecutor {
+
+    @Resource
+    private DataSource dataSource;
+
+    @Override
+    public Connection getConnection() {
+        return DataSourceUtils.getConnection(dataSource);
+    }
+
+    @Override
+    public void releaseConnection(Connection connection) {
+        DataSourceUtils.releaseConnection(connection, dataSource);
+    }
+}

+ 165 - 0
hsweb-web-service-impl-common/src/main/java/org/hsweb/web/service/impl/config/ConfigServiceImpl.java

@@ -0,0 +1,165 @@
+package org.hsweb.web.service.impl.config;
+
+import org.hsweb.web.bean.po.config.Config;
+import org.hsweb.web.dao.config.ConfigMapper;
+import org.hsweb.web.service.config.ConfigService;
+import org.hsweb.web.service.impl.AbstractServiceImpl;
+import org.springframework.cache.annotation.CacheEvict;
+import org.springframework.cache.annotation.Cacheable;
+import org.springframework.stereotype.Service;
+import org.webbuilder.utils.common.StringUtils;
+
+import javax.annotation.Resource;
+import java.util.Properties;
+
+/**
+ * 系统配置服务类
+ * Created by generator
+ */
+@Service("configService")
+public class ConfigServiceImpl extends AbstractServiceImpl<Config, String> implements ConfigService {
+
+    public static final String CACHE_KEY = "config";
+
+    //默认数据映射接口
+    @Resource
+    protected ConfigMapper configMapper;
+
+    @Override
+    protected ConfigMapper getMapper() {
+        return this.configMapper;
+    }
+
+    @Override
+    @CacheEvict(value = CACHE_KEY, allEntries = true)
+    public int update(Config data) throws Exception {
+        return super.update(data);
+    }
+
+    /**
+     * 根据配置名称,获取配置内容
+     *
+     * @param name 配置名称
+     * @return 配置内容
+     * @throws Exception 异常信息
+     */
+    @Override
+    @Cacheable(value = CACHE_KEY, key = "'info.content_'+#name")
+    public String getContent(String name) throws Exception {
+        Config config = getMapper().selectByPk(name);
+        if (config == null) return null;
+        return config.getContent();
+    }
+
+    /**
+     * 根据配置名称,获取配置内容,并解析为Properties格式
+     *
+     * @param name 配置名称
+     * @return 配置内容
+     * @throws Exception 异常信息
+     */
+    @Override
+    @Cacheable(value = CACHE_KEY, key = "'info.'+#name")
+    public Properties get(String name) throws Exception {
+        Config config = getMapper().selectByPk(name);
+        if (config == null) return new Properties();
+        return config.toMap();
+    }
+
+    /**
+     * 获取配置中指定key的值
+     *
+     * @param name 配置名称
+     * @param key  key 异常信息
+     * @return 指定的key对应的value
+     * @throws Exception
+     */
+    @Override
+    @Cacheable(value = CACHE_KEY, key = "'info.'+#name+'.key.'+#key")
+    public String get(String name, String key) throws Exception {
+        return get(name).getProperty(key);
+    }
+
+    /**
+     * 获取配置中指定key的值,并指定一个默认值,如果对应的key未获取到,则返回默认值
+     *
+     * @param name         配置名称
+     * @param key          key 异常信息
+     * @param defaultValue 默认值
+     * @return 对应key的值,若为null,则返回默认值
+     */
+    @Override
+    @Cacheable(value = CACHE_KEY, key = "'info.'+#name+'.key.'+#key")
+    public String get(String name, String key, String defaultValue) {
+        String val;
+        try {
+            val = this.get(name).getProperty(key);
+            if (val == null) {
+                logger.error("获取配置:{}.{}失败,defaultValue:{}", name, key, defaultValue);
+                return defaultValue;
+            }
+        } catch (Exception e) {
+            logger.error("获取配置:{}.{}失败,defaultValue:{}", name, key, defaultValue, e);
+            return defaultValue;
+        }
+        return val;
+    }
+
+
+    /**
+     * 参照 {@link ConfigService#get(String, String)},将值转为int类型
+     */
+    @Override
+    @Cacheable(value = CACHE_KEY, key = "'info.'+#name+'.key.'+#key+'.int'")
+    public int getInt(String name, String key) throws Exception {
+        return StringUtils.toInt(get(name, key));
+    }
+
+    /**
+     * 参照 {@link ConfigService#get(String, String)},将值转为double类型
+     */
+    @Override
+    @Cacheable(value = CACHE_KEY, key = "'info.'+#name+'.key.'+#key+'.double'")
+    public double getDouble(String name, String key) throws Exception {
+        return StringUtils.toDouble(get(name, key));
+    }
+
+    /**
+     * 参照 {@link ConfigService#get(String, String)},将值转为long类型
+     */
+    @Override
+    @Cacheable(value = CACHE_KEY, key = "'info.'+#name+'.key.'+#key+'.long'")
+    public long getLong(String name, String key) throws Exception {
+        return StringUtils.toLong(get(name, key));
+    }
+
+    /**
+     * 参照 {@link ConfigService#get(String, String, String)},将值转为int类型
+     */
+    @Override
+    @Cacheable(value = CACHE_KEY, key = "'info.'+#name+'.key.'+#key+'.int'")
+    public int getInt(String name, String key, int defaultValue) {
+        return StringUtils.toInt(get(name, key, String.valueOf(defaultValue)));
+    }
+
+    /**
+     * 参照 {@link ConfigService#get(String, String, String)},将值转为double类型
+     */
+    @Override
+    @Cacheable(value = CACHE_KEY, key = "'info.'+#name+'.key.'+#key+'.double'")
+    public double getDouble(String name, String key, double defaultValue) {
+
+        return StringUtils.toDouble(get(name, key, String.valueOf(defaultValue)));
+    }
+
+    /**
+     * 参照 {@link ConfigService#get(String, String, String)},将值转为long类型
+     */
+    @Override
+    @Cacheable(value = CACHE_KEY, key = "'info.'+#name+'.key.'+#key+'.long'")
+    public long getLong(String name, String key, long defaultValue) {
+        return StringUtils.toLong(get(name, key, String.valueOf(defaultValue)));
+    }
+
+
+}

+ 65 - 0
hsweb-web-service-impl-common/src/main/java/org/hsweb/web/service/impl/form/FormServiceImpl.java

@@ -0,0 +1,65 @@
+package org.hsweb.web.service.impl.form;
+
+import org.hsweb.web.bean.po.form.Form;
+import org.hsweb.web.dao.GenericMapper;
+import org.hsweb.web.dao.form.FormMapper;
+import org.hsweb.web.exception.BusinessException;
+import org.hsweb.web.service.form.FormService;
+import org.hsweb.web.service.impl.AbstractServiceImpl;
+import org.springframework.cache.annotation.CacheEvict;
+import org.springframework.cache.annotation.Cacheable;
+import org.springframework.stereotype.Service;
+
+import javax.annotation.Resource;
+import java.util.Date;
+
+/**
+ * 自定义表单服务类
+ * Created by generator
+ */
+@Service("formService")
+public class FormServiceImpl extends AbstractServiceImpl<Form, String> implements FormService {
+
+    private static final String CACHE_KEY = "form";
+
+    @Resource
+    private FormMapper formMapper;
+
+    @Override
+    protected GenericMapper<Form, String> getMapper() {
+        return formMapper;
+    }
+
+    @Override
+    @Cacheable(value = CACHE_KEY, key = "#id")
+    public Form selectByPk(String id) throws Exception {
+        return super.selectByPk(id);
+    }
+
+    @Override
+    public String insert(Form data) throws Exception {
+        Form old = this.selectByPk(data.getU_id());
+        if (old != null)
+            throw new BusinessException("该表单已存在!");
+        data.setCreate_date(new Date());
+        super.insert(data);
+        return data.getU_id();
+    }
+
+    @Override
+    @CacheEvict(value = CACHE_KEY, key = "#data.u_id")
+    public int update(Form data) throws Exception {
+        Form old = this.selectByPk(data.getU_id());
+        if (old == null)
+            throw new BusinessException("该表单不存在!");
+        data.setUpdate_date(new Date());
+        return super.update(data);
+    }
+
+    @Override
+    public int delete(String s) throws Exception {
+        throw new BusinessException("此服务已关闭!");
+    }
+
+
+}

+ 55 - 0
hsweb-web-service-impl-common/src/main/java/org/hsweb/web/service/impl/module/ModuleServiceImpl.java

@@ -0,0 +1,55 @@
+package org.hsweb.web.service.impl.module;
+
+import org.hsweb.web.bean.common.QueryParam;
+import org.hsweb.web.bean.po.module.Module;
+import org.hsweb.web.dao.module.ModuleMapper;
+import org.hsweb.web.exception.BusinessException;
+import org.hsweb.web.service.impl.AbstractServiceImpl;
+import org.hsweb.web.service.module.ModuleService;
+import org.springframework.stereotype.Service;
+
+import javax.annotation.Resource;
+import java.util.List;
+
+/**
+ * 系统模块服务类
+ * Created by generator
+ */
+@Service("moduleService")
+public class ModuleServiceImpl extends AbstractServiceImpl<Module, String> implements ModuleService {
+
+    //默认数据映射接口
+    @Resource
+    protected ModuleMapper moduleMapper;
+
+    @Override
+    protected ModuleMapper getMapper() {
+        return this.moduleMapper;
+    }
+
+    @Override
+    public int update(List<Module> datas) throws Exception {
+        int size = 0;
+        for (Module module : datas) {
+            tryValidPo(module);
+            boolean doUpdate = (this.selectByPk(module.getOld_id()) != null);
+            if (!module.getU_id().equals(module.getOld_id())) {
+                if (doUpdate && this.selectByPk(module.getU_id()) != null) {
+                    throw new BusinessException(String.format("标识:%s已存在", module.getU_id()));
+                }
+            }
+            if (doUpdate) {
+                size += this.update(module);
+            } else {
+                this.insert(module);
+                size++;
+            }
+        }
+        return size;
+    }
+
+    @Override
+    public List<Module> selectByPid(String pid) throws Exception {
+        return this.select(new QueryParam().where("p_id", pid));
+    }
+}

+ 90 - 0
hsweb-web-service-impl-common/src/main/java/org/hsweb/web/service/impl/resource/FileServiceImpl.java

@@ -0,0 +1,90 @@
+package org.hsweb.web.service.impl.resource;
+
+/**
+ * Created by 浩 on 2015-11-26 0026.
+ */
+
+import org.apache.commons.codec.digest.DigestUtils;
+import org.hsweb.web.bean.po.resource.Resources;
+import org.hsweb.web.bean.po.user.User;
+import org.hsweb.web.service.config.ConfigService;
+import org.hsweb.web.service.resource.FileService;
+import org.hsweb.web.service.resource.ResourcesService;
+import org.hsweb.web.utils.WebUtil;
+import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
+import org.webbuilder.utils.common.DateTimeUtils;
+import org.webbuilder.utils.common.MD5;
+
+import javax.annotation.Resource;
+import java.io.*;
+import java.util.Date;
+
+
+@Service("fileService")
+public class FileServiceImpl implements FileService {
+    @Resource
+    protected ConfigService configService;
+    @Resource
+    protected ResourcesService resourcesService;
+
+    @Transactional(rollbackFor = Throwable.class)
+    public Resources saveFile(InputStream is, String fileName) throws Exception {
+        //配置中的文件上传根路径
+        String fileBasePath = configService.get("upload", "basePath", "/upload").trim();
+        //文件存储的相对路径,以日期分隔,每天创建一个新的目录
+        String filePath = "/file/".concat(DateTimeUtils.format(new Date(), DateTimeUtils.YEAR_MONTH_DAY));
+        //文件存储绝对路径
+        String absPath = fileBasePath.concat(filePath);
+        File path = new File(absPath);
+        if (!path.exists()) path.mkdirs(); //创建目录
+        String newName = MD5.encode(String.valueOf(System.nanoTime())); //临时文件名 ,纳秒的md5值
+        String fileAbsName = absPath.concat("/").concat(newName);
+        //try with resource
+        try (BufferedInputStream in = new BufferedInputStream(is);
+             //写出文件
+             BufferedOutputStream os = new BufferedOutputStream(new FileOutputStream(fileAbsName))) {
+            byte[] buffer = new byte[2048 * 10];
+            int len;
+            while ((len = in.read(buffer)) != -1) {
+                os.write(buffer, 0, len);
+            }
+            os.flush();
+        }
+        File newFile = new File(fileAbsName);
+        //获取文件的md5值
+        String md5;
+        try (FileInputStream inputStream = new FileInputStream(newFile)) {
+            md5 = DigestUtils.md5Hex(inputStream);
+        }
+        //判断文件是否已经存在
+        Resources resources = resourcesService.selectByMd5(md5);
+        if (resources != null) {
+            newFile.delete();//文件已存在则删除临时文件不做处理
+            return resources;
+        } else {
+            newName = md5;
+            newFile.renameTo(new File(absPath.concat("/").concat(newName)));
+        }
+        resources = new Resources();
+        resources.setStatus(1);
+        resources.setPath(filePath);
+        resources.setMd5(md5);
+        resources.setCreate_date(new Date());
+        resources.setType("file");
+        resources.setName(fileName);
+        try {
+            User user = WebUtil.getLoginUser();
+            if (user != null) {
+                resources.setCreator_id(user.getU_id());
+            } else {
+                resources.setCreator_id("-1");
+            }
+        } catch (Exception e) {
+            resources.setCreator_id("-1");
+        }
+
+        resourcesService.insert(resources);
+        return resources;
+    }
+}

+ 76 - 0
hsweb-web-service-impl-common/src/main/java/org/hsweb/web/service/impl/resource/ResourcesServiceImpl.java

@@ -0,0 +1,76 @@
+package org.hsweb.web.service.impl.resource;
+
+import org.hsweb.web.bean.common.QueryParam;
+import org.hsweb.web.bean.po.resource.Resources;
+import org.hsweb.web.dao.resource.ResourcesMapper;
+import org.hsweb.web.service.config.ConfigService;
+import org.hsweb.web.service.impl.AbstractServiceImpl;
+import org.hsweb.web.service.resource.ResourcesService;
+import org.hsweb.web.utils.RandomUtil;
+import org.springframework.cache.annotation.Cacheable;
+import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
+
+import javax.annotation.Resource;
+import java.util.List;
+
+/**
+ * 资源服务类
+ * Created by generator
+ */
+@Service("resourcesService")
+public class ResourcesServiceImpl extends AbstractServiceImpl<Resources, String> implements ResourcesService {
+    public static final String CACHE_KEY = "resources";
+    @Resource
+    protected ConfigService configService;
+
+    //默认数据映射接口
+    @Resource
+    protected ResourcesMapper resourcesMapper;
+
+    @Override
+    protected ResourcesMapper getMapper() {
+        return this.resourcesMapper;
+    }
+
+    @Override
+    @Cacheable(value = CACHE_KEY, key = "'id.'+#id")
+    @Transactional(readOnly = true)
+    public Resources selectByPk(String id) throws Exception {
+        return super.selectByPk(id);
+    }
+
+    /**
+     * 根据资源md5 查询资源信息
+     *
+     * @param md5 md5值
+     * @return 资源对象
+     * @throws Exception
+     */
+    @Cacheable(value = CACHE_KEY, key = "'md5.'+#md5")
+    @Transactional(readOnly = true)
+    public Resources selectByMd5(String md5) throws Exception {
+        List<Resources> resources = this.select(new QueryParam().where("md5", md5));
+        if (resources != null && resources.size() > 0)
+            return resources.get(0);
+        return null;
+    }
+
+
+    @Override
+    @Transactional(rollbackFor = Throwable.class)
+    public String insert(Resources data) throws Exception {
+        data.setU_id(this.newUid(6));//6位随机id
+        return super.insert(data);
+    }
+
+    public String newUid(int len) throws Exception {
+        String uid = RandomUtil.randomChar(len);
+        for (int i = 0; i < 10; i++) {
+            if (this.selectByPk(uid) == null) {
+                return uid;
+            }
+        }  //如果10次存在重复则位数+1
+        return newUid(len + 1);
+    }
+}

+ 69 - 0
hsweb-web-service-impl-common/src/main/java/org/hsweb/web/service/impl/role/RoleServiceImpl.java

@@ -0,0 +1,69 @@
+package org.hsweb.web.service.impl.role;
+
+import org.hsweb.web.bean.po.role.Role;
+import org.hsweb.web.bean.po.role.RoleModule;
+import org.hsweb.web.dao.role.RoleMapper;
+import org.hsweb.web.dao.role.RoleModuleMapper;
+import org.hsweb.web.service.impl.AbstractServiceImpl;
+import org.hsweb.web.service.module.ModuleService;
+import org.hsweb.web.service.role.RoleService;
+import org.hsweb.web.utils.RandomUtil;
+import org.springframework.stereotype.Service;
+
+import javax.annotation.Resource;
+import java.util.List;
+
+/**
+ * 后台管理角色服务类
+ * Created by zh.sqy@qq.com
+ */
+@Service("roleService")
+public class RoleServiceImpl extends AbstractServiceImpl<Role, String> implements RoleService {
+
+    //默认数据映射接口
+    @Resource
+    protected RoleMapper roleMapper;
+
+    @Resource
+    protected RoleModuleMapper roleModuleMapper;
+
+    @Resource
+    protected ModuleService moduleService;
+
+    @Override
+    protected RoleMapper getMapper() {
+        return this.roleMapper;
+    }
+
+    @Override
+    public String insert(Role data) throws Exception {
+        String id = super.insert(data);
+        List<RoleModule> roleModule = data.getModules();
+        if (roleModule != null && roleModule.size() > 0) {
+            //保存角色模块关联
+            for (RoleModule module : roleModule) {
+                module.setU_id(RandomUtil.randomChar(6));
+                module.setRole_id(data.getU_id());
+                roleModuleMapper.insert(module);
+            }
+        }
+        return id;
+    }
+
+    @Override
+    public int update(Role data) throws Exception {
+        int l = super.update(data);
+        List<RoleModule> roleModule = data.getModules();
+        if (roleModule != null && roleModule.size() > 0) {
+            //先删除所有roleModule
+            roleModuleMapper.deleteByRoleId(data.getU_id());
+            //保存角色模块关联
+            for (RoleModule module : roleModule) {
+                module.setU_id(RandomUtil.randomChar(6));
+                module.setRole_id(data.getU_id());
+                roleModuleMapper.insert(module);
+            }
+        }
+        return l;
+    }
+}

+ 90 - 0
hsweb-web-service-impl-common/src/main/java/org/hsweb/web/service/impl/script/DynamicScriptServiceImpl.java

@@ -0,0 +1,90 @@
+package org.hsweb.web.service.impl.script;
+
+import org.hsweb.web.bean.po.script.DynamicScript;
+import org.hsweb.web.dao.script.DynamicScriptMapper;
+import org.hsweb.web.exception.BusinessException;
+import org.hsweb.web.service.impl.AbstractServiceImpl;
+import org.hsweb.web.service.script.DynamicScriptService;
+import org.springframework.cache.annotation.CacheEvict;
+import org.springframework.cache.annotation.Cacheable;
+import org.springframework.stereotype.Service;
+import org.webbuilder.utils.script.engine.DynamicScriptEngine;
+import org.webbuilder.utils.script.engine.DynamicScriptEngineFactory;
+
+import javax.annotation.Resource;
+import java.util.List;
+
+/**
+ * 动态脚本服务类
+ * Created by generator
+ */
+@Service("dynamicScriptService")
+public class DynamicScriptServiceImpl extends AbstractServiceImpl<DynamicScript, String> implements DynamicScriptService {
+
+    private static final String CACHE_KEY = "dynamic_script";
+
+    //默认数据映射接口
+    @Resource
+    protected DynamicScriptMapper dynamicScriptMapper;
+
+    @Override
+    protected DynamicScriptMapper getMapper() {
+        return this.dynamicScriptMapper;
+    }
+
+    @Override
+    @Cacheable(value = CACHE_KEY, key = "#pk")
+    public DynamicScript selectByPk(String pk) throws Exception {
+        return super.selectByPk(pk);
+    }
+
+    @Override
+    @CacheEvict(value = CACHE_KEY, key = "#data.u_id")
+    public int update(DynamicScript data) throws Exception {
+        int i = super.update(data);
+        DynamicScriptEngine engine = DynamicScriptEngineFactory.getEngine(data.getType());
+        engine.compile(data.getU_id(), data.getContent());
+        return i;
+    }
+
+    @Override
+    @CacheEvict(value = CACHE_KEY, allEntries = true)
+    public int update(List<DynamicScript> datas) throws Exception {
+        int i = super.update(datas);
+        for (DynamicScript data : datas) {
+            DynamicScriptEngine engine = DynamicScriptEngineFactory.getEngine(data.getType());
+            engine.compile(data.getU_id(), data.getContent());
+        }
+        return i;
+    }
+
+    @Override
+    @CacheEvict(value = CACHE_KEY, key = "#pk")
+    public int delete(String pk) throws Exception {
+        return super.delete(pk);
+    }
+
+    public void compile(String id) throws Exception {
+        DynamicScript script = this.selectByPk(id);
+        if (script == null) throw new BusinessException(String.format("脚本[%s]不存在", id));
+        DynamicScriptEngine engine = DynamicScriptEngineFactory.getEngine(script.getType());
+        try {
+            engine.compile(script.getU_id(), script.getContent());
+        } catch (Exception e) {
+            logger.error("compile error!", e);
+        }
+    }
+
+    public void compileAll() throws Exception {
+        List<DynamicScript> list = this.select();
+        for (DynamicScript script : list) {
+            DynamicScriptEngine engine = DynamicScriptEngineFactory.getEngine(script.getType());
+            try {
+                engine.compile(script.getU_id(), script.getContent());
+            } catch (Exception e) {
+                logger.error("compile error!", e);
+            }
+        }
+    }
+
+}

+ 111 - 0
hsweb-web-service-impl-common/src/main/java/org/hsweb/web/service/impl/user/UserServiceImpl.java

@@ -0,0 +1,111 @@
+package org.hsweb.web.service.impl.user;
+
+import org.hsweb.web.bean.common.QueryParam;
+import org.hsweb.web.bean.po.module.Module;
+import org.hsweb.web.bean.po.role.UserRole;
+import org.hsweb.web.bean.po.user.User;
+import org.hsweb.web.dao.role.UserRoleMapper;
+import org.hsweb.web.dao.user.UserMapper;
+import org.hsweb.web.exception.BusinessException;
+import org.hsweb.web.service.impl.AbstractServiceImpl;
+import org.hsweb.web.service.module.ModuleService;
+import org.hsweb.web.service.user.UserService;
+import org.hsweb.web.utils.RandomUtil;
+import org.springframework.stereotype.Service;
+import org.webbuilder.utils.common.MD5;
+
+import javax.annotation.Resource;
+import java.util.*;
+
+/**
+ * 后台管理用户服务类
+ * Created by generator
+ */
+@Service("userService")
+public class UserServiceImpl extends AbstractServiceImpl<User, String> implements UserService {
+
+    //默认数据映射接口
+    @Resource
+    protected UserMapper userMapper;
+
+    @Resource
+    protected UserRoleMapper userRoleMapper;
+
+    @Resource
+    protected ModuleService moduleService;
+
+    @Override
+    protected UserMapper getMapper() {
+        return this.userMapper;
+    }
+
+    public User selectByUserName(String username) throws Exception {
+        return this.getMapper().selectByUserName(username);
+    }
+
+    @Override
+    public String insert(User data) throws Exception {
+        tryValidPo(data);
+        if (selectByUserName(data.getUsername()) != null) {
+            throw new BusinessException("用户名已存在!");
+        }
+        data.setU_id(RandomUtil.randomChar(6));
+        data.setCreate_date(new Date());
+        data.setUpdate_date(new Date());
+        data.setPassword(MD5.encode(data.getPassword()));
+        String id = userMapper.insert(data);
+        //添加角色关联
+        if (data.getUserRoles().size() != 0) {
+            for (UserRole userRole : data.getUserRoles()) {
+                userRole.setU_id(RandomUtil.randomChar());
+                userRole.setUser_id(data.getU_id());
+                userRoleMapper.insert(userRole);
+            }
+        }
+        return id;
+    }
+
+    @Override
+    public int update(User data) throws Exception {
+        tryValidPo(data);
+        User old = this.selectByUserName(data.getUsername());
+        if (old != null && !old.getU_id().equals(data.getU_id())) {
+            throw new BusinessException("用户名已存在!");
+        }
+        data.setUpdate_date(new Date());
+        if (!"$default".equals(data.getPassword())) {
+            data.setPassword(MD5.encode(data.getPassword()));
+            userMapper.updatePassword(data);
+        }
+        int i = userMapper.update(data);
+        if (data.getUserRoles().size() != 0) {
+            //删除所有
+            userRoleMapper.deleteByUserId(data.getU_id());
+            for (UserRole userRole : data.getUserRoles()) {
+                userRole.setU_id(RandomUtil.randomChar());
+                userRole.setUser_id(data.getU_id());
+                userRoleMapper.insert(userRole);
+            }
+        }
+        return i;
+    }
+
+    @Override
+    public void initAdminUser(User user) throws Exception {
+        QueryParam queryParam = new QueryParam();
+        queryParam.orderBy("sort_index");
+        List<Module> modules = moduleService.select(queryParam);
+        Map<Module, Set<String>> roleInfo = new LinkedHashMap<>();
+        for (Module module : modules) {
+            roleInfo.put(module, new LinkedHashSet<>(module.getM_optionMap().keySet()));
+        }
+        user.setRoleInfo(roleInfo);
+    }
+
+    @Override
+    public void initGuestUser(User user) throws Exception {
+        List<UserRole> userRoles = userRoleMapper.select(new QueryParam().where("role_id", "guest"));
+        user.setUserRoles(userRoles);
+        user.initRoleInfo();
+    }
+}