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

  async findByCompanyId(obj){
    if(obj.company_id){
      obj.search.company_id = obj.company_id;
    }
    var res = await this.dao.findAndCountAll(obj);
    return system.getResultSuccess(res);
  }
  /**
   * 创建模板
   * @param {*} pobj 
   */
  async createTemplate(pobj){
    var ab = {};
    var code = await this.getBusUid("mt");//自动生成模板编码
    var name = "营销模板";//模板名称
    ab.code = code;
    ab.name = name;
    ab.user_id=pobj.userid;
    ab.user_name=pobj.username;
    ab.company_id=pobj.company_id;
    ab.is_enabled=0;
    var res = await this.create(ab);
    return system.getResultSuccess(res);
  }
  /**
   * 根据模板编码获取模板信息
   * 编辑模板时使用此接口
   * @param {*} pobj 
   */
  async findOneByCode(pobj){
    var ab = pobj;
    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 getTemplateInfoByCode(pobj){
    var ab = pobj.actionBody;
    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 editTemplateContent(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("template_content")){
      return system.getResultFail(-102,"模板内容不能为空");
    }
    //根据模板编码获取模板信息
    var templateInfo = await this.dao.model.findOne({
      where:{code:ab.code},raw:true
    });
    if(!templateInfo || !templateInfo.id){
      return system.getResultFail(-300,"未知模板");
    }
    // if(templateInfo.is_enabled===1){
    //   return system.getResultFail(-301,"该模板正在使用中，不能执行此操作");
    // }
    await this.dao.update({id:templateInfo.id,template_content:ab.template_content});
    return system.getResultSuccess();
  }
  /**
   * 修改启用状态
   * @param {*} pobj 
   */
  async updateSwitchStatus(pobj){
    var ab = pobj;
    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 templateInfo = await this.dao.model.findOne({
      where:{code:ab.code},raw:true
    });
    if(!templateInfo || !templateInfo.id){
      return system.getResultFail(-300,"未知模板");
    }
    await this.dao.update({id:templateInfo.id,is_enabled:ab.is_enabled});
    return system.getResultSuccess();
  }
  /**
   * 编辑模板tdk信息
   * @param {*} pobj 
   */
  async editTemplateTdk(pobj){
    var ab = pobj;
    var updateObj = {};
    if(!ab){
      return system.getResultFail(-100,"参数错误");
    }
    if(!ab.code){
      return system.getResultFail(-101,"模板编码不能为空");
    }
    if(!ab.title){
      return system.getResultFail(-102,"网页标题不能为空");
    }
    updateObj.title = ab.title;
    if(!ab.keyword){
      return system.getResultFail(-103,"关键词不能为空");
    }
    updateObj.keyword = ab.keyword;
    if(!ab.pic_url){
      return system.getResultFail(-104,"网页图标不能为空");
    }
    updateObj.pic_url = ab.pic_url;
    if(!ab.describe){
      return system.getResultFail(-105,"描述不能为空");
    }
    updateObj.describe = ab.describe;
    //根据模板编码获取模板信息
    var templateInfo = await this.dao.model.findOne({
      where:{code:ab.code},raw:true
    });
    if(!templateInfo || !templateInfo.id){
      return system.getResultFail(-300,"未知模板");
    }
    updateObj.id = templateInfo.id;
    await this.dao.update(updateObj);
    return system.getResultSuccess();
  }
  
}
module.exports = TemplateinfoService;