package com.inkasso.factoring.steelwaiter.service.impl;

import cn.hutool.core.io.FileUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.inkasso.factoring.file.entity.FileStorageDO;
import com.inkasso.factoring.file.service.FileService;
import com.inkasso.factoring.steelwaiter.dao.SteelWaiterMapper;
import com.inkasso.factoring.steelwaiter.entity.SellBill;
import com.inkasso.factoring.steelwaiter.entity.SellBillItem;
import com.inkasso.factoring.steelwaiter.service.SteelWaiterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

/**
 * 钢小二service
 *
 * @author liutao
 * @date 2023-10-26
 */
@Service
public class SteelWaiterServiceImpl implements SteelWaiterService {

    @Autowired
    private SteelWaiterMapper steelWaiterMapper;

    @Autowired
    private FileService fileStorageService;

    @Autowired
    private RestTemplate restTemplate;

    @Override
    public List<String> findAllBillNumbers(String supplierName, String keyword) {
        List<String> numbers = steelWaiterMapper.findAllBillNumbers(supplierName,keyword);
        return numbers;
    }

    @Override
    public List<SellBillItem> getGoodsByBillNumber(String billNumber, String companyName) {
        List<SellBillItem> list = steelWaiterMapper.getGoodsByBillNumber(billNumber,companyName);
        return list;
    }

    @Override
    public List<FileStorageDO> getFileByBillNumber(String billNumber, String companyName) {
//        SellBill sellBill = steelWaiterMapper.getBillByBillNumber(billNumber,companyName);
//        String attachmentURL = sellBill.getAttachmentUrl();
//        JSONObject result = restTemplate.getForObject(attachmentURL, JSONObject.class);
//        String status = result.getStr("status");
//        List<FileStorageDO> fileStorages = new ArrayList<>();
//        if (status.equals("success")) {
//            JSONArray jsonArray = (JSONArray) result.get("data");
//            if (jsonArray.size() > 0) {
//                for (Object o : jsonArray) {
//                    JSONObject fileObject = new JSONObject(o);
//                    String path = fileObject.getStr("Path");
//                    String fileName = fileObject.getStr("FileName");
//                    String suffix = FileUtil.getSuffix(fileName);
//                    String absolutePath = FileUtil.getAbsolutePath(fileName.replace(suffix, ""), suffix, FileModuleDict.ChildEnum.ACCEPTANCE_FILE_STEEL_WAITER);
//                    String filePath = FileUtil.getFilePath(fileName.replace(suffix, ""), suffix, FileModuleDict.ChildEnum.ACCEPTANCE_FILE_STEEL_WAITER);
//                    File file = urlTransferMultipartFile(path, absolutePath);
//                    FileStorageDO fileStorage = new FileStorage(file,filePath);
//                    fileStorageService.add(fileStorage);
//                    fileStorages.add(fileStorage);
//                }
//            }
//        }
//        return fileStorages;
        return null;
    }

    @Override
    public SellBill getSellBillByNumber(String sellBillNumber, String companyName) {
        return steelWaiterMapper.getBillByBillNumber(sellBillNumber,companyName);
    }


    private File urlTransferMultipartFile(String url, String absolutePath) {
        File file = null;
        URL urlfile;
        InputStream inStream = null;
        OutputStream os = null;
        try {
            file = new File(absolutePath);
            if (!file.getParentFile().exists()) {
                file.getParentFile().mkdirs();
            }
            //下载
            urlfile = new URL(url);
            inStream = urlfile.openStream();
            os = new FileOutputStream(file);

            int bytesRead = 0;
            byte[] buffer = new byte[8192];
            while ((bytesRead = inStream.read(buffer, 0, 8192)) != -1) {
                os.write(buffer, 0, bytesRead);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != os) {
                    os.close();
                }
                if (null != inStream) {
                    inStream.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return file;
    }
}
