#
vincentlu
昨天 11e44b3012e829ff7c85363ce1a7c2c0457c72f0
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package com.zy.acs.manager.manager.controller;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zy.acs.common.constant.CommonConstant;
import com.zy.acs.common.utils.Utils;
import com.zy.acs.framework.common.Cools;
import com.zy.acs.framework.common.R;
import com.zy.acs.framework.exception.CoolException;
import com.zy.acs.manager.common.annotation.OperationLog;
import com.zy.acs.manager.common.domain.BaseParam;
import com.zy.acs.manager.common.domain.KeyValVo;
import com.zy.acs.manager.common.domain.PageParam;
import com.zy.acs.manager.common.domain.RouteExcel;
import com.zy.acs.manager.common.utils.ExcelUtil;
import com.zy.acs.manager.manager.entity.Code;
import com.zy.acs.manager.manager.entity.Route;
import com.zy.acs.manager.manager.service.CodeGapService;
import com.zy.acs.manager.manager.service.CodeService;
import com.zy.acs.manager.manager.service.RouteService;
import com.zy.acs.manager.system.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
 
import javax.servlet.http.HttpServletResponse;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
@RestController
@RequestMapping("/api")
public class RouteController extends BaseController {
 
    @Autowired
    private RouteService routeService;
    @Autowired
    private CodeService codeService;
    @Autowired
    private CodeGapService codeGapService;
 
    @PreAuthorize("hasAuthority('manager:route:list')")
    @PostMapping("/route/page")
    public R page(@RequestBody Map<String, Object> map) {
        BaseParam baseParam = buildParam(map, BaseParam.class);
        PageParam<Route, BaseParam> pageParam = new PageParam<>(baseParam, Route.class);
        return R.ok().add(routeService.page(pageParam, pageParam.buildWrapper(true)));
    }
 
    @PreAuthorize("hasAuthority('manager:route:list')")
    @PostMapping("/route/list")
    public R list(@RequestBody Map<String, Object> map) {
        return R.ok().add(routeService.list());
    }
 
    @PreAuthorize("hasAuthority('manager:route:list')")
    @PostMapping({"/route/many/{ids}", "/routes/many/{ids}"})
    public R many(@PathVariable Long[] ids) {
        return R.ok().add(routeService.listByIds(Arrays.asList(ids)));
    }
 
    @PreAuthorize("hasAuthority('manager:route:list')")
    @GetMapping("/route/{id}")
    public R get(@PathVariable("id") Long id) {
        return R.ok().add(routeService.getById(id));
    }
 
    @PreAuthorize("hasAuthority('manager:route:save')")
    @OperationLog("Create Route")
    @PostMapping("/route/save")
    @Transactional
    public R save(@RequestBody Route route) {
        if (route.getStartCode() == null || route.getEndCode() == null) {
            return R.error("Save Fail");
        }
        if (route.getStartCode().equals(route.getEndCode())) {
            return R.error("Save Fail");
        }
        if (routeService.count(new LambdaQueryWrapper<Route>()
                .eq(Route::getStartCode, route.getStartCode())
                .eq(Route::getEndCode, route.getEndCode())) > 0 ||
                routeService.count(new LambdaQueryWrapper<Route>()
                        .eq(Route::getStartCode, route.getEndCode())
                        .eq(Route::getEndCode, route.getStartCode())) > 0
        ) {
            return R.error("Save Fail");
        }
 
        Code startCode = codeService.getCacheById(route.getStartCode());
        Code endCode = codeService.getCacheById(route.getEndCode());
 
        if (null == startCode || null == endCode) {
            return R.error("Save Fail");
        }
 
        Route result = routeService.createRouteByCode(startCode, endCode, route.getDirection(), getLoginUserId());
        codeGapService.createCodeGapByCode(startCode, endCode, getLoginUserId());
 
        if (null == result) {
            throw new CoolException("Save Fail");
        }
        return R.ok("Save Success").add(route);
    }
 
    @PreAuthorize("hasAuthority('manager:route:update')")
    @OperationLog("Update Route")
    @PostMapping("/route/update")
    public R update(@RequestBody Route route) {
        Route origin = routeService.getById(route.getId());
        if (origin == null) {
            return R.error("Update Fail");
        }
        if (route.getStartCode().equals(route.getEndCode())) {
            return R.error("Update Fail");
        }
        if (routeService.count(new LambdaQueryWrapper<Route>()
                .eq(Route::getStartCode, route.getStartCode())
                .eq(Route::getEndCode, route.getEndCode())
                .ne(Route::getId, route.getId())) > 0 ||
                routeService.count(new LambdaQueryWrapper<Route>()
                        .eq(Route::getStartCode, route.getEndCode())
                        .eq(Route::getEndCode, route.getStartCode())
                        .ne(Route::getId, route.getId())) > 0
        ) {
            return R.error("Update Fail");
        }
 
        route.setUpdateBy(getLoginUserId());
        route.setUpdateTime(new Date());
        if (!routeService.updateById(route)) {
            return R.error("Update Fail");
        }
        List<Long> affectedCodeIds = new ArrayList<>();
        if (origin.getStartCode() != null) {
            affectedCodeIds.add(origin.getStartCode());
        }
        if (origin.getEndCode() != null) {
            affectedCodeIds.add(origin.getEndCode());
        }
        if (route.getStartCode() != null) {
            affectedCodeIds.add(route.getStartCode());
        }
        if (route.getEndCode() != null) {
            affectedCodeIds.add(route.getEndCode());
        }
        codeService.refreshCornerByCodeIds(affectedCodeIds);
        return R.ok("Update Success").add(route);
    }
 
    @PreAuthorize("hasAuthority('manager:route:remove')")
    @OperationLog("Delete Route")
    @PostMapping("/route/remove/{ids}")
    public R remove(@PathVariable Long[] ids) {
        List<Route> routes = routeService.listByIds(Arrays.asList(ids));
        List<Long> affectedCodeIds = new ArrayList<>();
        for (Route route : routes) {
            if (route.getStartCode() != null) {
                affectedCodeIds.add(route.getStartCode());
            }
            if (route.getEndCode() != null) {
                affectedCodeIds.add(route.getEndCode());
            }
        }
        if (!routeService.removeByIds(Arrays.asList(ids))) {
            return R.error("Delete Fail");
        }
        codeService.refreshCornerByCodeIds(affectedCodeIds);
        return R.ok("Delete Success").add(ids);
    }
 
    @PreAuthorize("hasAuthority('manager:route:list')")
    @PostMapping("/route/query")
    public R query(@RequestParam(required = false) String condition) {
        List<KeyValVo> vos = new ArrayList<>();
        LambdaQueryWrapper<Route> wrapper = new LambdaQueryWrapper<>();
        if (!Cools.isEmpty(condition)) {
            wrapper.like(Route::getUuid, condition);
        }
        routeService.page(new Page<>(1, 30), wrapper).getRecords().forEach(
                item -> vos.add(new KeyValVo(item.getId(), item.getUuid()))
        );
        return R.ok().add(vos);
    }
 
    @PreAuthorize("hasAuthority('manager:route:list')")
    @PostMapping("/route/export")
    public void export(@RequestBody Map<String, Object> map, HttpServletResponse response) throws Exception {
        ExcelUtil.build(ExcelUtil.create(routeService.list(), Route.class), response);
    }
 
    @PreAuthorize("hasAuthority('manager:route:save')")
    @PostMapping("/route/import")
    public R importBatch(@RequestBody List<Map<String, Object>> list) {
        Long userId = getLoginUserId();
        String regex = "([a-zA-Z]+)\\(";
        Pattern pattern = Pattern.compile(regex);
        for (Map<String, Object> map : list) {
            Map<String, Object> one = new HashMap<>();
            for (Map.Entry<String, Object> entry : map.entrySet()) {
                String key = entry.getKey();
                Matcher matcher = pattern.matcher(key);
                if (matcher.find()) {
                    key = matcher.group(1);
                }
                key = Utils.toCamelCase(key);
                one.put(key, entry.getValue());
            }
            RouteExcel excelDto = Cools.convert(one, RouteExcel.class);
 
            Code code0 = codeService.getCacheByData(Utils.zeroFill(excelDto.getStartCode(), CommonConstant.QR_CODE_LEN));
            Code code1 = codeService.getCacheByData(Utils.zeroFill(excelDto.getEndCode(), CommonConstant.QR_CODE_LEN));
 
            if (null == code0 || null == code1) { continue; }
 
            routeService.createRouteByCode(code0, code1, excelDto.getDirection(), userId);
            codeGapService.createCodeGapByCode(code0, code1, userId);
        }
        return R.ok();
    }
 
}