package com.bonus.canteen.core.common.sysfile.service.impl;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bonus.common.core.exception.ServiceException;
import com.bonus.canteen.core.common.enums.FileUploadSourceEnum;
import com.bonus.canteen.core.common.sysfile.mapper.SysFileMapper;
import com.bonus.canteen.core.common.sysfile.model.SysFile;
import com.bonus.canteen.core.common.sysfile.service.SysFileService;
import com.bonus.canteen.core.common.sysfile.vo.SysFileUploadVO;
import com.bonus.canteen.core.common.utils.TenantContextHolder;
import com.bonus.common.houqin.oss.OssProperties;
import com.bonus.common.houqin.oss.service.OssTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;


@Service
public class SysFileServiceImpl extends ServiceImpl<SysFileMapper, SysFile> implements SysFileService {
   private static final Logger log = LoggerFactory.getLogger(SysFileServiceImpl.class);
   @Resource
   @Lazy
   private OssProperties ossProperties;
   @Resource
   @Lazy
   private OssTemplate minioTemplate;

   @Override
   public SysFileUploadVO uploadFile(MultipartFile file, String uploadKey) {
      Long merchantId = TenantContextHolder.getTenantId();
      String merchantIdString;
      if (merchantId != null && merchantId != -1L && merchantId != 1L) {
         merchantIdString = String.valueOf(merchantId);
      } else {
         merchantIdString = "unknown";
      }

      String fileName;
      if (FileUploadSourceEnum.DEVICE_LOG.key().equalsIgnoreCase(uploadKey)) {
         fileName = file.getOriginalFilename();
      } else {
         String var10000 = IdUtil.simpleUUID();
         fileName = var10000 + "." + FileUtil.extName(file.getOriginalFilename());
      }

      String fileUrl = merchantIdString + "/" + uploadKey + "/" + fileName;
      SysFileUploadVO result = new SysFileUploadVO();
      String bucketName = this.ossProperties.getBucketName();
      String fileNameUrl = "/" + bucketName + "/" + fileUrl;
      result.setBucketName(bucketName);
      result.setFileNameUrl(fileNameUrl);
      result.setUrl(String.format("/admin/api/v1/sysfile/%s/%s", bucketName, fileUrl));
      result.setOriginalFileName(file.getOriginalFilename());

      try {
         log.error("***上传参数bucketName=" + bucketName + ",fileUrl=" + fileUrl);
         this.minioTemplate.putObject(bucketName, fileUrl, file.getInputStream());
         this.fileLog(file, fileUrl);
         return result;
      } catch (Exception var11) {
         log.error("***上传失败", var11);
         throw new ServiceException("上传失败!!!");
      }
   }

   private void fileLog(MultipartFile file, String fileName) {
      SysFile sysFile = new SysFile();
      String original = file.getOriginalFilename();
      sysFile.setFileName(fileName);
      sysFile.setOriginal(original);
      sysFile.setFileSize(file.getSize());
      sysFile.setType(FileUtil.extName(original));
      sysFile.setBucketName(this.ossProperties.getBucketName());
      sysFile.setCreateUser("");
      this.save(sysFile);
   }
}
