CustomerServiceServiceImpl.java 2.93 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
package cn.wisenergy.service.app.impl;

import cn.wisenergy.common.utils.R;
import cn.wisenergy.mapper.CustomerServiceMapper;
import cn.wisenergy.model.app.CustomerService;
import cn.wisenergy.service.app.CustomerServiceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
@Slf4j
public class CustomerServiceServiceImpl implements CustomerServiceService {

    @Autowired
    private CustomerServiceMapper customerServiceMapper;

    @Value("${uploadFile.location}")
    private String uploadQRImagesLocation;

    @Override
    public CustomerService getServiceByRand(){
        return customerServiceMapper.randService();
    }

    @Override
    public void setWeChatQRImg(String wechatId, String wechatImgUrl) {
        CustomerService customerService = new CustomerService();
        customerService.setWechatId(wechatId);
        customerService.setWechatImgUrl(wechatImgUrl);
        customerService.setCreateTime(new Date());
        customerService.setUpdateTime(new Date());
        customerServiceMapper.insert(customerService);
    }
    /**
     *  专属客服二维码图片文件上传
     */
    @Override
    public R uploadImage(MultipartFile file, HttpServletRequest request, String wechatId) throws IOException {

50
        String basePath = request.getScheme() + "://" + request.getServerName()
51
                + ":" + request.getServerPort();
52 53 54 55 56 57 58

        Long time = System.currentTimeMillis();

        String originalFilename = file.getOriginalFilename();//文件原始名称
        String suffixName = originalFilename.substring(originalFilename.lastIndexOf("."));//从最后一个.开始截取。截取zxName的后缀名
        String newName = time+suffixName; //文件新名称
        //设置文件存储路径,可以存放在你想要指定的路径里面
59 60
        String rootPath="/opt/upload/video/"; //上传图片存放位置
        String filePath = rootPath + newName;
61 62 63 64 65 66 67 68 69
        File newFile = new File(filePath);
        //判断目标文件所在目录是否存在
        if(!newFile.getParentFile().exists()){
            //如果目标文件所在的目录不存在,则创建父目录
            newFile.getParentFile().mkdirs();
        }
        //将内存中的数据写入磁盘
        file.transferTo(newFile);

70 71
        //入库地址
        String serviceUrl = basePath + "/upload/" + newName;
72 73
        CustomerService customerService = new CustomerService();
        customerService.setWechatId(wechatId);
74
        customerService.setWechatImgUrl(serviceUrl);
75 76 77 78
        customerServiceMapper.insert(customerService);
        return R.ok("上传成功");
    }
}