package cn.com.capcloud.core.noentitydb;

import cn.com.capcloud.core.websocket.RedisPublication;
import cn.com.capcloud.core.system.user.User;
import org.joda.time.DateTime;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.transaction.Transactional;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Service
public class BaseService {
	@Resource private JdbcTemplate jdbcTemplate;
	//单条数据
	public Map<String,Object> save(String tableName,Map<String,Object> objMap){
		BaseDao.setDefaultValue(objMap,null);
		long id=BaseDao.addForLongPkId(tableName, objMap, jdbcTemplate);
		if(id!=-1){
			String pkFieldName=BaseDao.getPkFieldName(tableName, jdbcTemplate);
			objMap.put(pkFieldName, id);
		}
		return objMap;
	}
	//单条数据(需自动填充创建人信息,如：创建人,创建人部门等)
	public Map<String,Object> save(String tableName, Map<String,Object> objMap, User user){
		BaseDao.setDefaultValue(objMap,user);
		long id=BaseDao.addForLongPkId(tableName, objMap, jdbcTemplate);
		if(id!=-1){
			String pkFieldName=BaseDao.getPkFieldName(tableName, jdbcTemplate);
			objMap.put(pkFieldName, id);
		}
		return objMap;
	}
	//批量数据
	public Map<String,Object> save(String tableName,List<Map<String,Object>> objMaps){
		for (int i = 0; i < objMaps.size(); i++) {
			BaseDao.setDefaultValue(objMaps.get(i),null);
		}
		return BaseDao.saveBatch(tableName, objMaps, jdbcTemplate,null,null,null);
	}
	//批量数据
	public Map<String,Object> save(String tableName, List<Map<String,Object>> objMaps, User user){
		for (int i = 0; i < objMaps.size(); i++) {
			BaseDao.setDefaultValue(objMaps.get(i),user);
		}
		return BaseDao.saveBatch(tableName, objMaps, jdbcTemplate,null,null,null);
	}
	//批量数据
	public Map<String,Object> save(String tableName, List<Map<String,Object>> objMaps, RedisPublication redisPublication, String wsCode, User user){
		for (int i = 0; i < objMaps.size(); i++) {
			BaseDao.setDefaultValue(objMaps.get(i),user);
		}
		return BaseDao.saveBatch(tableName, objMaps, jdbcTemplate,redisPublication,wsCode,user);
	}
	@Transactional
	public Map<String,Object> update(String tableName,Map<String,Object> objMap){
		BaseDao.setUpdateDefaultValue(objMap,null);
		BaseDao.updateOne(tableName, jdbcTemplate, objMap);
		return objMap;
	}
	@Transactional
	public List<Map<String,Object>> update(String tableName,List<Map<String,Object>> objMaps){
		for (int i = 0; i < objMaps.size(); i++) {
			BaseDao.setUpdateDefaultValue(objMaps.get(i),null);
		}
		BaseDao.updateBatch(tableName,jdbcTemplate, objMaps);
		return objMaps;
	}
	@Transactional
	public List<Map<String,Object>> update(String tableName,List<Map<String,Object>> objMaps,User user){
		for (int i = 0; i < objMaps.size(); i++) {
			BaseDao.setUpdateDefaultValue(objMaps.get(i),user);
		}
		BaseDao.updateBatch(tableName,jdbcTemplate, objMaps);
		return objMaps;
	}
	public Map<String,Object> findOne(String tableName,String pkId){
		return BaseDao.findOne(tableName, pkId, jdbcTemplate); 
	}
	public Map<String,Object> findOne(String tableName,String pkId,String pkFileId){
		return BaseDao.findOne(tableName, pkId, jdbcTemplate,pkFileId);
	}
	public void delete(String tableName,String pkId) {
		String sql="delete from "+tableName;
		String pkFieldName=BaseDao.getPkFieldName(tableName, jdbcTemplate);
		sql+=" where "+pkFieldName+"="+pkId;
		this.jdbcTemplate.execute(sql);
	}
	
	public JtPage query(String tableName,String where,int page,int pageSize) {
		return BaseDao.getPageListAllCol(tableName, where, page, pageSize, jdbcTemplate);
	}
	
	public JtPage query(String sql,int page,int pageSize) {
		return BaseDao.getPageListAllCol(sql, page, pageSize, jdbcTemplate);
	}
	public JtPage query(String sql,int page,int pageSize,String sqlCount) {
		return BaseDao.getPageListAllCol(sql, page, pageSize, jdbcTemplate,sqlCount);
	}
	public List<Map<String,Object>> queryNotPage(String tableName,String where){
		return BaseDao.queryNotPage(tableName, where, jdbcTemplate);
	}
}
