package com.project.demo.service.base;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.project.demo.constant.FindConfig;
import com.project.demo.dao.base.BaseMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.ParameterizedType;
import java.net.URLDecoder;
import java.security.MessageDigest;
import java.util.*;
/**
*/
@Slf4j
public class BaseService<E>{
@Autowired
private BaseMapper<E> baseMapper;
Class<E> eClass = (Class<E>)((ParameterizedType)getClass().getGenericSuperclass()).getActualTypeArguments()[0];
private final String table = humpToLine(eClass.getSimpleName());
public List selectBaseList(String select) {
List<Map<String,Object>> mapList = baseMapper.selectBaseList(select);
List<E> list = new ArrayList<>();
for (Map<String,Object> map:mapList) {
list.add(JSON.parseObject(JSON.toJSONString(map),eClass));
}
return list;
}
public int selectBaseCount(String sql) {
return baseMapper.selectBaseCount(sql);
}
public int deleteBaseSql(String sql) {
return baseMapper.deleteBaseSql(sql);
}
public int updateBaseSql(String sql) {
return baseMapper.updateBaseSql(sql);
}
public void insert(Map<String,Object> body){
E entity = JSON.parseObject(JSON.toJSONString(body),eClass);
baseMapper.insert(entity);
log.info("[{}] - 插入操作:{}",entity);
}
@Transactional
public void update(Map<String,String> query,Map<String,String> config,Map<String,Object> body){
QueryWrapper wrapper = new QueryWrapper<E>();
toWhereWrapper(query,"0".equals(config.get(FindConfig.LIKE)),wrapper);
E entity = JSON.parseObject(JSON.toJSONString(body),eClass);
baseMapper.update(entity,wrapper);
log.info("[{}] - 更新操作:{}",entity);
}
public Map<String,Object> selectToPage(Map<String,String> query,Map<String,String> config){
Map<String,Object> map = new HashMap<>();
List list = baseMapper.selectBaseList(select(query, config));
map.put("list",list);
map.put("count",baseMapper.selectBaseCount(count(query,config)));
return map;
}
public Map<String,Object> selectToList(Map<String,String> query,Map<String,String> config){
Map<String,Object> map = new HashMap<>();
List<Map<String,Object>> resultList = baseMapper.selectBaseList(selectGroupCount(query, config));
List list = new ArrayList();
for (Map<String,Object> resultMap:resultList) {
List subList = new ArrayList();
for(String key:resultMap.keySet()){//keySet获取map集合key的集合 然后在遍历key即可
subList.add(resultMap.get(key));
}
list.add(subList);
}
map.put("list",list);
return map;
}
public Integer selectSqlToInteger(String sql){
Integer value = baseMapper.selectBaseCount(sql);
return value;
}
public Map<String,Object> selectBarGroup(Map<String,String> query,Map<String,String> config){
Map<String,Object> map = new HashMap<>();
List<Map<String,Object>> resultList = baseMapper.selectBaseList(barGroup(query, config));
List list = new ArrayList();
for (Map<String,Object> resultMap:resultList) {
List subList = new ArrayList();
for(String key:resultMap.keySet()){//keySet获取map集合key的集合 然后在遍历key即可
subList.add(resultMap.get(key));
}
list.add(subList);
}
map.put("list",list);
return map;
}
// public void barGroup(Map<String,String> query,Map<String,String> config,QueryWrapper wrapper){
// if (config.get(FindConfig.GROUP_BY) != null && !"".equals(config.get(FindConfig.GROUP_BY))){
// wrapper.select(config.get(FindConfig.GROUP_BY));
// if (config.get(FindConfig.FIELD) != null && !"".equals(config.get(FindConfig.FIELD))){
// String[] fieldList = config.get(FindConfig.FIELD).split(",");
// for (int i=0;i<fieldList.length;i++)
// wrapper.select("SUM("+fieldList[i]+")");
// }
// toWhereSql(query, "0".equals(config.get(FindConfig.LIKE)),wrapper);
// wrapper.groupBy(config.get(FindConfig.GROUP_BY));
// }else {
// wrapper.select("SUM("+config.get(FindConfig.GROUP_BY)+")");
// toWhereSql(query, "0".equals(config.get(FindConfig.LIKE)),wrapper);
// }
// log.info("[{}] - 查询操作,sql: {}",wrapper.getSqlSelect());
// }
public String barGroup(Map<String,String> query,Map<String,String> config){
StringBuffer sql = new StringBuffer(" SELECT ");
if (config.get(FindConfig.GROUP_BY) != null && !"".equals(config.get(FindConfig.GROUP_BY))){
sql.append(config.get(FindConfig.GROUP_BY));
if (config.get(FindConfig.FIELD) != null && !"".equals(config.get(FindConfig.FIELD))){
String[] fieldList = config.get(FindConfig.FIELD).split(",");
for (int i=0;i<fieldList.length;i++)
sql.append(" ,SUM(").append(fieldList[i]).append(")");
}
sql.append(" FROM ").append("`").append(table).append("`");
sql.append(toWhereSql(query, "0".equals(config.get(FindConfig.LIKE))));
sql.append(" ").append("GROUP BY ").append(config.get(FindConfig.GROUP_BY));
}else {
sql.append(" SUM(").append(config.get(FindConfig.GROUP_BY)).append(") FROM ").append("`").append(table).append("`");
sql.append(toWhereSql(query, "0".equals(config.get(FindConfig.LIKE))));
}
log.info("[{}] - 查询操作,sql: {}",table,sql);
return sql.toString();
}
// public void selectGroupCount(Map<String,String> query,Map<String,String> config,QueryWrapper wrapper){
// wrapper.select("count(*) AS count_value",config.get(FindConfig.GROUP_BY));
// toWhereSql(query, "0".equals(config.get(FindConfig.LIKE)),wrapper);
// if (config.get(FindConfig.GROUP_BY) != null && !"".equals(config.get(FindConfig.GROUP_BY))){
// wrapper.groupBy(config.get(FindConfig.GROUP_BY));
// }
// log.info("[{}] - 查询操作,sql: {}",wrapper.getSqlSelect());
// }
public String selectGroupCount(Map<String,String> query,Map<String,String> config){
StringBuffer sql = new StringBuffer("select COUNT(*) AS count_value, ");
sql.append(config.get(FindConfig.GROUP_BY)).append(" ");
sql.append("from ").append("`").append(table).append("` ");
sql.append(toWhereSql(query, "0".equals(config.get(FindConfig.LIKE))));
if (config.get(FindConfig.GROUP_BY) != null && !"".equals(config.get(FindConfig.GROUP_BY))){
sql.append("group by ").append(config.get(FindConfig.GROUP_BY)).append(" ");
}
log.info("[{}] - 查询操作,sql: {}",table,sql);
return sql.toString();
}
// public void select(Map<String,String> query,Map<String,String> config,QueryWrapper wrapper){
// wr