const system = require("../../../system");
const ServiceBase = require("../../sve.base");
const settings = require("../../../../config/settings");
class TemplatelinkService extends ServiceBase {
  constructor() {
    super("template", ServiceBase.getDaoName(TemplatelinkService));
  }

  async findAndCountAll(obj){
    var res = await this.dao.findAndCountAll(obj);
    return system.getResultSuccess(res);
  }

  async checkAndPackageParams(ab,xctx){
    if(!ab.channel_id){
      return system.getResultFail(-101,"渠道主体不能为空");
    }
    if(!ab.business_type_id){
      return system.getResultFail(-102,"业务类型不能为空");
    }
    if(!ab.lauch_type_id){
      return system.getResultFail(-103,"投放方式不能为空");
    }
    if(!ab.marketing_subject_id){
      return system.getResultFail(-104,"营销主体不能为空");
    }
    if(!ab.delivery_word){
      return system.getResultFail(-105,"投放词不能为空");
    }
    if(!ab.else_channel_param){
      return system.getResultFail(-106,"其它渠道参数不能为空");
    }
    if(!ab.template_id){
      return system.getResultFail(-107,"模板参数不能为空");
    }
    if(!ab.name){
      return system.getResultFail(-107,"任务名称不能为空");
    }
    if(!ab.code){//新增
      ab.code = await this.getBusUid("tl");
      ab.user_id=xctx.credid;
      ab.user_name=xctx.username;
      ab.company_id=xctx.companyid;
      ab.is_enabled=0;
    }else{//修改
      var linkinfo = await this.dao.model.findOne({
        where:{code:ab.code},raw:true
      });
      if(!linkinfo || !linkinfo.id){
        return system.getResultFail(-300,"未知模板");
      }
      if(linkinfo.is_enabled===1){
        return system.getResultFail(-301,"该链接正在投放中，不能执行此操作");
      }
      ab.id = linkinfo.id;
    }
    return ab;
  }

  /**
   * 创建模板链接数据
   * @param {*} pobj 
   */
  async createTemplateLink(pobj){
    var ab = pobj.actionBody;
    var xctx = pobj.xctx;
    if(!ab){
      return system.getResultFail(-100,"参数错误");
    }
    var checkres = await this.checkAndPackageParams(ab,xctx);
    if(checkres && checkres.status<0){
      return checkres;
    }
    ab = checkres;
    var res = await this.create(ab);
    return system.getResultSuccess(res);
  }

  /**
   * 修改模板链接
   * @param {*} pobj 
   */
  async editTemplateLink(pobj){
    var ab = pobj.actionBody;
    var xctx = pobj.xctx;
    if(!ab){
      return system.getResultFail(-100,"参数错误");
    }
    if(!ab.code){
      return system.getResultFail(-100,"模板编码不能为空");
    }
    var checkres = await this.checkAndPackageParams(ab,xctx);
    if(checkres && checkres.status<0){
      return checkres;
    }
    ab = checkres;
    await this.dao.update(ab);
    return system.getResultSuccess();
  }

  /**
   * 获取模板链接详情数据
   * @param {*} pobj 
   */
  async findOneByCode(pobj){
    var ab = pobj.actionBody;
    var xctx = pobj.xctx;
    if(!ab){
      return system.getResultFail(-100,"参数错误");
    }
    if(!ab.code){
      return system.getResultFail(-101,"模板编码不能为空");
    }
    var templateInfo = await this.dao.model.findOne({
      where:{code:ab.code},raw:true
    });
    return system.getResultSuccess(templateInfo);
  }

  /**
   * 修改投放状态
   * @param {*} pobj 
   */
  async updateLaunchStatus(pobj){
    var ab = pobj.actionBody;
    var xctx = pobj.xctx;
    if(!ab){
      return system.getResultFail(-100,"参数错误");
    }
    if(!ab.code){
      return system.getResultFail(-101,"链接编码不能为空");
    }
    if(!ab.hasOwnProperty("is_enabled")){
      return system.getResultFail(-102,"投放状态不能为空");
    }
    if(ab.is_enabled!==0 && ab.is_enabled!==1){
      return system.getResultFail(-103,"投放状态参数错误");
    }
    var linkinfo = await this.dao.model.findOne({
      where:{code:ab.code},raw:true
    });
    if(!linkinfo || !linkinfo.id){
      return system.getResultFail(-300,"未知链接");
    }
    await this.dao.update({id:linkinfo.id,is_enabled:ab.is_enabled});
    return system.getResultSuccess();
  }
  /**
   * 删除模板链接
   * @param {*} pobj 
   */
  async deleteTemplateLink(pobj){
    var ab = pobj.actionBody;
    var xctx = pobj.xctx;
    if(!ab){
      return system.getResultFail(-100,"参数错误");
    }
    if(!ab.code){
      return system.getResultFail(-101,"链接编码不能为空");
    }
    var linkinfo = await this.dao.model.findOne({
      where:{code:ab.code},raw:true
    });
    if(!linkinfo || !linkinfo.id){
      return system.getResultFail(-300,"未知模板链接");
    }
    if(linkinfo.is_enabled===1){
      return system.getResultFail(-301,"该链接正在投放中，不能执行此操作");
    }
    await this.dao.delete({id:linkinfo.id});
    return system.getResultSuccess();
  }

  
  
}
module.exports = TemplatelinkService;