<?php

namespace web;

class cwebcomon {
    static function mlmbonus($db, $money, $userrow, $bonusset) {
        if ($userrow['upid'] <= 0)
            return;
        if (!isset($bonusset[0]))
            return;
        $csql = new \ciy\sql('ap_user');
        $csql->where('id', $userrow['upid']);
        $uprow = $db->getone($csql);
        if (!is_array($uprow))
            return;
        self::usercashoe($db, 20, $uprow['id'], $money * $bonusset[0]['percent'], $bonusset[0]['name']);
        if ($uprow['upid'] <= 0)
            return;
        if (!isset($bonusset[1]))
            return;
        $csql = new \ciy\sql('ap_user');
        $csql->where('id', $uprow['upid']);
        $up2row = $db->getone($csql);
        if (!is_array($up2row))
            return;
        self::usercashoe($db, 21, $up2row['id'], $money * $bonusset[1]['percent'], $bonusset[1]['name']);
        if ($up2row['upid'] <= 0)
            return;
        if (!isset($bonusset[2]))
            return;
        $csql = new \ciy\sql('ap_user');
        $csql->where('id', $up2row['upid']);
        $up3row = $db->getone($csql);
        if (!is_array($up3row))
            return;
        self::usercashoe($db, 22, $up3row['id'], $money * $bonusset[2]['percent'], $bonusset[2]['name']);
        //最高三级代理
    }
    static function usercashie($db, $type, $userid, $money, $name = '') {
        $updata = array();
        if ($money > 0)
            $updata['mymoney'] = array('mymoney+' . $money);
        else
            $updata['mymoney'] = array('mymoney' . $money);
        $csql = new \ciy\sql('ap_user');
        $csql->where('id', $userid);
        if ($db->update($csql, $updata) === false)
            throw new \Exception('账户余额更新失败:' . $db->error);
        //直接打款
        $updata = array();
        $updata['cashtype'] = $type;
        $updata['iemoney'] = $money;
        $updata['vuser'] = $userid;
        $updata['name'] = $name;
        $updata['addtimes'] = tostamp();
        $csql = new \ciy\sql('ap_cash_ie');
        if ($db->insert($csql, $updata) === false)
            throw new \Exception('新建消费收支失败:' . $db->error);
    }
    static function usercashoe($db, $type, $userid, $money, $name = '') {
        $updata = array();
        if ($money > 0)
            $updata['mycashmoney'] = array('mycashmoney+' . $money);
        else
            $updata['mycashmoney'] = array('mycashmoney' . $money);
        $csql = new \ciy\sql('ap_user');
        $csql->where('id', $userid);
        if ($db->update($csql, $updata) === false)
            throw new \Exception('账户余额更新失败:' . $db->error);
        //直接打款
        $updata = array();
        $updata['cashtype'] = $type;
        $updata['oemoney'] = $money;
        $updata['vuser'] = $userid;
        $updata['name'] = $name;
        $updata['addtimes'] = tostamp();
        $csql = new \ciy\sql('ap_cash_oe');
        if ($db->insert($csql, $updata) === false)
            throw new \Exception('新建佣金收支失败:' . $db->error);
    }
    static function www_showpage($count, $pageno, $pagecount, $pageurl, $pagecb) {
        if ($count <= 0) {
            // 1...3,4,5,6,7,8
            $isend = (-$count < $pagecount);
            //if($pageno < 20) $isend = false;
            $epage = $pageno + 2;
            $spage = $pageno - 6;
            if ($isend)
                $epage = $pageno;
            if ($spage < 1)
                $spage = 1;
            echo '<a ' . ($pageno <= 1 ? ' class="disabled"' : " href=\"" . $pageurl . $pagecb($pageno - 1) . "\"") . '>上页</a>';

            if ($spage > 1)
                echo '<a href="' . $pageurl . $pagecb(1) . '">1</a>';
            if ($spage > 2)
                echo '<span>...</span>';

            for ($i = $spage; $i <= $epage; $i++)
                echo '<a ' . ($i == $pageno ? ' class="active"' : " href=\"" . $pageurl . $pagecb($i) . "\"") . '>' . $i . '</a>';
            echo '<a ' . ($isend ? ' class="disabled"' : " href=\"" . $pageurl . $pagecb($pageno + 1) . "\"") . '>下页</a>';
        } else {
            //1...3,4,5,6,7...n
            $epage = $pageno + 6;
            $spage = $pageno - 2;
            $pagemax = ceil($count / $pagecount);
            if ($spage < 1)
                $spage = 1;
            if ($epage > $pagemax)
                $epage = $pagemax;
            echo '<a ' . ($pageno <= 1 ? ' class="disabled"' : " href=\"" . $pageurl . $pagecb($pageno - 1) . "\"") . '>上页</a>';
            if ($spage > 1)
                echo '<a href="' . $pageurl . $pagecb(1) . '">1</a>';
            if ($spage > 2)
                echo '<span>...</span>';

            for ($i = $spage; $i <= $epage; $i++)
                echo '<a ' . ($i == $pageno ? ' class="active"' : " href=\"" . $pageurl . $pagecb($i) . "\"") . '>' . $i . '</a>';
            if ($epage < $pagemax - 1)
                echo '<span>...</span>';
            if ($epage < $pagemax)
                echo '<a href="' . $pageurl . $pagecb($pagemax) . '">' . $pagemax . '</a>';
            echo '<a ' . ($pageno >= $pagemax ? ' class="disabled"' : " href=\"" . $pageurl . $pagecb($pageno + 1) . "\"") . '>下页</a>';
        }
    }
    static function www_convertseoword($seowords, $content, $keystr = '') {
        $seo = array();
        $max = rand(2, 5);
        shuffle($seowords);
        foreach ($seowords as $row) {
            if ($row['rank'] > rand(0, 100))
                $seo[$row['name']] = $row['url'];
        }
        $repcnt = 0;
        foreach ($seo as $key => $val) {
            $ind = strpos($content, $key);
            if ($ind === false)
                continue;
            $tmp = substr($content, 0, $ind);
            $inda = strrpos($tmp, '<a ciy href=');
            if ($inda !== false) {
                $indb = strpos($tmp, '</a>', $inda);
                if ($indb === false) //包含
                    continue;
            }
            $content = substr_replace($content, '<a ciy href="' . $val . '">' . $key . '</a>', $ind, strlen($key));
            $repcnt++;
            if ($repcnt >= $max)
                break;
        }
        if (!empty($keystr)) {
            $keystr = str_replace('，', ',', $keystr);
            $keystr = str_replace(';', ',', $keystr);
            $keystr = str_replace('；', ',', $keystr);
            $keys = explode(',', $keystr);
            foreach ($keys as $key) {
                if (empty($key))
                    continue;
                $ind = strpos($content, $key);
                if ($ind === false)
                    continue;
                $tmp = substr($content, 0, $ind);
                $inda = strrpos($tmp, '<strong>');
                if ($inda !== false) {
                    $indb = strpos($tmp, '</strong>', $inda);
                    if ($indb === false) //包含
                        continue;
                }
                $content = substr_replace($content, '<strong>' . $key . '</strong>', $ind, strlen($key));
            }
        }
        return $content;
    }
}
