| | |
| | | |
| | | import com.alibaba.fastjson.JSON; |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
| | | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
| | | import com.zy.acs.common.constant.CommonConstant; |
| | | import com.zy.acs.common.utils.GsonUtils; |
| | | import com.zy.acs.common.utils.Utils; |
| | | import com.zy.acs.common.utils.QrCodeCodecSupport; |
| | | 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.utils.ExcelUtil; |
| | | import com.zy.acs.manager.manager.entity.Code; |
| | | import com.zy.acs.manager.manager.entity.CodeGap; |
| | | import com.zy.acs.manager.manager.entity.FuncSta; |
| | | import com.zy.acs.manager.manager.entity.Route; |
| | | import com.zy.acs.manager.manager.enums.StatusType; |
| | | import com.zy.acs.manager.manager.service.CodeGapService; |
| | | import com.zy.acs.manager.manager.service.CodeService; |
| | | import com.zy.acs.manager.manager.service.impl.CodeServiceImpl; |
| | | import com.zy.acs.manager.manager.service.FuncStaService; |
| | | import com.zy.acs.manager.manager.service.RouteService; |
| | | import com.zy.acs.manager.system.controller.BaseController; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | 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; |
| | |
| | | |
| | | @Autowired |
| | | private CodeService codeService; |
| | | @Autowired |
| | | private CodeGapService codeGapService; |
| | | @Autowired |
| | | private RouteService routeService; |
| | | @Autowired |
| | | private FuncStaService funcStaService; |
| | | |
| | | @PreAuthorize("hasAuthority('manager:code:list')") |
| | | @PostMapping("/code/page") |
| | |
| | | return R.ok().add(codeService.getCacheById(id)); |
| | | } |
| | | |
| | | @PreAuthorize("hasAuthority('manager:code:list')") |
| | | @PostMapping("/code/info") |
| | | public R info(@RequestParam(required = false) String codeData) { |
| | | if (Cools.isEmpty(codeData)) { |
| | | return R.error(); |
| | | } |
| | | Code code = codeService.getCacheByData(codeData); |
| | | if (code == null) { |
| | | return R.error("Code Not Found"); |
| | | } |
| | | return R.ok().add(code); |
| | | } |
| | | |
| | | @PreAuthorize("hasAuthority('manager:code:list')") |
| | | @PostMapping("/code/route/list") |
| | | public R routeList(@RequestParam(required = false) String codeData) { |
| | | if (Cools.isEmpty(codeData)) { |
| | | return R.error(); |
| | | } |
| | | Code code = codeService.getCacheByData(codeData); |
| | | if (code == null) { |
| | | return R.ok().add(Collections.emptyList()); |
| | | } |
| | | List<Route> routeList = routeService.list(new LambdaQueryWrapper<Route>() |
| | | .eq(Route::getStatus, StatusType.ENABLE.val) |
| | | .and(wrapper -> wrapper.eq(Route::getStartCode, code.getId()).or().eq(Route::getEndCode, code.getId())) |
| | | .orderByAsc(Route::getId)); |
| | | return R.ok().add(routeList); |
| | | } |
| | | |
| | | @PreAuthorize("hasAuthority('manager:code:list')") |
| | | @PostMapping("/code/funcSta/list") |
| | | public R funcStaList(@RequestParam(required = false) String codeData) { |
| | | if (Cools.isEmpty(codeData)) { |
| | | return R.error(); |
| | | } |
| | | Code code = codeService.getCacheByData(codeData); |
| | | if (code == null) { |
| | | return R.ok().add(Collections.emptyList()); |
| | | } |
| | | List<FuncSta> funcStaList = funcStaService.list(new LambdaQueryWrapper<FuncSta>() |
| | | .eq(FuncSta::getCode, code.getId()) |
| | | .eq(FuncSta::getStatus, StatusType.ENABLE.val) |
| | | .orderByAsc(FuncSta::getId)); |
| | | return R.ok().add(funcStaList); |
| | | } |
| | | |
| | | @PreAuthorize("hasAuthority('manager:code:save')") |
| | | @OperationLog("Create Code") |
| | | @PostMapping("/code/save") |
| | | public R save(@RequestBody Code code) { |
| | | code.setData(Utils.zeroFill(code.getData(), CommonConstant.QR_CODE_LEN)); |
| | | code.setData(QrCodeCodecSupport.normalize(code.getData())); |
| | | code.setUuid("code".concat(code.getData())); |
| | | code.setCreateBy(getLoginUserId()); |
| | | code.setCreateTime(new Date()); |
| | |
| | | code.setUpdateTime(new Date()); |
| | | if (!codeService.save(code)) { |
| | | return R.error("Save Fail"); |
| | | } else { |
| | | CodeServiceImpl.CODE_ID_CACHE.put(code.getId(), code); |
| | | CodeServiceImpl.CODE_DATA_CACHE.put(code.getData(), code); |
| | | } |
| | | codeService.refreshCacheById(code.getId()); |
| | | return R.ok("Save Success").add(code); |
| | | } |
| | | |
| | |
| | | @OperationLog("Update Code") |
| | | @PostMapping("/code/update") |
| | | public R update(@RequestBody Code code) { |
| | | code.setData(Utils.zeroFill(code.getData(), CommonConstant.QR_CODE_LEN)); |
| | | Code origin = codeService.getById(code.getId()); |
| | | boolean cornerChanged = origin != null |
| | | && code.getCorner() != null |
| | | && !Objects.equals(origin.getCorner(), code.getCorner()); |
| | | code.setData(QrCodeCodecSupport.normalize(code.getData())); |
| | | code.setUpdateBy(getLoginUserId()); |
| | | code.setUpdateTime(new Date()); |
| | | if (!codeService.updateById(code)) { |
| | | return R.error("Update Fail"); |
| | | } else { |
| | | CodeServiceImpl.CODE_ID_CACHE.put(code.getId(), code); |
| | | CodeServiceImpl.CODE_DATA_CACHE.put(code.getData(), code); |
| | | } |
| | | List<Long> affectedCodeIds = new ArrayList<>(routeService.getAdjacencyNode(code.getId())); |
| | | if (!cornerChanged) { |
| | | affectedCodeIds.add(code.getId()); |
| | | } |
| | | codeService.evictCacheById(code.getId(), origin == null ? null : origin.getData()); |
| | | codeService.refreshCacheById(code.getId()); |
| | | if (!affectedCodeIds.isEmpty()) { |
| | | codeService.refreshCornerByCodeIds(affectedCodeIds); |
| | | } |
| | | return R.ok("Update Success").add(code); |
| | | } |
| | |
| | | @PreAuthorize("hasAuthority('manager:code:remove')") |
| | | @OperationLog("Delete Code") |
| | | @PostMapping("/code/remove/{ids}") |
| | | @Transactional |
| | | public R remove(@PathVariable Long[] ids) { |
| | | if (!codeService.removeByIds(Arrays.asList(ids))) { |
| | | return R.error("Delete Fail"); |
| | | List<Long> affectedCodeIds = new ArrayList<>(); |
| | | for (Long id : ids) { |
| | | Code code = codeService.getById(id); |
| | | if (null == code) { |
| | | continue; |
| | | } |
| | | affectedCodeIds.addAll(routeService.getAdjacencyNode(code.getId())); |
| | | codeGapService.remove(new LambdaQueryWrapper<CodeGap>().eq(CodeGap::getCode0, code.getId()).or().eq(CodeGap::getCode1, code.getId())); |
| | | routeService.remove(new LambdaQueryWrapper<Route>().eq(Route::getStartCode, code.getId()).or().eq(Route::getEndCode, code.getId())); |
| | | if (!codeService.removeById(id)) { |
| | | throw new CoolException("failed to remove code"); |
| | | } |
| | | codeService.evictCacheById(code.getId(), code.getData()); |
| | | } |
| | | codeService.refreshCornerByCodeIds(affectedCodeIds); |
| | | return R.ok("Delete Success").add(ids); |
| | | } |
| | | |
| | |
| | | @PreAuthorize("hasAuthority('manager:code:list')") |
| | | @PostMapping("/code/export") |
| | | public void export(@RequestBody Map<String, Object> map, HttpServletResponse response) throws Exception { |
| | | ExcelUtil.build(ExcelUtil.create(codeService.list(), Code.class), response); |
| | | // 1. 提取筛选条件(兼容前端 { filter: {...} } 格式) |
| | | Map<String, Object> filter = map; |
| | | if (map != null && map.containsKey("filter")) { |
| | | Object filterObj = map.get("filter"); |
| | | if (filterObj instanceof Map) { |
| | | filter = (Map<String, Object>) filterObj; |
| | | } |
| | | } |
| | | |
| | | // 2. 构建查询条件(MyBatis-Plus) |
| | | QueryWrapper<Code> wrapper = new QueryWrapper<>(); |
| | | if (filter != null && !filter.isEmpty()) { |
| | | // 根据前端可能传递的字段添加条件(字段名需与数据库列或实体属性对应) |
| | | if (filter.containsKey("corner")) { |
| | | wrapper.eq("corner", filter.get("corner")); |
| | | } |
| | | if (filter.containsKey("condition")) { |
| | | String condition = (String) filter.get("condition"); |
| | | wrapper.and(w -> w.like("data", condition).or().like("uuid", condition)); |
| | | } |
| | | if (filter.containsKey("timeStart")) { |
| | | wrapper.ge("create_time", filter.get("timeStart")); |
| | | } |
| | | if (filter.containsKey("timeEnd")) { |
| | | wrapper.le("create_time", filter.get("timeEnd")); |
| | | } |
| | | if (filter.containsKey("x")) { |
| | | wrapper.eq("x", filter.get("x")); |
| | | } |
| | | if (filter.containsKey("y")) { |
| | | wrapper.eq("y", filter.get("y")); |
| | | } |
| | | if (filter.containsKey("memo")) { |
| | | wrapper.like("memo", filter.get("memo")); |
| | | } |
| | | if (filter.containsKey("status")) { |
| | | wrapper.eq("status", filter.get("status")); |
| | | } |
| | | // 还可以添加其他字段如 data, uuid 等 |
| | | } |
| | | |
| | | // 3. 查询符合条件的数据 |
| | | List<Code> list = codeService.list(wrapper); |
| | | |
| | | // 4. 导出 Excel |
| | | ExcelUtil.build(ExcelUtil.create(list, Code.class), response); |
| | | } |
| | | |
| | | @PreAuthorize("hasAuthority('manager:code:save')") |
| | |
| | | public R importBatch(@RequestBody List<Map<String, Object>> list) { |
| | | Date now = new Date(); Long userId = getLoginUserId(); |
| | | for (Map<String, Object> map : list) { |
| | | Code code = Cools.conver(map, Code.class); |
| | | Code code = Cools.convert(map, Code.class); |
| | | code.setData(QrCodeCodecSupport.normalize(code.getData())); |
| | | if (null != codeService.getCacheByData(code.getData())) { |
| | | continue; |
| | | } |
| | | code.setData(Utils.zeroFill(code.getData(), CommonConstant.QR_CODE_LEN)); |
| | | code.setUuid("code".concat(code.getData())); |
| | | // code.setCorner(0); |
| | | code.setScale(GsonUtils.toJson(Cools.add("x", 1).add("y", 1))); |
| | | code.setStatus(StatusType.ENABLE.val); |
| | | code.setCreateBy(userId); |
| | | code.setCreateTime(now); |
| | | code.setUpdateBy(userId); |
| | | code.setUpdateTime(now); |
| | | if (!codeService.save(code)) { |
| | | log.error("failed to save code {}", JSON.toJSONString(map)); |
| | | } else { |
| | | CodeServiceImpl.CODE_ID_CACHE.put(code.getId(), code); |
| | | CodeServiceImpl.CODE_DATA_CACHE.put(code.getData(), code); |
| | | } |
| | | codeService.refreshCacheById(code.getId()); |
| | | } |
| | | return R.ok(); |
| | | } |