//#import $owl_orgs:services/modelService.jsx
//#import $owl_merchant_apply:services/modelService.jsx
//#import @handlers/include/checklogin.jsx
function main() {

    var env = checklogin();
    if (!env) {
        return;
    }
    var roleId = env.roleId
    var orgId = env.orgId;
    try {
        var params = JSON.parse($body);
        var certifyLogId = params.id;
        if (!certifyLogId) {
            out.print(JSON.stringify({state: "err", msg: "参数错误"}));
            return;
        }
        var jMerchantApply = owl_merchant_applyService.get(certifyLogId);
        if (!jMerchantApply) {
            out.print(JSON.stringify({state: "err", msg: "参数错误"}));
            return;
        }

        if (jMerchantApply && jMerchantApply && jMerchantApply._orgIds) {
            //检查是否有权限修改
            if (roleId !== 'internal' && roleId != '0' && orgId != '0' && jMerchantApply._orgIds.indexOf(orgId) === -1) {
                var ret = {
                    state: 'err',
                    code: 'no permission',
                    msg: '没有权限'
                }
                out.print(JSON.stringify(ret));
                return;
            }
        }

        var permissions = getPermissions(roleId, 'owl_merchant_apply');
        if (roleId !== 'internal' && roleId != '0') {
            checkUpdatePermission(jMerchantApply, null, permissions, orgId);
        }

        if(!jMerchantApply.parent_id) {
            out.print(JSON.stringify({state: "err", msg: "所属父组织不能为空，请先设置"}));
            return;
        }

        var userId = env.loginUserId;

        jMerchantApply.certifyState = '1'
        jMerchantApply.certifyUserId = userId;
        jMerchantApply.certifyTime = new Date().getTime();
        owl_merchant_applyService.update(jMerchantApply);

        var jOrg = {};
        jOrg.source_id = "front";
        jOrg.source_name = "前端入驻申请";
        jOrg.parent_id = jMerchantApply.parent_id;
        jOrg.parent_name = jMerchantApply.parent_name;
        jOrg.orgType = jMerchantApply.orgType;
        jOrg.merType = jMerchantApply.merType;
        jOrg.name = jMerchantApply.name;
        jOrg.name_en = jMerchantApply.name_en;
        jOrg.regionId = jMerchantApply.regionId;
        jOrg.regionPath = jMerchantApply.regionPath;
        jOrg.ownerId = jMerchantApply.ownerId;
        jOrg.publishState = jMerchantApply.publishState;
        jOrg.certifyState = "1";
        jOrg.enableState = jMerchantApply.enableState;
        jOrg.productCanBuyState = jMerchantApply.productCanBuyState;
        jOrg.canGiveIntegral = jMerchantApply.canGiveIntegral;
        jOrg.contactInfo = jMerchantApply.contactInfo;
        jOrg.qiyu_info = jMerchantApply.qiyu_info;
        jOrg.memo = jMerchantApply.memo;
        jOrg.deliveryMemo = jMerchantApply.deliveryMemo;
        jOrg.deliveryMode = jMerchantApply.deliveryMode;
        jOrg.org_modules = jMerchantApply.org_modules;
        jOrg.delivery_price_template = jMerchantApply.delivery_price_template;
        jOrg.platform_category = jMerchantApply.platform_category;
        jOrg.can_buy_user_category = jMerchantApply.can_buy_user_category;
        jOrg.entityInfo = jMerchantApply.entityInfo;

        jOrg.settlement_date = jMerchantApply.settlement_date;
        jOrg.alipay_account = jMerchantApply.alipay_account;
        jOrg.alipay_name = jMerchantApply.alipay_name;
        jOrg.alipay_account_bundled = jMerchantApply.alipay_account_bundled;

        jOrg.logo = jMerchantApply.logo;
        jOrg.pics = jMerchantApply.pics;
        jOrg.subscribeMsgTitle = jMerchantApply.subscribeMsgTitle;
        jOrg.subscribeMsgUrl = jMerchantApply.subscribeMsgUrl;
        jOrg.subscribeMsgDesc = jMerchantApply.subscribeMsgDesc;
        jOrg.subscribeMsgPic = jMerchantApply.subscribeMsgPic;

        jOrg.orgCode = jMerchantApply.orgCode;
        jOrg.registrationNumber = jMerchantApply.registrationNumber;
        jOrg.address = jMerchantApply.address;
        jOrg.businessRange = jMerchantApply.businessRange;
        jOrg.licImage = jMerchantApply.licImage;
        jOrg.showLicImage = jMerchantApply.showLicImage;
        jOrg.licBeginDate = jMerchantApply.licBeginDate;
        jOrg.licEndDate = jMerchantApply.licEndDate;

        jOrg.qrcode = jMerchantApply.qrcode;
        jOrg.bankInfo = jMerchantApply.bankInfo;
        jOrg.invoice_info = jMerchantApply.invoice_info;
        jOrg.dyna_setting = jMerchantApply.dyna_setting;
        jOrg.company_base_info = jMerchantApply.company_base_info;
        jOrg.bank_info = jMerchantApply.bank_info;
        jOrg.company_address_info = jMerchantApply.company_address_info;
        jOrg.company_contact_info = jMerchantApply.company_contact_info;
        jOrg.fin_business_range_info = jMerchantApply.fin_business_range_info;
        jOrg.enterprise_info = jMerchantApply.enterprise_info;

        jOrg.owl_createUserId = userId;

        jOrg = owl_orgsService.add(jOrg);

        var ret = {
            state: 'ok',
            msg: "操作成功"
        }
        out.print(JSON.stringify(ret));
    } catch (e) {
        var ret = {
            state: 'err',
            msg: e.toString()

        }
        out.print(JSON.stringify(ret));
    }

}

main();