package com.zfire.jiasm.syncdata.base;

import com.zfire.jiasm.syncdata.plus.entity.Brand;
import com.zfire.jiasm.syncdata.plus.service.BrandService;
import com.zfire.jiasm.syncdata.plus.service.BrandWebsitService;
import com.zfire.jiasm.syncdata.plus.service.CustomerInformationWebsitService;
import com.zfire.jiasm.syncdata.plus.service.WebsitOrderSignService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.BufferedInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 基础服务实现类
 * @author yaozx
 * @since 2023-01-01
 */
@Slf4j
@Service
public class BaseService {
    @Resource
    BrandService brandService;



     public Brand getBrand() {
        Brand brand = brandService.lambdaQuery().eq(Brand::getBrand,"格力")
                .last("limit 1")
                .one();
        return brand;
    }

    public void saveToFile(String desUrl,String id){
        FileOutputStream fos = null;
        BufferedInputStream bis = null ;
        HttpURLConnection httpUrl =null;
        URL url =null;
        int BUFFER_SIZE = 1024;
        byte[] buf = new byte[BUFFER_SIZE];
        int size = -1;

        try {
            url = new URL(desUrl);
            httpUrl = (HttpURLConnection) url.openConnection();
            httpUrl.connect();
            bis = new BufferedInputStream(httpUrl.getInputStream());
            fos = new FileOutputStream(id);
            while (true){
                size = bis.read(buf);
                if (size == -1){
                    break;
                }
                fos.write(buf,0,size);
            }
            fos.flush();
        } catch (IOException e){
        } catch (ClassCastException e){
        } finally {
            try{
                fos.close();
                bis.close();
                httpUrl.disconnect();
            }catch (IOException e){
            }catch (NullPointerException e){
            }
        }
    }


    public void initFile(String id){
        FileOutputStream fos = null;
        BufferedInputStream bis = null ;
        int BUFFER_SIZE = 1024;
        byte[] buf = new byte[BUFFER_SIZE];
        int size = -1;

        try {
            bis = new BufferedInputStream(this.getClass().getResourceAsStream("/static/bx.jpg"));
            fos = new FileOutputStream(id);
            while (true){
                size = bis.read(buf);
                if (size == -1){
                    break;
                }
                fos.write(buf,0,size);
            }
            fos.flush();
        } catch (IOException e){
        } catch (ClassCastException e){
        } finally {
            try{
                fos.close();
                bis.close();
            }catch (IOException e){
            }catch (NullPointerException e){
            }
        }
    }


    public String dateSub(Date d) {
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.US);
        long localTime = d.getTime()-8*60*60*1000;
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(localTime);
        Date utcDate = new Date(calendar.getTimeInMillis());
        return sf.format(utcDate);
    }

    public String dateAdd(Date d) {
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.US);
        long localTime = d.getTime()-8*60*60*1000;
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(localTime);
        Date utcDate = new Date(calendar.getTimeInMillis());
        return sf.format(utcDate);
    }

    public Date utcToDateStr(String utcTime) throws ParseException {
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.US);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        TimeZone utcZone = TimeZone.getTimeZone("UTC");
        sf.setTimeZone(utcZone);
        Date date = null;
        String dateTime = "";
        try {
            date = sf.parse(utcTime);
            dateTime = sdf.format(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return sdf.parse(dateTime);
    }


}
