const system = require("../../../system");
const ServiceBase = require("../../sve.base");
const settings = require("../../../../config/settings");
class TemplatelinkService extends ServiceBase {
  constructor() {
    super("template", ServiceBase.getDaoName(TemplatelinkService));
    this.templateLinkUrl = settings.templateLinkUrl();
    this.templateinfoDao = system.getObject("db.template.templateinfoDao");
    this.browsingrecordsDao = system.getObject("db.template.browsingrecordsDao");
    this.launchchannelDao = system.getObject("db.configmag.launchchannelDao");
    this.launchtypeDao = system.getObject("db.configmag.launchtypeDao");
    this.marketingsubjectDao = system.getObject("db.configmag.marketingsubjectDao");
  }
  /**
   * 获取模板链接配置参数
   * @param {*} pobj 
   */
  async getLinkConfigParams(pobj){
    var ab = pobj.actionBody;
    var xctx = pobj.xctx;
    var company_id=xctx.companyid;
    var whereObj = {company_id:company_id};
    //渠道主体
    var launchChannelList = await this.launchchannelDao.model.findAll({
      attributes:["id","code","name"],
      where:whereObj,raw:true
    });
    //业务参数
    //投放方式
    var launchTypeList = await this.launchtypeDao.model.findAll({
      attributes:["id","code","name"],
      where:whereObj,raw:true
    });
    //营销主体
    var marketingSubjectList = await this.marketingsubjectDao.model.findAll({
      attributes:["id","code","name"],
      where:whereObj,raw:true
    });
    var res = {launchChannelList:launchChannelList,launchTypeList:launchTypeList,marketingSubjectList:marketingSubjectList};
    return system.getResultSuccess(res);
  }
  /**
   * 链接列表
   * @param {*} obj 
   */
  async findAndCountAll(obj){
    if(!obj || !obj.search || !obj.search.template_id){
      return system.getResultFail(-101,"模板id不能为空");
    }
    var res = await this.dao.findAndCountAll(obj);
    return system.getResultSuccess(res);
  }
  /**
   * 校验封装参数
   * @param {*} ab 
   * @param {*} xctx 
   */
  async checkAndPackageParams(ab,xctx){
    if(!ab.channel_code){
      return system.getResultFail(-101,"渠道主体编码不能为空");
    }
    if(!ab.business_type_code){
      return system.getResultFail(-102,"业务类型编码不能为空");
    }
    if(!ab.lauch_type_code){
      return system.getResultFail(-103,"投放方式编码不能为空");
    }
    if(!ab.marketing_subject_code){
      return system.getResultFail(-104,"营销主体编码不能为空");
    }
    if(!ab.channel_name){
      return system.getResultFail(-105,"渠道主体名称不能为空");
    }
    if(!ab.business_type_name){
      return system.getResultFail(-106,"业务类型名称不能为空");
    }
    if(!ab.lauch_type_name){
      return system.getResultFail(-107,"投放方式名称不能为空");
    }
    if(!ab.marketing_subject_name){
      return system.getResultFail(-108,"营销主体名称不能为空");
    }
    if(!ab.delivery_word){
      return system.getResultFail(-109,"投放词不能为空");
    }
    if(!ab.else_channel_param){
      return system.getResultFail(-110,"其它渠道参数不能为空");
    }
    if(!ab.template_id){
      return system.getResultFail(-111,"模板参数不能为空");
    }
    if(!ab.name){
      return system.getResultFail(-112,"任务名称不能为空");
    }
    if(!ab.code){//新增
      ab.code = await this.getBusUid("tl");
      ab.user_id=ab.userid;
      ab.user_name=ab.username;
      ab.company_id=ab.company_id;
      ab.is_enabled=0;
      //查重
      var distinctObj = {
        template_id:ab.template_id,channel_code:ab.channel_code,else_channel_param:ab.else_channel_param,
        business_type_code:ab.business_type_code,lauch_type_code:ab.lauch_type_code,marketing_subject_code:ab.marketing_subject_code
      };
      var distinctres = await this.dao.model.findOne({
        where:distinctObj,raw:true
      });
      if(distinctres && distinctres.id){
        return system.getResultFail(-113,"操作失败，存在重复的链接");
      }
    }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;
      //查重
      var distinctObj = {
        template_id:ab.template_id,channel_code:ab.channel_code,else_channel_param:ab.else_channel_param,
        business_type_code:ab.business_type_code,lauch_type_code:ab.lauch_type_code,marketing_subject_code:ab.marketing_subject_code,
        id:{[this.db.Op.ne]:linkinfo.id}
      };
      var distinctres = await this.dao.model.findOne({
        where:distinctObj,raw:true
      });
      if(distinctres && distinctres.id){
        return system.getResultFail(-113,"操作失败，存在重复的链接");
      }
    }
    ab.link_url_pc = this.templateLinkUrl+"?id="+ab.template_id+"&source="+ab.channel_code+"&device=pc&event="+ab.else_channel_param+
    "&business="+ab.business_type_code+"&type="+ab.lauch_type_code+"for="+ab.marketing_subject_code;
    ab.link_url_mobile = this.templateLinkUrl+"?id="+ab.template_id+"&source="+ab.channel_code+"&device=mobile&event="+ab.else_channel_param+
    "&business="+ab.business_type_code+"&type="+ab.lauch_type_code+"for="+ab.marketing_subject_code;
    return ab;
  }

  /**
   * 创建模板链接数据
   * @param {*} pobj 
   */
  async createTemplateLink(pobj){
    var ab = pobj;
    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();
  }


  /**
   * 根据链接参数获取模板链接信息
   * @param {*} pobj
   */
  async getTemplateAndLinkInfo(pobj){
    var ab = pobj.actionBody;
    var xctx = pobj.xctx;
    if(!ab){
      return system.getResultFail(-100,"参数错误");
    }
    if(!ab.channel_code){
      return system.getResultFail(-101,"渠道主体编码不能为空");
    }
    if(!ab.business_type_code){
      return system.getResultFail(-102,"业务类型编码不能为空");
    }
    if(!ab.lauch_type_code){
      return system.getResultFail(-103,"投放方式编码不能为空");
    }
    if(!ab.marketing_subject_code){
      return system.getResultFail(-104,"营销主体编码不能为空");
    }
    if(!ab.else_channel_param){
      return system.getResultFail(-110,"其它渠道参数不能为空");
    }
    if(!ab.template_id){
      return system.getResultFail(-111,"模板参数不能为空");
    }
    if(!ab.device){
      return system.getResultFail(-112,"投放终端参数不能为空");
    }
    var linkObj = {
      template_id:ab.template_id,channel_code:ab.channel_code,else_channel_param:ab.else_channel_param,
      business_type_code:ab.business_type_code,lauch_type_code:ab.lauch_type_code,
      marketing_subject_code:ab.marketing_subject_code
    };
    //获取链接数据
    var linkinfo = await this.dao.model.findOne({
      where:linkObj,raw:true
    });
    if(!linkinfo || !linkinfo.id){
      return system.getResultFail(-300,"未知链接");
    }
    if(!linkinfo.is_enabled || linkinfo.is_enabled!==1){
      return system.getResultFail(-301,"无效链接，该链接未投放");
    }
    var tempObj={id:ab.template_id};
    var templateinfo = await this.templateinfoDao.model.findOne({
      where:tempObj,raw:true
    });
    if(!templateinfo || !templateinfo.id){
      return system.getResultFail(-400,"未知模板");
    }
    if(!templateinfo.is_enabled || templateinfo.is_enabled!==1){
      return system.getResultFail(-401,"无效模板，该模板未启用");
    }
    var resultObj = {
      templateinfo:templateinfo,linkinfo:linkinfo
    };
    var addObj = {
      ...ab,
      link_code:linkinfo.code,link_name:linkinfo.name,
      channel_name:linkinfo.channel_name,business_type_name:linkinfo.business_type_name,
      lauch_type_name:linkinfo.lauch_type_name,marketing_subject_name:linkinfo.marketing_subject_name,
      client_ip:pobj.clientIp

    };
    this.browsingrecordsDao.create(addObj);//添加链接浏览记录
    return system.getResultSuccess(resultObj);
  }

}
module.exports = TemplatelinkService;