package com.ruixuan.salesLeads.utils;


import cn.hutool.core.text.csv.CsvWriter;
import com.ruixuan.common.utils.StringUtils;
import com.ruixuan.common.utils.file.FileUtils;
import de.siegmar.fastcsv.reader.CsvContainer;
import de.siegmar.fastcsv.reader.CsvReader;
import de.siegmar.fastcsv.reader.CsvRow;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.util.IOUtils;
import org.bouncycastle.util.test.Test;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ***************************************************
 *
 * @Auther: zianY
 * @Descipion: 销售线索-csv 格式读取
 * @CreateDate: 2022-05-26
 * ****************************************************
 */
@Slf4j
public class CsvImportUtil {


    /**
     * 读取csv数据
     * @param multipartFile
     * @return
     * @throws Exception
     */
    public static List<Map<String,String>> readCSV(MultipartFile multipartFile)throws Exception {
        //multipartFile 转 file
        File file = CsvImportUtil.multipartFile2File(multipartFile);

        CsvReader csvReader = new CsvReader();
        csvReader.setContainsHeader(true);
/*

//        CsvContainer csv = csvReader.read(file, StandardCharsets.UTF_8);
        CsvContainer csv = csvReader.read(file, Charset.forName("gbk"));
*/

//        String encoding = System.getProperty("file.encoding");
//        System.out.println("系统编码===="+encoding);
//        String os = System.getProperty("os.name");
//        System.out.println("system----------"+os);

        String charset = FileUtils.getFileEncoding(file);
        System.out.println("文件编码===="+charset);

        //FileUtils.addBom(file);

        InputStreamReader isr = new InputStreamReader(new FileInputStream(file),charset);
        CsvContainer csv = csvReader.read(isr);

        //行数据
        List<CsvRow> rows = csv.getRows();

        //List<String> header = csv.getHeader();

        List<Map<String,String>> lisMap = new ArrayList<>();

        for (int i = 0; i < rows.size(); i++) {

            Map<String, String> objectMap = new HashMap<>();

            Map<String,String> rowMap = rows.get(i).getFieldMap();

            rowMap.forEach((key,vlue)->{
                //System.out.println(key);

                if (StringUtils.isNotNull(key)){
                    String keyStr = key.replace(" ","");

                    if (StringUtils.isNotNull(vlue)){
                        vlue = vlue.replace(" ","");
                    }



                    if (keyStr.contains("家ID")){
                        objectMap.put("sellerId",vlue);
                    }
                    if (keyStr.contains("昵称")){
                        objectMap.put("sellerName",vlue);
                    }
                    if (keyStr.contains("粉丝")){
                        objectMap.put("fans",vlue);
                    }
                    if (keyStr.contains("行业")){
                        objectMap.put("industryId",vlue);
                    }
                    if (keyStr.contains("省份")){
                        objectMap.put("provinceId",vlue);
                    }
                    if (keyStr.contains("城市")){
                        objectMap.put("cityId",vlue);
                    }
                    if (keyStr.contains("联系")){
                        objectMap.put("telephone",vlue);
                    }
                    if (keyStr.contains("绑定服务商")){
                        //objectMap.put("providerFlag",StringUtils.isEmpty(vlue) ? "否" : vlue);
                        objectMap.put("providerFlag",vlue);
                    }
                    if (keyStr.contains("跃迁")){
                        //objectMap.put("transitionFlag",StringUtils.isEmpty(vlue) ? "否" : vlue);
                        objectMap.put("transitionFlag",vlue);
                    }
                    if ((keyStr).contains("30天")){
                        objectMap.put("NearlyThirtyDays",vlue);
                    }

                    if ((keyStr).contains("gmv") || keyStr.contains("GMV")){
                        objectMap.put("gmvMonth",keyStr.substring(0,keyStr.length()-3));
                        objectMap.put("gmv",vlue);
                    }

                    if (keyStr.contains("mtd") || keyStr.contains("MTD")){
                        objectMap.put("mtdDate",keyStr.substring(0,keyStr.length()-3));
                        objectMap.put("mtd",vlue);
                    }
                }


            });


            lisMap.add(objectMap);

        }


        return lisMap;

    }


    /**
     * multipartFile转File
     **/
    public static File multipartFile2File(MultipartFile multipartFile){
        File file = null;
        if (multipartFile != null){
            try {
                file=File.createTempFile("tmp", null);
                multipartFile.transferTo(file);
                System.gc();
                file.deleteOnExit();
            }catch (Exception e){
                e.printStackTrace();
                log.warn("multipartFile转File发生异常："+e);
            }
        }
        return file;
    }
}
