//#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 merchantApplyId = params.id;
        var certifyReason = params.certifyReason;
        if (!merchantApplyId) {
            out.print(JSON.stringify({state: "err", msg: "参数错误"}));
            return;
        }
        var jMerchantApply = owl_merchant_applyService.get(merchantApplyId);
        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);
        }

        var userId = env.loginUserId;

        jMerchantApply.state = '2'
        jMerchantApply.certifyUserId = userId;
        jMerchantApply.certifyTime = new Date().getTime();
        jMerchantApply.certifyReason = certifyReason;
        owl_merchant_applyService.update(jMerchantApply);

        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();