const system = require("../../system");
const ServiceBase = require("../sve.base");
const moment = require("moment");

class EcompanybusiService extends ServiceBase {
    constructor() {
        super(ServiceBase.getDaoName(EcompanybusiService));
        this.ecompanyDao = system.getObject("db.ecompanyDao");
    }


    async allList(params) {
        var currentPage = Number(params.currentPage || 0);
        var pageSize = Number(params.pageSize || 10);

        var where = {};
        if (params.appId) {
            where.appId = {
                [this.db.Op.eq]: params.appId
            };
        }
        if (params.mchtId) {
            where.mchtId = {
                [this.db.Op.eq]: params.mchtId
            };
        }

        var orderby = [
            ["id", 'desc']
        ];

        var page = await this.getPageList(currentPage, pageSize, where, orderby);
        if (page && page.rows) {
            for (var row of page.rows) {
                this.handleDate(row, ["created_at"]);
            }

            await this.setCompany(page.rows);
        }
        return page;
    }



    async setCompany(list) {
        if (!list || list.length == 0) {
            return;
        }

        var companyIds = [];
        for (var item of list) {
            companyIds.push(item.ecompany_id || 0);
        }
        var companyMap = await this.ecompanyDao.findMapByIds(companyIds, " id, name, contractMobile ");
        for (var item of list) {
            item.ecompany = companyMap["id_" + (item.ecompany_id || 0)];
        }
    }
}

module.exports = EcompanybusiService;
