<?php

namespace web\admin\ap;

class usr_buy_user {
    static function setwhere($db, $post) {
        $query = $post->get('query');
        $csql = new \ciy\sql('ap_usr_buy_user');
        $liid = objint($query, 'liid');
        if ($liid == 1)
            $csql->where('paytimes=0');
        if ($liid == 2)
            $csql->where('paytimes>0');
        $val = objstr($query, 'vuser');
        if (!empty($val)) {
            $csqlt = new \ciy\sql('ap_user');
            $csqlt->where('name like', $val);
            $trow = $db->getone($csqlt);
            if (is_array($trow)) {
                $csql->where('vuser', $trow['id']);
                $query['vuser'] = $trow['name'];
            } else {
                $csql->where('vuser=0');
            }
        }
        $val = objstr($query, 'bundleid');
        if (!empty($val)) {
            $csqlt = new \ciy\sql('ap_usr_buy_bundle');
            $csqlt->where('name like', $val);
            $trow = $db->getone($csqlt);
            if (is_array($trow)) {
                $csql->where('bundleid', $trow['id']);
                $query['bundleid'] = $trow['name'];
            } else {
                $csql->where('bundleid=0');
            }
        }
        $csql->where('userlevel', objstr($query, 'userlevel'));
        $csql->wherenumrange('days', objstr($query, 'days_1'), objstr($query, 'days_2'), 1);
        $csql->wherenumrange('buydaymoney', objstr($query, 'buydaymoney_1'), objstr($query, 'buydaymoney_2'), 100);
        $csql->wheredaterange('addtimes', objstr($query, 'addtimes'));
        $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);
        if($rows === false)
            return errjson($db->error);
        $ret = array('searchwhere' => $where, 'pageno' => $pageno, 'pagecount' => $pagecount, 'count' => $mainrowcount, 'list' => $rows);
        if ($post->getbool('field')) {
            $field = array();
            $fshow = $db->getfield($field, 'ap_usr_buy_user');
            foreach ($field as $fr => $v) {
                if (get('_' . $fr))
                    $field[$fr]['c'] = ',' . $field[$fr]['c'];
            }
            $ret['field'] = $field;
            $ret['fshow'] = $fshow;
        }
        if ($post->getbool('once')) {
            $ret['once'] = true;
            $input = array();
            $input[] = array('type' => 'input', 'form' => 'vuser', 'name' => '所属用户', 'prop' => ' style="width:8em;"');
            $input[] = array('type' => 'input', 'form' => 'bundleid', 'name' => '所属套餐', 'prop' => ' style="width:8em;"');
            $input[] = array('type' => 'select', 'form' => 'userlevel', 'name' => '对应等级', 'all' => '全部', 'select' => 'userlevel');
            $input[] = array('type' => 'num', 'form' => 'days', 'name' => '会员天数', 'prop' => ' style="width:4em;"');
            $input[] = array('type' => 'num', 'form' => 'buydaymoney', 'name' => '金额', 'prop' => ' style="width:4em;"');
            $input[] = array('type' => 'daterange', 'form' => 'addtimes', 'name' => '提交时间');
            $ret['searchinput'] = $input;
        }
        $ret['ap_user'] = getrelation($db, $rows, 'ap_user', 'vuser');
        $ret['ap_usr_buy_bundle'] = getrelation($db, $rows, 'ap_usr_buy_bundle', 'bundleid');
        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_usr_buy_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['vuser']);
                $ret['ap_user'] = $db->get($csql);
                $csql = (new \ciy\sql('ap_usr_buy_bundle'))->column('id,name');
                $csql->where('id', $row['bundleid']);
                $ret['ap_usr_buy_bundle'] = $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);
            $csql = (new \ciy\sql('ap_usr_buy_bundle'))->column('id,name');
            $ret['ap_usr_buy_bundle'] = $db->get($csql);
        }
        return succjson($ret);
    }


    public static function json_del() {
        global $db;
        $rsuser = verifyfast();
        //if (nopower($db, $rsuser['id'], 'p d'))
        //    return errjson('您未被授权操作');
        $post = new \ciy\post();
        $ids = $post->get('ids');
        if (empty($ids))
            return errjson('请选择至少一条');
        $csql = new \ciy\sql('ap_usr_buy_user');
        $csql->where('id in', $ids);
        $rows = $db->get($csql);
        $vids = array();
        try {
            $db->begin();
            foreach ($rows as $row) {
                $delid = $row['id'];
                //delcheck($db, $delid, 'tablexx', 'xxid', '管理员');
                //delall($db, $delid, 'tablexx', 'xxid', '运动员');  //deltimeall
                delme($db, $delid, 'ap_usr_buy_user');
                savelogdb($db, $rsuser['id'], 'ap_usr_buy_user', $row, null);
                $vids[] = $delid;
            }
            $db->commit();
        } catch (\Exception $ex) {
            $db->rollback();
            savelogfile('err_db', $ex->getMessage());
            return errjson($ex->getMessage());
        }
        $ret['ids'] = $vids;
        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' => 100,  'field' => 'vuser', 'name' => '所属用户');
        $fields[] = array('style' => 'l', 'width' => 100,  'field' => 'bundleid', 'name' => '所属套餐');
        $fields[] = array('style' => 'l', 'width' => 100,  'field' => 'userlevel', 'name' => '对应等级');
        $fields[] = array('style' => 'r', 'width' => 100,  'field' => 'days', 'name' => '会员天数');
        $fields[] = array('style' => 'r', 'width' => 100,  'field' => 'buydaymoney', 'name' => '金额');
        $fields[] = array('style' => 'l', 'width' => 100,  'field' => 'addtimes', 'name' => '提交时间');
        $fields[] = array('style' => 'l', 'width' => 100,  'field' => 'paytimes', 'name' => '支付时间');
        $code_vuser = getrelation($db, $rows, 'ap_user', 'vuser', 'id,name');
        $code_bundleid = getrelation($db, $rows, 'ap_usr_buy_bundle', 'bundleid', 'id,name');
        $code_userlevel = getcatas($db, 'userlevel');
        $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 == 'vuser')
                    $val = ccode($code_vuser, $val);
                if ($field == 'bundleid')
                    $val = ccode($code_bundleid, $val);
                if ($field == 'userlevel')
                    $val = ccode($code_userlevel, $val);
                if ($field == 'buydaymoney')
                    $val = number_format($val / 100, 2);
                if ($field == 'addtimes')
                    $val = ($val == 0 ? '--' : date('Y-m-d H:i', $val));
                if ($field == 'paytimes')
                    $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));
    }
}
