import json
import multiprocessing
import os


def init_shunxu():

    with open('cedian.txt', 'r') as f:
        datas = [i.strip() for i in f.readline() if i.strip()]

    result_map = dict()

    for data in datas:
        da = data.split('\t')
        point = Mesurepoint(da[0], da[2], da[4], da[5], da[6])

        if point.wind_factory in result_map.keys():
            if point.wind_no in result_map[point.wind_factory].keys():
                result_map[point.wind_factory][point.wind_no].append(point)
            else:
                result_map[point.wind_factory][point.wind_no] = [point]
        else:
            result_map[point.wind_factory] = {point.wind_no: [point]}

    return result_map


class Mesurepoint(object):

    def __init__(self, wind_factory, wind_no, cn_name, shuxun, en_name):
        self.wind_factory = wind_factory
        self.wind_no = wind_no
        self.cn_name = cn_name
        self.shuxun = int(shuxun) + 16388
        self.en_name = en_name


def mult_solver(wind_factory, base_data, result_map, is_first, seconds):
    for wind_no in result_map[wind_factory].keys():
        shunxuhaos = [point.shuxun for point in result_map[wind_factory][wind_no]]
        shunxuhaos.insert(0, '-1')
        os.makedirs(os.path.join('104', wind_factory, str(seconds) + '秒'), exist_ok=True)
        with open(os.path.join('104', wind_factory, str(seconds) + '秒', '风机号' + str(wind_no) + '.csv'),
                  'a') as f:
            if is_first:
                cols = ['time']
                for point in result_map[wind_factory][wind_no]:
                    cols.append(point.cn_name)

                f.write(",".join(cols))
                f.write('\n')

            datas = [str(base_data[str(i)]) for i in shunxuhaos]
            f.write(",".join(datas))
            f.write('\n')


def save_csv(base_data, seconds, is_first=False):
    wind_factories = result_map.keys()
    with multiprocessing.Pool(6) as pool:
        pool.starmap(mult_solver, [(i, base_data, result_map, is_first, seconds) for i in wind_factories])


def generate_data(base_data, all_datas, seconds):
    save_csv(base_data, seconds, True)
    now_time = base_data['-1']
    for data in all_datas:
        data['-1'] = data['-1'].split('.')[0]
        base_data.update(data)
        if now_time != data['-1'] and int(data['-1'][-2:]) % seconds == 0:
            now_time = data['-1']
            save_csv(base_data, seconds)


if __name__ == '__main__':

    file_path = r'C:\Users\wzl\Desktop\241\2404 (复件)\20250310.txt'

    result_dict = dict()

    begin_end = [16388, 22744]
    base_data = {}
    for i in range(begin_end[0], begin_end[1] + 1):
        base_data[str(i)] = 0

    base_data['-1'] = ''

    result_map = init_shunxu()

    all_datas = list()
    with open(file_path, 'r') as f:
        line = f.readline()
        while line:
            fixed_str = line.replace('{', '{"').replace(':"', '":"').replace(',', ',"')
            line_data = {}
            line_data = json.loads(fixed_str)
            all_datas.append(line_data)
            line = f.readline()

    generate_data(base_data, all_datas, 1)

    generate_data(base_data, all_datas, 3)

    generate_data(base_data, all_datas, 5)
