var system = require("../../../system");
var settings = require("../../../../config/settings");
const CtlBase = require("../../ctl.base");
const { json, JSON } = require("sequelize");
const appconfig = system.getSysConfig();
class FitaxschemeCtl extends CtlBase {
    constructor() {
        super("bizchance", CtlBase.getServiceName(FitaxschemeCtl));
        this.rysUrl = settings.rysUrl();
    }
    /**
     * 新建方案信息
     * @param {*} mobj
    */
    async insertInfo(mobj, qobj, req) {
        let pobj = mobj.d;
        try {
            if (!pobj.demand_code || !pobj.company_type || !pobj.taxpayer_type || !pobj.service_code || !pobj.number) {
                return system.getResultError("fitaxschemeCtl/insertInfo 缺少必要参数!");
            }
            let res = await this.service.insertInfo(pobj);
            //同步方案信息，获取方案编号(走队列，再写一个接收队列方案编号的方法，将方案编号更新到数据库 融易算接口文档2.2)
            //构造参数
            let pushObj = this.buildObj(pobj);
            system.queueOper(pushObj);
            return system.getResult("新建方案成功!");
        } catch (error) {
            console.log("fitaxschemeCtl/insertInfo---------------------------------------------------------------- " + error);
            return system.getResultError("fitaxschemeCtl/insertInfo 新建方案出错!");
        }
    }

    /**
     * @param {*} pobj
     * @param {*} qobj
     * @param {*} req
    */
    async updateInfo(pobj, qobj, req) {
        let obj = {};
        if (pobj.actionBody.IntentionBizId) {//需求编号
            obj.demand_code = pobj.actionBody.IntentionBizId;
        }
        if (pobj.actionBody.resultInfo.data.SolutionBizId) {//方案编号
            obj.solution_bizid = pobj.actionBody.resultInfo.data.SolutionBizId;
        }
        try {
            let res = await this.service.updateInfo(obj);
            return system.getResult("添加方案编号成功！");
        } catch (error) {
            console.log("fitaxschemeCtl/updateInfo error: " + error);
            return system.getResultError("添加方案编号失败: " + error)
        }
    }

    /**
     * @param {*} pobj
     * @param {*} qobj
     * @param {*} req
    */
    async findInfoByBizid(pobj, qobj, req) {//方案
        try {
            if (pobj.bizId) {
                var res = await this.service.findInfoByBizid(pobj);
                return system.getResult(res);
            }
            else {
                return system.getResultError("缺少方案编号");
            }
        }
        catch (error) {
            return system.getResultError(error);
        }
    }

    buildObj(pobj) {
        let pushUrl = this.rysUrl + "leads/proposal/create";
        let notifyUrl = "http://book.com/web/bizchance/fitaxschemeCtl/updateInfo";
        let companyType, sreaCode, quantity, taxQualify;
        if (pobj.company_type) { //公司类型
            if (pobj.company_type == "商业型") {
                companyType = 1;
            } else if (pobj.company_type == "服务型") {
                companyType = 2;
            } else if (pobj.company_type == "特殊行业") {
                companyType = 3;
            } else {
                companyType = 4;
            }
        }
        if (pobj.service_code) { //区域编码
            sreaCode = parseInt(pobj.service_code);
        }
        if (pobj.number) { //数量
            quantity = parseInt(pobj.number);
        }
        if (pobj.taxpayer_type) { //纳税人类型
            if (pobj.taxpayer_type == "一般纳税人") {
                taxQualify = 1;
            } else {
                taxQualify = 2;
            }
        }
        let pushObj = {
            pushUrl: pushUrl,
            actionType: "create",
            notifyUrl: notifyUrl,
            messageBody: {
                IntentionBizId: pobj.demand_code,
                BizType: "bookkeeping",
                Solution: {
                    CompanyType: companyType,
                    SreaCode: sreaCode,
                    Quantity: quantity,
                    TaxQualify: taxQualify
                }
            },
            headData: {
                'Authorization': 'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE1OTg0MzQ2ODcsImlzcyI6InJFRVN6TU5ZUlM2RnBWNnZlY09vdExPNWdPcUNXSmFQIiwiaWF0IjoxNTk4NDMxMDg3fQ.isQ40HqHHfJ-rTsUQOhRIdu4XCitZU-AfzeqSH4kPbQ',
                'XAPPKEY': 'f6b59b74-f943-4735-bb86-e05d6b7fd78e',
                'Source': 'GSB'
            }
        }
        return pushObj;
    }
}
module.exports = FitaxschemeCtl;

