package com.heyu.api.controller;


import com.alibaba.fastjson.JSON;
import com.heyu.api.data.annotation.NotIntercept;
import com.heyu.api.data.constants.RedisConstans;
import com.heyu.api.data.dao.api.ApiPostCodeDao;
import com.heyu.api.data.dao.calca.CalcaAccountDao;
import com.heyu.api.data.entity.api.ApiPostCodeEntity;
import com.heyu.api.data.entity.calca.CalcaAccountEntity;
import com.heyu.api.data.utils.MD5Utils;
import com.heyu.api.data.utils.R;
import com.heyu.api.data.utils.RedisUtils;
import com.heyu.api.data.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.stream.Stream;

@Slf4j
@RestController
@RequestMapping("/app/test")
@NotIntercept
public class TestController {

    @Autowired
    private CalcaAccountDao calcaAccountDao;

    @Autowired
    private RedisUtils redisUtils;


    @Autowired
    private ApiPostCodeDao apiPostCodeDao;


    @Autowired
    private RabbitTemplate rabbitTemplate;



    public static  ExecutorService cachedThreadPool = Executors.newFixedThreadPool(4);

    @Value("${eb.config.rabbitQueue.postCodeData}")
    private String accountLogNameQueue;


    // http://localhost:8888/app/test/contractFile
    @RequestMapping("/contractFile")
    public R contractFile() {
        CalcaAccountEntity calcaAccountEntity = calcaAccountDao.selectCalcaAccountById(4L);
        System.out.println(JSON.toJSONString(calcaAccountEntity));
        return R.ok();
    }

    // http://localhost:8888/app/test/redis
    @RequestMapping("/redis")
    public R redis() {
        redisUtils.set("hello", "world", RedisConstans.SECOND_OF_THREE);
        String a = redisUtils.get("hello");
        System.out.println("xaaa:" + a);
        return R.ok();
    }


    // http://localhost:8888/app/test/email?key=AX
    @RequestMapping("/email")
    public R email(String key) throws Exception {
        String fileName = "/Users/quyixiao/Desktop/" + key + "/" + key + ".txt";
        // 读取文件内容到Stream流中，按行读取
        Stream<String> lines = Files.lines(Paths.get(fileName));
        StringBuffer sb = new StringBuffer();
        // 随机行顺序进行数据处理
        lines.forEach(ele -> {
            int max_length = 32;
            String value = "";
            int index = -1;
            String a[] = ele.split("\t");
            String b[] = new String[12];
            if (a != null && a.length == 11) {
                for (int i = 0; i < a.length; i++) {
                    b[i] = a[i];
                }
                b[11] = "";
                a = b;
            }

            if (a != null && a.length == 12) {
                try {
                    sb.append("INSERT INTO api_post_code ( is_delete, create_time, modify_time, param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12) VALUES\n" +
                            "                            ( 0, '2025-03-11 00:36:20', '2025-03-11 00:36:20'" +
                            ", '" + a[0] + "'" +
                            ", '" + a[1] + "'" +
                            ", '" + a[2] + "'" +
                            ", '" + a[3] + "'" +
                            ", '" + a[4] + "'" +
                            ", '" + a[5] + "'" +
                            ", '" + a[6] + "'" +
                            ", '" + a[7] + "'" +
                            ", '" + a[8] + "'" +
                            ", '" + a[9] + "'" +
                            ", '" + a[10] + "'" +
                            ", '" + a[11] + "');");

                    sb.append("\n");

                    if (a[0].length() > 32) {
                        max_length = a[0].length();
                        value = a[0];
                        index = 0;
                        System.out.println(" max_length:" + max_length + ",value:" + value + ",index:" + index);
                    }
                    if (a[1].length() > 64) {
                        max_length = a[1].length();
                        value = a[1];
                        index = 1;
                        System.out.println(" max_length:" + max_length + ",value:" + value + ",index:" + index);
                    }
                    if (a[2].length() > 255) {
                        max_length = a[2].length();
                        value = a[2];
                        index = 2;
                        System.out.println(" max_length:" + max_length + ",value:" + value + ",index:" + index);
                    }

                    if (a[3].length() > 64) {
                        max_length = a[3].length();
                        value = a[3];
                        index = 3;
                        System.out.println(" max_length:" + max_length + ",value:" + value + ",index:" + index);
                    }

                    if (a[4].length() > 64) {
                        max_length = a[4].length();
                        value = a[4];
                        index = 4;
                        System.out.println(" max_length:" + max_length + ",value:" + value + ",index:" + index);
                    }
                    if (a[5].length() > 64) {
                        max_length = a[5].length();
                        value = a[5];
                        index = 5;
                        System.out.println(" max_length:" + max_length + ",value:" + value + ",index:" + index);
                    }
                    if (a[6].length() > 64) {
                        max_length = a[6].length();
                        value = a[6];
                        index = 6;
                        System.out.println(" max_length:" + max_length + ",value:" + value + ",index:" + index);
                    }
                    if (a[7].length() > 64) {
                        max_length = a[7].length();
                        value = a[7];
                        index = 7;
                        System.out.println(" max_length:" + max_length + ",value:" + value + ",index:" + index);
                    }
                    if (a[8].length() > 64) {
                        max_length = a[8].length();
                        value = a[8];
                        index = 8;
                        System.out.println(" max_length:" + max_length + ",value:" + value + ",index:" + index);
                    }
                    if (a[9].length() > 32) {
                        max_length = a[9].length();
                        value = a[9];
                        index = 9;
                        System.out.println(" max_length:" + max_length + ",value:" + value + ",index:" + index);
                    }
                    if (a[10].length() > 32) {
                        max_length = a[10].length();
                        value = a[10];
                        index = 10;
                        System.out.println(" max_length:" + max_length + ",value:" + value + ",index:" + index);
                    }
                    if (a[11].length() > 32) {
                        max_length = a[11].length();
                        value = a[11];
                        index = 11;
                        System.out.println(" max_length:" + max_length + ",value:" + value + ",index:" + index);
                    }


                } catch (Exception e) {
                    log.error(e.getMessage());
                }
            } else {
                log.info("数据格式不正确 ： " + ele + ",length" + a.length);
            }
        });
        String newFile = "/Users/quyixiao/Desktop/post_code.sql";
        FileOutputStream fos = new FileOutputStream(newFile);

        Writer out = new OutputStreamWriter(fos, "UTF-8");
        out.write(sb.toString());
        out.close();
        fos.close();
        return R.ok();
    }



    public static int count_flag = 0;


    // http://localhost:8888/app/test/email2?key=AD
    @RequestMapping("/email2")
    public R email2(String key) throws Exception {
        File file = new File("/mnt/admin/test");
        List<String> list = new ArrayList<String>();
        showList(list, file);
        log.info("----------------x---------");

        for(String a: list){
            log.info("===============>" + a );
            doSend(a);
        }
        return R.ok();
    }


    public String getValue(String value) {
        if (StringUtils.isBlank(value)) {
            return "";
        }
        return value;
    }


    private static void showList(List<String> list, File file) {
        if (file.isDirectory()) {//如果是目录
            File[] listFiles = file.listFiles();//获取当前路径下的所有文件和目录,返回File对象数组
            for (File f : listFiles) {//将目录内的内容对象化并遍历
                String fName = f.getPath();
                System.out.println("文件:" + fName);
                if (fName.endsWith(".txt") & !fName.contains("readme.txt")) {
                    list.add(fName);
                }
                if (f.isDirectory()) {
                    showList(list, new File(fName));
                }
            }
        } else if (file.isFile()) {//如果是文件
            String fileName = file.getPath();
            System.out.println("文件:" + fileName);
            if (fileName.endsWith(".txt") & !fileName.contains("readme.txt")) {
                list.add(fileName);
            }
        }
    }


    public void doSend(String fileName) throws Exception{
        count_flag = 0;
        // 读取文件内容到Stream流中，按行读取
        Stream<String> lines = Files.lines(Paths.get(fileName));
        StringBuffer sb = new StringBuffer();

        // 随机行顺序进行数据处理
        lines.forEach(ele -> {

            String a[] = ele.split("\t");
            String b[] = new String[12];
            if (a != null && a.length == 11) {
                for (int i = 0; i < a.length; i++) {
                    b[i] = a[i];
                }
                b[11] = "";
                a = b;
            }


            if (a != null && a.length == 12) {
                ApiPostCodeEntity apiPostCodeEntity = new ApiPostCodeEntity();
                String param1 = getValue(a[0]);
                String param2 = getValue(a[1]);
                String param3 = getValue(a[2]);
                String param4 = getValue(a[3]);
                String param5 = getValue(a[4]);
                String param6 = getValue(a[5]);
                String param7 = getValue(a[6]);
                String param8 = getValue(a[7]);
                String param9 = getValue(a[8]);
                String param10 = getValue(a[9]);
                String param11 = getValue(a[10]);
                String param12 = getValue(a[11]);
                apiPostCodeEntity.setParam1(param1);
                apiPostCodeEntity.setParam2(param2);
                apiPostCodeEntity.setParam3(param3);
                apiPostCodeEntity.setParam4(param4);
                apiPostCodeEntity.setParam5(param5);
                apiPostCodeEntity.setParam6(param6);
                apiPostCodeEntity.setParam7(param7);
                apiPostCodeEntity.setParam8(param8);
                apiPostCodeEntity.setParam9(param9);
                apiPostCodeEntity.setParam10(param10);
                apiPostCodeEntity.setParam11(param11);
                apiPostCodeEntity.setParam12(param12);
                String param = param1
                        + param2
                        + param3
                        + param4
                        + param5
                        + param6
                        + param7
                        + param8
                        + param9
                        + param10
                        + param11
                        + param12;
                String md5 = MD5Utils.encode(param);
                apiPostCodeEntity.setMd5Unique(md5);
                log.info("=-------------------->"+ count_flag);
                rabbitTemplate.convertAndSend(accountLogNameQueue, JSON.toJSONString(apiPostCodeEntity));
            }
        });
    }
}



