<?php

namespace web\admin\rigger;

class mock {
    static function setwhere($db, $post) {
        $query = $post->get('query', array());
        $csql = new \ciy\sql('zc_ymock');
        $csql->where('name like', objstr($query, 'name'));
        $csql->where('ufunc like', objstr($query, 'ufunc'));
        $order = objstr($query, 'order', 'id desc');
        $csql->order($order);
        $query['order'] = $order;
        return [$query, $csql];
    }
    public static function json_init() {
        global $db;
        $rsuser = verifyfast();
        $post = new \ciy\post();
        list($where, $csql) = self::setwhere($db, $post);
        $pageno = $post->getint('pageno', 1);
        $pagecount = $post->getint('pagecount', 10);
        $csql->limit($pageno, $pagecount);
        $mainrowcount = $post->getint('count');
        $rows = $db->get($csql, $mainrowcount);
        $rows[] = array('id' => 0, 'name' => '', 'ufunc' => '', 'mockjson' => '{"code":1}');
        $ret = array('searchwhere' => $where, 'pageno' => $pageno, 'pagecount' => $pagecount, 'count' => $mainrowcount, 'list' => $rows);
        if ($post->getbool('field')) {
            $field = array();
            $fshow = '';
            $fshow = fieldadd($fshow, $field, -1, 'mock', 'Mock');
            $fshow = fieldadd($fshow, $field, -1, 'mockjson', 'JSON');
            $fshow = fieldadd($fshow, $field, -1, '_btn', '操作');
            $field['mock']['thwidth'] = '20em';
            $field['mockjson']['thwidth'] = '50em';
            $ret['fshow'] = $fshow;
            $ret['field'] = $field;
        }
        if ($post->getbool('once')) {
            $ret['once'] = true;
            $input = array();
            $input[] = array(
                'type' => 'input', 'form' => 'name', 'name' => '名称', 'prop' => ' style="width:8em;"'
            );
            $input[] = array(
                'type' => 'input', 'form' => 'ufunc', 'name' => '类.函数', 'prop' => ' style="width:8em;"'
            );
            $ret['searchinput'] = $input;
        }
        return succjson($ret);
    }

    public static function json_update() {
        global $db;
        $rsuser = verifyfast();
        if (nopower($db, $rsuser['id'], 'p942u'))
            return errjson('您未被授权操作');
        $post = new \ciy\post();
        $id = $post->getint('id');
        $name = $post->get('name');
        $ufunc = $post->get('ufunc');
        $mockjson = $post->get('mockjson', '', 'all');
        if (empty($mockjson))
            return errjson('请填写JSON');
        if (substr($mockjson, 0, 7) != 'http://') {
            $jsn = json_decode($mockjson, true);
            if ($jsn == null)
                return errjson('JSON格式错误');
        }
        if (empty($ufunc))
            return errjson('请填写类.函数');


        $datarow = null;
        if ($id > 0) {
            $csql = new \ciy\sql('zc_ymock');
            $csql->where('id', $id);
            $datarow = $db->getone($csql);
            if (!is_array($datarow))
                return errjson('数据不存在');
        }
        try {
            $db->begin();
            $csql = new \ciy\sql('zc_ymock');
            $csql->where('ufunc', $ufunc);
            $csql->column('id');
            $chkid = toint($db->get1($csql));
            if ($chkid > 0 && (($id > 0 && $chkid != $id) || $id == 0))
                throw new \Exception('CIYIGN类函数重复');

            $updata = array();
            $updata['name'] = $name;
            $updata['ufunc'] = $ufunc;
            $updata['mockjson'] = $mockjson;
            $csql = new \ciy\sql('zc_ymock');
            if ($id > 0) {
                $csql->where('id', $id);
                if ($db->update($csql, $updata) === false)
                    throw new \Exception('更新失败:' . $db->error);
            } else {
                $updata['addtimes'] = tostamp();
                if ($db->insert($csql, $updata) === false)
                    throw new \Exception('更新失败:' . $db->error);
                $id = $db->insert_id();
            }
            $updata['id'] = $id;
            $db->commit();
        } catch (\Exception $ex) {
            $db->rollback();
            savelogfile('err_db', $ex->getMessage());
            return errjson($ex->getMessage());
        }
        $ret['data'] = $updata;
        return succjson($ret);
    }

    public static function json_del() {
        global $db;
        $rsuser = verifyfast();

        if (nopower($db, $rsuser['id'], 'p942u'))
            return errjson('您未被授权操作');
        $post = new \ciy\post();
        $ids = $post->get('ids');
        if (empty($ids))
            return errjson('请选择至少一条');
        $csql = new \ciy\sql('zc_ymock');
        $csql->where('id in', $ids);
        $rows = $db->get($csql);
        $vids = array();
        try {
            $db->begin();
            foreach ($rows as $row) {
                $delid = $row['id'];
                delme($db, $delid, 'zc_ymock');
                $vids[] = $delid;
            }
            $db->commit();
        } catch (\Exception $ex) {
            $db->rollback();
            savelogfile('err_db', $ex->getMessage());
            return errjson($ex->getMessage());
        }
        $ret['ids'] = $vids;
        return succjson($ret);
    }
}
