All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cn.basics.service.impl.MyBaseServiceImpl Maven / Gradle / Ivy

package cn.basics.service.impl;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import cn.basics.dao.BaseDao;
import cn.basics.model.PageEntity;
import cn.basics.service.MyBaseService;
import cn.basics.statics.Statics;
import cn.basics.util.IdGZQ;
import cn.basics.util.MyBatisUtil;
import cn.basics.util.PageUtil;
import cn.basics.util.ReturnUtil;
import cn.basics.util.extend.ReflectHelperUtil;

import com.alibaba.fastjson.JSONObject;

/**
 * @ClassName: MyBaseServiceImpl 
 * @Description: TODOG(数据库公共服务层接口实现) 
 * @author [email protected](苟志强)
 * @date 2017-2-10 上午11:25:11
 */
@Service("myBaseServiceImpl")
public class MyBaseServiceImpl implements MyBaseService {
	@Resource(name="baseDaoImpl")
	private BaseDao baseDao;
	public BaseDao getBaseDao() {
		return baseDao;
	}
	
	/**获取日志记录对象*/
	private ReturnUtil setOperationLog(String declareName,Object obj, ReturnUtil returnUtil){
		if(Statics.Record_Operation_Log && !Statics.not_add_log_declare.contains(declareName)){
			if(!Statics.chaeck_log_operation_table){
				//检查表是否存在
				JSONObject sqlParameter = new JSONObject();
				sqlParameter.put("table_name", "log_operation");
				ReturnUtil tableReturn = ReturnUtil.init().addData(baseDao.get("systemMapper.queryTable"+Statics.DATABASE_TYPE,sqlParameter));
				if(tableReturn.getCode()!=ReturnUtil.SUCCESS_CODE){
					//如果不存在则手动建表
					JSONObject json = new JSONObject();
					json.put("report_table_name", "log_operation");//表名
					//字段(可传入查询语句)例如:select * from userinfo
					json.put("update_sql", "`id` bigint(20) NOT NULL COMMENT '操作ID',`system_log_id` bigint(20) NOT NULL COMMENT '系统日志ID',`table_name` varchar(255) NOT NULL COMMENT '表名',`table_explain` varchar(255) DEFAULT NULL COMMENT '表说明',`operation_type` tinyint(1) NOT NULL COMMENT '操作类型(0增,1删,2改,3查)',`condition_sql` text NOT NULL COMMENT '操作语句',`condition` text COMMENT '操作语句条件',`content` text COMMENT '操作内容',`memberId` bigint(20) NOT NULL COMMENT '操作会员',`operation_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '操作时间',PRIMARY KEY (`id`)");
					json.put("report_name", "操作(业务)日志表\r\ntable_list_not_show;-- 在查询表列表时不可见");//表说明
					baseDao.add("systemMapper.createTable"+Statics.DATABASE_TYPE,json);
					outLog("【MyBase】:动态创建《操作(业务)日志表》数据表成功。",1);
				}
				Statics.chaeck_log_operation_table = true;
			}
			JSONObject operationLog = MyBatisUtil.getOperationLog(baseDao.getSession(), declareName, obj);
			if(operationLog!=null && operationLog.getInteger("operation_type")!=3){
				List operationLogs = ReflectHelperUtil.getInstance(returnUtil, "operation_logs");
				if(operationLogs==null)operationLogs = new ArrayList();
				operationLogs.add(operationLog);
				returnUtil = ReflectHelperUtil.setInstance(returnUtil, "operation_logs", operationLogs);
			}
		}
		return returnUtil;
	}
	
	/**
	 * mapper名称 若存在 则自动加入前缀
	 */
	private String mapperName;
	
	/**
	 * @Title: setMapperName 
	 * @Description: TODO(设置并指定mapper名称 设置后自动加入前缀) 
	 * @param @param mapperName mapper XML namespace属性
	 * @author [email protected](苟志强)
	 */
	public void setMapperName(String mapperName) {
		this.mapperName = mapperName;
	}
	/**
	 * @Title: isDebug 
	 * @Description: TODO(指定为Debug模式,Debug模式下带有控制台输出。可重写outLog方法,实现其他输出方式。) 
	 * @author [email protected](苟志强)
	 */
	public void isDebug() {
		Statics.MyBaseServiceDebug = true;
		outLog("【MyBase】:开启日志输出。",1);
	}
	public void isNotDebug() {
		Statics.MyBaseServiceDebug = false;
	}
	/**
	 * @Title: outLog 
	 * @Description: TODO(Debug输出) 
	 * @param @param log
	 * @param @param ln 设定文件 
	 * @return void 返回类型 
	 * @author [email protected](苟志强)
	 */
	private void outLog(String log,Object... ln){
		if (Statics.MyBaseServiceDebug) {
			if(ln.length>0){
				System.out.println(log);
			}else{
				System.out.print(log);
			}
		}
	}
	/**
	 * @Title: HandleId 
	 * @Description: TODO(Mapper ID 处理器) 
	 * @param @param id
	 * @param @return 设定文件 
	 * @return String 返回类型 
	 * @author [email protected](苟志强)
	 */
	private String HandleId(String id) {
		if(id.indexOf(".")==-1){
			if(mapperName!=null&&!"".equals(mapperName)){
				id = mapperName+"."+id;
			}
		}
		outLog("Mapper ID:"+id,1);
		return id;
	}
	/**
	 * @Title: findPage 
	 * @Description: TODO(分页查询) 
	 * @param @param page 分页对象 与传统一致
	 * @param @param id	mapper sql id
	 * @param @return 设定文件 
	 * @return ReturnUtil 返回类型 
	 * @author [email protected](苟志强)
	 */
	@Override
	public PageEntity findPage(PageEntity page,String id,Object obj) {
		outLog("【MyBase】:执行findPage数据查询,");
		id = HandleId(id);
		PageUtil.ObjectToPage(page, baseDao.getListPage(id, page));
		outLog("【MyBase】:执行结果:"+JSONObject.toJSONString(page),1);
		return page;
	}
	/**
	 * @Title: get 
	 * @Description: TODO(条件查询 一条) 
	 * @param @param map 查询条件 例如:{id:10001}
	 * @param @param id mapper sql id
	 * @param @return 设定文件 
	 * @return ReturnUtil 返回类型 
	 * @author [email protected](苟志强)
	 */
	@Override
	public ReturnUtil get(ReturnUtil returnUtil,Object obj,String id) {
		outLog("【MyBase】:执行get数据查询,");
		id = HandleId(id);
		if(returnUtil==null) returnUtil = ReturnUtil.init();
		returnUtil = setOperationLog(id, obj,returnUtil).addDataNotCode(baseDao.get(id, obj));
		outLog("【MyBase】:执行结果:"+returnUtil.toString(),1);
		return returnUtil;
	}
	/**
	 * @Title: getList 
	 * @Description: TODO(条件查询 多条) 
	 * @param @param map 查询条件 例如:{sex:1,age:12}
	 * @param @param id mapper sql id
	 * @param @return 设定文件 
	 * @return ReturnUtil 返回类型 
	 * @author [email protected](苟志强)
	 */
	@Override
	public ReturnUtil getList(ReturnUtil returnUtil,Object obj,String id) {
		outLog("【MyBase】:执行getList数据查询,");
		id = HandleId(id);
		if(returnUtil==null) returnUtil = ReturnUtil.init();
		returnUtil = setOperationLog(id, obj,returnUtil).addDataNotCode(baseDao.getList(id, obj));
		outLog("【MyBase】:执行结果:"+returnUtil.toString(),1);
		return returnUtil;
	}
	/**
	 * @Title: getListAndColumnComment 
	 * @Description: TODO(条件查询 多条<.包含字段信息>) 
	 * @param @param map 查询条件 例如:{sex:1,age:12}
	 * @param @param id mapper sql id
	 * @param @return 设定文件 
	 * @return ReturnUtil 返回类型 
	 * @author [email protected](苟志强)
	 */
	@SuppressWarnings("unchecked")
	@Override
	public ReturnUtil getListAndColumnComment(ReturnUtil returnUtil,Object obj,String id) {
		returnUtil = getList(returnUtil, obj, id);
		id = HandleId(id);
		JSONObject operationLog = MyBatisUtil.getOperationLog(baseDao.getSession(), id, obj);
		try {
			returnUtil.setResult_data_column_comment((List) baseDao.getList("systemMapper.queryColumn" + Statics.DATABASE_TYPE,operationLog));
		} catch (Exception e) {
		}
		return returnUtil;
	}
	/**
	 * @Title: add 
	 * @Description: TODOG(新增) 
	 * @param @param obj 新增对象
	 * @param @param id mapper sql id
	 * @param @param bool 是否追加ID属性(true 若存在“id”属性,自动生成ID并插入“id”属性中)
	 * @param @return 设定文件 
	 * @return ReturnUtil 返回类型 
	 * @author [email protected](苟志强)
	 */
	@SuppressWarnings("unchecked")
	@Override
	public ReturnUtil add(ReturnUtil returnUtil,Object obj, String id,boolean... bool) {
		if(returnUtil==null) returnUtil = ReturnUtil.init();
		try {
			outLog("【MyBase】:执行add数据添加,");
			id = HandleId(id);
			long newId = -1994;
			if(bool.length==1&&bool[0]){
				if(obj instanceof JSONObject){
//					newId = IdUtil.getId();
					newId = IdGZQ.getId();
					outLog("【MyBase】:获取到“id”属性,自动生成id:"+newId+",");
					JSONObject json = JSONObject.parseObject(JSONObject.toJSONString(obj));
					json.put("id", newId);
					obj = json;
				}else if(obj instanceof Map){
//					newId = IdUtil.getId();
					newId = IdGZQ.getId();
					outLog("【MyBase】:获取到“id”属性,自动生成id:"+newId+",");
					((Map) obj).put("id", newId);
				}else{
					Class classs = obj.getClass();
					//利用反射机制 判断Obj是否有“id”属性
					try {
						//查找到“id”属性
						Field field = classs.getDeclaredField("id");
						//判断“id”属性是否为long类型
						if(field.toString().indexOf("java.lang.Long "+classs.toString().split(" ")[1]+".id")!=-1
								|| field.toString().indexOf("long "+classs.toString().split(" ")[1]+".id")!=-1){
//						newId = IdUtil.getId();
							newId = IdGZQ.getId();
							outLog("【MyBase】:获取到“id”属性,自动生成id:"+newId+",");
							try {
								field.setAccessible(true);
								field.set(obj, newId);
							} catch (IllegalArgumentException e) {
								//发生异常标记ID使用失败
//							IdUtil.setUseFailId(newId);
								outLog("【MyBase】:执行结果:不合法的参数异常,生成的ID与“id”属性不匹配!",1);
								return returnUtil.addError("不合法的参数异常,生成的ID与“id”属性不匹配!");
							} catch (IllegalAccessException e) {
								//发生异常标记ID使用失败
//							IdUtil.setUseFailId(newId);
								outLog("【MyBase】:执行结果:安全权限异常,无权限控制名称为“id”的字段!",1);
								return returnUtil.addError("安全权限异常,无权限控制名称为“id”的字段!");
							}
						}
					} catch (NoSuchFieldException e) {
						if(newId!=-1994){
							//发生异常标记ID使用失败
//						IdUtil.setUseFailId(newId);
						}
						outLog("【MyBase】:执行结果:没有找到名称为“id”的字段!",1);
						return returnUtil.addError("没有找到名称为“id”的字段!");
					} catch (SecurityException e) {
						if(newId!=-1994){
							//发生异常标记ID使用失败
//						IdUtil.setUseFailId(newId);
						}
						outLog("【MyBase】:执行结果:安全管理存在,无权限控制名称为“id”的字段!",1);
						return returnUtil.addError("安全管理存在,无权限控制名称为“id”的字段!");
					}
				}
			}
			returnUtil = setOperationLog(id, obj,returnUtil);
			//插入数据
			Integer co = baseDao.add(id, obj);
			returnUtil.addData(co>0);
			//如果newId不为初始化ID(即有自动生成ID)
			if(newId!=-1994 && returnUtil.getCode()==ReturnUtil.SUCCESS_CODE){
				//如果插入成功
				returnUtil.addExtendInfo(newId);
			}
			outLog("【MyBase】:执行结果:"+returnUtil.toString(),1);
		} catch (Exception e) {
			returnUtil.addError("【MyBase】:程序异常:"+e.toString());
		}
		return returnUtil;
	}
	/**
	 * @Title: del 
	 * @Description: TODOG(删除) 
	 * @param @param obj 删除条件
	 * @param @param id mapper sql id
	 * @param @return 设定文件 
	 * @return ReturnUtil 返回类型 
	 * @author [email protected](苟志强)
	 */
	@Override
	public ReturnUtil del(ReturnUtil returnUtil,Object obj, String id) {
		try {
			outLog("【MyBase】:执行del数据删除,");
			id = HandleId(id);
			if(returnUtil==null) returnUtil = ReturnUtil.init();
			returnUtil = setOperationLog(id, obj,returnUtil).addData(baseDao.del(id, obj) > 0);
			outLog("【MyBase】:执行结果:" + returnUtil.toString(), 1);
			return returnUtil;
		} catch (Exception e) {
			return ReturnUtil.init().addError("【MyBase】:程序异常:"+e.toString());
		}
	}
	/**
	 * @Title: upData 
	 * @Description: TODOG(修改) 
	 * @param @param obj 修改条件
	 * @param @param id mapper sql id
	 * @param @return 设定文件 
	 * @return ReturnUtil 返回类型 
	 * @author [email protected](苟志强)
	 */
	@Override
	public ReturnUtil upData(ReturnUtil returnUtil,Object obj, String id) {
		try {
			outLog("【MyBase】:执行upData数据修改,");
			id = HandleId(id);
			if(returnUtil==null) returnUtil = ReturnUtil.init();
			returnUtil = setOperationLog(id, obj,returnUtil).addData(baseDao.update(id, obj)>0);
			outLog("【MyBase】:执行结果:"+returnUtil.toString(),1);
			return returnUtil;
		} catch (Exception e) {
			return ReturnUtil.init().addError("【MyBase】:程序异常:"+e.toString());
		}
	}
	
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy