const BaseClient = require("./baseClient")
const settings = require("../../../config/settings");
const system = require("../../system");
const { appKey, secret } = settings;
/**
 * 食品 网文 产品
 */
class WangwenClient extends BaseClient {
    constructor() {
        super(appKey, secret);
    }

    /**
     *  文网文 递交材料,状态变更
        601: "完成账户注册",
        602: "服务商提交资料",
        603: "服务商完成提交资料到⼯信部",
        604: "⼯商部已受理",
        605: "⼯商部不予受理",
        606: "⼯商部通过",
        607: "⼯商部未通过"
    */
    async submitMaterials(username, deliverData, materials) {
        const channelType = {
            "wangwen": "esp.wangwen",
            "food": "esp.food"
        };
        let req = {};
        switch (deliverData.delivery_status) {
            case system.SERVERSESTATUS.COLLECTING:
            case system.SERVERSESTATUS.USERCONFIRMATIONREJECT:
                let { basicInfo, materialList } = materials;
                let extInfo = {};

                for (let val in basicInfo) {
                    if (basicInfo[val]) {
                        extInfo[val] = basicInfo[val];
                    }
                }
                for (let val of materialList) {
                    if (val.files || val.files.length > 0) {
                        let urls = [];
                        for (let file of val.files) {
                            if (file && file.url) {
                                urls.push(file.url);
                            }
                        }
                        if (urls.length > 0) {
                            extInfo[val.key] = urls
                        }
                    }
                }
                req = {
                    "ApplicationStatus": 601,
                    "extInfo": JSON.stringify(extInfo)
                }
                break
            case system.SERVERSESTATUS.USERCONFIRMATIONRESOLVE:
                req.ApplicationStatus = 602;
                break
            case system.SERVERSESTATUS.ACCOUNTREGISTRATION:
                req.ApplicationStatus = 603;
                break
            case system.SERVERSESTATUS.SUBMITING:
                req.ApplicationStatus = 604;
                break
            case system.SERVERSESTATUS.DISPOSEING:
                req.ApplicationStatus = 606;
                break
            default:
                throw new Error("此状态手动不能更改");
        }

        await this.pushQiFuTong(username, {
            actionType: "serviceSubmitOption",
            actionBody: {
                "channelType": channelType[deliverData.product_code],
                "orderNo": deliverData.delivery_code,
                "extInfo": "{}",
                ...req,
            }
        });
    }

    async changeStatus(username, deliverData, materials) {
        await this.submitMaterials(username, deliverData, materials);
    }
}

module.exports = WangwenClient