package com.lovenav.service.serviceImpl;

import com.lovenav.dao.AttachmentDao;
import com.lovenav.dao.ConfigDao;
import com.lovenav.entity.Attachment;
import com.lovenav.entity.Config;
import com.lovenav.service.AttachmentService;
import com.lovenav.utils.MD5Utils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ResourceUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Slf4j
@Service
public class AttachmentServiceImpl implements AttachmentService {
    @Autowired
    AttachmentDao attachmentDao;

    @Override
    public HashMap<String,String> upload(MultipartFile file,String cate,Long id) throws IOException {
        HashMap<String, String> map = storeFile(file,cate,id);
        return map;
    }
    @Override
    public HashMap<String, String> storeFile(MultipartFile multipartFile,String cate,Long id) throws IOException {
        HashMap<String,String> map = new HashMap<>();
        Attachment attachment=new Attachment();
        File path = null;
        try {
            path = new File(ResourceUtils.getURL("classpath:").getPath());
            File upload = new File(path.getAbsolutePath(),"static/"+cate+"/");
            Date date=new Date();

            DateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
            DateFormat format1 = new SimpleDateFormat("yyyyMM");
            String date1=format.format(date);
            String fileName = multipartFile.getOriginalFilename();//文件全名
            String date2=format1.format(date);
            File parentDir = new File(upload.getAbsolutePath()+"/" + date2);
            if(!upload.exists()){
                upload.mkdirs();
            }
            if(!parentDir.exists()){
                parentDir.mkdirs();
            }
            String suffix = suffix(fileName);//文件后缀
            String finalFileName=UUID.randomUUID().toString().replaceAll("-","");
            String relPath = "/" + date2 + "/"  + finalFileName + suffix;
            File fileUp = new File(upload.getAbsolutePath()+ relPath);

            attachment.setCreatetime(date);
            attachment.setFileName(finalFileName);
            attachment.setPath(upload.getAbsolutePath()+ "/" + date2 + "/");
            attachment.setSuffix(suffix);
            attachment.setMd5(MD5Utils.md5(upload.getAbsolutePath()+ relPath));
            attachment.setStorage("local");
            attachment.setId(id);
            attachmentDao.insertSelective(attachment);
            map.put("id", String.valueOf(attachment.getId()));
            multipartFile.transferTo(fileUp);
            map.put("url", "/img" + relPath);
            log.info(relPath);
            return map;
        } catch (FileNotFoundException e) {
            throw e;
        } catch (IOException e) {
            throw e;
        }
    }

    @Override
    public Attachment selectAttachment(Long id) {
        Attachment attachment=attachmentDao.selectByPrimaryKey(id);
        return attachment;
    }

    private static String suffix(String fileName) {
        int i = fileName.lastIndexOf('.');
        return i == -1 ? "" : fileName.substring(i);
    }
}
