get('query'); $csql = new \ciy\sql('ap_usr_buy_bundle'); $csql->where('name like', objstr($query, 'name')); $csql->where('userlevel', objstr($query, 'userlevel')); $csql->wherenumrange('days', objstr($query, 'days_1'), objstr($query, 'days_2'), 1); $csql->wherenumrange('usermoney', objstr($query, 'usermoney_1'), objstr($query, 'usermoney_2'), 100); $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_bundle'); foreach ($field as $fr => $v) { if (get('_' . $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' => 'name', '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' => 'usermoney', 'name' => '金额', 'prop' => ' style="width:4em;"'); $ret['searchinput'] = $input; } return succjson($ret); } public static function json_update() { global $db; $rsuser = verifyfast(); //if (nopower($db, $rsuser['id'], 'p u')) // return errjson('您未被授权操作'); $post = new \ciy\post(); $id = $post->getint('id'); $name = $post->get('name'); $userlevel = $post->getint('userlevel'); $days = $post->getint('days'); $usermoney = $post->getint('usermoney'); if (empty($name)) return errjson('请填写套餐说明'); if ($userlevel == 0) return errjson('请填写对应等级'); if ($days == 0) return errjson('请填写天数'); if ($usermoney == 0) return errjson('请填写金额'); $datarow = null; if ($id > 0) { $csql = new \ciy\sql('ap_usr_buy_bundle'); $csql->where('id', $id); $datarow = $db->getone($csql); if (!is_array($datarow)) return errjson('数据不存在'); } try { $db->begin(); $updata = array(); $updata['name'] = $name; $updata['userlevel'] = $userlevel; $updata['days'] = $days; $updata['usermoney'] = $usermoney; $csql = new \ciy\sql('ap_usr_buy_bundle'); if ($id > 0) { $csql->where('id', $id); if ($db->update($csql, $updata) === false) throw new \Exception('更新失败:' . $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'], 'ap_usr_buy_bundle', $datarow, $updata); $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'], 'p d')) // return errjson('您未被授权操作'); $post = new \ciy\post(); $ids = $post->get('ids'); if (empty($ids)) return errjson('请选择至少一条'); $csql = new \ciy\sql('ap_usr_buy_bundle'); $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_bundle'); savelogdb($db, $rsuser['id'], 'ap_usr_buy_bundle', $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' => 150, 'field' => 'name', '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' => 'usermoney', '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 == 'userlevel') $val = ccode($code_userlevel, $val); if ($field == 'usermoney') $val = number_format($val / 100, 2); $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)); } public static function json_importxls_in() { global $db; $rsuser = verifyfast(); //if (nopower($db, $rsuser['id'], 'p i')) // return errjson('您未被授权操作'); $post = new \ciy\post(); $file = PATH_WEB . '/ud' . $post->get('file'); if (!file_exists($file)) return errjson('文件不存在'); require_once PATH_ROOT . '../libs/phpoffice/autoload.php'; $spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load($file); $sheet = $spreadsheet->getActiveSheet(); $datas = $sheet->toArray('', true, true, false); $datacnt = count($datas); if ($datacnt < 2) return errjson('数据为空'); $html = ''; $headsn = array(); $headsn[] = '行码.id'; $headsn[] = '套餐说明.name'; $headsn[] = '对应等级.userlevel'; $headsn[] = '天数.days'; $headsn[] = '金额.usermoney'; $xlsidx = 1; if (empty($datas[0][count($headsn) - 1])) $xlsidx = 2; $heads = array(); foreach ($headsn as $_head) { $hd = explode('.', $_head); if (count($hd) < 2) continue; $heads[] = array( 'idx' => array_search($hd[0], $datas[$xlsidx - 1]), 'fld' => $hd[1], 'name' => $hd[0] ); } $code_userlevel = getcatas($db, 'userlevel'); $html .= '
'; $html .= ''; $html .= ''; foreach ($heads as $arr) { $html .= ''; } $html .= ''; $cnt = 0; $uniques = array(); $id = 0; for ($rowidx = $xlsidx; $rowidx < $datacnt; $rowidx++) { $lineidx = $rowidx - $xlsidx + 1; $hrhtml = ''; $firsthtml = ''; $bempty = true; $unqs = array(); $csql = new \ciy\sql('ap_usr_buy_bundle'); foreach ($heads as $arr) { $name = $arr['name']; $errmsg = ''; //数据有误,显示红色说明 $showdat = ''; //显示在表格中的数据 if ($arr['idx'] > -1) $showdat = trim($datas[$rowidx][$arr['idx']]); if ($showdat == '--') $showdat = ''; $value = $showdat; //在表单中的数据(转换后) $ext = ''; //扩展表单 if ($name == '行码') { if (empty($showdat)) { $value = 0; $showdat = '新增'; } else { $id = deid($showdat); if ($id == 0) $errmsg = $name . '解析错误'; else { $csqlchk = new \ciy\sql('ap_usr_buy_bundle'); $csqlchk->where('id', $id)->column('id'); $chkid = toint($db->get1($csqlchk)); if ($chkid != $id) $errmsg = $name . '在数据库中不存在'; $value = $id; } } } else if ($name == '套餐说明') { if (empty($showdat)) { $errmsg = $name . '为必填项'; } else { } } else if ($name == '对应等级') { if (empty($showdat)) { $errmsg = $name . '为必填项'; } else { $value = dcode($code_userlevel, $showdat); if ($value == -1) $errmsg = $name . '文字与系统数据不匹配'; } } else if ($name == '天数') { if (empty($showdat)) { $errmsg = $name . '为必填项'; } else { $showdat = str_replace(',', '', $showdat); if (!is_numeric($showdat)) $errmsg = $name . '不是数字'; else $value = toint($showdat); } } else if ($name == '金额') { if (empty($showdat)) { $errmsg = $name . '为必填项'; } else { $showdat = str_replace(',', '', $showdat); if (!is_numeric($showdat)) $errmsg = $name . '不是数字'; else $value = toint((float)$showdat * 100); } } if (!empty($showdat)) $bempty = false; if (empty($errmsg)) $hrhtml .= ''; else $hrhtml .= ''; } if ($bempty) continue; if (count($unqs) > 0) { $unq = implode('|', $unqs); if (in_array($unq, $uniques)) $firsthtml = ''; else { $uniques[] = $unq; $csql->column('id'); $chkid = toint($db->get1($csql)); if ($chkid > 0 && (($id > 0 && $chkid != $id) || $id == 0)) $firsthtml = ''; } } $html .= '' . $firsthtml . $hrhtml . ''; $cnt++; } $html .= '
#' . $arr['name'] . '
' . $lineidx . '
' . $showdat . '' . $ext . '
' . $showdat . '
重复
重复
'; $html .= ''; $html .= '共' . $cnt . '条数据'; return succjson(array('html' => $html, 'count' => $cnt)); } public static function json_importxls_data() { global $db; $rsuser = verifyfast(); //if (nopower($db, $rsuser['id'], 'p i')) // return errjson('您未被授权操作'); $post = new \ciy\post(); $total = $post->getint('total'); try { $db->begin(); for ($i = 1; $i <= $total; $i++) { $id = $post->getint('id_' . $i); $updata = array(); $updata['name'] = $post->get('name_' . $i); $updata['userlevel'] = $post->get('userlevel_' . $i); $updata['days'] = $post->get('days_' . $i); $updata['usermoney'] = $post->get('usermoney_' . $i); $csql = new \ciy\sql('ap_usr_buy_bundle'); if ($id == 0) { if ($db->insert($csql, $updata) === false) throw new \Exception('新增导入失败:' . $db->error); } else { $csql->where('id', $id); if ($db->update($csql, $updata) === false) throw new \Exception('更新导入失败:' . $db->error); } } $db->commit(); } catch (\Exception $ex) { $db->rollback(); savelogfile('err_db', $ex->getMessage()); return errjson($ex->getMessage()); } return succjson(); } }