/**
 * CLD数据同步计划任务
 *
 * @author CaiAoLin
 * @date 2017/7/26
 * @version
 */
import ManagerModel from "../modules/users/models/manager_model";
import DBManager from "../config/db/db_manager";
import config from "../config/config";
import fs from "fs";

config.setToLocalDb();
let connect = DBManager.connect();
let counterFile = "./counter.txt";

let main = async function () {
    // 读取计数器
    let id = fs.readFileSync(counterFile, "utf-8");
    let CLDAsyncUrl = 'http://cld.smartcost.com.cn/api/async-user';

    let managerModel = new ManagerModel();
    try {
        // 生成加密token
        let [encryptToken, time] = managerModel.generateCLDToken();
        let postData = {
            time: time,
            token: encryptToken,
            last_id: id,
            app: 'yanghu'
        };
        let postOption = {
            url: CLDAsyncUrl,
            form: postData,
            encoding: 'utf8'
        };
        let responseData = await managerModel.CLDRequest(postOption);
        let userList = responseData;
        if (userList.length <= 0) {
            throw {code: 30001, msg: "没有数据"};
        }

        let usernameList = [];
        for (let tmp of userList) {
            usernameList.push(tmp.username);
        }
        // 查找对应的数据
        let condition = {username: {"$in": usernameList}};
        let managerData = await managerModel.findDataByCondition(condition, null, false, 'username');

        // 准备插入数据库
        let current = new Date().getTime();
        let insertData = [];
        let lastId = 0;
        for (let tmp of userList) {
            if (managerData[tmp.username] !== undefined) {
                continue;
            }
            let userTmp = {
                username: tmp.username,
                password: '',
                token: '',
                create_time: current,
                office: tmp.office,
                can_login: 0
            };
            insertData.push(userTmp);
            lastId = tmp.sid;
        }

        if (insertData.length <= 0) {
            throw '没有需要操作的数据';
        }
        managerModel.setScene('cldInsert');
        let insertResult = await managerModel.createManager(insertData);
        if(insertResult.length > 0) {
            // 成功则更新id
            fs.writeFileSync(counterFile, lastId, "utf-8");
        }

    } catch (error) {
        // 重置起始标识（如果需要循环才开启，开启后后台删除则过一段时间又会插入回数据库）
        // let code = error.code !== undefined ? error.code : '';
        // if (code === 30001) {
        //     fs.writeFileSync(counterFile, 0, "utf-8");
        // }
        console.log(error);
    }


    // 用完关闭
    connect.disconnect();
};

main();