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

class NeedinfoService extends ServiceBase {
    constructor() {
        super("media", ServiceBase.getDaoName(NeedinfoService));
        this.launchchannelDao = system.getObject("db.configmag.launchchannelDao");
    }
    //提交商机/线索
    async submitNeed(pobj){
        if(!pobj || !pobj.actionBody){
            return system.getResultFail(-100,"参数错误");
        }
        var ab = pobj.actionBody;
        var res = await this.checkAndPackageSubmitParams(ab);//校验封装参数
        if(res && res.status && res.status<0){
            return res;
        }
        await this.dao.create(res);
        return system.getResultSuccess();

    }
    //校验封装参数
    async checkAndPackageSubmitParams(ab){
        var params={};
        if(!ab.submit_type){
            return system.getResultFail(-101,"提交方式不能为空！")
        }else if(ab.submit_type!="1" && ab.submit_type!="2"){
            return system.getResultFail(-102,"提交方式参数错误！")
        }
        if(!ab.channel_code){
            return system.getResultFail(-103,"渠道编码不能为空！")
        }
        if(!ab.page_code){
            return system.getResultFail(-104,"页面编码不能为空！")
        }
        if(!ab.page_name){
            return system.getResultFail(-105,"页面名称不能为空！")
        }
        if(!ab.contact_mobile){
            return system.getResultFail(-106,"联系电话不能为空！")
        }
        var channel = await this.launchchannelDao.model.findOne({
            attributes:["code","name"],
            where:{code:ab.channel_code},raw:true
        });
        if(!channel || !channel.code){
            return system.getResultFail(-300,"渠道信息不存在")
        }
        ab["channel_name"] = channel.name;
        return ab;
    }
}

module.exports = NeedinfoService;