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.getById(route.getStartCode());
|
Code endCode = codeService.getById(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) {
|
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");
|
}
|
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) {
|
if (!routeService.removeByIds(Arrays.asList(ids))) {
|
return R.error("Delete Fail");
|
}
|
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.conver(one, RouteExcel.class);
|
|
Code code0 = codeService.selectByData(Utils.zeroFill(excelDto.getStartCode(), CommonConstant.QR_CODE_LEN));
|
Code code1 = codeService.selectByData(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();
|
}
|
|
}
|