<?php

namespace web\admin\rigger;

class role_u {
    public static function json_init() {
        global $db;
        $rsuser = verifyfast();
        $id = getint('id');
        $csql = new \ciy\sql('zc_role');
        $csql->where('id', $id);
        $ret['data'] = $db->getone($csql);
        $csql = new \ciy\sql('zc_menu');
        $csql->where('isuse', 1)->order('csort desc,id');
        $ret['pcmenu'] = $db->get($csql);
        $csql = new \ciy\sql('zc_power');
        $csql->order('csort desc,id');
        $ret['powers'] = $db->get($csql);
        return succjson($ret);
    }
    public static function json_update() {
        global $db;
        $rsuser = verifyfast();
        if (nopower($db, $rsuser['id'], 'p502u'))
            return errjson('您未被授权操作');
        $post = new \ciy\post();
        $id = getint('id');
        $name = $post->get('name');
        if ($name == '')
            return errjson('请填写角色名称');
        $memo = $post->get('memo');
        $pman = $post->get('pman');
        $pmans = explode(',', $pman);
        $powers = array();
        foreach ($pmans as $pmain) {
            if (empty($pmain))
                continue;
            $power = $post->get('power_' . $pmain);
            if (empty($power))
                continue;
            $powerno = $post->get('power_' . $pmain . '_noid');
            if (empty($powerno))
                $powers[] = $pmain;
            else {
                $ps = explode(',', $power);
                foreach ($ps as $p) {
                    if (empty($p))
                        continue;
                    $powers[] = $p;
                }
            }
        }
        $power = '.' . implode('.', $powers) . '.';
        $datarow = null;
        if ($id > 0) {
            $csql = new \ciy\sql('zc_role');
            $csql->where('id', $id);
            $datarow = $db->getone($csql);
            if (!is_array($datarow))
                return errjson('数据不存在');
        }
        try {
            $db->begin();
            $updata = array();
            $updata['name'] = $name;
            $updata['power'] = $power;
            $updata['memo'] = $memo;
            $updata['uptimes'] = tostamp();
            $csql = new \ciy\sql('zc_role');
            if ($id > 0) {
                $csql->where('id', $id);
                if ($db->update($csql, $updata) === false)
                    throw new \Exception('更新失败:' . $db->error);
                if ($datarow['power'] != $power) {
                    if ($db->execute('update zc_admin set power=? where roleid=?', array($power, $id)) === false)
                        throw new \Exception('更新admin失败:' . $db->error);
                    if ($db->execute('update zc_online set usrchg=2 where user in (select id from zc_admin where roleid=?)', array($id)) === false)
                        throw new \Exception('更新online失败:' . $db->error);
                }
            } else {
                if ($db->insert($csql, $updata) === false)
                    throw new \Exception('新增失败:' . $db->error);
                $id = $db->insert_id();
            }
            $updata['id'] = $id;
            savelogdb($db, $rsuser['id'], 'zc_role', $datarow, $updata);
            $db->commit();
        } catch (\Exception $ex) {
            $db->rollback();
            savelogfile('err_db', $ex->getMessage());
            return errjson($ex->getMessage());
        }
        $ret['data'] = $updata;
        return succjson($ret);
    }
}
