<?php

namespace web\admin\demo;

class normal {
    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);
        $val = objstr($query, 'audituser');
        if (!empty($val)) {
            $csqlt = new \ciy\sql('zc_cata');
            $csqlt->where('cbid in (select id from zc_cata where cbid=0 and codeid=\'audituser\')');
            $csqlt->where('name like', $val);
            $trow = $db->getone($csqlt);
            if (is_array($trow)) {
                $csql->where('audituser', $trow['codeid']);
                $query['audituser'] = $trow['name'];
            } else {
                $csql->where('audituser=0');
            }
        }
        $csql->wheredaterange('audittimes', objstr($query, 'audittimes'));
        $csql->where('auditmsg like', objstr($query, 'auditmsg'));
        $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->where('isopen', objstr($query, 'isopen'));
        $csql->where('mauditstatus like', ',' . objstr($query, 'mauditstatus') . ',');
        $csql->wherenumrange('ton', objstr($query, 'ton_1'), objstr($query, 'ton_2'), 1000000);
        $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,md', $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);
        if($rows === false)
            return errjson($db->error);
        $ret = array('where' => $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 (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'] = array();
            $input = array();
            $input[] = array('type' => 'input', 'form' => 'audituser', 'name' => '审核人', 'prop' => ' style="width:8em;"');
            $input[] = array('type' => 'daterange', 'form' => 'audittimes', 'name' => '审核时间');
            $input[] = array('type' => 'input', 'form' => 'auditmsg', 'name' => '审核理由', 'prop' => ' style="width:8em;"');
            $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' => 'select', 'form' => 'isopen', 'name' => '是否开启', 'all' => '全部', 'select' => '开启.关闭');
            $input[] = array('type' => 'select', 'form' => 'mauditstatus', 'name' => '多选状态', 'all' => '全部', 'select' => 'auditstatus');
            $input[] = array('type' => 'num', 'form' => 'ton', 'name' => '吨位', 'prop' => ' style="width:4em;"');
            $ret['once']['input'] = $input;
            $csql = (new \ciy\sql('zc_depart'))->column('id,name,upid');
            $ret['once']['zc_depart'] = $db->get($csql);
        }
        $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();
        $id = $post->getint('id');
        $act = $post->get('act');
        if ($id > 0) {
            $csql = new \ciy\sql('demo_normal');
            $csql->where('id', $id);
            $row = $db->getone($csql);
            if (!is_array($row))
                return errjson('数据不存在');
            if ($act == 'view' || $act == 'review') {
                $csql = (new \ciy\sql('zc_menu'))->column('id,name');
                $csql->where('id', $row['menuid']);
                $ret['zc_menu'] = $db->get($csql);
            }
        } else {
            $row = array();
        }
        $ret['data'] = $row;
        if ($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');
        $qian = $post->getint('qian');
        $meishi = $post->getint('meishi');
        $weight = $post->getint('weight');
        $height = $post->getint('height');
        $skt = $post->getint('skt');
        $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();
            $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['qian'] = $qian;
            $updata['meishi'] = $meishi;
            $updata['weight'] = $weight;
            $updata['height'] = $height;
            $updata['skt'] = $skt;
            $updata['idcard'] = $idcard;
            $updata['addtimes'] = tostamp();
            $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['id'] = 0;
                $updata['auditstatus'] = 0;
                $updata['audituser'] = 0;
                $updata['audittimes'] = 0;
                $updata['auditmsg'] = '';
                $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());
        }
        $ret['data'] = $updata;
        $ret['zc_menu'] = getrelation($db, [$updata], 'zc_menu', 'menuid');
        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('demo_normal');
        $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, 'demo_normal');
                savelogdb($db, $rsuser['id'], 'demo_normal', $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_audit() {
        global $db;
        $rsuser = verifyfast();
        //if (nopower($db, $rsuser['id'], 'p a'))
        //    return errjson('您未被授权操作');
        $post = new \ciy\post();
        $ids = $post->get('ids');
        if (empty($ids))
            return errjson('请选择至少一条');
        $auditstatus = $post->getint('auditstatus');
        $auditmsg = $post->get('auditmsg');
        if ($auditstatus == 90 && empty($auditmsg))
            return errjson('请填写驳回原因');
        $csql = new \ciy\sql('demo_normal');
        $csql->where('id in', $ids);
        $rows = $db->get($csql);
        $ids = array();
        try {
            $db->begin();
            foreach ($rows as $row) {
                $csql = new \ciy\sql('demo_normal');
                $csql->where('id', $row['id']);
                $updata = array();
                $updata['auditstatus'] = $auditstatus;
                $updata['audituser'] = $rsuser['id'];
                $updata['audittimes'] = tostamp();
                $updata['auditmsg'] = $auditmsg;
                $csql = new \ciy\sql('demo_normal');
                $csql->where('id', $row['id']);
                if ($db->update($csql, $updata) === false)
                    throw new \Exception('审核失败:' . $db->error);
                $ids[] = $row['id'];
                if ($auditstatus == 100) {
                }
            }
            $db->commit();
        } catch (\Exception $ex) {
            $db->rollback();
            savelogfile('err_db', $ex->getMessage());
            return errjson($ex->getMessage());
        }
        $ret['data'] = $updata;
        $ret['ids'] = $ids;
        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' => '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' => 150,  'field' => 'auditmsg', 'name' => '审核理由');
        $fields[] = array('style' => 'l', 'width' => 150,  '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' => 'dayclock', 'name' => '营业时间');
        $fields[] = array('style' => 'c', 'width' => 300,  'field' => 'downurl', 'name' => '文档');
        $fields[] = array('style' => 'l', 'width' => 150,  'field' => 'avar', 'name' => '头像');
        $fields[] = array('style' => 'c', 'width' => 100,  'field' => 'isuse', 'name' => '是否使用');
        $fields[] = array('style' => 'c', 'width' => 100,  'field' => 'isopen', 'name' => '是否开启');
        $fields[] = array('style' => 'l', 'width' => 150,  'field' => 'unit', 'name' => '库存单位');
        $fields[] = array('style' => 'c', 'width' => 100,  'field' => 'sigstatus', 'name' => '单选状态');
        $fields[] = array('style' => 'l', 'width' => 150,  'field' => 'mauditstatus', 'name' => '多选状态');
        $fields[] = array('style' => 'l', 'width' => 150,  'field' => 'prodcata', 'name' => '组织结构');
        $fields[] = array('style' => 'l', 'width' => 150,  'field' => 'areacode', 'name' => '所在地区');
        $fields[] = array('style' => 'l', 'width' => 100,  'field' => 'renzheng', 'name' => '认证情况');
        $fields[] = array('style' => 'c', 'width' => 100,  'field' => 'ppint', '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' => 'l', 'width' => 300,  'field' => 'imgs', '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' => 'r', 'width' => 100,  'field' => 'pct', 'name' => '百分比');
        $fields[] = array('style' => 'l', 'width' => 150,  'field' => 'url', 'name' => '链接');
        $fields[] = array('style' => 'r', 'width' => 100,  'field' => 'weightg', 'name' => '体重');
        $fields[] = array('style' => 'r', 'width' => 100,  'field' => 'eartmpr', 'name' => '耳温');
        $fields[] = array('style' => 'r', 'width' => 100,  'field' => 'qian', 'name' => '中式钱');
        $fields[] = array('style' => 'r', 'width' => 100,  'field' => 'meishi', 'name' => '美式钱');
        $fields[] = array('style' => 'r', 'width' => 100,  'field' => 'weight', 'name' => '重量');
        $fields[] = array('style' => 'r', 'width' => 100,  'field' => 'height', 'name' => '长度');
        $fields[] = array('style' => 'r', 'width' => 100,  'field' => 'skt', 'name' => '科学数');
        $fields[] = array('style' => 'l', 'width' => 150,  'field' => 'idcard', 'name' => '身份证号');
        $fields[] = array('style' => 'l', 'width' => 100,  'field' => 'addtimes', 'name' => '添加时间');
        $fields[] = array('style' => 'l', 'width' => 150,  'field' => 'content', 'name' => '介绍');
        $code_auditstatus = getcatas($db, 'auditstatus');
        $code_audituser = getcatas($db, 'adminuser');
        $code_menuid = getrelation($db, $rows, 'zc_menu', 'menuid', 'id,name');
        $code_isuse = $db->getdbcodes('demo_normal', 'isuse');
        $code_isopen = $db->getdbcodes('demo_normal', 'isopen');
        $code_sigstatus = getcatas($db, 'auditstatus');
        $code_mauditstatus = getcatas($db, 'auditstatus');
        $code_prodcata = $db->get((new \ciy\sql('zc_depart'))->column('id,name,upid'));
        $code_areacode = $db->get((new \ciy\sql('ciy_arearpc'))->column('id,name,upid'));
        $code_renzheng = $db->getdbcodes('demo_normal', 'renzheng');
        $code_ppint = $db->getdbcodes('demo_normal', 'ppint');
        $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 == 'filesize')
                    $val = number_format($val / 1024, 2) . 'KB';
                if ($field == 'metre')
                    $val = number_format($val / 1000, 2) . '米';
                if ($field == 'bankmoney')
                    $val = 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 == 'dayclock')
                    $val = ($val == 0 ? '--' : totimepoint($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 == 'unit')
                    $val = ($val == '' ? '--' : implode(',', tounit($val)));
                if ($field == 'sigstatus')
                    $val = ccode($code_sigstatus, $val);
                if ($field == 'mauditstatus')
                    $val = implode(',', scode($code_mauditstatus, $val));
                if ($field == 'prodcata')
                    $val = implode('-', mcode($code_prodcata, $val));
                if ($field == 'areacode')
                    $val = implode('-', mcode($code_areacode, $val));
                if ($field == 'renzheng')
                    $val = implode(',', bcode($code_renzheng, $val));
                if ($field == 'ppint')
                    $val = ccode($code_ppint, $val);
                if ($field == 'npcyc')
                    $val = ($val == 0 ? '--' : tocyc($val));
                if ($field == 'runsec')
                    $val .= '秒';
                if ($field == 'ton')
                    $val = number_format($val / 1000000, 3) . '吨';
                if ($field == 'price')
                    $val = number_format($val / 100, 2);
                if ($field == 'pct')
                    $val = number_format($val / 100, 2) . '%';
                if ($field == 'weightg')
                    $val = number_format($val / 1000, 3) . 'KG';
                if ($field == 'eartmpr')
                    $val = number_format($val / 1000, 1) . '℃';
                if ($field == 'qian')
                    $val = number_format($val / 100, 2) . '元';
                if ($field == 'weight')
                    $val = number_format($val / 1000, 3) . 'KG';
                if ($field == 'height')
                    $val = number_format($val / 1000, 3) . '米';
                if ($field == 'skt')
                    $val = number_format($val / 1000, 3) . 'SKT';
                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'] = '复杂功能数据报表';
        $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[] = '所属菜单.menuid';
        $headsn[] = '文件大小.filesize';
        $headsn[] = '长度.metre';
        $headsn[] = '贷款金额.bankmoney';
        $headsn[] = '设置日期.setdate';
        $headsn[] = '设置时间.settimes';
        $headsn[] = '营业时间.dayclock';
        $headsn[] = '是否使用.isuse';
        $headsn[] = '是否开启.isopen';
        $headsn[] = '库存单位.unit';
        $headsn[] = '单选状态.sigstatus';
        $headsn[] = '多选状态.mauditstatus';
        $headsn[] = '组织结构.prodcata';
        $headsn[] = '所在地区.areacode';
        $headsn[] = '认证情况.renzheng';
        $headsn[] = '临时字典.ppint';
        $headsn[] = '执行周期.npcyc';
        $headsn[] = '执行用时.runsec';
        $headsn[] = '活动期数.acttm';
        $headsn[] = '吨位.ton';
        $headsn[] = '单价.price';
        $headsn[] = '位置.lng';
        $headsn[] = '百分比.pct';
        $headsn[] = '链接.url';
        $headsn[] = '体重.weightg';
        $headsn[] = '耳温.eartmpr';
        $headsn[] = '中式钱.qian';
        $headsn[] = '美式钱.meishi';
        $headsn[] = '重量.weight';
        $headsn[] = '长度.height';
        $headsn[] = '科学数.skt';
        $headsn[] = '身份证号.idcard';
        $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_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_sigstatus = getcatas($db, 'auditstatus');
        $code_mauditstatus = getcatas($db, 'auditstatus');
        $code_prodcata = $db->get((new \ciy\sql('zc_depart'))->column('id,name,upid'));
        $code_areacode = $db->get((new \ciy\sql('ciy_arearpc'))->column('id,name,upid'));
        $code_renzheng = $db->getdbcodes('demo_normal', 'renzheng');
        $code_ppint = $db->getdbcodes('demo_normal', 'ppint');
        $html .= '<div class="table" style="width: 100%;height: calc(100% - 2.2em);overflow: auto;">';
        $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)) {
                        $errmsg = $name . '为必填项';
                    } else {
                    }
                } 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 {
                        $showdat = str_replace(',', '', str_replace('KB', '', $showdat));
                        if (!is_numeric($showdat))
                            $errmsg = $name . '不是数字';
                        else
                            $value = toint((float)$showdat * 1024);
                            $showdat .= 'KB';
                    }
                } else if ($name == '长度') {
                    if (empty($showdat)) {
                        $value = 0;
                    } else {
                        $showdat = str_replace(',', '', str_replace('米', '', $showdat));
                        if (!is_numeric($showdat))
                            $errmsg = $name . '不是数字';
                        else
                            $value = toint((float)$showdat * 1000);
                            $showdat .= '米';
                    }
                } else if ($name == '贷款金额') {
                    if (empty($showdat)) {
                        $value = 0;
                    } else {
                        $showdat = str_replace(',', '', str_replace('万元', '', $showdat));
                        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', $value);
                        }
                    }
                } else if ($name == '营业时间') {
                    if (empty($showdat)) {
                        $value = 0;
                    } else {
                        $tps = explode(':', $showdat);
                        $value = 0;
                        if (count($tps) == 2)
                            $value = $tps[0] * 3600 + $tps[1] * 60;
                        else if (count($tps) == 3)
                            $value = $tps[0] * 3600 + $tps[1] * 60 + $tps[2];
                        if ($value === 0) {
                            $errmsg = $name . '时间格式错误';
                        } else {
                            $showdat = totimepoint($value, true);
                        }
                    }
                } 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_isopen[0]['name'];
                        } else {
                            $value = 2;
                            $showdat = $code_isopen[1]['name'];
                        }
                    }
                } else if ($name == '库存单位') {
                    if (empty($showdat)) {
                        $value = '';
                    } else {
                        $us = explode(',', $showdat);
                        $us = array_reverse($us);
                        if (preg_match('/1(.*?)(\d+)(.*)/', $us[0], $matches)) {
                            $unitx = $matches[1];
                            $value = $matches[3] . '|' . $matches[2] . '|' . $matches[1];
                            if (isset($us[1])) {
                                if (preg_match('/1(.*?)(\d+)(.*)/', $us[1], $matches)) {
                                    if ($unitx != $matches[3]) {
                                        $errmsg = $name . '中间单位不匹配';
                                    } else {
                                        $value .= '|' . $matches[2] . '|' . $matches[1];
                                    }
                                } else {
                                    $errmsg = $name . '第二段格式错误';
                                }
                            }
                        } else {
                            if (preg_match('/\d/', $us[0]) > 0)
                                $errmsg = $name . '第一段不能包含数字';
                            else
                                $value = $us[0];
                        }
                    }
                } else if ($name == '单选状态') {
                    if (empty($showdat)) {
                        $value = 0;
                    } else {
                        $value = dcode($code_sigstatus, $showdat);
                        if ($value == -1)
                            $errmsg = $name . '文字与系统数据不匹配';
                    }
                } else if ($name == '多选状态') {
                    if (empty($showdat)) {
                        $value = '';
                    } 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 {
                        $value = dcode($code_ppint, $showdat);
                        if ($value == -1)
                            $errmsg = $name . '文字与系统数据不匹配';
                    }
                } 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 {
                        $showdat = str_replace(',', '', str_replace('秒', '', $showdat));
                        if (!is_numeric($showdat))
                            $errmsg = $name . '不是数字';
                        else
                            $value = toint($showdat);
                            $showdat .= '秒';
                    }
                } else if ($name == '活动期数') {
                    if (empty($showdat)) {
                        $value = 0;
                    } else {
                        $showdat = str_replace(',', '', $showdat);
                        if (!is_numeric($showdat))
                            $errmsg = $name . '不是数字';
                        else
                            $value = toint($showdat);
                    }
                } else if ($name == '吨位') {
                    if (empty($showdat)) {
                        $value = 0;
                    } else {
                        $showdat = str_replace(',', '', str_replace('吨', '', $showdat));
                        if (!is_numeric($showdat))
                            $errmsg = $name . '不是数字';
                        else
                            $value = toint((float)$showdat * 1000000);
                            $showdat .= '吨';
                    }
                } else if ($name == '单价') {
                    if (empty($showdat)) {
                        $value = 0;
                    } else {
                        $showdat = str_replace(',', '', $showdat);
                        if (!is_numeric($showdat))
                            $errmsg = $name . '不是数字';
                        else
                            $value = toint((float)$showdat * 100);
                    }
                } else if ($name == '位置') {
                    if (empty($showdat)) {
                        $value = 0;
                    } else {
                        $showdat = str_replace(',', '', $showdat);
                        if (!is_numeric($showdat))
                            $errmsg = $name . '不是数字';
                        else
                            $value = (float)$showdat;
                    }
                } else if ($name == '监管数量') {
                    if (empty($showdat)) {
                        $value = 0;
                    } else {
                        $showdat = str_replace(',', '', $showdat);
                        if (!is_numeric($showdat))
                            $errmsg = $name . '不是数字';
                        else
                            $value = (float)$showdat;
                    }
                } else if ($name == '百分比') {
                    if (empty($showdat)) {
                        $value = 0;
                    } else {
                        $showdat = str_replace(',', '', str_replace('%', '', $showdat));
                        if (!is_numeric($showdat))
                            $errmsg = $name . '不是数字';
                        else
                            $value = toint((float)$showdat * 100);
                            $showdat .= '%';
                    }
                } else if ($name == '链接') {
                    if (empty($showdat)) {
                        $value = '';
                    } else {
                    }
                } else if ($name == '体重') {
                    if (empty($showdat)) {
                        $value = 0;
                    } else {
                        $showdat = str_replace(',', '', str_replace('KG', '', $showdat));
                        if (!is_numeric($showdat))
                            $errmsg = $name . '不是数字';
                        else
                            $value = toint((float)$showdat * 1000);
                            $showdat .= 'KG';
                    }
                } else if ($name == '耳温') {
                    if (empty($showdat)) {
                        $value = 0;
                    } else {
                        $showdat = str_replace(',', '', str_replace('℃', '', $showdat));
                        if (!is_numeric($showdat))
                            $errmsg = $name . '不是数字';
                        else
                            $value = toint((float)$showdat * 1000);
                            $showdat .= '℃';
                    }
                } else if ($name == '中式钱') {
                    if (empty($showdat)) {
                        $value = 0;
                    } else {
                        $showdat = str_replace(',', '', str_replace('元', '', $showdat));
                        if (!is_numeric($showdat))
                            $errmsg = $name . '不是数字';
                        else
                            $value = toint((float)$showdat * 100);
                            $showdat .= '元';
                    }
                } else if ($name == '美式钱') {
                    if (empty($showdat)) {
                        $value = 0;
                    } else {
                        $showdat = str_replace(',', '', str_replace('¤', '', $showdat));
                        if (!is_numeric($showdat))
                            $errmsg = $name . '不是数字';
                        else
                            $value = toint((float)$showdat * 100);
                            $showdat .= '¤';
                    }
                } else if ($name == '重量') {
                    if (empty($showdat)) {
                        $value = 0;
                    } else {
                        $showdat = str_replace(',', '', str_replace('KG', '', $showdat));
                        if (!is_numeric($showdat))
                            $errmsg = $name . '不是数字';
                        else
                            $value = toint((float)$showdat * 1000);
                            $showdat .= 'KG';
                    }
                } else if ($name == '长度') {
                    if (empty($showdat)) {
                        $value = 0;
                    } else {
                        $showdat = str_replace(',', '', str_replace('米', '', $showdat));
                        if (!is_numeric($showdat))
                            $errmsg = $name . '不是数字';
                        else
                            $value = toint((float)$showdat * 1000);
                            $showdat .= '米';
                    }
                } else if ($name == '科学数') {
                    if (empty($showdat)) {
                        $value = 0;
                    } else {
                        $showdat = str_replace(',', '', str_replace('SKT', '', $showdat));
                        if (!is_numeric($showdat))
                            $errmsg = $name . '不是数字';
                        else
                            $value = toint((float)$showdat * 1000);
                            $showdat .= 'SKT';
                    }
                } else if ($name == '身份证号') {
                    if (empty($showdat)) {
                        $value = '';
                    } else {
                    }
                } else if ($name == '介绍') {
                    if (empty($showdat)) {
                        $value = '';
                    } else {
                    }
                }
                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);
                $updata = array();
                $updata['name'] = $post->get('name_' . $i);
                $updata['menuid'] = $post->get('menuid_' . $i);
                $updata['filesize'] = $post->get('filesize_' . $i);
                $updata['metre'] = $post->get('metre_' . $i);
                $updata['bankmoney'] = $post->get('bankmoney_' . $i);
                $updata['setdate'] = $post->get('setdate_' . $i);
                $updata['settimes'] = $post->get('settimes_' . $i);
                $updata['dayclock'] = $post->get('dayclock_' . $i);
                $updata['isuse'] = $post->get('isuse_' . $i);
                $updata['isopen'] = $post->get('isopen_' . $i);
                $updata['unit'] = $post->get('unit_' . $i);
                $updata['sigstatus'] = $post->get('sigstatus_' . $i);
                $updata['mauditstatus'] = $post->get('mauditstatus_' . $i);
                $updata['prodcata'] = $post->get('prodcata_' . $i);
                $updata['areacode'] = $post->get('areacode_' . $i);
                $updata['renzheng'] = $post->get('renzheng_' . $i);
                $updata['ppint'] = $post->get('ppint_' . $i);
                $updata['npcyc'] = $post->get('npcyc_' . $i);
                $updata['runsec'] = $post->get('runsec_' . $i);
                $updata['acttm'] = $post->get('acttm_' . $i);
                $updata['ton'] = $post->get('ton_' . $i);
                $updata['price'] = $post->get('price_' . $i);
                $updata['lng'] = $post->get('lng_' . $i);
                $updata['pct'] = $post->get('pct_' . $i);
                $updata['url'] = $post->get('url_' . $i);
                $updata['weightg'] = $post->get('weightg_' . $i);
                $updata['eartmpr'] = $post->get('eartmpr_' . $i);
                $updata['qian'] = $post->get('qian_' . $i);
                $updata['meishi'] = $post->get('meishi_' . $i);
                $updata['weight'] = $post->get('weight_' . $i);
                $updata['height'] = $post->get('height_' . $i);
                $updata['skt'] = $post->get('skt_' . $i);
                $updata['idcard'] = $post->get('idcard_' . $i);
                $updata['content'] = $post->get('content_' . $i);
                $csql = new \ciy\sql('demo_normal');
                if ($id == 0) {
                    $updata['auditstatus'] = 2;
                    $updata['addtimes'] = tostamp();
                    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();
    }
}
