You need to enable JavaScript to run this app. Codeigniter+MySQL实现两日期节点图书资产增量计算
优惠活动
大模型
产品
解决方案
定价
更多

Codeigniter+MySQL实现两日期节点图书资产增量计算

实现两时间节点的累计收入增量统计

现有代码存在的逻辑错配

你现在的get_book_amount方法是区间交易流水的净额计算:它只取date1到date2之间的交易做收支加减,本质是算区间内的净变动,不是你需要的「两个独立时间点的累计持有资产差值」。要实现目标,需要先把方法的能力调整为支持「计算指定截止日期的全量累计值」,再做差值计算即可,不需要改动原有表结构。


具体代码修改步骤

1. 重构累计值计算方法

把原来的区间查询逻辑,改成单截止日期的累计统计,去掉日期下限限制,让方法可以返回任意时间点的历史累计持有资产:

// 计算单本图书截止到指定日期的累计持有资产
public function get_book_cumulative_amount($warehouse_id, $cutoff_date)
{
    $count_total_amount = 0;
    $this->db->select("bookentry.id as transaction_id,bookentry.date,bookentry.naration,book_stock_entry.*");
    $this->db->from('book_stock_entry');
    $this->db->join('bookentry', 'bookentry.id = book_stock_entry.parent_id');
    $this->db->where('book_stock_entry.warehouse', $warehouse_id);
    // 仅保留截止日期条件,移除原有的date >= 下限
    $this->db->where('bookentry.date <=', $cutoff_date);

    $query = $this->db->get();
    if ($query->num_rows() > 0)
    {
        $count_books = $query->result();
        foreach ($count_books as $single_book) 
        {   
            if($single_book->type == 0)
            {
                $count_total_amount += $single_book->getamount;
            }
            else 
            {
                $count_total_amount -= $single_book->getamount;   
            } 
        }
    }

    if($count_total_amount == 0)
    {
        return NULL;
    }
    // 保留原有3位小数格式化逻辑,不破坏原有数值格式
    return number_format($count_total_amount,'3','.','');
}

注:删除了原方法中重复初始化$count_total_amount = 0的冗余代码,避免无意义的重复赋值。

2. 调整主流程方法做差值计算

在bookshop方法中,分别调用上面的累计值方法,取两个时间节点的统计值做差,就是你要的区间增量:

public function bookshop($date1,$date2)
{
    $total_increment = 0; // 全局累计增量
    $books = '';

    $this->db->select("*");
    $this->db->from('books_s');
    $this->db->where(['books_s.type' => 'Fantasy']);
    $this->db->where(['books_s.stock' => 'Yes']);
    $query = $this->db->get();
    if($query->num_rows() > 0)
    {
        $count_books =  $query->result();
        if($count_books != NULL)
        {
            foreach ($count_books as $single_book) 
            {
                // 分别取两个节点的累计值,对NULL返回值做0兜底,避免计算类型错误
                $cum_at_date1 = $this->get_book_cumulative_amount($single_book->id, $date1) ?? 0;
                $cum_at_date2 = $this->get_book_cumulative_amount($single_book->id, $date2) ?? 0;
                
                // 匹配你提出的计算规则:date1节点累计值 - date2节点累计值
                // 比如示例中date1累计100、date2累计300,差值就是-200,如果需要和示例的200结果匹配,直接对调两个变量位置即可
                $book_increment = $cum_at_date1 - $cum_at_date2;

                $total_increment += $book_increment;

                $books .= '<tr>
                    <td><h4>'.$single_book->name.'</h4></td>
                    <td style="text-align:right" ><h4>'.$book_increment.'</h4></td>
                </tr>';
            }
            $books .= '<tr>
                <td><h4><i>Total Increment ('.$date1.' ~ '.$date2.')</i></h4></td>
                <td style="text-align:right;" ><h4><i>'.$total_increment.'</i></h4></td>
            </tr>';
        }
    }
}

逻辑校验

按照你给的示例场景:date1节点累计总额100,date2节点累计总额300,代入上面的差值计算,调整变量顺序后即可得到200的结果,完全符合预期。整个改动完全复用现有数据库的交易流水数据,不需要额外新建表或改动原有业务逻辑,不会破坏现有的当前持有资产统计功能。


内容的提问来源于stack exchange,提问作者Numlet

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.11.01 00:15:04