com.javajy.api.service.BaseService Maven / Gradle / Ivy
The newest version!
package com.javajy.api.service;
import com.javajy.api.component.Page;
import com.javajy.api.e.a;
import com.javajy.api.entity.RequestEntity;
import com.javajy.api.mapper.BaseMapper;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class BaseService {
protected BaseMapper baseMapper;
public BaseService() {
}
public Page list(RequestEntity var1, Class var2) {
ArrayList var3 = new ArrayList();
String var4 = "";
new StringBuffer();
int var6 = 0;
int var7 = var1.getCurrentPage();
int var8 = var1.getPageSize();
if (var8 == 0) {
var8 = 15;
}
if (var7 != 0) {
var6 = (var7 - 1) * var8;
}
List var9 = this.baseMapper.getAll(var2.getSimpleName().toUpperCase(), var6, var8);
Integer var10 = this.baseMapper.getAllCount(var2.getSimpleName().toUpperCase());
Iterator var11 = var9.iterator();
while(var11.hasNext()) {
Entity var12 = (Entity)var11.next();
var3.add(a.a(var12, var2));
}
return new Page(var7, var8, var10, var9);
}
public Integer save(T var1) {
new ArrayList();
StringBuffer var3 = new StringBuffer();
StringBuffer var4 = new StringBuffer();
Entity var5 = new Entity();
Method[] var6 = var1.getClass().getDeclaredMethods();
Method var7 = null;
for(int var8 = 0; var8 < var6.length; ++var8) {
Method var9 = var6[var8];
String var10 = var9.getName();
if (var10.startsWith("get") && !var10.equals("getId")) {
var10 = var10.replace("get", "");
try {
Class var11 = var9.getReturnType();
Object var12 = var9.invoke(var1, (Object[])null);
String var13 = "'";
if (var11 != Integer.class && var11 != Double.class) {
if (var12 == null) {
var12 = "";
}
} else {
var13 = "";
if (var12 == null) {
var12 = 0;
}
}
var4.append(var13).append(var12).append(var13).append(",");
var3.append(var10.substring(0, 1).toLowerCase()).append(var10.substring(1)).append(",");
} catch (Exception var15) {
var15.printStackTrace();
}
} else if (var10.equals("setId")) {
var7 = var9;
}
}
String var16 = var3.substring(0, var3.length() - 1);
var4 = new StringBuffer(var4.substring(0, var4.length() - 1));
var5.setColumns(var16);
var5.setValues(var4.toString());
var5.setTableName(var1.getClass().getSimpleName());
this.baseMapper.save(var5);
try {
var7.invoke(var1, var5.getId());
} catch (Exception var14) {
var14.printStackTrace();
}
return var5.getId();
}
public Integer update(T var1) {
new ArrayList();
StringBuffer var3 = new StringBuffer();
Entity var4 = new Entity();
Method[] var5 = var1.getClass().getDeclaredMethods();
for(int var6 = 0; var6 < var5.length; ++var6) {
Method var7 = var5[var6];
String var8 = var7.getName();
if (var8.startsWith("get")) {
var8 = var8.replace("get", "");
try {
Class var9 = var7.getReturnType();
String var10 = var8.substring(0, 1).toLowerCase() + var8.substring(1);
Object var11 = var7.invoke(var1, (Object[])null);
String var12 = "'";
if (var9 != Integer.class && var9 != Double.class) {
if (var11 == null) {
var11 = "";
}
} else {
var12 = "";
if (var11 == null) {
var11 = 0;
}
}
var3.append(var10).append("=").append(var12).append(var11).append(var12).append(",");
} catch (Exception var13) {
var13.printStackTrace();
}
}
}
var4.setTableName(var1.getClass().getSimpleName());
var4.setColumnsValues(var3.substring(0, var3.length() - 1));
this.baseMapper.update(var4);
return var4.getId();
}
public Integer delete(T var1) {
new ArrayList();
new StringBuffer();
Entity var4 = new Entity();
Method var5 = null;
Method[] var6 = var1.getClass().getDeclaredMethods();
Method[] var7 = var6;
int var8 = var6.length;
for(int var9 = 0; var9 < var8; ++var9) {
Method var10 = var7[var9];
String var11 = var10.getName();
if (var11.startsWith("getId")) {
var5 = var10;
break;
}
}
var4.setTableName(var1.getClass().getSimpleName());
try {
var4.setId(Integer.parseInt(var5.invoke(var1, (Object[])null).toString()));
this.baseMapper.delete(var4);
} catch (Exception var12) {
var12.printStackTrace();
}
return var4.getId();
}
public Page search(T var1, Integer var2, Integer var3) {
ArrayList var4 = new ArrayList();
StringBuffer var5 = new StringBuffer();
Entity var6 = new Entity();
Method[] var7 = var1.getClass().getDeclaredMethods();
int var8 = 0;
if (var3 == 0) {
var3 = 15;
}
if (var2 != 0) {
var8 = (var2 - 1) * var3;
}
var6.setStartIndex(var8);
var6.setPageSize(var3);
for(int var9 = 0; var9 < var7.length; ++var9) {
Method var10 = var7[var9];
String var11 = var10.getName();
if (var11.startsWith("get")) {
var11 = var11.replace("get", "");
try {
Class var12 = var10.getReturnType();
String var13 = var11.substring(0, 1).toLowerCase() + var11.substring(1);
Object var14 = var10.invoke(var1, (Object[])null);
String var15 = "'";
if (var12 == Integer.class || var12 == Double.class) {
var15 = "";
}
if (var14 != null) {
var5.append(var13).append("=").append(var15).append(var14).append(var15).append(",");
}
} catch (Exception var16) {
var16.printStackTrace();
}
}
}
var6.setTableName(var1.getClass().getSimpleName());
var6.setColumnsValues(var5.substring(0, var5.length() - 1).replace(",", " and "));
List var17 = this.baseMapper.getByCondition(var6);
Integer var18 = this.baseMapper.getByConditionCount(var6);
Iterator var19 = var17.iterator();
while(var19.hasNext()) {
Entity var20 = (Entity)var19.next();
var4.add(a.a(var20, var1.getClass()));
}
return new Page(var2, var3, var18, var17);
}
public Page search(T var1, Integer var2) {
ArrayList var3 = new ArrayList();
StringBuffer var4 = new StringBuffer();
Entity var5 = new Entity();
Method[] var6 = var1.getClass().getDeclaredMethods();
int var7 = 0;
byte var8 = 15;
if (var2 != 0) {
var7 = (var2 - 1) * var8;
}
var5.setStartIndex(var7);
var5.setPageSize(Integer.valueOf(var8));
for(int var9 = 0; var9 < var6.length; ++var9) {
Method var10 = var6[var9];
String var11 = var10.getName();
if (var11.startsWith("get")) {
var11 = var11.replace("get", "");
try {
Class var12 = var10.getReturnType();
String var13 = var11.substring(0, 1).toLowerCase() + var11.substring(1);
Object var14 = var10.invoke(var1, (Object[])null);
String var15 = "'";
if (var12 == Integer.class || var12 == Double.class) {
var15 = "";
}
if (var14 != null) {
var4.append(var13).append("=").append(var15).append(var14).append(var15).append(",");
}
} catch (Exception var16) {
var16.printStackTrace();
}
}
}
var5.setTableName(var1.getClass().getSimpleName());
var5.setColumnsValues(var4.substring(0, var4.length() - 1).replace(",", " and "));
List var17 = this.baseMapper.getByCondition(var5);
Integer var18 = this.baseMapper.getByConditionCount(var5);
Iterator var19 = var17.iterator();
while(var19.hasNext()) {
Entity var20 = (Entity)var19.next();
var3.add(a.a(var20, var1.getClass()));
}
return new Page(var2, Integer.valueOf(var8), var18, var17);
}
}