HEX
Server: Apache/2.4.59 (Debian)
System: Linux keymana 4.19.0-21-cloud-amd64 #1 SMP Debian 4.19.249-2 (2022-06-30) x86_64
User: lijunjie (1003)
PHP: 7.4.33
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Upload Files
File: /var/www/uileague/wp-content/themes/uileague/helper/team_pts2.php
<?php
class TeamPts2Helper
{
    public static $pb_table = "orm_team_pts_2";
    public static $pb_table_change = "orm_team_pts_change";

    public static function getList($args)
    {
        global $wpdb;
        $keyword = htmlspecialchars($args['keyword']);
        $keyword = empty($keyword) ? '' : trim($keyword);
        $page = intval($args['cpage']) > 1 ? intval($args['cpage']) : 1;
        $all = intval($args['pageLimit']) == -1;
        $pageLimit = intval($args['pageLimit']) > 1 ? intval($args['pageLimit']) : 10;
        $filterTeamId = trim($args['team_id']);
        $filterLeagueId = trim($args['league_id']);
        $group_by = trim($args['group_by']);
        $filterSql = "";
        if ($filterTeamId != "" && is_numeric($filterTeamId)) {
            $filterSql .= $wpdb->prepare(" AND `team_id` = %d ", $filterTeamId);
        }
        if ($filterLeagueId != "" && is_numeric($filterLeagueId)) {
            $filterSql .= $wpdb->prepare(" AND `league_id` = %d ", $filterLeagueId);
        }
        // 分組,默認按 player_id 分組:統計入球、紅黃牌及排名
        if ($group_by != "") {
            $filterSql .= " group by {$group_by} ";
        } else {
            $filterSql .= " group by team_id ";
        }
        $table = self::$pb_table;
        $total = intval($wpdb->get_var("select count(*) from (SELECT id FROM `{$table}` where deleted is null " . $filterSql . ") as a"));
        if ($all) {
            $results = $wpdb->get_results("SELECT id,team_id,league_id,match_id,created,updated,deleted,SUM(p) as p,SUM(w) as w,SUM(d) as d,SUM(l) as l,SUM(f) as f,SUM(a) as a,SUM(pts) as pts,SUM(abstain) as abstain,SUM(yellow_card) as yellow_card,SUM(red_card) as red_card,ROW_NUMBER() OVER(order by pts desc) pos FROM `{$table}` WHERE `deleted` is null " . $filterSql . " order by pts desc");
        } else {
            $results = $wpdb->get_results("SELECT id,team_id,league_id,match_id,created,updated,deleted,SUM(p) as p,SUM(w) as w,SUM(d) as d,SUM(l) as l,SUM(f) as f,SUM(a) as a,SUM(pts) as pts,SUM(abstain) as abstain,SUM(yellow_card) as yellow_card,SUM(red_card) as red_card,ROW_NUMBER() OVER(order by pts desc) pos FROM `{$table}` WHERE `deleted` is null " . $filterSql . $wpdb->prepare(" order by pts desc LIMIT %d,%d", ($page - 1) * $pageLimit, $pageLimit));
        }
        $dataList = array_map(function ($item) {
            return TeamPts2Helper::map_item($item);
        }, $results);
        $result = new stdClass();
        $result->rc = ApiStatus::SUCCESS[0];
        $result->msg = ApiStatus::SUCCESS[1];
        $result->currentPage = $page;
        $result->total = $total;
        $result->data = $dataList;
        return $result;
    }

    public static function get_by_id($id, $map_item = true)
    {
        if (empty($id)) return null;
        global $wpdb;
        $row = $wpdb->get_row($wpdb->prepare("SELECT id,team_id,league_id,match_id,created,updated,deleted,SUM(p) as p,SUM(w) as w,SUM(d) as d,SUM(l) as l,SUM(f) as f,SUM(a) as a,SUM(pts) as pts,SUM(abstain) as abstain,SUM(yellow_card) as yellow_card,SUM(red_card) as red_card,ROW_NUMBER() OVER(order by pts desc) pos from `" . self::$pb_table . "` where `deleted` is null AND id = %d ", $id));
        return self::map_item($row, $map_item);
    }

    public static function get_by_team($team_id, $match_id = 0, $map_item = true)
    {
        if (empty($team_id)) return null;
        global $wpdb;
        $sql = $wpdb->prepare("SELECT id,team_id,league_id,match_id,created,updated,deleted,SUM(p) as p,SUM(w) as w,SUM(d) as d,SUM(l) as l,SUM(f) as f,SUM(a) as a,SUM(pts) as pts,SUM(abstain) as abstain,SUM(yellow_card) as yellow_card,SUM(red_card) as red_card,ROW_NUMBER() OVER(order by pts desc) pos from `" . self::$pb_table . "` where `deleted` is null AND team_id = %d AND match_id = %d", $team_id, $match_id);
        $row = $wpdb->get_row($sql);
        return self::map_item($row, $map_item);
    }

    public static function get_abstain_team($match_id){
        if (empty($match_id)) return null;
        global $wpdb;
        $sql = $wpdb->prepare("SELECT team_id from `" . self::$pb_table . "` where `deleted` is null AND match_id = %d AND abstain > 0", $match_id);
        $var = $wpdb->get_var($sql);
        return $var;
    }

    public static function map_item($row, $map_item = true)
    {
        if (empty($row)) return null;
        $row->fa_diff = (string)($row->f - $row->a);
        if ($map_item) {
            $row->team_obj = TeamHelper::get_by_id($row->team_id);
            $row->league_obj = LeagueHelper::get_by_id($row->league_id);
            $row->match_obj = MatchesHelper::get_by_id($row->match_id);
        }
        return $row;
    }

    // 添加比赛的时候必须调用
    public static function init($team_id, $match_id = 0, $uid = 0, $wpuid = 0)
    {
        if (!self::team_pts_exists($team_id, $match_id)) {
            $args = array(
                'team_id' => $team_id,
                'match_id' => $match_id,
                'team_p' => 0,
                'team_w' => 0,
                'team_d' => 0,
                'team_l' => 0,
                'team_f' => 0,
                'team_a' => 0,
                'pts' => 0,
                'abstain' => 0,
                'yellow_card' => 0,
                'red_card' => 0,
            );
            self::add($args, $uid, $wpuid);
        }
    }

    // 對球隊比賽結果進行操作
    public static function pts_action($args, $uid = 0, $wpuid = 0, callable $func)
    {
        $result = new stdClass();
        $team_id = intval($args['team_id']);
        $match_id = intval($args['match_id']);
        if (empty($team_id) || empty($match_id)) {
            $result->rc = ApiStatus::ERR_PARAMS[0];
            $result->msg = ApiStatus::ERR_PARAMS[1];
        } else {
            $row = TeamPts2Helper::get_by_team($team_id, $match_id);
            $result = $func($args, $row, $uid, $wpuid);
        }
        return $result;
    }

    /**
     * 计算球队比赛得分情况
     * 
     * @param $match_id 比赛ID
     * @param $abstain  弃权的球队ID,默认0,没有弃权
     */
    public static function match_finished($match_id, $abstain = 0, $uid = 0, $wpuid = 0)
    {
        $result = new stdClass();
        if (empty($match_id)) {
            $result->rc = ApiStatus::ERR_PARAMS[0];
            $result->msg = ApiStatus::ERR_PARAMS[1];
        } else {
            $match_obj = MatchesHelper::get_by_id($match_id);
            // 球队id
            $team_1 = $match_obj->team_1;
            $team_2 = $match_obj->team_2;
            // 统计球员入球、红黄牌
            $teamPlayersPts_1 = PlayersPtsHelper::get_pts_by_team($match_id, $team_1);
            $teamPlayersPts_2 = PlayersPtsHelper::get_pts_by_team($match_id, $team_2);
            // 入球
            $team_f_1 = $teamPlayersPts_1['goal'];
            $team_f_2 = $teamPlayersPts_2['goal'];
            // 黄牌
            $yellow_cards_1 = $teamPlayersPts_1['yellow_cards'];
            $yellow_cards_2 = $teamPlayersPts_2['yellow_cards'];
            // 红牌
            $red_cards_1 = $teamPlayersPts_1['red_cards'];
            $red_cards_2 = $teamPlayersPts_2['red_cards'];
            // 失球
            $team_a_1 = $team_f_2;
            $team_a_2 = $team_f_1;
            // 弃权
            $abstain_1 = ($abstain > 0 && $abstain == $team_1) ? 1 : 0;
            $abstain_2 = ($abstain > 0 && $abstain == $team_2) ? 1 : 0;
            // 得分
            if ($abstain > 0) { // 有球队弃权
                // 完赛(此时如果还未开始比赛,则不算完赛)
                $team_p_1 = ($match_obj->status == 0) ? 0 : 1;
                $team_p_2 = $team_p_1;
                // 胜(不弃权)
                $team_w_1 = ($abstain_1 == 0) ? 1 : 0;
                $team_w_2 = ($abstain_2 == 0) ? 1 : 0;
                // 和
                $team_d_1 = 0;
                $team_d_2 = 0;
                // 负(弃权)
                $team_l_1 = ($abstain_1 == 1) ? 1 : 0;
                $team_l_2 = ($abstain_2 == 1) ? 1 : 0;
            } else { // 正常比赛
                // 完赛
                $team_p_1 = 1;
                $team_p_2 = $team_p_1;
                // 胜
                $team_w_1 = ($team_f_1 > $team_f_2) ? 1 : 0;
                $team_w_2 = ($team_f_2 > $team_f_1) ? 1 : 0;
                // 和
                $team_d_1 = ($team_f_1 == $team_f_2) ? 1 : 0;
                $team_d_2 = ($team_f_2 == $team_f_1) ? 1 : 0;
                // 负
                $team_l_1 = ($team_f_1 < $team_f_2) ? 1 : 0;
                $team_l_2 = ($team_f_2 < $team_f_1) ? 1 : 0;
            }
            // 得分:(胜 * 3) + (和 * 1)
            $pts_1 = $team_w_1 * 3 + $team_d_1;
            $pts_2 = $team_w_2 * 3 + $team_d_2;
            $params_1 = array(
                'team_id' => $team_1,
                'match_id' => $match_id,
                'team_p' => $team_p_1,
                'team_w' => $team_w_1,
                'team_d' => $team_d_1,
                'team_l' => $team_l_1,
                'team_f' => $team_f_1,
                'team_a' => $team_a_1,
                'pts' => $pts_1,
                'abstain' => $abstain_1,
                'yellow_card' => $yellow_cards_1,
                'red_card' => $red_cards_1,
            );
            $params_2 = array(
                'team_id' => $team_2,
                'match_id' => $match_id,
                'team_p' => $team_p_2,
                'team_w' => $team_w_2,
                'team_d' => $team_d_2,
                'team_l' => $team_l_2,
                'team_f' => $team_f_2,
                'team_a' => $team_a_2,
                'pts' => $pts_2,
                'abstain' => $abstain_2,
                'yellow_card' => $yellow_cards_2,
                'red_card' => $red_cards_2,
            );
        }
        // 更新球队1
        $res1 = TeamPts2Helper::pts_action($params_1, $uid, $wpuid, function ($args, $row, $uid, $wpuid) {
            if ($row == null) {
                $result = TeamPts2Helper::add($args, $uid, $wpuid);
            } else {
                $result = TeamPts2Helper::update($args, $uid, $wpuid);
            }
            return $result;
        });
        // 更新球队2
        $res2 = TeamPts2Helper::pts_action($params_2, $uid, $wpuid, function ($args, $row, $uid, $wpuid) {
            if ($row == null) {
                $result = TeamPts2Helper::add($args, $uid, $wpuid);
            } else {
                $result = TeamPts2Helper::update($args, $uid, $wpuid);
            }
            return $result;
        });
        if ($res1->rc == ApiStatus::SUCCESS[0] && $res2->rc == ApiStatus::SUCCESS[0]) { // 2个球队都更新成功
            return $res1;
        } else if ($res1->rc == ApiStatus::SUCCESS[0]) { // 只有球队1更新成功,球队2更新失败
            return $res2;
        } else { // 球队1更新失败或2队都失败
            return $res1;
        }
    }

    public static function add($args, $uid = 0, $wpuid = 0)
    {
        global $wpdb;
        $team_id = intval($args['team_id']);
        $match_id = intval($args['match_id']);
        $p = intval($args['team_p']);    // 完赛
        $w = intval($args['team_w']);    // 胜
        $d = intval($args['team_d']);    // 平
        $l = intval($args['team_l']);    // 负
        $f = intval($args['team_f']);    // 入球
        $a = intval($args['team_a']);    // 失球
        $pts = intval($args['pts']);    // 失球
        $abstain = intval($args['abstain']);    // 棄權
        $yellow_card = intval($args['yellow_card']);    // 黃牌
        $red_card = intval($args['red_card']);    // 紅牌

        $match = MatchesHelper::get_by_id($match_id);
        $league_id = empty($match) ? 0 : $match->league_id;

        $result = new stdClass();

        if (empty($team_id) || empty($match_id)) {
            $result->rc = ApiStatus::ERR_PARAMS[0];
            $result->msg = ApiStatus::ERR_PARAMS[1];
        } else if (TeamPts2Helper::team_pts_exists($team_id, $match_id)) {
            $result->rc = ApiStatus::ERR_TEAM_PTS_EXISTS[0];
            $result->msg = ApiStatus::ERR_TEAM_PTS_EXISTS[1];
        } else {
            $new_data = array(
                'team_id' => $team_id,
                'match_id' => $match_id,
                'league_id' => $league_id,
                'p' => $p,
                'w' => $w,
                'd' => $d,
                'l' => $l,
                'f' => $f,
                'a' => $a,
                'pts' => $pts,
                'abstain' => $abstain,
                'yellow_card' => $yellow_card,
                'red_card' => $red_card,
                'created' => current_time('mysql'),
                'updated' => current_time('mysql'),
            );
            $res = $wpdb->insert(
                self::$pb_table,
                $new_data,
                array('%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%s', '%s')
            );
            if ($res === false) {
                $result->rc = ApiStatus::ERR_TEAM_PTS_ADD[0];
                $result->msg = ApiStatus::ERR_TEAM_PTS_ADD[1];
            } else {
                $insert_id = $wpdb->insert_id;
                Logger::log("添加了比賽結果", self::$pb_table, $insert_id, null, $new_data, $uid, $wpuid);
                // // 更新排名
                // $row = TeamPts2Helper::get_by_id($insert_id);
                // if ($row != null) {
                //     TeamPts2Helper::add_pts_change($team_id, $league_id, $row->pts, $row->pos);
                // }
                $result->rc = ApiStatus::SUCCESS[0];
                $result->msg = ApiStatus::SUCCESS[1];
                $result->data = $insert_id;
            }
        }
        return $result;
    }

    public static function update($args, $uid = 0, $wpuid = 0)
    {
        global $wpdb;
        // 方式一
        $id = intval($args['id']);
        // 方式二
        $team_id = intval($args['team_id']);
        $match_id = intval($args['match_id']);
        // 公共參數
        $p = intval($args['team_p']);    // 完赛
        $w = intval($args['team_w']);    // 胜
        $d = intval($args['team_d']);    // 平
        $l = intval($args['team_l']);    // 负
        $f = intval($args['team_f']);    // 入球
        $a = intval($args['team_a']);    // 失球
        $pts = intval($args['pts']);    // 失球
        $abstain = intval($args['abstain']);    // 棄權
        $yellow_card = intval($args['yellow_card']);    // 黃牌
        $red_card = intval($args['red_card']);    // 紅牌

        $result = new stdClass();

        if (empty($id) && empty($team_id) && empty($match_id)) {
            $result->rc = ApiStatus::ERR_PARAMS[0];
            $result->msg = ApiStatus::ERR_PARAMS[1];
        } else {
            global $wpdb;
            if (empty($id)) {
                $row = TeamPts2Helper::get_by_team($team_id, $match_id);
                $id = $row->id;
            } else {
                $row = TeamPts2Helper::get_by_id($id);
            }
            if ($row == null) {
                $result->rc = ApiStatus::ERR_TEAM_PTS_NONE[0];
                $result->msg = ApiStatus::ERR_TEAM_PTS_NONE[1];
            } else {
                $new_data = array(
                    'p' => $p,
                    'w' => $w,
                    'd' => $d,
                    'l' => $l,
                    'f' => $f,
                    'a' => $a,
                    'pts' => $pts,
                    'abstain' => $abstain,
                    'yellow_card' => $yellow_card,
                    'red_card' => $red_card,
                    'updated' => current_time('mysql'),
                );
                $res = $wpdb->update(
                    self::$pb_table,
                    $new_data,
                    array('id' => $id),
                    array('%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%s'),
                    array('%d')
                );
                if ($res === false) {
                    $result->rc = ApiStatus::ERR_TEAM_PTS_UPDATE[0];
                    $result->msg = ApiStatus::ERR_TEAM_PTS_UPDATE[1];
                } else {
                    Logger::log("更新了比賽結果", self::$pb_table, $id, $row, $new_data, $uid, $wpuid);
                    // // 更新排名
                    // $row = TeamPts2Helper::get_by_id($id);
                    // if ($row != null) {
                    //     TeamPts2Helper::add_pts_change($row->team_id, $row->league_id, $row->pts, $row->pos);
                    // }
                    $result->rc = ApiStatus::SUCCESS[0];
                    $result->msg = ApiStatus::SUCCESS[1];
                }
            }
        }
        return $result;
    }

    public static function delete($args, $uid = 0, $wpuid = 0)
    {
        $id = intval($args['id']);
        $row = TeamPts2Helper::get_by_id($id);

        $result = new stdClass();

        if ($row == null) {
            $result->rc = ApiStatus::ERR_TEAM_PTS_NONE[0];
            $result->msg = ApiStatus::ERR_TEAM_PTS_NONE[1];
        } else {
            global $wpdb;
            // $res = $wpdb->delete(self::$pb_table, array('id' => $id), array('%d'));
            $res = $wpdb->update(
                self::$pb_table,
                array('deleted' => current_time('mysql')),
                array('id' => $id),
                array('%s'),
                array('%d')
            );
            if ($res === false) {
                $result->rc = ApiStatus::ERR_TEAM_PTS_DELETE[0];
                $result->msg = ApiStatus::ERR_TEAM_PTS_DELETE[1];
            } else {
                Logger::log("刪除了比賽結果", self::$pb_table, $id, $row, null, $uid, $wpuid);
                $result->rc = ApiStatus::SUCCESS[0];
                $result->msg = ApiStatus::SUCCESS[1];
            }
        }
        return $result;
    }

    public static function delete_by_team($team_id, $match_id, $uid = 0, $wpuid = 0)
    {
        $row = TeamPts2Helper::get_by_team($team_id, $match_id);

        $result = new stdClass();

        if ($row == null) {
            $result->rc = ApiStatus::ERR_TEAM_PTS_NONE[0];
            $result->msg = ApiStatus::ERR_TEAM_PTS_NONE[1];
        } else {
            global $wpdb;
            $res = $wpdb->update(
                self::$pb_table,
                array('deleted' => current_time('mysql')),
                array('team_id' => $team_id, 'match_id' => $match_id),
                array('%s'),
                array('%d', '%d')
            );
            if ($res === false) {
                $result->rc = ApiStatus::ERR_TEAM_PTS_DELETE[0];
                $result->msg = ApiStatus::ERR_TEAM_PTS_DELETE[1];
            } else {
                Logger::log("刪除了比賽結果", self::$pb_table, $row->id, $row, null, $uid, $wpuid);
                $result->rc = ApiStatus::SUCCESS[0];
                $result->msg = ApiStatus::SUCCESS[1];
            }
        }
        return $result;
    }

    public static function team_pts_exists($team_id, $match_id)
    {
        $row = TeamPts2Helper::get_by_team($team_id, $match_id);
        return $row != null;
    }

    public static function add_pts_change($team_id, $league_id, $pts, $pos)
    {
        global $wpdb;
        if (!empty($team_id)) {
            $res = $wpdb->insert(
                self::$pb_table_change,
                array(
                    'team_id' => $team_id,
                    'league_id' => $league_id,
                    'pos' => $pos,
                    'pts' => $pts,
                    'created' => current_time('mysql'),
                    'updated' => current_time('mysql'),
                ),
                array('%d', '%d', '%d', '%d', '%s', '%s'),
            );
            return $res !== false;
        }
        return false;
    }

    public static function get_latest_pts_change($team_id, $league_id)
    {
        global $wpdb;
        return $wpdb->get_row($wpdb->prepare("select * from `" . self::$pb_table_change . "` where `deleted` is null AND team_id = %d AND league_id = %d order by updated desc limit 1", $team_id, $league_id));
    }
}