CustomerServiceServiceImpl.java 3.57 KB
Newer Older
1 2 3 4
package cn.wisenergy.service.app.impl;

import cn.wisenergy.common.utils.R;
import cn.wisenergy.mapper.CustomerServiceMapper;
codezwjava's avatar
codezwjava committed
5
import cn.wisenergy.mapper.UsersMapper;
6
import cn.wisenergy.model.app.CustomerService;
codezwjava's avatar
codezwjava committed
7
import cn.wisenergy.model.app.User;
8 9 10 11 12 13
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;
14

15 16 17 18 19

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

21 22 23 24 25 26 27 28

@Service
@Slf4j
public class CustomerServiceServiceImpl implements CustomerServiceService {

    @Autowired
    private CustomerServiceMapper customerServiceMapper;

codezwjava's avatar
codezwjava committed
29 30 31
    @Autowired
    private UsersMapper usersMapper;

32 33 34 35
    @Value("${uploadFile.location}")
    private String uploadQRImagesLocation;

    @Override
codezwjava's avatar
codezwjava committed
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
    public CustomerService getServiceByRand(String userId){

        User byUserId = usersMapper.getByUserId(userId);
        String customerServiceId = byUserId.getCustomerServiceId();
        if (null == customerServiceId){
            CustomerService customerService = customerServiceMapper.randService();
            String wechatImgUrl = customerService.getWechatId();
            byUserId.setCustomerServiceId(wechatImgUrl);
            usersMapper.updateById(byUserId);
            return customerService;
        }else {
            CustomerService customerService = customerServiceMapper.selectbyWeChatId(customerServiceId);
            return customerService;
        }


52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
    }

    @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 {

69
        String basePath = request.getScheme() + "://" + request.getServerName()
70
                + ":" + request.getServerPort();
71 72 73 74 75 76 77

        Long time = System.currentTimeMillis();

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

89
        //入库地址
90
        String serviceUrl = "/upload/" + newName;
91 92
        CustomerService customerService = new CustomerService();
        customerService.setWechatId(wechatId);
93
        customerService.setWechatImgUrl(serviceUrl);
94 95 96 97
        customerServiceMapper.insert(customerService);
        return R.ok("上传成功");
    }
}