package service;

import entity.Expression;
import util.sql.DBSqlExe;

import java.util.List;
import java.util.Map;
import java.util.Random;

/**
 * Created by Bruce-Jiang on 2016/10/4.
 * 表达式业务逻辑实现类
 */
public class ExpressionServiceImp extends ExpressionService{
    @Override
    public int insertManyExpre(List<Expression> mapList) {
        int re = 0;
        for (Expression exp: mapList) {
            int cur = insertOneExpre(exp);
            re += cur;
        }
        return re;
    }

    @Override
    public int insertOneExpre(Expression exp) {
        int re = 0;
        String sqlInsert = "INSERT INTO expression(id, e_expre, e_result, e_rank, e_tip) " +
                "VALUE(null,?,?,?,?)";
        String sqlQuery = "SELECT * FROM expression WHERE e_expre = ? AND e_result = ? AND e_rank = ?";

        DBSqlExe dbSqlExe = new DBSqlExe();

        List<Map<String,Object>> mapList = dbSqlExe.exeQueryNTransaction(sqlQuery,new Object[]{exp.geteExpre(),exp.geteResult(),exp.geteRank()});

        if(mapList.isEmpty()) {
            re = dbSqlExe.exeUpdateTransatcion(sqlInsert, new Object[]{exp.geteExpre(), exp.geteResult(), exp.geteRank(),exp.geteTip()});
        }
        return re;
    }

    @Override
    public Expression obtainOneExp(int rank){
        Random random = new Random();
        String sql = "SELECT * FROM expression WHERE e_rank = ? ORDER BY id";
        //String total = "SELECT COUNT(*) FROM expression WHERE e_rank = ?";
        DBSqlExe dbSqlExe = new DBSqlExe();
        //注意这里是存在败笔的
        if(rank == 0){
            rank = random.nextInt(4)+1;
        }
        List<Map<String,Object>> mapList = dbSqlExe.exeQueryNTransaction(sql,new Object[]{rank});
        int index = random.nextInt(mapList.size());
        Map<String,Object> map = mapList.get(index);
        Expression exp = new Expression();
        exp.setId((Integer) map.get("id"));
        exp.seteExpre((String)map.get("e_expre"));
        exp.seteResult((String)map.get("e_result"));
        exp.seteRank((Integer)map.get("e_rank"));
        exp.seteTip((String)map.get("e_tip"));
        System.out.println(exp.toString());
        return exp;
    }

    @Override
    public List<Expression> obtainManyExp(int rank) {
        return null;
    }
}

@@ -939,16 +939,33 @@
 qlQuery,
+%0A                
 new Obje
@@ -1403,16 +1403,78 @@
 BY id%22;%0A
+        String sql5 = %22SELECT * FROM expression ORDER BY id%22;%0A
         
@@ -1582,24 +1582,73 @@
 DBSqlExe();%0A
+        List%3CMap%3CString,Object%3E%3E mapList = null;%0A
         //%E6%B3%A8%E6%84%8F
@@ -1675,17 +1675,17 @@
 rank == 
-0
+5
 )%7B%0A     
@@ -1730,43 +1730,105 @@
     
- %7D%0A
      
-   
+map
 List
-%3CMap%3CString,Object%3E%3E
+ = dbSqlExe.exeQueryNTransaction(sql5,new Object%5B%5D%7Brank%7D);%0A        %7Delse%7B%0A           
  map
@@ -1881,32 +1881,42 @@
 bject%5B%5D%7Brank%7D);%0A
+        %7D%0A
         int inde

reconstruct Expression service