935090232@qq.com
2021-11-21 59634aeabb04aae0e819bd4c5fe909bb9cdbeb28
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package com.matrix.system.common.tools;
 
import com.matrix.core.constance.MatrixConstance;
import com.matrix.system.common.dao.UtilDao;
import com.matrix.core.tools.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.util.HashMap;
 
/**
 * 
 *  提供一些特殊的sql操作
 * @author:姜友瑶
 * @date 2016年8月28日
 */
@Service("ServiceUtil")
public class ServiceUtil {
    @Autowired
    private UtilDao utilDao;
 
    /**
     * 单字段判断去重 ß
     * 
     * @author jyy
     * @time 2016-07-31
     * @param tableName
     *            表明
     * @param column
     *            字段名
     * @param value
     *            字段值
     * @return
     */
    public boolean addCheckRepeat(String tableName, String column, Object value) {
        HashMap<String, Object> query = excuteQuery(tableName, column, value);
        if (query != null) {
            return true;
        } else {
            return false;
        }
    }
 
    /**
     * 双字段去重
     * 
     * @author jyy
     * @time 2016-07-31
     * @param tableName
     *            表名
     * @param column1
     *            字段名1
     * @param value1
     *            字段名1的值
     * @param column2
     *            字段名2
     * @param value2
     *            字段名2的值
     * @return
     */
    public boolean addCheckRepeatTowColumn(String tableName, String column1, Object value1, String column2,
            Object value2) {
        HashMap<String, Object> query = excuteTow(tableName, column1, value1, column2, value2);
        if (query != null) {
            return true;
        } else {
            return false;
        }
    }
 
    /**
     * 双字段去重 更新时调用
     * 
     * @author jyy
     * @time 2016-07-31
     * @param tableName
     * @param column1
     * @param value1
     * @param column2
     * @param value2
     * @param idName
     *            数据库id名称
     * @param idValue
     *            更新数据的id
     * @return
     */
    public boolean updateCheckRepeatTowColumn(String tableName, String column1, Object value1, String column2,
            Object value2, String idName, Object idValue) {
        HashMap<String, Object> query = excuteTow(tableName, column1, value1, column2, value2);
        return query != null && !query.get(idName).equals(idValue);
    }
 
    /**
     * 单字段去重 更新时调用
     * 
     * @author jyy
     * @time 2016-07-31
     * @param tableName
     * @param column
     * @param value
     * @param idName
     *            数据库id名称
     * @param idValue
     *            更新数据的id
     * @return
     */
    public boolean updateCheckRepeat(String tableName, String column, Object value, String idName, Object idValue) {
        HashMap<String, Object> query = excuteQuery(tableName, column, value);
        boolean flag = query != null && !String.valueOf(query.get(idName)).equals(String.valueOf(idValue));
        return flag;
    }
 
    private HashMap<String, Object> excuteQuery(String tableName, String column, Object value) {
        HashMap<String, Object> query = new HashMap<>(MatrixConstance.COLLECTION_SIZE);
        query.put("tableName", tableName);
        query.put("column", column);
        query.put("value", value);
        query = (HashMap<String, Object>) utilDao.selectRepeat(query);
        return query;
    }
 
    private HashMap<String, Object> excuteTow(String tableName, String column1, Object value1, String column2,
            Object value2) {
        HashMap<String, Object> query = new HashMap<>(MatrixConstance.COLLECTION_SIZE);
        query.put("tableName", tableName);
        query.put("column1", column1);
        query.put("value1", value1);
        query.put("column2", column2);
        query.put("value2", value2);
        query = (HashMap<String, Object>) utilDao.selectRepeatTowColumn(query);
        return query;
    }
 
    /**
     * 获取一个表中,其中一个字段的最大值
     * @Title: getMaxValue
      * @author:jyy
     * @param tableName
     * @param column
     * @return
     * String    返回类型
     * @date 2016年8月22日 上午10:51:03
     * @throws
     */
    public String getMaxValue(String tableName, String column) {
        String max=utilDao.selectMaxValue( tableName, column);
        return max;
    }
    /**
     *根据0001 的自增算法计算出要设置的值,编号会超过1000的不要用该方法。
     *编号超过1000后,或者最大编号不为数字会随机返回一个时间戳作为编号
     *
     * @Title: getMaxValue
     * @return
     * String    返回类型
     * @date 2016年8月22日 上午10:51:03
     * @throws
     */
    public String getMaxAddNo(String tableName, String column) {
        String max=utilDao.selectMaxValue( tableName, column);
        if(max==null||max.equals(""))
        {
            max="0001";
        }
        try{
            int s=Integer.parseInt(max);
            s=s+1;
            if(s>=1000){
                return DateUtil.getTimeCode();
            }
            return s>=10?(s>=100?s+"":"00"+s):"000"+s; // 计算 转型
        }catch(Exception e){
            return DateUtil.getTimeCode();
        }
 
 
    }
 
 
}