|  |  |  | 
|---|
|  |  |  | package com.zy.acs.manager.manager.controller; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import com.alibaba.fastjson.JSON; | 
|---|
|  |  |  | 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.GsonUtils; | 
|---|
|  |  |  | 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.manager.common.annotation.OperationLog; | 
|---|
|  |  |  | 
|---|
|  |  |  | import com.zy.acs.manager.common.utils.ExcelUtil; | 
|---|
|  |  |  | import com.zy.acs.manager.manager.entity.Code; | 
|---|
|  |  |  | import com.zy.acs.manager.manager.service.CodeService; | 
|---|
|  |  |  | import com.zy.acs.manager.manager.service.impl.CodeServiceImpl; | 
|---|
|  |  |  | 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.web.bind.annotation.*; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import javax.servlet.http.HttpServletResponse; | 
|---|
|  |  |  | import java.util.*; | 
|---|
|  |  |  | import java.util.stream.Collectors; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Slf4j | 
|---|
|  |  |  | @RestController | 
|---|
|  |  |  | @RequestMapping("/api") | 
|---|
|  |  |  | public class CodeController extends BaseController { | 
|---|
|  |  |  | 
|---|
|  |  |  | @PreAuthorize("hasAuthority('manager:code:list')") | 
|---|
|  |  |  | @GetMapping("/code/{id}") | 
|---|
|  |  |  | public R get(@PathVariable("id") Long id) { | 
|---|
|  |  |  | return R.ok().add(codeService.getById(id)); | 
|---|
|  |  |  | return R.ok().add(codeService.getCacheById(id)); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @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.setUuid("code".concat(code.getData())); | 
|---|
|  |  |  | code.setCreateBy(getLoginUserId()); | 
|---|
|  |  |  | code.setCreateTime(new Date()); | 
|---|
|  |  |  | code.setUpdateBy(getLoginUserId()); | 
|---|
|  |  |  | 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); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | 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.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); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return R.ok("Update Success").add(code); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @PreAuthorize("hasAuthority('manager:loc:update')") | 
|---|
|  |  |  | @OperationLog("Update Code") | 
|---|
|  |  |  | @PostMapping("/code/update/many") | 
|---|
|  |  |  | public R updateMany(@RequestBody List<Code> list) { | 
|---|
|  |  |  | if (!Cools.isEmpty(list)) { | 
|---|
|  |  |  | for (Code code : list) { | 
|---|
|  |  |  | code.setUpdateBy(getLoginUserId()); | 
|---|
|  |  |  | code.setUpdateTime(new Date()); | 
|---|
|  |  |  | if (!codeService.updateById(code)) { | 
|---|
|  |  |  | return R.error("Update Fail"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return R.ok("Update Success").add(list.stream().map(Code::getId).collect(Collectors.toList())); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @PreAuthorize("hasAuthority('manager:code:remove')") | 
|---|
|  |  |  | 
|---|
|  |  |  | @PreAuthorize("hasAuthority('manager:code:save')") | 
|---|
|  |  |  | @PostMapping("/code/import") | 
|---|
|  |  |  | 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); | 
|---|
|  |  |  | System.out.println(GsonUtils.toJson(code)); | 
|---|
|  |  |  | 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.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); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return R.ok(); | 
|---|
|  |  |  | } | 
|---|