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

class PlatformchannelService extends ServiceBase {
    constructor() {
        super("channel", ServiceBase.getDaoName(PlatformchannelService));
        this.channelDao = system.getObject("db.channel.channelDao");
        this.channelmerchantDao = system.getObject("db.channel.channelmerchantDao");
    }

    async bindPlatform(params){
        var data = await this.channelDao.findById(params.channel_id);
        if(data.length == 0){
           return  system.getErrResult("渠道ID不存在");
        }
        var platformchannel = {
            platform_id : this.trim(params.platform_id),
            channel_id : this.trim(params.channel_id),
            platform_channel_id : this.trim(params.platform_channel_id)
        };
        var platformchannelData = await this.dao.findOne(platformchannel);
        if(platformchannelData != null){
            return system.getResultSuccess(platformchannelData.id);
        }
        let suData = await this.dao.create(platformchannel);
        if(this.trim(suData.id)){
            return system.getResultSuccess(suData.id)
        }
        return system.getErrResult("绑定失败")
    }

    //绑定渠道推送
    async pushbindingchannel(params){
        var merchantS = await this.dao.findByamong(params);

        if(merchantS.length > 0){
            return system.getSuccResult(merchantS)
        }
        return system.getSuccResult(null)
    }

    async allChannelIds(params) {
        return await this.dao.allChannelIds();
    }


    trim(o) {
        if(!o) {
            return "";
        }
        return o.toString().trim();
    }

    unique(arr) {
        if (!Array.isArray(arr)) {
            console.log('type error!')
            return
        }
        var array = [];
        for (var i = 0; i < arr.length; i++) {
            if (array .indexOf(arr[i]) === -1) {
                array .push(arr[i])
            }
        }
        return array;
    }
}

module.exports = PlatformchannelService;
