<?php

namespace web\ambap\demo;

class demo {
    static function setwhere($db, $post) {
        $query = $post->get('query');
        $csql = new \ciy\sql('demo_normal');
        $liid = objint($query, 'liid');
        if ($liid > 0)
            $csql->where('auditstatus', $liid);
        $csql->wheredaterange('audittimes', objstr($query, 'audittimes'));
        $csql->where('name like', objstr($query, 'name'));
        $val = objstr($query, 'menuid');
        if (!empty($val)) {
            $csqlt = new \ciy\sql('zc_menu');
            $csqlt->where('name like', $val);
            $trow = $db->getone($csqlt);
            if (is_array($trow)) {
                $csql->where('menuid', $trow['id']);
                $query['menuid'] = $trow['name'];
            } else {
                $csql->where('menuid=0');
            }
        }
        $csql->wheredaterange('setdate', objstr($query, 'setdate'), 'month');
        $csql->wheredaterange('settimes', objstr($query, 'settimes'), 'day');
        $csql->wherenumrange('bankmoney', objstr($query, 'bankmoney_1'), objstr($query, 'bankmoney_2'), 1000000);
        $csql->where('isopen', objstr($query, 'isopen'));
        // if ($post->is('isopen'))
        //     $csql->where('isopen', $post->getbool('isopen'));
        $csql->where('mauditstatus like', objstr($query, 'mauditstatus'));
        $val = objint($query, 'renzheng');
        if ($val > 0) {
            $val = 1 << ($val - 1);
            $csql->where('renzheng&' . $val . '=' . $val);
        }
        $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);
        $csql->column('!content', $db->getraw('show full fields from demo_normal'));
        $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, 'demo_normal');
            foreach ($field as $fr => $v) {
                if ($post->is('_' . $fr))
                    $field[$fr]['c'] = ',' . $field[$fr]['c'];
            }
            $fshow = fieldadd($fshow, $field, 0, '_btn', '操作');
            $field['filesize']['order'] = 'r';
            $field['audittimes']['order'] = 'l';
            $field['bankmoney']['order'] = 'l';
            $field['settimes']['order'] = 'l';
            $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' => 'input',
                'form' => 'menuid',
                'name' => '所属菜单',
                'prop' => ' style="width:8em;"'
            );
            $input[] = array(
                'type' => 'daterange',
                'form' => 'audittimes',
                'name' => '日期范围'
            );
            $input[] = array(
                'type' => 'month',
                'form' => 'setdate',
                'name' => '按月'
            );
            $input[] = array(
                'type' => 'day',
                'form' => 'settimes',
                'name' => '按日'
            );
            $input[] = array(
                'type' => 'num',
                'form' => 'bankmoney',
                'name' => '金额区间',
                'prop' => ' style="width:4em;"'
            );
            $vals = $db->getdbcodes('demo_normal', 'isopen');
            $input[] = array(
                'type' => 'radio',
                'form' => 'isopen',
                'name' => '是否开启',
                'select' => $vals,
                'all' => '全部'
            );
            $input[] = array(
                'type' => 'bool',
                'form' => 'locc',
                'name' => '搜附近',
                'value' => '附近,全部',
                'def' => 2,
                'tip' => '水电费'
            );
            $input[] = array(
                'type' => 'select',
                'form' => 'mauditstatus',
                'name' => '多选状态',
                'select' => 'auditstatus',
                'all' => '全部'
            );
            $input[] = array(
                'type' => 'select',
                'form' => 'renzheng',
                'name' => '认证情况',
                'select' => 'renzheng',
                'all' => '全部'
            );
            $vals = array();
            $vals[] = array('id' => 'id desc', 'name' => '默认排序');
            $vals[] = array('id' => 'audittimes desc', 'name' => '审核时间排序');
            $vals[] = array('id' => 'bankmoney', 'name' => '贷款金额小到大');
            $vals[] = array('id' => 'bankmoney desc', 'name' => '贷款金额大到小');

            $input[] = array(
                'type' => 'select',
                'form' => 'order',
                'name' => '排序',
                'select' => $vals
            );
            $ret['searchinput'] = $input;
            $csql = (new \ciy\sql('zc_depart'))->column('id,name,upid');
            $ret['zc_depart'] = $db->get($csql); //列表数据较少，一次性加载
            $ret['ppint'] = $db->getdbcodes('demo_normal', 'ppint');
        }
        $ret['xa_user'] = getrelation($db, $rows, 'xa_user', 'audituser'); //列表数据较多，按需加载
        $ret['zc_menu'] = getrelation($db, $rows, 'zc_menu', 'menuid'); //列表数据较多，按需加载
        return succjson($ret);
    }
    public static function json_getdata() {
        global $db;
        $rsuser = verifyfast();
        $post = new \ciy\post();
        $csql = new \ciy\sql('demo_normal');
        $id = $post->getint('id');
        $csql->where('id', $id);
        $row = $db->getone($csql);
        if (!is_array($row))
            $row = new \emptyclass;
        $ret['data'] = $row;
        if ($post->get('act') == 'edit') {
            $csql = (new \ciy\sql('zc_menu'))->column('id,name');
            $ret['zc_menu'] = $db->get($csql);
        }
        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');
        $menuid = $post->getint('menuid');
        $filesize = $post->getint('filesize');
        $metre = $post->getint('metre');
        $bankmoney = $post->getint('bankmoney');
        $setdate = $post->getint('setdate');
        $settimes = $post->getint('settimes');
        $dayclock = $post->getint('dayclock');
        $downurl = $post->get('downurl');
        $avar = $post->get('avar');
        $isuse = $post->getint('isuse');
        $isopen = $post->getint('isopen');
        $unit = $post->get('unit');
        $sigstatus = $post->getint('sigstatus');
        $mauditstatus = $post->get('mauditstatus');
        $prodcata = $post->getint('prodcata');
        $areacode = $post->getint('areacode');
        $renzheng = $post->getint('renzheng');
        $ppint = $post->getint('ppint');
        $npcyc = $post->getint('npcyc');
        $runsec = $post->getint('runsec');
        $acttm = $post->getint('acttm');
        $imgs = $post->get('imgs');
        $ton = $post->getint('ton');
        $price = $post->getint('price');
        $lat = $post->getint('lat');
        $lng = $post->getint('lng');
        $pct = $post->getint('pct');
        $url = $post->get('url');
        $weightg = $post->getint('weightg');
        $eartmpr = $post->getint('eartmpr');
        $idcard = $post->get('idcard');
        $content = $post->get('content');
        $md = $post->get('md');
        if (empty($name))
            return errjson('请填写默认标题');
        $datarow = null;
        if ($id > 0) {
            $csql = new \ciy\sql('demo_normal');
            $csql->where('id', $id);
            $datarow = $db->getone($csql);
            if (!is_array($datarow))
                return errjson('数据不存在');
        }
        try {
            $db->begin();
            $csql = new \ciy\sql('demo_normal');
            $csql->where('name', $name);
            $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['menuid'] = $menuid;
            $updata['filesize'] = $filesize;
            $updata['metre'] = $metre;
            $updata['bankmoney'] = $bankmoney;
            $updata['setdate'] = $setdate;
            $updata['settimes'] = $settimes;
            $updata['dayclock'] = $dayclock;
            $updata['downurl'] = $downurl;
            $updata['avar'] = $avar;
            $updata['isuse'] = $isuse;
            $updata['isopen'] = $isopen;
            $updata['unit'] = $unit;
            $updata['sigstatus'] = $sigstatus;
            $updata['mauditstatus'] = $mauditstatus;
            $updata['prodcata'] = $prodcata;
            $updata['areacode'] = $areacode;
            $updata['renzheng'] = $renzheng;
            $updata['ppint'] = $ppint;
            $updata['npcyc'] = $npcyc;
            $updata['runsec'] = $runsec;
            $updata['acttm'] = $acttm;
            $updata['imgs'] = $imgs;
            $updata['ton'] = $ton;
            $updata['price'] = $price;
            $updata['lat'] = $lat;
            $updata['lng'] = $lng;
            $updata['pct'] = $pct;
            $updata['url'] = $url;
            $updata['weightg'] = $weightg;
            $updata['eartmpr'] = $eartmpr;
            $updata['idcard'] = $idcard;
            $updata['content'] = $content;
            $updata['md'] = $md;
            $csql = new \ciy\sql('demo_normal');
            if ($id > 0) {
                $csql->where('id', $id);
                if ($db->update($csql, $updata) === false)
                    throw new \Exception('更新失败:' . $db->error);
            } else {
                $updata['auditstatus'] = 20;
                $updata['addtimes'] = tostamp();
                if ($db->insert($csql, $updata) === false)
                    throw new \Exception('更新失败:' . $db->error);
                $id = $db->insert_id();
                $updata['id'] = $id;
            }
            savelogdb($db, $rsuser['id'], 'demo_normal', $datarow, $updata);
            $db->commit();
        } catch (\Exception $ex) {
            $db->rollback();
            savelogfile('err_db', $ex->getMessage());
            return errjson($ex->getMessage());
        }
        $csql = new \ciy\sql('demo_normal');
        $csql->where('id', $id);
        $datarow = $db->getone($csql);
        $ret['data'] = $datarow;
        return succjson($ret);
    }
    public static function json_audit() {
        global $db;
        $rsuser = verifyfast();
        //if (nopower($db, $rsuser['id'], 'p a'))
        //    return errjson('您未被授权操作');
        $post = new \ciy\post();
        $status = $post->getint('status');
        $msg = $post->get('msg');
        $id = $post->getint('id');
        if ($status == 90 && empty($msg))
            return errjson('请填写驳回原因');
        $csql = new \ciy\sql('demo_normal');
        $csql->where('id', $id);
        $row = $db->getone($csql);
        if (!is_array($row))
            return errjson('数据不存在');
        try {
            $db->begin();
            $updata = array();
            $updata['auditstatus'] = $status;
            $updata['auditmsg'] = $msg;
            $updata['audituser'] = $rsuser['id'];
            $updata['audittimes'] = tostamp();
            $csql = new \ciy\sql('demo_normal');
            $csql->where('id', $row['id']);
            if ($db->update($csql, $updata) === false)
                throw new \Exception('审核失败:' . $db->error);
            $updata['id'] = $row['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'], 'p d'))
        //    return errjson('您未被授权操作');

        $post = new \ciy\post();
        $id = $post->getint('id');
        $csql = new \ciy\sql('demo_normal');
        $csql->where('id', $id);
        $row = $db->getone($csql);
        if (!is_array($row))
            return errjson('数据不存在');
        try {
            $db->begin();
            $delid = $row['id'];
            //delcheck($db, $delid, 'tablexx', 'xxid', '管理员');
            //delall($db, $delid, 'tablexx', 'xxid', '运动员');
            delme($db, $delid, 'demo_normal');
            savelogdb($db, $rsuser['id'], 'demo_normal', $row, null);
            $db->commit();
        } catch (\Exception $ex) {
            $db->rollback();
            savelogfile('err_db', $ex->getMessage());
            return errjson($ex->getMessage());
        }
        return succjson();
    }
    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('field' => 'id', 'name' => '行码', 'width' => 60, 'style' => 'c');
        $fields[] = array('style' => 'c', 'width' => 100, 'field' => 'auditstatus', 'name' => '审核状态');
        $fields[] = array('style' => 'l', 'width' => 100, 'field' => 'audituser', 'name' => '审核人');
        $fields[] = array('style' => 'l', 'width' => 100, 'field' => 'audittimes', 'name' => '审核时间');
        $fields[] = array('style' => 'l', 'width' => 100, 'field' => 'name', 'name' => '默认标题');
        $fields[] = array('style' => 'l', 'width' => 100, 'field' => 'menuid', 'name' => '所属菜单');
        $fields[] = array('style' => 'r', 'width' => 100, 'field' => 'filesize', 'name' => '文件大小');
        $fields[] = array('style' => 'r', 'width' => 100, 'field' => 'metre', 'name' => '长度');
        $fields[] = array('style' => 'r', 'width' => 100, 'field' => 'bankmoney', 'name' => '贷款金额');
        $fields[] = array('style' => 'l', 'width' => 100, 'field' => 'setdate', 'name' => '设置日期');
        $fields[] = array('style' => 'l', 'width' => 100, 'field' => 'settimes', 'name' => '设置时间');
        $fields[] = array('style' => 'c', 'width' => 100, 'field' => 'isuse', 'name' => '是否使用');
        $fields[] = array('style' => 'c', 'width' => 100, 'field' => 'isopen', 'name' => '是否开启');
        $fields[] = array('style' => 'l', 'width' => 100, 'field' => 'unit', 'name' => '库存单位');
        $fields[] = array('style' => 'l', 'width' => 100, 'field' => 'sigstatus', 'name' => '单选状态');
        $fields[] = array('style' => 'l', 'width' => 100, 'field' => 'mauditstatus', 'name' => '多选状态');
        $fields[] = array('style' => 'l', 'width' => 100, 'field' => 'prodcata', 'name' => '商品分类');
        $fields[] = array('style' => 'l', 'width' => 100, 'field' => 'areacode', 'name' => '所在地区');
        $fields[] = array('style' => 'l', 'width' => 100, 'field' => 'renzheng', 'name' => '认证情况');
        $fields[] = array('style' => 'c', 'width' => 100, 'field' => 'npcyc', 'name' => '执行周期');
        $fields[] = array('style' => 'r', 'width' => 100, 'field' => 'runsec', 'name' => '执行用时');
        $fields[] = array('style' => 'r', 'width' => 100, 'field' => 'acttm', 'name' => '活动期数');
        $fields[] = array('style' => 'r', 'width' => 100, 'field' => 'ton', 'name' => '吨位');
        $fields[] = array('style' => 'r', 'width' => 100, 'field' => 'price', 'name' => '单价');
        $fields[] = array('style' => 'l', 'width' => 100, 'field' => 'lat', 'name' => '纬度');
        $fields[] = array('style' => 'c', 'width' => 100, 'field' => 'lng', 'name' => '位置');
        $fields[] = array('style' => 'l', 'width' => 100, 'field' => 'url', 'name' => '链接');
        $fields[] = array('style' => 'l', 'width' => 100, 'field' => 'idcard', 'name' => '身份证号');
        $fields[] = array('style' => 'r', 'width' => 100, 'field' => 'weightg', 'name' => '体重');
        $fields[] = array('style' => 'r', 'width' => 100, 'field' => 'eartmpr', 'name' => '耳温');
        $fields[] = array('style' => 'l', 'width' => 100, 'field' => 'content', 'name' => '介绍');
        $fields[] = array('style' => 'l', 'width' => 100, 'field' => 'addtimes', 'name' => '添加时间');
        $code_auditstatus = getcatas($db, 'auditstatus');
        $code_audituser = getrelation($db, $rows, 'zc_admin', 'audituser');
        $code_userid = $db->get((new \ciy\sql('zc_admin'))->column('id,name'));
        $code_menuid = $db->get((new \ciy\sql('zc_menu'))->column('id,name'));
        $code_isuse = $db->getdbcodes('demo_normal', 'isuse');
        $code_isopen = $db->getdbcodes('demo_normal', 'isopen');
        $code_mauditstatus = getcatas($db, 'auditstatus');
        $code_prodcata = getcatas($db, 'prodcata');
        $code_areacode = $db->get((new \ciy\sql('ciy_arearpc'))->column('id,name,upid'));
        $code_renzheng = getcatas($db, 'renzheng');
        $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 == 'auditstatus')
                    $val = ccode($code_auditstatus, $val);
                if ($field == 'audituser')
                    $val = ccode($code_audituser, $val);
                if ($field == 'audittimes')
                    $val = ($val == 0 ? '--' : date('Y-m-d H:i', $val));
                if ($field == 'menuid')
                    $val = ccode($code_menuid, $val);
                if ($field == 'metre')
                    $val = ($val == 0 ? '--' : number_format($val / 1000, 2));
                if ($field == 'bankmoney')
                    $val = ($val == 0 ? '--' : number_format($val / 1000000, 3));
                if ($field == 'setdate')
                    $val = ($val == 0 ? '--' : date('Y-m-d', $val));
                if ($field == 'settimes')
                    $val = ($val == 0 ? '--' : date('Y-m-d H:i', $val));
                if ($field == 'isuse')
                    $val = ($val == 1 ? $code_isuse[0]['name'] : @$code_isuse[1]['name']);
                if ($field == 'isopen')
                    $val = ($val == 1 ? $code_isopen[0]['name'] : @$code_isopen[1]['name']);
                if ($field == 'sigstatus')
                    $val = ccode($code_auditstatus, $val);
                if ($field == 'mauditstatus')
                    $val = implode(',', scode($code_mauditstatus, $val));
                if ($field == 'renzheng') {
                    $strs = array();
                    $val = toint($val);
                    foreach ($code_renzheng as $cod) {
                        if (($val & (1 << (toint($cod['id']) - 1))) != 0) {
                            $strs[] = $cod['name'];
                        }
                    }
                    $val = implode(',', $strs);
                }
                if ($field == 'npcyc') {
                    if ($val < 0)
                        $val = -$val . '月';
                    else if ($val >= 86400)
                        $val = toint($val / 86400) . '天';
                    else
                        $val = $val . '秒';
                }
                if ($field == 'unit') {
                    $us = explode('|', $val);
                    $val = '';
                    if (count($us) == 5)
                        $val = '1' . $us[4] . $us[3] . $us[2] . ',';
                    if (count($us) > 2)
                        $val .= '1' . $us[2] . $us[1] . $us[0];
                    if (empty($val) && !empty($us[0]))
                        $val = $us[0];
                }
                if ($field == 'prodcata')
                    $val = implode('-', mcode($code_prodcata, $val));
                if ($field == 'areacode')
                    $val = implode('-', mcode($code_areacode, $val));
                if ($field == 'ton')
                    $val = ($val == 0 ? '--' : number_format($val / 1000000, 2));
                if ($field == 'price')
                    $val = ($val == 0 ? '--' : number_format($val / 100, 2));
                if ($field == 'weightg')
                    $val = ($val == 0 ? '--' : number_format($val / 1000, 2));
                if ($field == 'eartmpr')
                    $val = ($val == 0 ? '--' : number_format($val / 1000, 1));
                if ($field == 'addtimes')
                    $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));
    }
    public static function json_importxls_in() {
        global $db;
        $rsuser = verifyfast();
        //if (nopower($db, $rsuser['id'], 'p i'))
        //    return errjson('您未被授权操作');

        $post = new \ciy\post();
        $file = $post->get('file');
        if (!file_exists(PATH_WEB . $file))
            return errjson('文件不存在');
        require_once PATH_ROOT . '../libs/phpoffice/autoload.php';
        $spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load(PATH_WEB . $file);
        $sheet = $spreadsheet->getActiveSheet();
        $datas = $sheet->toArray('', true, true, false);
        $datacnt = count($datas);
        if ($datacnt < 2)
            return errjson('数据为空');
        $html = '';
        $headsn = array();
        $headsn[] = '行码.id';
        $headsn[] = '审核状态.auditstatus';
        $headsn[] = '审核人.audituser';
        $headsn[] = '审核时间.audittimes';
        $headsn[] = '默认标题.name';
        $headsn[] = '所属菜单.menuid';
        $headsn[] = '文件大小.filesize';
        $headsn[] = '长度.metre';
        $headsn[] = '贷款金额.bankmoney';
        $headsn[] = '设置日期.setdate';
        $headsn[] = '设置时间.settimes';
        $headsn[] = '是否使用.isuse';
        $headsn[] = '是否开启.isopen';
        $headsn[] = '库存单位.unit';
        $headsn[] = '单选状态.sigstatus';
        $headsn[] = '多选状态.mauditstatus';
        $headsn[] = '商品分类.prodcata';
        $headsn[] = '所在地区.areacode';
        $headsn[] = '认证情况.renzheng';
        $headsn[] = '执行周期.npcyc';
        $headsn[] = '执行用时.runsec';
        $headsn[] = '活动期数.acttm';
        $headsn[] = '吨位.ton';
        $headsn[] = '单价.price';
        $headsn[] = '纬度.lat';
        $headsn[] = '位置.lng';
        $headsn[] = '链接.url';
        $headsn[] = '身份证号.idcard';
        $headsn[] = '体重.weightg';
        $headsn[] = '耳温.eartmpr';
        $headsn[] = '介绍.content';
        $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_auditstatus = getcatas($db, 'auditstatus');
        $code_userid = $db->get((new \ciy\sql('zc_admin'))->column('id,name'));
        $code_menuid = $db->get((new \ciy\sql('zc_menu'))->column('id,name'));
        $code_isuse = $db->getdbcodes('demo_normal', 'isuse');
        $code_isopen = $db->getdbcodes('demo_normal', 'isopen');
        $code_mauditstatus = getcatas($db, 'auditstatus');
        $code_prodcata = getcatas($db, 'prodcata');
        $code_areacode = $db->get((new \ciy\sql('ciy_arearpc'))->column('id,name,upid'));
        $code_renzheng = getcatas($db, 'renzheng');
        $html .= '<div class="table">';
        $html .= '<table><tbody><tr>';
        $html .= '<th>#</th>';
        foreach ($heads as $arr) {
            $html .= '<th>' . $arr['name'] . '</th>';
        }
        $html .= '</tr>';
        $cnt = 0;
        $uniques = array();
        $id = 0;
        for ($rowidx = $xlsidx; $rowidx < $datacnt; $rowidx++) {
            $lineidx = $rowidx - $xlsidx + 1;
            $hrhtml = '';
            $firsthtml = '<td><div>' . $lineidx . '</div></td>';
            $bempty = true;
            $unqs = array();
            $csql = new \ciy\sql('demo_normal');
            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 = '<kbd>新增</kbd>';
                    } else {
                        $id = deid($showdat);
                        if ($id == 0)
                            $errmsg = $name . '解析错误';
                        else {
                            $csqlchk = new \ciy\sql('demo_normal');
                            $csqlchk->where('id', $id)->column('id');
                            $chkid = toint($db->get1($csqlchk));
                            if ($chkid != $id)
                                $errmsg = $name . '在数据库中不存在';
                            $value = $id;
                        }
                    }
                } else if ($name == '审核状态') {
                    if (empty($showdat)) {
                        $value = 0;
                    } else {
                        $value = dcode($code_auditstatus, $showdat);
                        if ($value == -1)
                            $errmsg = $name . '文字与系统数据不匹配';
                    }
                } else if ($name == '审核人') {
                    if (empty($showdat)) {
                        $value = 0;
                    } else {
                        $value = dcode($code_userid, $showdat);
                        if ($value == -1)
                            $errmsg = $name . '文字与系统数据不匹配';
                    }
                } else if ($name == '审核时间') {
                    if (empty($showdat)) {
                        $value = 0;
                    } else {
                        $value = tostamp($showdat);
                        if ($value === 0) {
                            $errmsg = $name . '时间格式错误';
                        } else {
                            $showdat = date('Y-m-d H:i:s', $value);
                        }
                    }
                } else if ($name == '默认标题') {
                    if (empty($showdat)) {
                        $errmsg = $name . '为必填项';
                    } else {
                        $csql->where('name', $showdat);
                        $unqs[] = $showdat;
                    }
                } else if ($name == '所属菜单') {
                    if (empty($showdat)) {
                        $value = 0;
                    } else {
                        $value = dcode($code_menuid, $showdat);
                        if ($value == -1)
                            $errmsg = $name . '文字与系统数据不匹配';
                    }
                } else if ($name == '文件大小') {
                    if (empty($showdat)) {
                        $value = 0;
                    } else {
                        if (!is_numeric($showdat))
                            $errmsg = $name . '不是数字';
                        else
                            $value = toint($showdat);
                    }
                } else if ($name == '长度') {
                    if (empty($showdat)) {
                        $value = 0;
                    } else {
                        if (!is_numeric($showdat))
                            $errmsg = $name . '不是数字';
                        else {
                            $value = toint((float)$showdat * 1000);
                            $showdat .= '米';
                        }
                    }
                } else if ($name == '贷款金额') {
                    if (empty($showdat)) {
                        $value = 0;
                    } else {
                        if (!is_numeric($showdat))
                            $errmsg = $name . '不是数字';
                        else {
                            $value = toint((float)$showdat * 1000000);
                            $showdat .= '万元';
                        }
                    }
                } else if ($name == '设置日期') {
                    if (empty($showdat)) {
                        $value = 0;
                    } else {
                        $value = tostamp($showdat);
                        if ($value === 0) {
                            $errmsg = $name . '时间格式错误';
                        } else {
                            $showdat = date('Y-m-d', $value);
                        }
                    }
                } else if ($name == '设置时间') {
                    if (empty($showdat)) {
                        $value = 0;
                    } else {
                        $value = tostamp($showdat);
                        if ($value === 0) {
                            $errmsg = $name . '时间格式错误';
                        } else {
                            $showdat = date('Y-m-d H:i:s', $value);
                        }
                    }
                } else if ($name == '是否使用') {
                    if (empty($showdat)) {
                        $value = 0;
                    } else {
                        if ($showdat == $code_isuse[0]['name']) {
                            $value = 1;
                            $showdat = $code_isuse[0]['name'];
                        } else {
                            $value = 2;
                            $showdat = $code_isuse[1]['name'];
                        }
                    }
                } else if ($name == '是否开启') {
                    if (empty($showdat)) {
                        $value = 0;
                    } else {
                        if ($showdat == $code_isopen[0]['name']) {
                            $value = 1;
                            $showdat = $code_isuse[0]['name'];
                        } else {
                            $value = 2;
                            $showdat = $code_isuse[1]['name'];
                        }
                    }
                } else if ($name == '库存单位') {
                    //显示   1箱=5盒,1盒=10袋     存储  袋|10|盒|5|箱
                    if (empty($showdat)) {
                    } else {
                        $us = explode(',', $showdat);
                        $us = array_reverse($us);
                        $us[0] = substr($us[0], 1);
                        if (preg_match('/(.*?)(\d+)(.*)/', $us[0], $matches)) {
                            $unitx = $matches[1];
                            $value = $matches[3] . '|' . $matches[2] . '|' . $matches[1];
                            if (isset($us[1])) {
                                $us[1] = substr($us[1], 1);
                                if (preg_match('/(.*?)(\d+)(.*)/', $us[1], $matches)) {
                                    if ($unitx != $matches[3]) {
                                        $errmsg = $name . '中间单位不匹配';
                                    } else {
                                        $value .= '|' . $matches[2] . '|' . $matches[1];
                                    }
                                } else {
                                    $errmsg = $name . '第二段格式错误';
                                }
                            }
                        } else {
                            $errmsg = $name . '第一段格式错误';
                        }
                    }
                } else if ($name == '多选状态') {
                    if (empty($showdat)) {
                    } else {
                        $tdats = explode(',', $showdat);
                        $vals = array();
                        foreach ($tdats as $da) {
                            $tval = dcode($code_mauditstatus, $da);
                            if ($tval == -1)
                                $errmsg = $name . '文字与系统数据不匹配';
                            $vals[] = $tval;
                        }
                        $value = implode(',', $vals);
                    }
                } else if ($name == '商品分类') {
                    if (empty($showdat)) {
                        $value = 0;
                    } else {
                        $dats = explode('-', $showdat);
                        $value = dcode($code_prodcata, $dats[count($dats) - 1]);
                        if ($value == -1)
                            $errmsg = $name . '文字与系统数据不匹配';
                    }
                } else if ($name == '所在地区') {
                    if (empty($showdat)) {
                        $value = 0;
                    } else {
                        $dats = explode('-', $showdat);
                        $value = dcode($code_areacode, $dats[count($dats) - 1]);
                        if ($value == -1)
                            $errmsg = $name . '文字与系统数据不匹配';
                    }
                } else if ($name == '认证情况') {
                    if (empty($showdat)) {
                        $value = 0;
                    } else {
                        $dats = explode(',', $showdat);
                        $tdat = 0;
                        foreach ($dats as $da) {
                            $bti = dcode($code_renzheng, $da);
                            if ($bti < 1) {
                                $errmsg = $name . '文字与系统数据不匹配';
                            } else {
                                $tdat += (1 << ($bti - 1));
                            }
                        }
                        $value = $tdat;
                    }
                } else if ($name == '执行周期') {
                    if (empty($showdat)) {
                        $value = 0;
                    } else {
                        $last = substr($showdat, -3);
                        $dd = toint(substr($showdat, 0, -3));
                        if ($dd == 0) {
                            $errmsg = $name . '周期数字错误';
                        } else if ($last == '天') {
                            $value = $dd * 86400;
                        } else if ($last == '月') {
                            $value = -$dd;
                        } else if ($last == '秒') {
                            $value = $dd;
                        } else {
                            $errmsg = $name . '周期单位错误';
                        }
                    }
                } else if ($name == '执行用时') {
                    if (empty($showdat)) {
                        $value = 0;
                    } else {
                        if (!is_numeric($showdat))
                            $errmsg = $name . '不是数字';
                        else
                            $value = toint($showdat);
                    }
                } else if ($name == '活动期数') {
                    if (empty($showdat)) {
                        $value = 0;
                    } else {
                        if (!is_numeric($showdat))
                            $errmsg = $name . '不是数字';
                        else
                            $value = toint($showdat);
                    }
                } else if ($name == '吨位') {
                    if (empty($showdat)) {
                        $value = 0;
                    } else {
                        if (!is_numeric($showdat))
                            $errmsg = $name . '不是数字';
                        else {
                            $value = toint((float)$showdat * 1000000);
                            $showdat .= '吨';
                        }
                    }
                } else if ($name == '单价') {
                    if (empty($showdat)) {
                        $value = 0;
                    } else {
                        if (!is_numeric($showdat))
                            $errmsg = $name . '不是数字';
                        else {
                            $value = toint((float)$showdat * 100);
                            $showdat .= '';
                        }
                    }
                } else if ($name == '纬度') {
                    if (empty($showdat)) {
                        $value = 0;
                    } else {
                        if (!is_numeric($showdat))
                            $errmsg = $name . '不是数字';
                        else {
                            $value = (float)$showdat;
                        }
                    }
                } else if ($name == '位置') {
                    if (empty($showdat)) {
                        $value = 0;
                    } else {
                        if (!is_numeric($showdat))
                            $errmsg = $name . '不是数字';
                        else {
                            $value = (float)$showdat;
                        }
                    }
                } else if ($name == '监管数量') {
                    if (empty($showdat)) {
                        $value = 0;
                    } else {
                        if (!is_numeric($showdat))
                            $errmsg = $name . '不是数字';
                        else {
                            $value = (float)$showdat;
                        }
                    }
                } else if ($name == '链接') {
                } else if ($name == '身份证号') {
                } else if ($name == '体重') {
                    if (empty($showdat)) {
                        $value = 0;
                    } else {
                        if (!is_numeric($showdat))
                            $errmsg = $name . '不是数字';
                        else {
                            $value = toint((float)$showdat * 1000);
                            $showdat .= 'KG';
                        }
                    }
                } else if ($name == '耳温') {
                    if (empty($showdat)) {
                        $value = 0;
                    } else {
                        if (!is_numeric($showdat))
                            $errmsg = $name . '不是数字';
                        else {
                            $value = toint((float)$showdat * 1000);
                            $showdat .= '℃';
                        }
                    }
                } else if ($name == '介绍') {
                }
                if (!empty($showdat))
                    $bempty = false;

                if (empty($errmsg))
                    $hrhtml .= '<td><div>' . $showdat . '<input type="hidden" name="' . $arr['fld'] . '_' . $lineidx . '" value="' . $value . '"/>' . $ext . '</div></td>';
                else
                    $hrhtml .= '<td style="background:#ffe8c5;" title="#' . $lineidx . ':' . $errmsg . '"><div>' . $showdat . '</div></td>';
            }
            if ($bempty)
                continue;

            if (count($unqs) > 0) {
                $unq = implode('|', $unqs);
                if (in_array($unq, $uniques))
                    $firsthtml = '<td style="background:#ffe8c5;" title="#' . $lineidx . ':该行与待导入数据有重复"><div class="lang">重复</div></td>';
                else {
                    $uniques[] = $unq;
                    $csql->column('id');
                    $chkid = toint($db->get1($csql));
                    if ($chkid > 0 && (($id > 0 && $chkid != $id) || $id == 0))
                        $firsthtml = '<td style="background:#ffe8c5;" title="#' . $lineidx . ':该行与数据库数据有重复"><div class="lang">重复</div></td>';
                }
            }
            $html .= '<tr>' . $firsthtml . $hrhtml . '</tr>';
            $cnt++;
        }
        $html .= '</tbody></table></div>';
        $html .= '<input type="hidden" name="total" value="' . $cnt . '"/>';
        $html .= '<code>共' . $cnt . '条数据</code>';
        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);
                $auditstatus = $post->get('auditstatus_' . $i);
                $audituser = $post->get('audituser_' . $i);
                $audittimes = $post->get('audittimes_' . $i);
                $name = $post->get('name_' . $i);
                $menuid = $post->get('menuid_' . $i);
                $filesize = $post->get('filesize_' . $i);
                $metre = $post->get('metre_' . $i);
                $bankmoney = $post->get('bankmoney_' . $i);
                $setdate = $post->get('setdate_' . $i);
                $settimes = $post->get('settimes_' . $i);
                $isuse = $post->get('isuse_' . $i);
                $isopen = $post->get('isopen_' . $i);
                $unit = $post->get('unit_' . $i);
                $mauditstatus = $post->get('mauditstatus_' . $i);
                $prodcata = $post->get('prodcata_' . $i);
                $areacode = $post->get('areacode_' . $i);
                $renzheng = $post->get('renzheng_' . $i);
                $npcyc = $post->get('npcyc_' . $i);
                $runsec = $post->get('runsec_' . $i);
                $acttm = $post->get('acttm_' . $i);
                $ton = $post->get('ton_' . $i);
                $price = $post->get('price_' . $i);
                $lat = $post->get('lat_' . $i);
                $lng = $post->get('lng_' . $i);
                $url = $post->get('url_' . $i);
                $idcard = $post->get('idcard_' . $i);
                $weightg = $post->get('weightg_' . $i);
                $eartmpr = $post->get('eartmpr_' . $i);
                $content = $post->get('content_' . $i);
                $csql = new \ciy\sql('demo_normal');
                $csql->where('name', $name);
                $csql->column('id');
                $chkid = toint($db->get1($csql));
                if ($chkid > 0 && (($id > 0 && $chkid != $id) || $id == 0))
                    throw new \Exception('CIYIGN发现数据有重复');
                $updata = array();
                $updata['auditstatus'] = $auditstatus;
                $updata['audituser'] = $audituser;
                $updata['audittimes'] = $audittimes;
                $updata['name'] = $name;
                $updata['menuid'] = $menuid;
                $updata['filesize'] = $filesize;
                $updata['metre'] = $metre;
                $updata['bankmoney'] = $bankmoney;
                $updata['setdate'] = $setdate;
                $updata['settimes'] = $settimes;
                $updata['isuse'] = $isuse;
                $updata['isopen'] = $isopen;
                $updata['unit'] = $unit;
                $updata['mauditstatus'] = $mauditstatus;
                $updata['prodcata'] = $prodcata;
                $updata['areacode'] = $areacode;
                $updata['renzheng'] = $renzheng;
                $updata['npcyc'] = $npcyc;
                $updata['runsec'] = $runsec;
                $updata['acttm'] = $acttm;
                $updata['ton'] = $ton;
                $updata['price'] = $price;
                $updata['lat'] = $lat;
                $updata['lng'] = $lng;
                $updata['url'] = $url;
                $updata['idcard'] = $idcard;
                $updata['weightg'] = $weightg;
                $updata['eartmpr'] = $eartmpr;
                $updata['content'] = $content;
                $updata['addtimes'] = tostamp();
                $csql = new \ciy\sql('demo_normal');
                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();
    }
}
