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

class LaunchchannelService extends ServiceBase {
    constructor() {
        super("configmag", ServiceBase.getDaoName(LaunchchannelService));
    }

    /**
     * 根据company_id 和 code 去重
     * @param pobj
     * @returns {Promise<{msg: string, data: *, bizmsg: string, status: number}|{msg: *, data: *, status: *}>}
     */
    async createChannel(pobj) {
        if (!pobj.company_id) {
            return system.getResult(null, 'company_id can not be empty');
        }
        if (!pobj.name) {
            return system.getResult(null, 'name can not be empty');
        }
        if (!pobj.code) {
            return system.getResult(null, 'code can not be empty');
        }
        let whereParams = {
            code: pobj.code,
            company_id: pobj.company_id
        }
        let searchResult = await this.findOne(whereParams, []);
        if (searchResult) {
            return system.getResult(null,'渠道已存在，请勿重复添加');
        }
        let channelData = {
            name: pobj.name,
            code: pobj.code,
            user_id: pobj.userid,
            user_name: pobj.username,
            company_id: pobj.company_id
        }
        let result = await this.create(channelData);
        return system.getResult(result);
    }

    /**
     * 根据company_id 查询
     * @param pobj
     * @returns {Promise<null|[]>}
     */
    async findByCompanyId(pobj) {
        if (pobj.company_id) {
            pobj.search.company_id = pobj.company_id;
        }
        let reuslt = await this.findAndCountAll(pobj);
        return system.getResult(reuslt);
    }

}

module.exports = LaunchchannelService;