const system = require("../../../system");
const ServiceBase = require("../../svems.base");
const axios = require('axios')
class ChanceService extends ServiceBase {
    constructor() {
        super();
        this.deliverSve = system.getObject("service.common.deliverSve");
        this.orderSve = system.getObject("service.order.orderSve");
        this.ALI_PRODUCT_CODE = "10020000";
    }

    /**
     * TODO:这里需要一个方法去自动分配业务员
     *
     * 保存线索
     * @param params
     * @returns {Promise<{msg: string, data, bizmsg: *|string, status: number}|*|undefined>}
     */
    async saveClue(params) {
        params.operator_id=params.deliver_user_id;
        params.operator_path = params.deliver_user_path;
        params.clue_type_id = this.trim(params.clue_type_id) || this.ALI_PRODUCT_CODE;
        params.clue_type_name = this.trim(params.clue_type_name) || "个体户注册一体化";
        return await this.callms("chance", "saveClue", params);
    }

    /**
     * 关闭商机
     * @param params
     * @returns {Promise<{msg: string, data, bizmsg: *|string, status: number}|*|undefined>}
     */
    async closeClue(params) {
        let res =  await this.callms("chance", "closeClue", params);
        if(res.status==0 && res.data.callback_url){
            this.pushClue(params,res);
        }else{
            return  res;
        }
        return system.getResultSuccess();
    }

    /**
     * TODO: 这里需要推送到阿里（待完善）
     * fn:消息推送
     * @param params
     * @param res
     */
    async pushClue(params,res){
        console.log("推送参数  params:" + JSON.stringify(params) + "===> res:" + JSON.stringify(res));

        // let result = await axios({
        //     method: 'post',
        //     url: url,
        //     data: data
        // });
    }


    /**
     * fn:列表商机
     * ps:需要将业务员的信息查询
     * @param params
     * @returns {Promise<{msg: string, data, bizmsg: *|string, status: number}|*|undefined>}
     */
    async listClue(params) {
        let rs =  await this.callms("chance", "listClue", params);
        if (rs.status === 0 && rs.data) {
            await this.setUcUser(rs.data.rows);
        }
        //格式化服务内容
        let listProductRes = await this.orderSve.productDics({pid:this.ALI_PRODUCT_CODE});
        if(listProductRes.status!=0 || listProductRes.data.length<=0){
            return system.getResult(null, `获取数据失败 请联系管理员`);
        }
        let productMap = {};
        for(let item of listProductRes.data){
            productMap[item.id] = item.desc;
        }
        let temp = [],p_list=[];
        for(let ele of rs.data.rows){
            if(!rs.data.rows || rs.data.rows.length<=0 || !ele.cscheme.service_ids){
                continue;
            }
            temp = ele.cscheme.service_ids.split(",");
            //这里需要明天去噶哎
            for (let r of temp) {
                let _p = productMap[r] || {};
                p_list.push({id:r,name:_p.desc})
            }
            ele.service_product = p_list;
        }
        return rs;
    }

    /**
     * fn:查询操作员信息
     * @param rows
     * @returns {Promise<void>}
     */
    async setUcUser(rows) {
        if (!rows || rows.length == 0) {
            return;
        }

        let ids = [];
        for (let row of rows) {
            if (row.operator_id) {
                ids.push(row.operator_id);
            }
        }

        let map = await this.deliverSve.mayByIds(ids);
        for (let row of rows) {
            let user = map[row.operator_id] || {};
            row.operator_name = user.real_name || "";
        }
    }

    /**
     * 列表商机字典
     * @param params
     * @returns {Promise<{msg: string, data, bizmsg: *|string, status: number}|*|undefined>}
     */
    async clueStatusDict(params) {
        return await this.callms("chance", "clueStatusDict", params);
    }

    /**
     * 关闭商机字典
     * @param params
     * @returns {Promise<{msg: string, data, bizmsg: *|string, status: number}|*|undefined>}
     */
    async closeReasonDict(params) {
        return await this.callms("chance", "closeReasonDict", params);
    }

    /**
     * 根据线索ID查询详细
     * @param params
     * @returns {Promise<{msg: string, data, bizmsg: *|string, status: number}|*|undefined>}
     */
    async getClueById(params) {
        return await this.callms("chance", "getClueById", params);
    }

    /**
     * fn:保存跟进进度
     * @param params
     * @returns {Promise<{msg: string, data, bizmsg: *|string, status: number}|*|undefined>}
     */
    async saveCfollowLog(params) {
        params.operator=params.deliver_user_id||"";
        return await this.callms("chance", "saveCfollowLog", params);
    }

    /**
     * 保存方案
     * @param params
     * @returns {Promise<{msg: string, data, bizmsg: *|string, status: number}|*|undefined>}
     */
    async saveCscheme(params) {
        return await this.callms("chance", "saveCscheme", params);
    }
}
module.exports = ChanceService;