package com.cool.bang.modules.system.service.impl;

import com.cool.bang.common.base.BaseServiceImpl;
import com.cool.bang.common.constant.Constants;
import com.cool.bang.modules.system.domain.po.Resource;
import com.cool.bang.modules.system.mapper.ResourceMapper;
import com.cool.bang.modules.system.service.IResourceService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * 文件
 *
 * @author PeiZhi Guo
 * 2022-09-08 15:12
 */
@Service
public class ResourceServiceImpl extends BaseServiceImpl<ResourceMapper, Resource> implements IResourceService {

    @Value("${cool.file-path}")
    private String filePath;

    @Override
    public List<Resource> queryByTableId(Long id,Integer wjlx) {
        return baseMapper.selectByTableId(id,wjlx);
    }


    @Override
    public String upload(MultipartFile file, String path) throws IOException {
        String yearMonthDay = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy/MM/dd"));

        byte[] data = file.getBytes();

        // 文件名
        String fn = file.getOriginalFilename();
        assert fn != null;
        String suffix = fn.substring(fn.lastIndexOf(".")).toLowerCase();
        String fileName = UUID.randomUUID() + suffix;

        String uploadPath = "";
        // 判断是否有次级目录，如果没有使用默认次级目录
        uploadPath = StringUtils.isEmpty(uploadPath) ? Constants.IMG : uploadPath;
        // 年月日字符串
        String relativePath = uploadPath +
                LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy/MM/dd")) + "/";
        // 生成目录
        File targetFile = new File(filePath + relativePath);
        if (!targetFile.exists()) {
            targetFile.mkdirs();
        }
        // 文件输出
        FileOutputStream out = new FileOutputStream(filePath + uploadPath + fileName);
        out.write(data);
        out.flush();
        out.close();
        return Constants.IMG_CHANGE + uploadPath + fileName; // 输出普通保存路径
    }



    @Override
    public Boolean deleteTableId(Long id,Integer type) {
            return baseMapper.deleteTableId(id,type)>0;
    }


    @Override
    public Boolean delete(String path) {
        System.out.println("csss"+path);
        File file = new File(path);
        if(file.exists()){
            file.delete();
            return true;
        }else{
            return false;
        }
    }

    /*
     * 添加文件
     */
    @Override
    public Boolean saveImg(List<Resource> resources, Long id) {
        if (null == resources){
            return true;
        }
        List<Resource> resourceList = new ArrayList<>();
        for (Resource list : resources) {
            if (list.getUrl() != null){
            list.setTableId(id);
            list.insertConfigure();
            resourceList.add(list);
            }
        }
        return baseMapper.insertBatch(resourceList)>0;
    }

}
