|
@@ -0,0 +1,176 @@
|
|
|
+package cn.com.qmth.examcloud.core.basic.api.controller;
|
|
|
+
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Iterator;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Optional;
|
|
|
+import java.util.regex.Pattern;
|
|
|
+
|
|
|
+import javax.persistence.criteria.Predicate;
|
|
|
+import javax.validation.constraints.NotNull;
|
|
|
+
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.springframework.beans.BeanUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.data.domain.Page;
|
|
|
+import org.springframework.data.domain.PageRequest;
|
|
|
+import org.springframework.data.domain.Sort;
|
|
|
+import org.springframework.data.domain.Sort.Direction;
|
|
|
+import org.springframework.data.jpa.domain.Specification;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+import org.springframework.web.bind.annotation.DeleteMapping;
|
|
|
+import org.springframework.web.bind.annotation.GetMapping;
|
|
|
+import org.springframework.web.bind.annotation.PathVariable;
|
|
|
+import org.springframework.web.bind.annotation.PostMapping;
|
|
|
+import org.springframework.web.bind.annotation.RequestBody;
|
|
|
+import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
+import org.springframework.web.bind.annotation.RequestParam;
|
|
|
+import org.springframework.web.bind.annotation.RequestPart;
|
|
|
+import org.springframework.web.bind.annotation.RestController;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
+
|
|
|
+import com.google.common.collect.Lists;
|
|
|
+
|
|
|
+import cn.com.qmth.examcloud.api.commons.exchange.PageInfo;
|
|
|
+import cn.com.qmth.examcloud.api.commons.security.bean.User;
|
|
|
+import cn.com.qmth.examcloud.commons.exception.StatusException;
|
|
|
+import cn.com.qmth.examcloud.core.basic.api.controller.bean.ResourceDomain;
|
|
|
+import cn.com.qmth.examcloud.core.basic.dao.ResourceRepo;
|
|
|
+import cn.com.qmth.examcloud.core.basic.dao.entity.ResourceEntity;
|
|
|
+import cn.com.qmth.examcloud.core.basic.service.ResourceService;
|
|
|
+import cn.com.qmth.examcloud.core.basic.service.bean.ResourceInfo;
|
|
|
+import cn.com.qmth.examcloud.web.support.ControllerSupport;
|
|
|
+import io.swagger.annotations.Api;
|
|
|
+import io.swagger.annotations.ApiOperation;
|
|
|
+
|
|
|
+@Api(tags = "资源管理")
|
|
|
+@Transactional
|
|
|
+@RestController
|
|
|
+@RequestMapping("${$rmp.ctr.basic}/resource")
|
|
|
+public class ResourceController extends ControllerSupport {
|
|
|
+
|
|
|
+ @Value("${$upyun.site.1.domain}")
|
|
|
+ private String upyunFileUrl;
|
|
|
+ @Autowired
|
|
|
+ private ResourceRepo resourceRepo;
|
|
|
+ @Autowired
|
|
|
+ private ResourceService resourceService;
|
|
|
+
|
|
|
+ @ApiOperation(value = "分页查询资源")
|
|
|
+ @GetMapping("page/{pageNo}/{pageSize}")
|
|
|
+ public PageInfo<ResourceDomain> getPage(@PathVariable Integer pageNo, @PathVariable Integer pageSize,
|
|
|
+ @RequestParam(required = false) String name,
|
|
|
+ @RequestParam(required = false) Long parentId,@RequestParam(required = true) Long rootOrgId) {
|
|
|
+
|
|
|
+ if(!isSuperAdmin()) {
|
|
|
+ User accessUser = getAccessUser();
|
|
|
+ if(!rootOrgId.equals(accessUser.getRootOrgId())) {
|
|
|
+ throw new StatusException("100000", "无效的请求");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ Specification<ResourceEntity> specification = (root, query, cb) -> {
|
|
|
+ List<Predicate> predicates = new ArrayList<>();
|
|
|
+
|
|
|
+ predicates.add(cb.equal(root.get("rootOrgId"), rootOrgId));
|
|
|
+
|
|
|
+ if (StringUtils.isNotBlank(name)) {
|
|
|
+ predicates.add(cb.like(root.get("name"), toSqlSearchPattern(name)));
|
|
|
+ }
|
|
|
+ if (null == parentId||-1 == parentId) {
|
|
|
+ predicates.add(cb.isNull(root.get("parentId")));
|
|
|
+ }else {
|
|
|
+ predicates.add(cb.equal(root.get("parentId"), parentId));
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ return cb.and(predicates.toArray(new Predicate[predicates.size()]));
|
|
|
+ };
|
|
|
+ PageRequest pageRequest = PageRequest.of(pageNo, pageSize, new Sort(Direction.ASC,"isFile").and(new Sort(Direction.DESC,"creationTime")));
|
|
|
+
|
|
|
+ Page<ResourceEntity> page = resourceRepo.findAll(specification, pageRequest);
|
|
|
+ Iterator<ResourceEntity> iterator = page.iterator();
|
|
|
+
|
|
|
+ List<ResourceDomain> resourceDomainList = Lists.newArrayList();
|
|
|
+
|
|
|
+ while (iterator.hasNext()) {
|
|
|
+ ResourceEntity next = iterator.next();
|
|
|
+ ResourceDomain bean = new ResourceDomain();
|
|
|
+ BeanUtils.copyProperties(next, bean);
|
|
|
+ bean.setFileUrl(upyunFileUrl+bean.getFilePath());
|
|
|
+ resourceDomainList.add(bean);
|
|
|
+ }
|
|
|
+ PageInfo<ResourceDomain> ret = new PageInfo<ResourceDomain>();
|
|
|
+ ret.setList(resourceDomainList);
|
|
|
+ ret.setTotal(page.getTotalElements());
|
|
|
+ return ret;
|
|
|
+ }
|
|
|
+
|
|
|
+ @ApiOperation(value = "新增资源")
|
|
|
+ @PostMapping("addFile")
|
|
|
+ public void addResource(@RequestParam @NotNull(message = "RootOrgId不能为空!") Long rootOrgId,@RequestParam @NotNull(message = "parentId不能为空!") Long parentId,
|
|
|
+ @RequestPart @NotNull(message = "上传文件不能为空!") MultipartFile dataFile) {
|
|
|
+ if(!isSuperAdmin()) {
|
|
|
+ User accessUser = getAccessUser();
|
|
|
+ if(!rootOrgId.equals(accessUser.getRootOrgId())) {
|
|
|
+ throw new StatusException("100021", "无效的请求");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ String fileName=dataFile.getOriginalFilename();
|
|
|
+ String regex="^[a-zA-Z\\-_0-9.]{1,50}$";
|
|
|
+ if(!Pattern.matches(regex, fileName)) {
|
|
|
+ throw new StatusException("100022", "文件名长度最大为50且只能包含字母,数字,'-','_','.'");
|
|
|
+ }
|
|
|
+ resourceService.addFile(rootOrgId,parentId,dataFile);
|
|
|
+ }
|
|
|
+ @ApiOperation(value = "删除资源")
|
|
|
+ @DeleteMapping("/{id}")
|
|
|
+ public void delResource(@PathVariable Long id) {
|
|
|
+ Optional<ResourceEntity> op=resourceRepo.findById(id);
|
|
|
+ if(op.isPresent()) {
|
|
|
+ ResourceEntity e=op.get();
|
|
|
+ if(!isSuperAdmin()) {
|
|
|
+ User accessUser = getAccessUser();
|
|
|
+ if(!e.getRootOrgId().equals(accessUser.getRootOrgId())) {
|
|
|
+ throw new StatusException("100031", "无效的请求");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if(!e.getIsFile()&&resourceRepo.countByRootOrgIdAndParentId(e.getRootOrgId(), id)>0) {
|
|
|
+ throw new StatusException("100032", "请先删除该目录下的文件及目录");
|
|
|
+ }
|
|
|
+ resourceRepo.deleteById(id);
|
|
|
+ }else {
|
|
|
+ throw new StatusException("100033", "文件或目录不存在");
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @ApiOperation(value = "新增目录")
|
|
|
+ @PostMapping("addDir")
|
|
|
+ public void addDir(@RequestBody ResourceDomain domain) {
|
|
|
+ if (StringUtils.isBlank(domain.getName())) {
|
|
|
+ throw new StatusException("100001", "目录名称不能为空");
|
|
|
+ }
|
|
|
+ String regex="^[a-zA-Z\\-_0-9]{1,50}$";
|
|
|
+ if(!Pattern.matches(regex, domain.getName())) {
|
|
|
+ throw new StatusException("100005", "目录名称必须为1-50位字母,数字,'-','_'组合");
|
|
|
+ }
|
|
|
+ if (domain.getRootOrgId()==null) {
|
|
|
+ throw new StatusException("100002", "RootOrgId不能为空");
|
|
|
+ }
|
|
|
+ if (domain.getParentId()==null) {
|
|
|
+ throw new StatusException("100003", "ParentId不能为空");
|
|
|
+ }
|
|
|
+ if(!isSuperAdmin()) {
|
|
|
+ User accessUser = getAccessUser();
|
|
|
+ if(!domain.getRootOrgId().equals(accessUser.getRootOrgId())) {
|
|
|
+ throw new StatusException("100004", "无效的请求");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ ResourceInfo info=new ResourceInfo();
|
|
|
+ BeanUtils.copyProperties(domain, info);
|
|
|
+ resourceService.addDir(info);
|
|
|
+ }
|
|
|
+
|
|
|
+}
|