Your Name 3 vuotta sitten
vanhempi
commit
61edbb0d36

+ 10
- 6
src/main/java/com/yunzhi/shigongli/controller/TaHotelController.java Näytä tiedosto

@@ -73,10 +73,11 @@ public class TaHotelController extends BaseController {
73 73
     @ApiOperation(value="保存", notes = "保存", httpMethod = "POST", response = ResponseBean.class)
74 74
     public ResponseBean taHotelAdd(@ApiParam("保存内容") @RequestBody TaHotel taHotel) throws Exception{
75 75
 
76
-        List<TaHotel> lst = iTaHotelService.getByName(taHotel.getHotelName());
77
-        if (null != lst && lst.size() > 0) {
78
-            return ResponseBean.error("民宿名称已存在", ResponseBean.ERROR_ILLEGAL_PARAMS);
79
-        }
76
+//        List<TaHotel> lst = iTaHotelService.getByName(taHotel.getHotelName());
77
+//        if (null != lst && lst.size() > 0) {
78
+//            return ResponseBean.error("民宿名称已存在", ResponseBean.ERROR_ILLEGAL_PARAMS);
79
+//        }
80
+        iTaHotelService.isExisted(TaHotel::getHotelName, taHotel.getHotelName());
80 81
 
81 82
         if (iTaHotelService.createNew(taHotel)){
82 83
             return ResponseBean.success(taHotel);
@@ -110,8 +111,11 @@ public class TaHotelController extends BaseController {
110 111
     public ResponseBean taHotelUpdate(@ApiParam("对象ID") @PathVariable String id,
111 112
                                         @ApiParam("更新内容") @RequestBody TaHotel taHotel) throws Exception{
112 113
 
113
-        List<TaHotel> lst = iTaHotelService.getByName(taHotel.getHotelName());
114
-        if (null != lst && lst.size() > 0) {
114
+        boolean exists = iTaHotelService.isExisted(TaHotel::getHotelName,
115
+                taHotel.getHotelName(),
116
+                TaHotel::getHotelId,
117
+                taHotel.getHotelId());
118
+        if (exists) {
115 119
             return ResponseBean.error("民宿名称已存在", ResponseBean.ERROR_ILLEGAL_PARAMS);
116 120
         }
117 121
 

+ 33
- 0
src/main/java/com/yunzhi/shigongli/service/IBaseService.java Näytä tiedosto

@@ -1,9 +1,42 @@
1 1
 package com.yunzhi.shigongli.service;
2 2
 
3
+import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
3 4
 import com.baomidou.mybatisplus.extension.service.IService;
4 5
 
5 6
 import java.io.Serializable;
7
+import java.util.List;
6 8
 
7 9
 public interface IBaseService<T> extends IService<T> {
10
+    /**
11
+     * 逻辑删除
12
+     * @param id
13
+     * @return
14
+     */
8 15
     boolean removeLogicById(Serializable id);
16
+
17
+    /**
18
+     * 依据字段值获取列表
19
+     * @param column
20
+     * @param val
21
+     * @return
22
+     */
23
+    List<T> getBy(SFunction<T, ?> column, Serializable val);
24
+
25
+    /**
26
+     * 是否存在, 校验唯一
27
+     * @param column
28
+     * @param val
29
+     * @return
30
+     */
31
+    boolean isExisted(SFunction<T, ?> column, Serializable val);
32
+
33
+    /**
34
+     * 排除 notVal 的条件下是否存在
35
+     * @param column
36
+     * @param val
37
+     * @param notEq
38
+     * @param notVal
39
+     * @return
40
+     */
41
+    boolean isExisted(SFunction<T, ?> column, Serializable val, SFunction<T, ?>  notEq, Serializable notVal);
9 42
 }

+ 28
- 0
src/main/java/com/yunzhi/shigongli/service/impl/BaseServiceImpl.java Näytä tiedosto

@@ -1,13 +1,18 @@
1 1
 package com.yunzhi.shigongli.service.impl;
2 2
 
3
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
4
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
3 5
 import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
4 6
 import com.baomidou.mybatisplus.core.mapper.BaseMapper;
5 7
 import com.baomidou.mybatisplus.core.metadata.TableInfo;
8
+import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
6 9
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
7 10
 import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
8 11
 import com.yunzhi.shigongli.service.IBaseService;
9 12
 
10 13
 import java.io.Serializable;
14
+import java.util.List;
15
+import java.util.function.Supplier;
11 16
 
12 17
 public class BaseServiceImpl <M extends BaseMapper<T>, T> extends ServiceImpl <M, T> implements IBaseService<T> {
13 18
     @Override
@@ -20,4 +25,27 @@ public class BaseServiceImpl <M extends BaseMapper<T>, T> extends ServiceImpl <M
20 25
 
21 26
         return update(updateWrapper);
22 27
     }
28
+
29
+    @Override
30
+    public List<T> getBy(SFunction<T, ?> column, Serializable val) {
31
+        LambdaQueryWrapper<T> queryWrapper = new LambdaQueryWrapper<>();
32
+        queryWrapper.eq(column, val);
33
+
34
+        return list(queryWrapper);
35
+    }
36
+
37
+    @Override
38
+    public boolean isExisted(SFunction<T, ?> column, Serializable val) {
39
+        return isExisted(column, val, (SFunction<T, ?>) null, null);
40
+    }
41
+
42
+    @Override
43
+    public boolean isExisted(SFunction<T, ?> column, Serializable val, SFunction<T, ?> notEq, Serializable notVal) {
44
+        LambdaQueryWrapper<T> queryWrapper = new LambdaQueryWrapper<>();
45
+        queryWrapper.eq(column, val);
46
+        queryWrapper.ne(null != notVal, notEq, notVal);
47
+
48
+        int cnt = count(queryWrapper);
49
+        return cnt > 0;
50
+    }
23 51
 }