|
@@ -0,0 +1,138 @@
|
|
|
+package com.gis.service.impl;
|
|
|
+
|
|
|
+import cn.hutool.core.date.DateUtil;
|
|
|
+import cn.hutool.core.io.FileUtil;
|
|
|
+import cn.hutool.extra.qrcode.QrCodeUtil;
|
|
|
+import com.gis.common.constant.MsgCode;
|
|
|
+import com.gis.common.exception.BaseRuntimeException;
|
|
|
+import com.gis.common.util.FileUtils;
|
|
|
+import com.gis.common.util.Result;
|
|
|
+import com.gis.domain.dto.PageDateDto;
|
|
|
+import com.gis.domain.dto.SceneAudioDto;
|
|
|
+import com.gis.domain.po.SceneAudioEntity;
|
|
|
+import com.gis.mapper.IBaseMapper;
|
|
|
+import com.gis.mapper.SceneAudioMapper;
|
|
|
+import com.gis.service.SceneAudioService;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.springframework.beans.BeanUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
+import tk.mybatis.mapper.entity.Condition;
|
|
|
+
|
|
|
+import java.io.File;
|
|
|
+import java.io.IOException;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.HashMap;
|
|
|
+
|
|
|
+
|
|
|
+/**
|
|
|
+ * Created by owen on 2021/6/2 0011 16:16
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+@Service
|
|
|
+public class SceneAudioServiceImpl extends IBaseServiceImpl<SceneAudioEntity, Long> implements SceneAudioService {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private SceneAudioMapper entityMapper;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public IBaseMapper<SceneAudioEntity, Long> getBaseMapper() {
|
|
|
+ return this.entityMapper;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Result<SceneAudioEntity> search(PageDateDto param) {
|
|
|
+ Condition condition = new Condition(SceneAudioEntity.class);
|
|
|
+ String searchKey = param.getSearchKey();
|
|
|
+ if (StringUtils.isNotBlank(searchKey)) {
|
|
|
+ condition.and().orLike("name", "%" + searchKey + "%");
|
|
|
+ }
|
|
|
+ String startTime = param.getStartTime();
|
|
|
+ String endTime = param.getEndTime();
|
|
|
+ if (StringUtils.isNotBlank(startTime) ) {
|
|
|
+ condition.and().andBetween("create_time", startTime, endTime);
|
|
|
+ }
|
|
|
+
|
|
|
+ return Result.success(this.findAll(condition, param.getPageNum(), param.getPageSize()));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Result saveEntity(SceneAudioDto param) {
|
|
|
+ SceneAudioEntity entity = null;
|
|
|
+ Long id = param.getId();
|
|
|
+ if (id == null) {
|
|
|
+ entity = new SceneAudioEntity();
|
|
|
+ BeanUtils.copyProperties(param, entity);
|
|
|
+ entity.setQrCode(createQrCode(param.getAudio()));
|
|
|
+
|
|
|
+ // 默认值
|
|
|
+ this.save(entity);
|
|
|
+ } else {
|
|
|
+ entity = this.findById(id);
|
|
|
+ if (entity == null) {
|
|
|
+ return Result.failure("对象id不存在");
|
|
|
+ }
|
|
|
+
|
|
|
+ BeanUtils.copyProperties(param, entity);
|
|
|
+ entity.setUpdateTime(new Date());
|
|
|
+ this.update(entity);
|
|
|
+
|
|
|
+ }
|
|
|
+ return Result.success(entity);
|
|
|
+ }
|
|
|
+
|
|
|
+ private String createQrCode(String path){
|
|
|
+ String domian = configConstant.serverDomain + path;
|
|
|
+ log.info("domina: {}", domian);
|
|
|
+ String time = DateUtil.format(new Date(), "yyyyMMdd_HHmmssSSS");
|
|
|
+ String middlePath = "sceneAudio/qrCode/";
|
|
|
+
|
|
|
+ String basePath = configConstant.serverBasePath + middlePath;
|
|
|
+ // 二维码工具不会自动创建目录
|
|
|
+ if (!FileUtil.isDirectory(basePath)) {
|
|
|
+ FileUtil.mkdir(basePath);
|
|
|
+ }
|
|
|
+ String url = middlePath + time + ".jpg";
|
|
|
+ String savePath = configConstant.serverBasePath + url;
|
|
|
+ QrCodeUtil.generate(domian, 200, 200, new File(savePath));
|
|
|
+ return url;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Result upload(MultipartFile file, String type) {
|
|
|
+
|
|
|
+ if (!FileUtils.checkFile(file)) {
|
|
|
+ throw new BaseRuntimeException(MsgCode.e3002, "非法文件");
|
|
|
+ }
|
|
|
+ String middlePath = "sceneAudio/" + type + "/";
|
|
|
+ String basePath = configConstant.serverBasePath + middlePath;
|
|
|
+ String originalFilename = file.getOriginalFilename();
|
|
|
+ String time = DateUtil.format(new Date(), "yyyyMMdd_HHmmssSSS");
|
|
|
+ String suffix = StringUtils.substringAfterLast(originalFilename, ".");
|
|
|
+ String fileName = time + "." + suffix;
|
|
|
+
|
|
|
+ String savePath = basePath + fileName;
|
|
|
+ try {
|
|
|
+ FileUtil.writeFromStream(file.getInputStream(), savePath);
|
|
|
+ HashMap<Object, Object> result = new HashMap<>();
|
|
|
+
|
|
|
+ // 前端访问url: ip + port/data + path, 需要使用data 前缀访问, data让前端自己配置
|
|
|
+// Object urlPath = configConstant.serverUrlPrefix + middlePath + fileName;
|
|
|
+ Object urlPath = middlePath + fileName;
|
|
|
+ result.put("fileName", originalFilename);
|
|
|
+ result.put("path", urlPath);
|
|
|
+ log.info("upload result: {}", result);
|
|
|
+ return Result.success(result);
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+}
|