const ToQiFuTong = require('../../../utils/qifutong/baseClient').getInstance();
class StatisticsService {
    async getAllChannels(pobj) {
        return ToQiFuTong.getAllChannels();
    }
    async getAllProducts(pobj) {
        return ToQiFuTong.getAllProducts();
    }
    async getStatisticsByUappId(pobj) {
        const data = await ToQiFuTong.getStatisticsByUappId({
            start: pobj.start,
            end: pobj.end,
            type_code: pobj.type_code,
            status: pobj.status
        });
        const dates = ToQiFuTong.getAllDate(pobj.start, pobj.end);
        return {
            x: dates,
            data
        };
    }
    async getStatisticsByProduct(pobj) {
        const data = await ToQiFuTong.getStatisticsByProduct({
            start: pobj.start,
            end: pobj.end,
            type_code: pobj.type_code,
        });
        let result = []; // 基础数据
        let channels = new Set(); //渠道
        let productList = []; //产品
        for (let val of data) {
            let productCount = 0;
            for (let info of val.data) {
                productCount += info.count;
                channels.add(info.uapp_id.toString());
                result.push({
                    uapp_id: info.uapp_id.toString(),
                    count: info.count,
                    type_code: val.type_code
                })
            }
            productList.push({
                count: productCount,
                type_code: val.type_code
            })
        }
        productList = productList.sort(function (a, b) { return a.count - b.count });
        return {
            channelList: [...channels],
            productList: productList.map((item) => { return item.type_code }),
            data: result
        };
    }

    async getNeedFunnelStatistics(pobj) {
        return ToQiFuTong.getNeedFunnelStatistics({
            start: pobj.start,
            end: pobj.end,
            type_code: pobj.type_code,
            uapp_id: pobj.uapp_id
        });
    }
}
module.exports = StatisticsService;