package com.mobvista.apptag.service.impl;

import com.github.pagehelper.PageHelper;
import com.mobvista.apptag.entity.TreeTag;
import com.mobvista.apptag.mapper.TreeTagDao;
import com.mobvista.apptag.service.TreeTagService;
import com.mobvista.apptag.utils.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.regex.Pattern;

/**
 * @package: com.mobvista.apptag.service.impl
 * @author: wangjf
 * @date: 2020/12/3
 * @time: 2:15 下午
 * @email: jinfeng.wang@mobvista.com
 * @phone: 152-1062-7698
 */
@Service("treeTagService")
public class TreeTagServiceImpl implements TreeTagService {

    Pattern idIosPkgPtn = Pattern.compile("^id\\d+$");
    Pattern iosPkgPtn = Pattern.compile("^\\d+$");
    Pattern adrPkgPtn = Pattern.compile("^(?=^.{3,255}$)[-a-zA-Z0-9_][-a-zA-Z0-9_]{0,62}(\\.[a-zA-Z0-9_][-a-zA-Z0-9_]{0,62})+$");
    Pattern strPtn = Pattern.compile("^[-& a-zA-Z0-9]+$");

    @Autowired
    private TreeTagDao treeTagDao;

    @Override
    public List<TreeTag> list(Query query) {
        PageHelper.startPage(query.getOffset() / query.getLimit() + 1, query.getLimit());
        return treeTagDao.list(query.getSearch());
    }

    @Override
    public int count(Query query) {
        return treeTagDao.count(query.getSearch());
    }

    @Override
    public boolean writeToMysql(File file) throws IOException {
        String encoding = "UTF8";
        // 考虑到编码格式
        InputStreamReader read = new InputStreamReader(new FileInputStream(file), encoding);
        BufferedReader bufferedReader = new BufferedReader(read);
        String lineTxt;
        List<TreeTag> list = new ArrayList<>();
        while ((lineTxt = bufferedReader.readLine()) != null) {

            TreeTag treeTag = new TreeTag();
            String[] strings = lineTxt.replaceAll("\uFEFF", "").split(",", -1);
            String packageName = strings[0].trim();
            String firstTag = strings[1];
            String secondTag = strings[2];
            String thirdTag = strings[3];
            String platform = "";
            if (idIosPkgPtn.matcher(packageName).matches()) {
                packageName = packageName.replace("id", "");
            }
            if (iosPkgPtn.matcher(packageName).matches()) {
                platform = "ios";
            } else if (adrPkgPtn.matcher(packageName).matches()) {
                platform = "android";
            }
            treeTag.setPackageName(packageName);
            treeTag.setPlatform(platform);
            if (strPtn.matcher(firstTag).matches()) {
                treeTag.setFirstTag(firstTag);
            }
            if (strPtn.matcher(secondTag).matches()) {
                treeTag.setSecondTag(secondTag);
            } else {
                treeTag.setSecondTag("");
            }
            if (strPtn.matcher(thirdTag).matches()) {
                treeTag.setThirdTag(thirdTag);
            } else {
                treeTag.setThirdTag("");
            }
            treeTag.setUpdateTime(new Date());
            list.add(treeTag);
            if (idIosPkgPtn.matcher(packageName).matches()) {
                treeTag.setPackageName(packageName.replace("id", ""));
                list.add(treeTag);
            }
            if (iosPkgPtn.matcher(packageName).matches()) {
                treeTag.setPackageName("id" + packageName);
                list.add(treeTag);
            }
        }
        bufferedReader.close();
        read.close();
        if (batchInsert(list)) {
            return true;
        }
        return false;
    }

    public boolean batchInsert(List<TreeTag> list) {
        for (int i = 0; i <= list.size() / 1000; i++) {
            int startIndex = i * 1000;
            int endIndex;
            if ((i + 1) * 1000 > list.size()) {
                endIndex = list.size();
            } else {
                endIndex = (i + 1) * 1000;
            }
            if (!treeTagDao.batchInsert(list.subList(startIndex, endIndex))) {
                return false;
            }
        }
        return true;
    }
}
