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

class DeliverChannelService extends ServiceBase {
  constructor() {
    super("deliver", ServiceBase.getDaoName(DeliverChannelService));
  }

  async byCondition(params) {
    let rs = await this.dao.byCondition(params);
    return system.getResultSuccess(rs);
  }

  async bindChannel(pobj) {
    let deliver_id = this.trim(pobj.deliver_id);
    let channel_id = this.trim(pobj.channel_id);
    if (!deliver_id || !channel_id) {
      return system.getResult(null, `参数错误deliver_id[${deliver_id}], channel_id${channel_id}`);
    }
    let dc = {
      deliver_id: deliver_id,
      channel_id: channel_id,
    };
    // 取消绑定
    if (pobj.type == "unbind") {
      let rs = await this.dao.delByDC(dc);
      return system.getResultSuccess(rs);
    }
    // 绑定
    let exists = await this.dao.findOne(dc);
    if (exists) {
      return system.getResultSuccess(exists);
    }
    dc.autoIncrement = true;
    dc = await this.dao.create(dc);
    return system.getResultSuccess(dc);
  }
}

module.exports = DeliverChannelService;
