<?php

namespace web\admin\datasse;

class expo {
    static function sse_pavi($senddata, $sendevent) {
        global $db;
        $rsuser = verifyfast();
        if (nopower($db, $rsuser['id'], 'p610r'))
            return errjson('您未被授权操作');
        $param = get('param');
        if(empty($param))
            $senddata('param 可填入 newexpo 建立展会，biaoji 标记展会，expoday 展会天数调整');
        else
            $senddata('param: ' . $param);
        if (true) {
            $csql = new \ciy\sql('c_fair_juzhan_pavi');
            $csql->where('diqu like', '中国-');
            $csql->where('conferenceid=0');
            $cpavirows = $db->get($csql);
            foreach ($cpavirows as $cpavirow) {
                $csql = new \ciy\sql('c_fair_juzhan_expo');
                $csql->where('paviid', $cpavirow['rawid']);
                $cnt = $db->get1($csql);
                if ($cnt > 0)
                    $senddata('展馆中有展会，但展馆未收录: ' . $cpavirow['name'] . ',' . $cpavirow['rawid']);
            }
        }
        if (true) {
            $csql = new \ciy\sql('c_fair_juzhan_pavi');
            $csql->where('diqu like', '中国-');
            $csql->where('conferenceid>0');
            $cpavirows = $db->get($csql);
            foreach ($cpavirows as $cpavirow) {
                $csql = new \ciy\sql('c_fair_juzhan_expo');
                $csql->where('paviid', $cpavirow['rawid']);
                $crows = $db->get($csql);
                // $csql = new \ciy\sql('ep_exposition');
                // $csql->where('conferenceid', $cpavirow['conferenceid']);
                // $csql->order('name');
                // $erows = $db->get($csql);
                // if(count($crows) == count($erows))
                //     continue;
                //$senddata('C'.count($crows) .'=E'. count($erows).'|'.$cpavirow['name'] . ',' . $cpavirow['rawid']);
                //检查C有，但E没有的展会有哪些
                foreach ($crows as $crow) {
                    if ($crow['expoid'] == 0) {
                        $csql = new \ciy\sql('ep_exposition');
                        $csql->where('conferenceid', $cpavirow['conferenceid']);
                        $csql->where('name', $crow['name']);
                        $chkrow = $db->getone($csql);
                        if (is_array($chkrow)) {
                            if ($param != 'biaoji') {
                                $senddata('[biaoji] 展会存在，但juzhan未标记: ' . $cpavirow['conferenceid'] . ',' . $crow['name']);
                                continue;
                            }

                            $updata = array();
                            $updata['status'] = 10;
                            $updata['expoid'] = $chkrow['id'];
                            $csql = new \ciy\sql('c_fair_juzhan_expo');
                            $csql->where('id', $crow['id']);
                            $execute = $db->data($updata)->set($csql, 'update');
                            if ($execute === false)
                                $senddata('ERR　' . $cpavirow['conferenceid'] . ',' . $crow['name'] . $db->error);
                            else
                                $senddata('展会存在，已补juahznid: ' . $cpavirow['conferenceid'] . ',' . $crow['name']);
                        } else {
                            if ($param != 'newexpo') {
                                $senddata('[newexpo] 展会不存在需建立: ' . $cpavirow['conferenceid'] . ',' . $crow['name']);
                                continue;
                            }
                            $updata = array();
                            $updata['conferenceid'] = $cpavirow['conferenceid'];
                            $updata['name'] = $crow['name'];
                            $updata['opentimes'] = $crow['opentimes'];
                            $updata['daycnt'] = \web\amb\expo::convday($crow);
                            $updata['content'] = '[MD]';
                            $csql = new \ciy\sql('ep_exposition');
                            $execute = $db->data($updata)->set($csql, 'insert');
                            if ($execute !== false) {
                                $exponewid = $db->insert_id();
                                $updata = array();
                                $updata['status'] = 10;
                                $updata['expoid'] = $exponewid;
                                $csql = new \ciy\sql('c_fair_juzhan_expo');
                                $csql->where('id', $crow['id']);
                                $execute = $db->data($updata)->set($csql, 'update');
                                if ($execute === false)
                                    $senddata('ERR　' . $cpavirow['conferenceid'] . ',' . $crow['name'] . $db->error);
                                else
                                    $senddata('已新建展会: ' . $cpavirow['conferenceid'] . ',' . $crow['name']);
                            } else {
                                $senddata('ERR　' . $cpavirow['conferenceid'] . ',' . $crow['name'] . $db->error);
                            }
                        }
                    }
                }
            }
        }
        if (true) {
            $csql = new \ciy\sql('ep_exposition');
            $csql->where('daycnt>30');
            $exporows = $db->get($csql);
            $csql = new \ciy\sql('zc_ai_key');
            $csql->where('id', 1);
            $aikeyrow = $db->getone($csql);
            if(!is_array($aikeyrow))
                return errjson('AI Key不存在');
            foreach ($exporows as $exporow) {
                if ($param != 'expoday') {
                    $senddata('[expoday] 展会持续: ' . $exporow['daycnt'] . '天,' . $exporow['name']);
                    continue;
                }
                $csql = new \ciy\sql('c_fair_juzhan_expo');
                $csql->where('expoid', $exporow['id']);
                $crow = $db->getone($csql);
                if (is_array($crow)) {
                    $n = \web\amb\expo::convday($crow);
                    if ($n != $exporow['daycnt'])
                        $senddata('展会天数调整到[' . $n . ']: ' . $exporow['daycnt'] . ',' . $exporow['name'] . $crow['lunchdate']);
                } else {
                    $param = array();
                    $param['id'] = 6;
                    $param['name'] = $exporow['name'];
                    $retai = \web\amb\ai::json_aidecision($param);
                    if (@$retai['code'] != 1){
                        $senddata('AI识别失败: ' . $exporow['name'] . ',' . $retai['errmsg']);
                        continue;
                    }

                    if (!isset($retai['ai']['day'])) {
                        $senddata('AI识别错误: ' . $exporow['name'] . ',day不存在');
                        continue;
                    }
                    $updata = array();
                    $updata['daycnt'] = $retai['ai']['day'];
                    $csql = new \ciy\sql('ep_exposition');
                    $csql->where('id', $exporow['id']);
                    $execute = $db->data($updata)->set($csql, 'update');
                    if ($execute === false)
                        $senddata('ERR　' . $retai['ai']['day'] . '==' . $exporow['daycnt'] . ',' . $exporow['name'] . $db->error);
                    else
                        $senddata('展会天数调整到[' .  $retai['ai']['day'] . ']: ' . $exporow['daycnt'] . ',' . $exporow['name']);
                }
            }
        }
        $sendevent('ok');
    }
}
