const system = require("../../../system");
const ServiceBase = require("../../sve.base");
const xlsx = require('node-xlsx');
const fs = require("fs");

class WorkloadService extends ServiceBase {
    constructor() {
        super("common", ServiceBase.getDaoName(WorkloadService));
        this.restClient = system.getObject("util.restClient");
    }

    async paramXlsx(userId, ossurl) {
        var result = {
            code: 0,
            msg: "",
            list: [],
        };
        var filePath = "/tmp/esettle_offline_" + userId + ".xlsx";
        try {
            await this.restClient.execDownload("'" + ossurl + "'", filePath);
            var sheets = xlsx.parse(filePath);
            var rows = sheets[0].data;
            var resu = [];
            for (let i = 0; i < rows.length; i++) {
                if (2 > i) {
                    continue;
                }
                var heads = rows[0];
                var cells = rows[i];
                for (let j = 0; j < cells.length; j++) {
                    if (cells[j] === undefined) {
                        result.msg = "excel序号为" + cells[0] + "的" + heads[j] + "不能为空";
                        return result;
                    }
                }
                var data = {};
                data.username = "" + cells[1];
                data.iphone = "" + cells[2];
                data.id_nu = "" + cells[3];
                data.card_nu = "" + cells[4];
                data.send_amount = "" + cells[5];
                data.created_time = "" + new Date(1900, 0, cells[6]);
                data.updated_time = "" + new Date(1900, 0, cells[7]);
                data.workload = "" + cells[8];
                resu.push(data)
            }
            await this.dao.model.bulkCreate(resu, {fields: ["username", "iphone", "id_nu", "card_nu", "send_amount", "created_time", "updated_time", "workload"]});
            result.code = 1;
            result.msg = "上传成功";
            return result;
        } catch (e) {
            console.log(e);
            result.msg = "接口异常";
            return result;
        }

    }
}

module.exports = WorkloadService;