//#import $owl_orgs:services/modelService.jsx
//#import $owl_org_certify_log: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 jCertifyLog = owl_org_certify_logService.get(certifyLogId);
        if (!jCertifyLog) {
            out.print(JSON.stringify({state: "err", msg: "参数错误"}));
            return;
        }

        if (jCertifyLog && jCertifyLog && jCertifyLog._orgIds) {
            //检查是否有权限修改
            if (roleId !== 'internal' && roleId != '0' && orgId != '0' && jCertifyLog._orgIds.indexOf(orgId) === -1) {
                var ret = {
                    state: 'err',
                    code: 'no permission',
                    msg: '没有权限'
                }
                out.print(JSON.stringify(ret));
                return;
            }
        }

        var permissions = getPermissions(roleId, 'owl_org_certify_log');
        if (roleId !== 'internal' && roleId != '0') {
            checkUpdatePermission(jCertifyLog, null, permissions, orgId);
        }

        var userId = env.loginUserId;

        jCertifyLog.state = '1'
        jCertifyLog.certifyUserId = userId;
        jCertifyLog.certifyTime = new Date().getTime();
        owl_org_certify_logService.update(jCertifyLog);

        var jOrg = owl_orgsService.get(jCertifyLog.org_id);
        if(jOrg) {
            var isUpdated = false;
            if(jCertifyLog.new_name) {
                jOrg.name = jCertifyLog.new_name;
                isUpdated = true;
            }
            if(jCertifyLog.licImage) {
                jOrg.licImage = jCertifyLog.licImage;
                isUpdated = true;
            }
            if(jCertifyLog.showLicImage) {
                jOrg.showLicImage = jCertifyLog.showLicImage;
                isUpdated = true;
            }
            if(jCertifyLog.licBeginDate) {
                jOrg.licBeginDate = jCertifyLog.licBeginDate;
                isUpdated = true;
            }
            if(jCertifyLog.licEndDate) {
                jOrg.licEndDate = jCertifyLog.licEndDate;
                isUpdated = true;
            }
            if(isUpdated) {
                owl_orgsService.update(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();