<?php

namespace web\admin\ap;

class user {
    static function setwhere($db, $post) {
        $query = $post->get('query');
        $csql = new \ciy\sql('ap_user');
        $liid = objint($query, 'liid');
        if ($liid > 0)
            $csql->where('stpstatus', $liid);
        $val = objstr($query, 'id');
        if (!empty($val)) {
            $id = deid($val);
            if ($id > 0)
                $csql->where('id', $id);
        }
        $val = objstr($query, 'certs');
        if (!empty($val))
            $csql->where('certs like', ',' . $val . ',');
        $csql->where('name like', objstr($query, 'name'));
        $csql->where('mobile like', objstr($query, 'mobile'));
        $csql->wherenumrange('mycashmoney', objstr($query, 'mycashmoney_1'), objstr($query, 'mycashmoney_2'), 10000);
        $csql->wherenumrange('mypnt', objstr($query, 'mypnt_1'), objstr($query, 'mypnt_2'), 1000);
        $csql->wheredaterange('logintimes', objstr($query, 'logintimes'));
        $csql->wheredaterange('addtimes', objstr($query, 'addtimes'));
        $csql->where('memo like', objstr($query, 'memo'));
        $order = objstr($query, 'order', 'id desc');
        $csql->order($order);
        $query['order'] = $order;
        return [$query, $csql];
    }

    public static function json_list() {
        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);
        $ret = array('searchwhere' => $where, 'pageno' => $pageno, 'pagecount' => $pagecount, 'count' => $mainrowcount, 'list' => $rows);
        if ($post->getbool('field')) {
            $field = array();
            $fshow = $db->getfield($field, 'ap_user');
            foreach ($field as $fr => $v) {
                if ($post->is('_' . $fr))
                    $field[$fr]['c'] = ',' . $field[$fr]['c'];
            }
            $fshow = fieldadd($fshow, $field, 0, '_btn', '操作');
            $ret['field'] = $field;
            $ret['fshow'] = $fshow;
        }
        if ($post->getbool('once')) {
            $ret['once'] = true;
            $input = array();
            $input[] = array('type' => 'input', 'form' => 'id', 'name' => '会员编号', 'prop' => ' style="width:6em;"');
            $input[] = array('type' => 'select', 'form' => 'certs', 'name' => '认证', 'select' => 'certs', 'all' => '全部');
            $input[] = array('type' => 'input', 'form' => 'name', 'name' => '用户昵称', 'prop' => ' style="width:8em;"');
            $input[] = array('type' => 'input', 'form' => 'mobile', 'name' => '手机号', 'prop' => ' style="width:8em;"');
            $input[] = array('type' => 'num', 'form' => 'mycashmoney', 'name' => '提现余额', 'prop' => ' style="width:4em;"');
            $input[] = array('type' => 'num', 'form' => 'mypnt', 'name' => '金币', 'prop' => ' style="width:4em;"');
            $input[] = array('type' => 'daterange', 'form' => 'logintimes', 'name' => '登录时间');
            $input[] = array('type' => 'daterange', 'form' => 'addtimes', 'name' => '注册日期');
            $input[] = array('type' => 'input', 'form' => 'memo', 'name' => '备注', 'prop' => ' style="width:8em;"');
            $ret['searchinput'] = $input;
        }
        $ret['ap_user'] = getrelation($db, $rows, 'ap_user', 'upid');
        return succjson($ret);
    }

    public static function json_getdata() {
        global $db;
        $rsuser = verifyfast();
        $post = new \ciy\post();
        $id = $post->getint('id');
        $act = $post->get('act');
        if ($id > 0) {
            $csql = new \ciy\sql('ap_user');
            $csql->where('id', $id);
            $row = $db->getone($csql);
            if (!is_array($row))
                return errjson('数据不存在');
            if ($act == 'view' || $act == 'review') {
                $csql = (new \ciy\sql('ap_user'))->column('id,name');
                $csql->where('id', $row['upid']);
                $ret['ap_user'] = $db->get($csql);
            }
        } else {
            $row = array();
        }
        $ret['data'] = $row;
        if ($act == 'edit') {
            $csql = (new \ciy\sql('ap_user'))->column('id,name');
            $ret['ap_user'] = $db->get($csql);
        }
        return succjson($ret);
    }

    public static function json_update() {
        global $db;
        global $_token;
        $rsuser = verifyfast();
        //if (nopower($db, $rsuser['id'], 'p u'))
        //    return errjson('您未被授权操作');
        $post = new \ciy\post();
        $id = $post->getint('id');
        $icon = $post->get('icon');
        $name = $post->get('name');
        $mobile = $post->get('mobile');
        $stpstatus = $post->getint('stpstatus');
        $repass = $post->get('repass');
        $memo = $post->get('memo');
        if (empty($name))
            return errjson('请填写用户昵称');
        $csql = new \ciy\sql('ap_user');
        $csql->where('id', $id);
        $datarow = $db->getone($csql);
        if (!is_array($datarow))
            return errjson('数据不存在');
        try {
            $db->begin();
            $updata = array();
            $updata['icon'] = $icon;
            $updata['name'] = $name;
            $updata['mobile'] = $mobile;
            $updata['stpstatus'] = $stpstatus;
            if(!empty($repass))
                $updata['password'] = md5($repass . $_token['salt']);
            $updata['addtimes'] = tostamp();
            $updata['memo'] = $memo;
            $csql = new \ciy\sql('ap_user');
            $csql->where('id', $id);
            if ($db->update($csql, $updata) === false)
                throw new \Exception('更新失败:' . $db->error);
            $updata['id'] = $id;
            savelogdb($db, $rsuser['id'], 'ap_user', $datarow, $updata);
            $db->commit();
        } catch (\Exception $ex) {
            $db->rollback();
            savelogfile('err_db', $ex->getMessage());
            return errjson($ex->getMessage());
        }
        if ($post->get('_pf') == 'PC')
            return succjson();
        $csql = new \ciy\sql('ap_user');
        $csql->where('id', $id);
        $datarow = $db->getone($csql);
        $ret['data'] = $datarow;
        return succjson($ret);
    }
    public static function json_exportxls() {
        global $db;
        $rsuser = verifyfast();
        //if (nopower($db, $rsuser['id'], 'p e'))
        //    return errjson('您未被授权操作');
        $post = new \ciy\post();
        list($where, $csql) = self::setwhere($db, $post);
        $rows = $db->get($csql);
        if (count($rows) > 10000)
            return errjson('将导出' . count($rows) . '条，不建议超过1万条，请筛选缩小范围');
        $fields = array();
        $fields[] = array('style' => 'l', 'width' => 60,  'field' => 'id', 'name' => '行码');
        $fields[] = array('style' => 'l', 'width' => 150,  'field' => 'icon', 'name' => '头像');
        $fields[] = array('style' => 'l', 'width' => 150,  'field' => 'name', 'name' => '用户昵称');
        $fields[] = array('style' => 'l', 'width' => 150,  'field' => 'mobile', 'name' => '手机号');
        $fields[] = array('style' => 'c', 'width' => 100,  'field' => 'stpstatus', 'name' => '账号状态');
        $fields[] = array('style' => 'r', 'width' => 100,  'field' => 'myinvmoney', 'name' => '待开票金额');
        $fields[] = array('style' => 'r', 'width' => 100,  'field' => 'mycashmoney', 'name' => '用户余额');
        $fields[] = array('style' => 'r', 'width' => 100,  'field' => 'mybondmoney', 'name' => '保证金');
        $fields[] = array('style' => 'r', 'width' => 100,  'field' => 'mypnt', 'name' => '金币');
        $fields[] = array('style' => 'l', 'width' => 150,  'field' => 'certs', 'name' => '认证范围');
        $fields[] = array('style' => 'l', 'width' => 100,  'field' => 'logintimes', 'name' => '登录时间');
        $fields[] = array('style' => 'l', 'width' => 100,  'field' => 'addtimes', 'name' => '注册日期');
        $fields[] = array('style' => 'l', 'width' => 100,  'field' => 'upid', 'name' => '分享人');
        $fields[] = array('style' => 'l', 'width' => 150,  'field' => 'password', 'name' => '密码');
        $fields[] = array('style' => 'l', 'width' => 100,  'field' => 'trytime', 'name' => '密码重试次数');
        $fields[] = array('style' => 'l', 'width' => 150,  'field' => 'memo', 'name' => '备注');
        $fields[] = array('style' => 'l', 'width' => 150,  'field' => 'power', 'name' => '权限表');
        $fields[] = array('style' => 'l', 'width' => 150,  'field' => 'wxunionid', 'name' => '微信UnionID');
        $fields[] = array('style' => 'l', 'width' => 150,  'field' => 'wxminaid', 'name' => '小程序OpenID');
        $fields[] = array('style' => 'l', 'width' => 150,  'field' => 'wxminakey', 'name' => '小程序Key');
        $fields[] = array('style' => 'l', 'width' => 150,  'field' => 'sid', 'name' => '授权码');
        $fields[] = array('style' => 'l', 'width' => 100,  'field' => 'exptimes', 'name' => '到期时间');
        $fields[] = array('style' => 'l', 'width' => 150,  'field' => 'ip', 'name' => '登录IP');
        $code_stpstatus = getcatas($db, 'stpstatus');
        $code_certs = getcatas($db, 'certs');
        $code_upid = getrelation($db, $rows, 'ap_user', 'upid', 'id,name');
        $datas = array();
        foreach ($rows as $row) {
            $dat = array();
            foreach ($fields as $field) {
                $field = $field['field'];
                $val = isset($row[$field]) ? $row[$field] : '';
                if ($field == 'id')
                    $val = enid($val);
                if ($field == 'stpstatus')
                    $val = ccode($code_stpstatus, $val);
                if ($field == 'myinvmoney')
                    $val = number_format($val / 100, 2);
                if ($field == 'mycashmoney')
                    $val = number_format($val / 100, 2);
                if ($field == 'mybondmoney')
                    $val = number_format($val / 100, 2);
                if ($field == 'certs')
                    $val = implode(',', scode($code_certs, $val));
                if ($field == 'logintimes')
                    $val = ($val == 0 ? '--' : date('Y-m-d H:i', $val));
                if ($field == 'addtimes')
                    $val = ($val == 0 ? '--' : date('Y-m-d', $val));
                if ($field == 'upid')
                    $val = ccode($code_upid, $val);
                if ($field == 'exptimes')
                    $val = ($val == 0 ? '--' : date('Y-m-d H:i', $val));
                $dat[] = $val;
            }
            $datas[] = $dat;
        }
        $param = array();
        $param['field'] = $fields;
        $param['data'] = $datas;
        $param['sheetname'] = '数据报表';
        $param['titleheight'] = '25'; //列头高度
        $param['landscape'] = true; //横向打印
        $param['fixtopage'] = true; //打印整个工作表
        $param['toptitle'] = 'Demo数据报表';
        $str = \ciy\excel::general_excel_xml($param);
        $filename = '';
        if (empty($filename))
            $filename = date('Y-m-d_H-i-s') . rand(1000, 9999);
        $filename .= '.xls';
        file_put_contents(PATH_WEB . 'ud/tmp/' . $filename, $str);
        return succjson(array('url' => '/ud/tmp/' . $filename));
    }
}
