Notes 17 day17 缓冲流

JavaIO流:缓冲流、转换流和Properties集合详解
本文详细介绍了Java中的缓冲流,包括BufferedInputStream和BufferedOutputStream用于字节流的高效读写,以及BufferedReader和BufferedWriter用于字符流的优化。文章还提到了转换流OutputStreamWriter和InputStreamReader在字符编码中的作用,以及序列化和反序列化对象的ObjectOutputStream和ObjectInputStream。最后,讨论了Properties集合的使用,它是Map体系的集合类,可以与IO流结合进行数据的存储和加载。

day17 缓冲流

概述

缓冲流,也叫高效流,是对4个基本的`FileXxx` 流的增强,所以也是4个流,按照数据类型分类
​
通过定义数组的方式确实比以前一次读取一个字节的方式快很多,所以,看来有一个缓冲区还是非常好的。
​
既然是这样的话,那么,java开始在设计的时候,它也考虑到了这个问题,就专门提供了带缓冲区的字节类。
​
这种类被称为:缓冲区类(高效类)
​
写数据:BufferedOutputStream
​
读数据:BufferedInputStream
​
构造方法可以指定缓冲区的大小,但是我们一般用不上,因为默认缓冲区大小就足够了。
​
为什么不传递一个具体的文件或者文件路径,而是传递一个OutputStream对象呢?
​
原因很简单,字节缓冲区流仅仅提供缓冲区,为高效而设计的。但是呢,真正的读写操作还得靠基本的流对象实现。

缓冲流分类

名称
字节缓冲流BufferedInputStream BufferedOutputStream
字符缓冲流BufferedReader BufferedWriter

字节缓冲流

构造方法

方法名说明
public BufferedInputStream(InputStream in)创建一个 新的缓冲输入流。
public BufferedInputStream(InputStream in,int size)创建 BufferedInputStream具有指定缓冲区大小,并保存其参数,输入流in,供以后使用。
public BufferedOutputStream(OutputStream out)创建一个新的缓冲输出流。
public BufferedOutputStream(OutputStream out,int size)创建一个新的缓冲输出流,以便以指定的缓冲区大小将数据写入指定的底层输出流。

二者基本都是用 InputStream 和 OutputStream 中的 writer() 和 reader() 的重载方法

字符缓冲流

构造方法

方法名说明
public BufferedReader(Reader in)创建一个 新的缓冲输入流。
public BufferedReader(Reader in,int size)创建使用默认大小的输入缓冲区的缓冲字符输入流
public BufferedWriter(Writer out)创建一个新的缓冲输出流。
BufferedWriter(Writer out, int sz)创建一个新的缓冲字符输出流,使用给定大小的输出缓冲区。

常用方法

二者基本都是用 Reader 和 Writer 中的 writer() 和 reader() 的重载方法

字符缓冲流的特有方法

类名方法名说明
BufferedReaderpublic String readLine()一次读取一行文字
BufferedWriterpublic void newLine()写一行行分隔符,由系统属性定义符号 // 换行符
注意

若是循环读取 用的是readLine方法 ,在while中判断的是用 null

转换流

编码

按照某种规则,将字符存储到计算机中

通俗理解::字符(能看懂的)--字节(看不懂的)==

解码

将存储在计算机中的二进制数按照某种规则解析显示出来

通俗理解:字节(看不懂的)-->字符(能看懂的)==

OutputStreamWriter类

是Writer的子类

将字节输出流转换为缓冲字符输出流

常用构造方法

方法名说明
OutputStreamWriter(OutputStream in)创建一个使用默认字符集的字符输出流。
OutputStreamWriter(OutputStream in, String charsetName)创建一个指定字符集的字符输出流。

InputStreamReader类

是Reader的子类

将字节输入流转化为缓冲字符输入流

常用构造方法

方法名说明
InputStreamReader(InputStream in)创建一个使用默认字符集的字符流。
InputStreamReader(InputStream in, String charsetName)创建一个指定字符集的字符流。
注意

用转换流复制时,转换流设置的输入输出流的编码一定要保持一致

序列化流

序列化: 把对象以流的形式进行流化,存储或者在网络中转输 对象 --- 流数据 ObjcetOutputStream

返序列化: 把文本中的数据据以流的形式进行还原成对象 流数据---对象 ObjectInPutStream

ObjectOutputStream类

父类是OutputStream

将Java对象的原始数据类型写出到文件,实现对象的持久存储。

常用的构造方法

4.2.1 构造方法

方法名说明
public ObjectOutputStream(OutputStream out)创建一个指定OutputStream的ObjectOutputStream。

常用方法

4.2.2 序列化常用方法

方法名说明
public final void writeObject (Object obj)将指定的对象写出。
注意

对某类要进行序列化,将其放入到文本文件中时,一定要将该类实现Serializable接口,并且要给该类一个版本号

ObjectInputStream类

父类是InputStream

将之前使用ObjectOutputStream序列化的原始数据恢复为对象。

常用构造方法

方法名说明
public ObjectInputStream(InputStream in)创建一个指定InputStream的ObjectInputStream。

常用方法

方法名说明
public final Object readObject ()读取一个对象。

关键字

transient

被该关键字修饰的成员,数据将无法序列化到文件中

被反序列化出来的时候,该成员将被系统给与默认初始化的值

static

被该关键字修饰的成员,数据将无法序列化到文件中

注意

若是在同一个类中同时同时进行系列化和反序列化,直接使用序列化和反序列化,还是通过方法调用其他类或本类序列化和反序列化,只要是在同一个类中同时进行就直接调用的是还没被序列化对象,而不是调用文本文件中的对象

打印流

Properties集合

Properties集合概述

  • - 是一个Map体系的集合类
    - Properties可以保存到流中或从流中加载
    - 属性列表中的每个键及其对应的值都是一个字符串

Properties基本使用

public class PropertiesDemo01 {
    public static void main(String[] args) {
        //创建集合对象
//        Properties<String,String> prop = new Properties<String,String>(); //错误
        Properties prop = new Properties();
​
        //存储元素
        prop.put("itfxp001", "林青霞");
        prop.put("itfxp002", "张曼玉");
        prop.put("itfxp003", "王祖贤");
​
        //遍历集合
        Set<Object> keySet = prop.keySet();
        for (Object key : keySet) {
            Object value = prop.get(key);
            System.out.println(key + "," + value);
        }
    }
}

Properties集合的特有方法

方法名说明
Object setProperty(String key, String value)设置集合的键和值,都是String类型,底层调用 Hashtable方法 put
String getProperty(String key)使用此属性列表中指定的键搜索属性
Set<String> stringPropertyNames()从该属性列表中返回一个不可修改的键集,其中键及其对应的值是字符串

代码演示

/*
 * 特殊功能:
 * public Object setProperty(String key,String value):添加元素
 * public String getProperty(String key):获取元素
 * public Set<String> stringPropertyNames():获取所有的键的集合
 */
public static void main(String[] args) throws IOException, ClassNotFoundException {
    // 创建集合对象
    Properties prop = new Properties();
​
    // 添加元素
    prop.setProperty("张三", "30");
    prop.setProperty("李四", "40");
    prop.setProperty("王五", "50");
​
    // public Set<String> stringPropertyNames():获取所有的键的集合
    Set<String> set = prop.stringPropertyNames();
    for (String key : set) {
        String value = prop.getProperty(key);//获取元素(value)
        System.out.println(key + "---" + value);
    }
}
​

Properties和IO流相结合的方法

方法名说明
void load(InputStream inStream)从输入字节流读取属性列表(键和元素对)
void load(Reader reader)从输入字符流读取属性列表(键和元素对)
void store(OutputStream out, String comments)将此属性列表(键和元素对)写入此 Properties表中,以适合于使用 load(InputStream)方法的格式写入输出字节流
void store(Writer writer, String comments)将此属性列表(键和元素对)写入此 Properties表中,以适合使用 load(Reader)方法的格式写入输出字符流

代码示例

/*
 * 这里的集合必须是Properties集合:
 * public void load(Reader reader):把文件中的数据读取到集合中
 * public void store(Writer writer,String comments):把集合中的数据存储到文件
 */
public static void main(String[] args) throws IOException, ClassNotFoundException {
​
    // myLoad();
    myStore();
}
​
//写数据
private static void myStore() throws IOException {
    // 创建集合对象
    Properties prop = new Properties();
​
    prop.setProperty("江一燕", "27");
    prop.setProperty("jack", "30");
    prop.setProperty("肉丝", "18");
​
    //public void store(Writer writer,String comments) 把集合中的数据存储到文件
    FileWriter w = new FileWriter("name.properties");
    //写
    prop.store(w, "写入文件的说明:因为我要学习所有我要写入");//comments:信息的描述
    w.close();
}
​
//读数据
private static void myLoad() throws IOException {
​
    Properties prop = new Properties();
    // public void load(Reader reader) 把文件中的数据读取到集合中
    // 注意:这个文件的数据必须是键值对形式
    FileReader r = new FileReader("name.properties");
    //读
    prop.load(r);
    r.close();      
    System.out.println("prop:" + prop);
}
​
/** * @notes 京东收到派服务费核对逻辑 * @param $yearMonth * @return bool * @throws DataNotFoundException * @throws DbException * @throws ModelNotFoundException * @author 胡军 * @date 2025/07/01 */ public function deliveryPrimaryRegionsVerifyDo($yearMonth):bool{ $monthTimeRange = $this->getMonthTimeRange($yearMonth); $status = true; // 初始化状态 // 使用use关键字将$yearMonth传入闭包 如果闭包返回 false,chunk() 会立即停止处理后续数据块 DeliveryPrimaryRegionsItemizationModel::whereBetween('order_time', [ $monthTimeRange['startTime'], $monthTimeRange['endTime'] ])->chunk(100, function ($items) use ($yearMonth,&$status) { // 临时存储当前批次的结果 $itemizationMonthList = []; foreach ($items as $item) { //print_r($item->id."处理完成".PHP_EOL); $processRet = $this->processItem($item, $yearMonth); if(!empty($processRet)){ $itemizationMonthList[] = $processRet; } } // 优化后的事务处理->将整个批次作为一个事务处理,减少事务开启次数 $itemizationModel = new DeliveryPrimaryRegionsItemizationModel(); try { $itemizationModel->startTrans(); // 直接处理整个批次数据 $itemizationModel->saveAll($itemizationMonthList); $itemizationModel->commit(); } catch (\Exception $e) { $itemizationModel->rollback(); Log::error('【京东收派服务费明细核对】--月份为:'.$yearMonth."的费用核对发生错误:" . $e->getMessage()); $status = false; return false; } }); return $status; } // 将处理逻辑封装到单独方法当中 private function processItem($item,$yearMonth) { //实际配送特惠送区域型 $deliveryPrimary = DeliveryPrimaryRegionsQuoteModel::where('origin_province','=',$item['origin_province']) ->where('origin_location','=',$item['origin_city']) ->where('destination_province','=',$item['destination_province']) ->where('destination_location','=',$item['destination_city']) ->find(); if(!empty($deliveryPrimary)){ $deliveryPrimaryData = $deliveryPrimary->toArray(); //1、实际配送特惠送区域型->始发省&始发市&目的省&目的市去匹配对应收派服务费报价表中的始发省&始发市&目的省&目的市对应的京东标快分区值 $item['actual_delivery_area_type'] = $deliveryPrimaryData['jd_standard_partition']; }else{ //记录错误日志 Log::warning('【京东收派服务费明细核对】--月份为:'.$yearMonth."的明细表la_primary_regions_mx当中的[始发省&始发市&目的省&目的市] 没有匹配到 京东收派服务费报价表:la_primary_regions_quote对应的 [始发省&始发市&目的省&目的市],明细记录id为".$item['id']); return []; } //本应发货特惠送区域类型 $laPrimaryRegions = DeliveryPrimaryRegionsModel::where('province','=',$item['destination_province']) ->where('city','=',$item['destination_city'])->find(); if(!empty($laPrimaryRegions)){ //2、本应发货特惠送区域类型->目的省&目的市匹配对应一级区域表中的省&市对应的京东标快分区值 $laPrimaryRegionsData = $laPrimaryRegions->toArray(); $item['expected_delivery_area_type'] = $laPrimaryRegionsData['jd_zone']; }else{ //记录错误日志 Log::warning('【京东收派服务费明细核对】--月份为:'.$yearMonth."的明细表la_primary_regions_mx当中的[目的省&目的市] 没有匹配到 京东收派服务费一级区域表:la_primary_regions对应的 [目的省&目的市],明细记录id为".$item['id']); return []; } //3、是否跨仓->当实际配送特惠送区域型=实际配送特惠送区域时写入不跨仓否则跨仓 $item['is_cross_warehouse'] = $item['actual_delivery_area_type'] == $item['expected_delivery_area_type'] ? '2' : '1'; //4、理论重量: 收派服务费核对明细的平台订单号关联la_scm_day_outbound_data当中的billNo字段,查询该订单billNo下货品goodsNo及数量quantity,再通过关联基础数据物料la_warehouse_facility表的单品重量(通过条码)计算该订单下各货品的重量,并按照订单号分组、求和。 $scmDayOutboundData = ScmDayOutboundDataModel::where('billNo','=',$item['platform_order_number'])->select()->toArray(); if(!empty($scmDayOutboundData)){ $weightTotal = 0; foreach($scmDayOutboundData as $k => $v){ //skuBarcode $goodsWeightModel = GoodsWeightModel::where('barcode','=',$v['skuBarcode'])->field('single_item_weight')->find(); if(!empty($goodsWeightModel)){ $goodsWeight = $goodsWeightModel->toArray(); //la_scm_day_outbound_data数量*la_warehouse_facility单品重量(kg) $weightTotal += $v['quantity'] * $goodsWeight['single_item_weight']; }else{ //记录错误日志 Log::warning('【京东收派服务费明细核对】--月份为:'.$yearMonth."的明细表平台订单号关联la_scm_day_outbound_data当中的billNo字段 但是billNo字段关联基础数据物料la_warehouse_facility表的条码skuBarcode失败,明细记录id为".$item['id']); return []; } } $item['theoretical_weight'] = $weightTotal; }else{ Log::warning('【京东收派服务费明细核对】--月份为:'.$yearMonth."的明细表la_primary_regions_mx当中的平台订单号platform_order_number 没有匹配到 每日出库数据明细表:la_scm_day_outbound_data对应的关联单号billNo,明细记录id为".$item['id']); return []; } //5、重量取整-> 理论重量向上取0.5 if(is_numeric($item['theoretical_weight'])) { //如果是浮点数那么向上取0.5 $floatValue = (float)$item['theoretical_weight']; $item['weight_rounded'] = ceil($floatValue * 2) / 2; }else{ //如果是整数那么就直接赋值即可 $item['weight_rounded'] = $item['theoretical_weight']; } //6、重量差异-> 计费重量-重量取整 $item['weight_difference'] = $item['billing_weight'] - $item['weight_rounded']; //7、金额核验->(存在重复代码待优化 赶工期......) // ①费用类型=快递改址费 5; // ②费用类型=保价费 0.3; // ③费用类型=快递_转寄服务费 5; // ④费用类型=快递超长超重,【计费重量】*1元/kg // ⑤费用类型=快递运费,始发省&始发市&目的省&目的市,匹配对应 收派服务费报价表中的 始发省&始发市&目的省&目的市 对应的报价,使用【计费重量】按照报价计算: // 若 计费重量<=1 计算公式为:首重金额*0.28; // 若1<计费用重量<=30 计算公式为:(首重金额+(计费重量-1)*续重1金额)*0.28; // 若 计费重量>30 计算公式为:(首重金额+(计费重量-1)*续重1金额+(计费重量-30)*续重2金额)*0.28 if($item['fee_type'] == '快递改址费' || $item['fee_type'] == '快递_转寄服务费'){ $item['amount_verification'] = 5; }elseif($item['fee_type'] == '保价费'){ $item['amount_verification'] = 0.3; }elseif($item['fee_type'] == '快递超长超重'){ $item['amount_verification'] = $item['billing_weight'] * 1; }elseif($item['fee_type'] == '快递运费'){ if($item['billing_weight'] <= 1){ $item['amount_verification'] = $deliveryPrimary->first_weight * 0.28; }elseif($item['billing_weight'] <= 30){ $item['amount_verification'] = ($deliveryPrimary->first_weight + ($item['billing_weight'] - 1) * $deliveryPrimary->continued_weight1) * 0.28; }else{ // >30的情况 $item['amount_verification'] = ($deliveryPrimary->first_weight + ($item['billing_weight'] - 1) * $deliveryPrimary->continued_weight1 + ($item['billing_weight'] - 30) * $deliveryPrimary->continued_weight2) * 0.28; } }else{ Log::warning('【京东收派服务费明细核对】--月份为:'.$yearMonth."的明细表la_primary_regions_mx当中的费用类型fee_type 没有匹配到 京东收派服务费报价表:la_delivery_primary_regions对应的费用类型,明细记录id为".$item['id']); return []; } //8、核验差异—>结算金额-金额核验 $item['verification_difference'] = $item['settlement_amount'] - $item['amount_verification']; //9、理论计费->(存在重复代码待优化 赶工期......) // ①费用类型=快递改址费 5; // ②费用类型=保价费 0.3; // ③费用类型=快递_转寄服务费 5; // ④费用类型=快递超长超重,【计费重量】*1元/kg // ⑤费用类型=快递运费, 始发省&始发市&目的省&目的市去匹配对应 收派服务费报价表中的 始发省&始发市&目的省&目的市 当中对应的报价,使用【理论重量】向上取整 1,按照报价计算 // 若理论重量取整后<=1 计算公式为->首重*0.28; // 若1<理论重量<=30 计算公式为:(首重+(理论重量-1)*续重1)*0.28; // 若理论重量>30 计算公式为:(首重+(理论重量-1)*续重1+(理论重量-30)*续重2)*0.28 if($item['fee_type'] == '快递改址费' || $item['fee_type'] == '快递_转寄服务费'){ $item['theoretical_billing'] = 5; }elseif($item['fee_type'] == '保价费'){ $item['theoretical_billing'] = 0.3; }elseif($item['fee_type'] == '快递超长超重'){ $item['theoretical_billing'] = $item['billing_weight'] * 1; }elseif($item['fee_type'] == '快递运费'){ $roundedUp = ceil($item['theoretical_weight']); if($roundedUp <= 1){ $item['theoretical_billing'] = $deliveryPrimary->first_weight * 0.28; }elseif($roundedUp <= 30){ $item['theoretical_billing'] = ($deliveryPrimary->first_weight + ($item['billing_weight'] - 1) * $deliveryPrimary->continued_weight1) * 0.28; }else{ // >30的情况 $item['theoretical_billing'] = ($deliveryPrimary->first_weight + ($item['billing_weight'] - 1) * $deliveryPrimary->continued_weight1 + ($item['billing_weight'] - 30) * $deliveryPrimary->continued_weight2) * 0.28; } }else{ Log::warning('【京东收派服务费明细核对】--月份为:'.$yearMonth."的明细表la_primary_regions_mx当中的费用类型fee_type 没有匹配到 京东收派服务费报价表:la_delivery_primary_regions对应的费用类型,明细记录id为".$item['id']); return []; } //10、理论差异->结算金额-理论计费 $item['theoretical_difference'] = $item['settlement_amount'] - $item['theoretical_billing']; //11、不跨仓收费->(存在重复代码待优化 赶工期......) // ①费用类型=快递改址费 5; // ②费用类型=保价费 0.3; // ③费用类型=快递_转寄服务费 5; // ④费用类型=快递超长超重,【计费重量】*1元/kg // ⑤费用类型=快递运费,目的省&目的市,匹配对应 收派服务费报价表中的 目的省&目的市 对应的报价,使用【计费重量】按照一级区域报价计算 // 若计费重量<=1 计算公式为:首重*0.28; // 若1<计费用重量<=30 计算公式为:(首重+(计费重量-1)*续重1)*0.28; // 若计费重量>30 计算公式为:(首重+(计费重量-1)*续重1+(计费重量-30)*续重2)*0.28; $deliveryPrimaryRegions = DeliveryPrimaryRegionsModel::where('province','=',$item['destination_province']) ->where('city','=',$item['destination_city']) ->find(); if(empty($deliveryPrimaryRegions)){ //记录错误日志 Log::warning('【京东收派服务费明细核对】--月份为:'.$yearMonth."的明细表la_primary_regions_mx当中的[目的省&目的市] 没有匹配到 京东收派服务费一级区域表:la_primary_regions对应的 [目的省&目的市],明细记录id为".$item['id']); return []; } if($item['fee_type'] == '快递改址费' || $item['fee_type'] == '快递_转寄服务费'){ $item['non_cross_warehouse_fee'] = 5; }elseif($item['fee_type'] == '保价费'){ $item['non_cross_warehouse_fee'] = 0.3; }elseif($item['fee_type'] == '快递超长超重'){ $item['non_cross_warehouse_fee'] = $item['billing_weight'] * 1; }elseif($item['fee_type'] == '快递运费'){ if($item['billing_weight'] <= 1){ $item['non_cross_warehouse_fee'] = $deliveryPrimaryRegions->first_wt * 0.28; }elseif($item['billing_weight'] <= 30){ $item['non_cross_warehouse_fee'] = ($deliveryPrimaryRegions->first_wt + ($item['billing_weight'] - 1) * $deliveryPrimaryRegions->add_wt1) * 0.28; }else{ // >30的情况 $item['non_cross_warehouse_fee'] = ($deliveryPrimaryRegions->first_wt + ($item['billing_weight'] - 1) * $deliveryPrimaryRegions->add_wt1 + ($item['billing_weight'] - 30) * $deliveryPrimaryRegions->add_wt2) * 0.28; } }else{ Log::warning('【京东收派服务费明细核对】--月份为:'.$yearMonth."的明细表la_primary_regions_mx当中的[目的省&目的市] 没有匹配到 京东收派服务费一级区域表:la_primary_regions对应的 [目的省&目的市],明细记录id为".$item['id']); return []; } //12、跨仓差异->结算金额-不跨仓收费 $item['cross_warehouse_difference'] = $item['settlement_amount'] - $item['theoretical_billing']; return $item->toArray(); } /** * @notes 根据年月比如2025-05获取当月时间范围 精确到秒 (git有问题未解决 暂时无法写入common.php当中 临时放这) * @param string $yearMonth * @return array * @author 胡军 * @date 2025/07/01 */ private function getMonthTimeRange(string $yearMonth): array { // 验证输入格式 (YYYY-MM) if (!preg_match('/^\d{4}-(0[1-9]|1[0-2])$/', $yearMonth)) { throw new InvalidArgumentException('输入格式不正确,必须为YYYY-MM格式'); } list($year, $month) = explode('-', $yearMonth); // 构建开始时间 $startTime = "{$year}-{$month}-01 00:00:00"; // 使用DateTime类计算当月最后一天 $lastDay = (new \DateTime("{$year}-{$month}-01")) ->modify('last day of this month') ->format('d'); // 构建结束时间 $endTime = "{$year}-{$month}-{$lastDay} 23:59:59"; return [ 'startTime' => $startTime, 'endTime' => $endTime ]; } 这是我核算100万数据的代码,虽然使用了分块处理但是我感觉还有优化的空间,我之前优化完的代码就很好我可以提供给你代码做参考: /** * @notes 非销售出库明细核对逻辑(优化版) * @param $yearMonth * @return bool * @throws DataNotFoundException * @throws DbException * @throws ModelNotFoundException * @author 胡军 * @date 2025/06/25 */ public function nonSalesFeeVerifyDo($yearMonth): bool { // 获取指定月份的时间范围(月初到月末) $monthTimeRange = $this->getMonthTimeRange($yearMonth); // 预加载并缓存报价数据,使用键值对存储(仓库名 => 报价信息) // 避免在后续循环中重复查询数据库,提高性能 $warehouseQuotes = []; $warehouseList = NonSalesItemizationModel::whereBetween('business_time', [ $monthTimeRange['startTime'], $monthTimeRange['endTime'] ])->group('warehouse_name')->column('warehouse_name'); // 批量获取所有仓库的报价记录 foreach ($warehouseList as $warehouse) { // 获取该仓库的报价记录 $quoteResult = NonSalesQuoteModel::where('warehouse', $warehouse)->select()->toArray(); if (!empty($quoteResult)) { $newQuoteResult = []; foreach ($quoteResult as $item) { $newQuoteResult[$item['fee_item']] = $item; } $warehouseQuotes[$warehouse] = $newQuoteResult; } } $status = true; // 处理状态标识 $batchSize = 500; // 批次大小即每次读取的数据条数 $reconnectInterval = 200; // 每10万条重新连接一次数据库(200批次=10万条) $updateCount = 0; // 记录已更新的总记录数 $batchCount = 0; // 记录当前批次数 try { // 使用游标分批处理数据,每次只加载少量数据到内存 // 避免一次性加载大量数据导致内存溢出 $query = NonSalesItemizationModel::whereBetween('business_time', [ $monthTimeRange['startTime'], $monthTimeRange['endTime'] ]); $query->chunk($batchSize, function ($items) use ( &$status, // 引用传递处理状态 &$updateCount, // 引用传递更新计数 &$batchCount, // 引用传递批次计数 $warehouseQuotes, // 仓库报价数据 $reconnectInterval, // 重连间隔 $yearMonth ) { $batchCount++; $updateBuffer = []; // 批量更新缓冲区,存储待更新的数据 // 遍历当前批次的所有明细记录 foreach ($items as $item) { $warehouseName = $item->warehouse_name; $itemData = $item->toArray(); // 检查仓库是否有对应的报价数据 if (isset($warehouseQuotes[$warehouseName])) { $quoteData = $warehouseQuotes[$warehouseName]; // 处理数量数据,确保不为空 $boxCount = !empty($itemData['box_count']) ? $itemData['box_count'] : 0; $looseItems = !empty($itemData['loose_items']) ? $itemData['loose_items'] : 0; // 计算理论费用 $theoreticalFee = $boxCount * $quoteData['整箱']['amount'] + $looseItems * $quoteData['拆零']['amount']; // 计算费用差异 $feeDifference = $itemData['charge_amount'] - $theoreticalFee; } else { // 记录无匹配报价的日志,方便后续排查 Log::warning('【菜鸟非销售出库费明细核对报价单仓库名称不匹配】--月份为:' . $yearMonth . "的订单明细核对匹配不到非销售出库费报价表la_nonsales_fee_quote当中的仓库名称,明细数据id为" . $itemData['id']); // 对于未匹配到的记录,设置为null以便标识 $theoreticalFee = null; $feeDifference = null; } // 准备更新数据,存入缓冲区 $updateBuffer[$itemData['id']] = [ 'theoretical_fee' => $theoreticalFee, // 理论费用 'fee_difference' => $feeDifference // 费用差异 ]; } // 使用高效的批量更新方法将缓冲区数据写入数据库 $this->batchUpdate('la_nosales_fee', $updateBuffer, [ 'theoretical_fee', 'fee_difference' ]); $updateCount += count($updateBuffer); // 更新总计数 // 定期重连数据库,避免长时间运行导致连接断开 if ($batchCount % $reconnectInterval === 0) { $this->reconnectDatabase(); Log::info("菜鸟非销售出库费用核对已处理 {$updateCount} 条,进行数据库重连"); } // 释放内存,避免内存泄漏 unset($items, $updateBuffer); }); } catch (\Exception $e) { // 记录异常信息,确保错误可追溯 Log::error('【菜鸟非销售出库费明细核对异常】--月份为:' . $yearMonth . "的费用核对发生错误:" . $e->getMessage()); $status = false; // 标记处理失败 } // 记录处理完成信息,包含更新的总记录数 Log::info("菜鸟非销售出库费用核对处理完成,共更新 {$updateCount} 条记录"); return $status; } /** * 高效批量更新方法(单SQL更新多条) * @param string $table 表名 * @param array $data 更新数据,格式:[id => ['field1' => 'value1', 'field2' => 'value2']] * @param array $fields 需要更新的字段 */ private function batchUpdate(string $table, array $data, array $fields) { // 数据为空时直接返回,避免无效操作 if (empty($data) || empty($fields)) return; $cases = []; // 存储CASE WHEN语句的数组 $ids = []; // 存储所有需要更新的ID $params = []; // 存储预处理语句的参数 // 构建CASE WHEN更新语句(每个字段一个CASE WHEN) foreach ($fields as $field) { $cases[$field] = 'CASE id '; // 为每个ID和对应字段值构建WHEN子句 foreach ($data as $id => $row) { $cases[$field] .= "WHEN {$id} THEN ? "; // 占位符用于预处理语句 $params[] = $row[$field]; // 对应的值 $ids[] = $id; // 记录ID } $cases[$field] .= 'END'; // 结束CASE语句 } // 去重并拼接ID列表 $idsStr = implode(',', array_unique($ids)); // 构建SET子句(每个字段的CASE WHEN语句) $setClauses = []; foreach ($fields as $field) { $setClauses[] = "{$field} = {$cases[$field]}"; } // 构建完整的UPDATE SQL语句 /* 最后的执行sql可以视为(案例sql): UPDATE la_reverse_delivery_fee SET theoretical_amount = CASE id WHEN 1001 THEN 100 -- 当ID=1001时,更新为100 WHEN 1002 THEN 200 -- 当ID=1002时,更新为200 END, fee_difference = CASE id WHEN 1001 THEN 5 -- 当ID=1001时,更新为5 WHEN 1002 THEN 10 -- 当ID=1002时,更新为10 END WHERE id IN (1001,1002); 其实就是巧妙地利用了 SQL 的CASE WHEN语法,将多行更新合并为单条 SQL,是一种常见的数据库优化技巧! */ $sql = "UPDATE {$table} SET " . implode(', ', $setClauses) . " WHERE id IN ({$idsStr})"; // 执行预处理语句,提高安全性和性能 Db::execute($sql, $params); } // 数据库重连方法,用于长时间运行的任务,避免连接超时 private function reconnectDatabase() { try { $connection = \think\facade\Db::connect(); $connection->close(); // 关闭当前连接 $connection->connect(); // 重新建立连接 } catch (\Exception $e) { // 记录重连失败日志,但不中断程序执行 Log::error('【菜鸟非销售出库费用核对数据库重连失败】' . $e->getMessage()); } } /** * @notes 根据年月比如2025-05获取当月时间范围 精确到秒 * @param string $yearMonth * @return array * @author 胡军 * @date 2025/06/25 */ private function getMonthTimeRange(string $yearMonth): array { // 验证输入格式 (YYYY-MM) if (!preg_match('/^\d{4}-(0[1-9]|1[0-2])$/', $yearMonth)) { throw new InvalidArgumentException('输入格式不正确,必须为YYYY-MM格式'); } list($year, $month) = explode('-', $yearMonth); // 构建开始时间 $startTime = "{$year}-{$month}-01 00:00:00"; // 使用DateTime类计算当月最后一天 $lastDay = (new \DateTime("{$year}-{$month}-01")) ->modify('last day of this month') ->format('d'); // 构建结束时间 $endTime = "{$year}-{$month}-{$lastDay} 23:59:59"; return [ 'startTime' => $startTime, 'endTime' => $endTime ]; } 请根据我后面给你的优化完的代码使用的技术方案来优化我给你的要优化的代码,记住一定最后要提供完整代码并且我的原有的注释信息请保留下来方便我阅读
07-09
/** * @notes 京东装卸服务费核对逻辑 * @param $yearMonth * @return bool * @throws DataNotFoundException * @throws DbException * @throws ModelNotFoundException * @author 胡军 * @date 2025/06/30 */ public function calCulateFeeVerifyDo($yearMonth):bool{ $monthTimeRange = $this->getMonthTimeRange($yearMonth); $itemizationMonthList = CalculateFeeItemizationModel::whereBetween('business_time',[$monthTimeRange['startTime'],$monthTimeRange['endTime']])->select()->toArray(); foreach($itemizationMonthList as $key => $value){ //订单体积(m³): 订单体积/1000000 $itemizationMonthList[$key]['order_volume_cubic_meter'] = $value['order_volume'] / 1000000; //订单重量(t): 订单重量/1000 $itemizationMonthList[$key]['order_weight_ton'] = $value['order_weight'] / 1000; if($itemizationMonthList[$key]['order_weight_ton'] > 0){ //商品体积(m³/t): 订单体积(m³)/ 订单重量(t) 保留两位小数 $itemizationMonthList[$key]['commodity_volume'] = round($itemizationMonthList[$key]['order_volume_cubic_meter'] / $itemizationMonthList[$key]['order_weight_ton'],2); //品类: 商品体积(m³/t)>3, 品类=泡货,否则=重货 $itemizationMonthList[$key]['category'] = $itemizationMonthList[$key]['commodity_volume'] > 3 ? '泡货' : '重货'; //费用单价: 匹配装卸服务费报价对应的品类始发库房名称(仓库)的报价 $calCulateFeeArr = CalculateFeeQuoteModel::where('warehouse', $value['origin_warehouse_name'])->find(); if ($calCulateFeeArr) { $calCulateFeeArr = $calCulateFeeArr->toArray(); } else { $calCulateFeeArr = []; } if(!empty($calCulateFeeArr)){ //理论费用: ① 品类=重货时,订单重量(t)*费用单价; ②品类=泡货时,订单体积(m³)*费用单价 if($itemizationMonthList[$key]['category'] == '泡货'){ //费用单价 $itemizationMonthList[$key]['unit_price'] = $calCulateFeeArr['bulky_goods']; //理论费用 $itemizationMonthList[$key]['theoretical_cost'] = $itemizationMonthList[$key]['order_volume_cubic_meter'] * $itemizationMonthList[$key]['unit_price']; } if($itemizationMonthList[$key]['category'] == '重货'){ //费用单价 $itemizationMonthList[$key]['unit_price'] = $calCulateFeeArr['heavy_goods']; //理论费用 $itemizationMonthList[$key]['theoretical_cost'] = $itemizationMonthList[$key]['order_weight_ton'] * $itemizationMonthList[$key]['unit_price']; } }else{ Log::warning('【京东装卸服务费明细核对】--月份为:'.$yearMonth."的明细表la_calculate没有匹配到装卸服务费报价表:la_calculate_quote对应的仓库名称,明细记录id为".$value['id']); unset($itemizationMonthList[$key]); continue; } //折后费用: 理论费用*0.8 $itemizationMonthList[$key]['discounted_cost'] = $itemizationMonthList[$key]['theoretical_cost'] * 0.8; //费用差异: 结算金额-折后费用 $itemizationMonthList[$key]['difference'] = $value['settlement_amount'] - $itemizationMonthList[$key]['discounted_cost']; }else{ Log::warning('【京东装卸服务费明细核对】--月份为:'.$yearMonth."的订单重量(t)为0导致被除数为0异常报错,明细记录id为".$value['id']); unset($itemizationMonthList[$key]); continue; } } //批量分批次更新数据库 $status = true; $batchSize = 50; // 每批10条记录 $totalCount = count($itemizationMonthList); $batchCount = ceil($totalCount / $batchSize); $itemizationModel = new CalculateFeeItemizationModel(); for ($i = 0; $i < $batchCount; $i++) { $batchData = array_slice($itemizationMonthList, $i * $batchSize, $batchSize); try { $itemizationModel->startTrans(); // 使用批量更新替代循环单条更新 $itemizationModel->saveAll($batchData); $itemizationModel->commit(); } catch (\Exception $e) { $itemizationModel->rollback(); //记录日志 Log::error('【京东装卸服务费明细核对异常】--月份为:'.$yearMonth."的明细费用核对发生错误:" . $e->getMessage()); //其中一个批次数据处理失败则直接退出循环 不再继续执行后续批次的数据处理 报错给前端显示 $status = false; break; } } return $status; } /** * @notes 根据年月比如2025-05获取当月时间范围 精确到秒 (git有问题未解决 暂时无法写入common.php当中 临时放这) * @param string $yearMonth * @return array * @author 胡军 * @date 2025/06/27 */ private function getMonthTimeRange(string $yearMonth): array { // 验证输入格式 (YYYY-MM) if (!preg_match('/^\d{4}-(0[1-9]|1[0-2])$/', $yearMonth)) { throw new InvalidArgumentException('输入格式不正确,必须为YYYY-MM格式'); } list($year, $month) = explode('-', $yearMonth); // 构建开始时间 $startTime = "{$year}-{$month}-01 00:00:00"; // 使用DateTime类计算当月最后一天 $lastDay = (new \DateTime("{$year}-{$month}-01")) ->modify('last day of this month') ->format('d'); // 构建结束时间 $endTime = "{$year}-{$month}-{$lastDay} 23:59:59"; return [ 'startTime' => $startTime, 'endTime' => $endTime ]; } 这是我的业务逻辑代码用户核验100万数据,但是我感觉肯定有性能问题,然后我之前优化了其他模块的100万数据核验,但是两者业务逻辑不通,我提供给你,请你按照我优化好的技术思路来优化我上面的代码,不要忘记为我提供最终的优化好的完整代码包括注释信息 一下是我优化好的模块的核验100万的代码: /** * @notes 非销售出库明细核对逻辑(优化版) * @param $yearMonth * @return bool * @throws DataNotFoundException * @throws DbException * @throws ModelNotFoundException * @author 胡军 * @date 2025/06/25 */ public function nonSalesFeeVerifyDo($yearMonth): bool { // 获取指定月份的时间范围(月初到月末) $monthTimeRange = $this->getMonthTimeRange($yearMonth); // 预加载并缓存报价数据,使用键值对存储(仓库名 => 报价信息) // 避免在后续循环中重复查询数据库,提高性能 $warehouseQuotes = []; $warehouseList = NonSalesItemizationModel::whereBetween('business_time', [ $monthTimeRange['startTime'], $monthTimeRange['endTime'] ])->group('warehouse_name')->column('warehouse_name'); // 批量获取所有仓库的报价记录 foreach ($warehouseList as $warehouse) { // 获取该仓库的报价记录 $quoteResult = NonSalesQuoteModel::where('warehouse', $warehouse)->select()->toArray(); if (!empty($quoteResult)) { $newQuoteResult = []; foreach ($quoteResult as $item) { $newQuoteResult[$item['fee_item']] = $item; } $warehouseQuotes[$warehouse] = $newQuoteResult; } } $status = true; // 处理状态标识 $batchSize = 500; // 批次大小即每次读取的数据条数 $reconnectInterval = 200; // 每10万条重新连接一次数据库(200批次=10万条) $updateCount = 0; // 记录已更新的总记录数 $batchCount = 0; // 记录当前批次数 try { // 使用游标分批处理数据,每次只加载少量数据到内存 // 避免一次性加载大量数据导致内存溢出 $query = NonSalesItemizationModel::whereBetween('business_time', [ $monthTimeRange['startTime'], $monthTimeRange['endTime'] ]); $query->chunk($batchSize, function ($items) use ( &$status, // 引用传递处理状态 &$updateCount, // 引用传递更新计数 &$batchCount, // 引用传递批次计数 $warehouseQuotes, // 仓库报价数据 $reconnectInterval, // 重连间隔 $yearMonth ) { $batchCount++; $updateBuffer = []; // 批量更新缓冲区,存储待更新的数据 // 遍历当前批次的所有明细记录 foreach ($items as $item) { $warehouseName = $item->warehouse_name; $itemData = $item->toArray(); // 检查仓库是否有对应的报价数据 if (isset($warehouseQuotes[$warehouseName])) { $quoteData = $warehouseQuotes[$warehouseName]; // 处理数量数据,确保不为空 $boxCount = !empty($itemData['box_count']) ? $itemData['box_count'] : 0; $looseItems = !empty($itemData['loose_items']) ? $itemData['loose_items'] : 0; // 计算理论费用 $theoreticalFee = $boxCount * $quoteData['整箱']['amount'] + $looseItems * $quoteData['拆零']['amount']; // 计算费用差异 $feeDifference = $itemData['charge_amount'] - $theoreticalFee; } else { // 记录无匹配报价的日志,方便后续排查 Log::warning('【菜鸟非销售出库费明细核对报价单仓库名称不匹配】--月份为:' . $yearMonth . "的订单明细核对匹配不到非销售出库费报价表la_nonsales_fee_quote当中的仓库名称,明细数据id为" . $itemData['id']); // 对于未匹配到的记录,设置为null以便标识 $theoreticalFee = null; $feeDifference = null; } // 准备更新数据,存入缓冲区 $updateBuffer[$itemData['id']] = [ 'theoretical_fee' => $theoreticalFee, // 理论费用 'fee_difference' => $feeDifference // 费用差异 ]; } // 使用高效的批量更新方法将缓冲区数据写入数据库 $this->batchUpdate('la_nosales_fee', $updateBuffer, [ 'theoretical_fee', 'fee_difference' ]); $updateCount += count($updateBuffer); // 更新总计数 // 定期重连数据库,避免长时间运行导致连接断开 if ($batchCount % $reconnectInterval === 0) { $this->reconnectDatabase(); Log::info("菜鸟非销售出库费用核对已处理 {$updateCount} 条,进行数据库重连"); } // 释放内存,避免内存泄漏 unset($items, $updateBuffer); }); } catch (\Exception $e) { // 记录异常信息,确保错误可追溯 Log::error('【菜鸟非销售出库费明细核对异常】--月份为:' . $yearMonth . "的费用核对发生错误:" . $e->getMessage()); $status = false; // 标记处理失败 } // 记录处理完成信息,包含更新的总记录数 Log::info("菜鸟非销售出库费用核对处理完成,共更新 {$updateCount} 条记录"); return $status; } /** * 高效批量更新方法(单SQL更新多条) * @param string $table 表名 * @param array $data 更新数据,格式:[id => ['field1' => 'value1', 'field2' => 'value2']] * @param array $fields 需要更新的字段 */ private function batchUpdate(string $table, array $data, array $fields) { // 数据为空时直接返回,避免无效操作 if (empty($data) || empty($fields)) return; $cases = []; // 存储CASE WHEN语句的数组 $ids = []; // 存储所有需要更新的ID $params = []; // 存储预处理语句的参数 // 构建CASE WHEN更新语句(每个字段一个CASE WHEN) foreach ($fields as $field) { $cases[$field] = 'CASE id '; // 为每个ID和对应字段值构建WHEN子句 foreach ($data as $id => $row) { $cases[$field] .= "WHEN {$id} THEN ? "; // 占位符用于预处理语句 $params[] = $row[$field]; // 对应的值 $ids[] = $id; // 记录ID } $cases[$field] .= 'END'; // 结束CASE语句 } // 去重并拼接ID列表 $idsStr = implode(',', array_unique($ids)); // 构建SET子句(每个字段的CASE WHEN语句) $setClauses = []; foreach ($fields as $field) { $setClauses[] = "{$field} = {$cases[$field]}"; } // 构建完整的UPDATE SQL语句 /* 最后的执行sql可以视为(案例sql): UPDATE la_reverse_delivery_fee SET theoretical_amount = CASE id WHEN 1001 THEN 100 -- 当ID=1001时,更新为100 WHEN 1002 THEN 200 -- 当ID=1002时,更新为200 END, fee_difference = CASE id WHEN 1001 THEN 5 -- 当ID=1001时,更新为5 WHEN 1002 THEN 10 -- 当ID=1002时,更新为10 END WHERE id IN (1001,1002); 其实就是巧妙地利用了 SQL 的CASE WHEN语法,将多行更新合并为单条 SQL,是一种常见的数据库优化技巧! */ $sql = "UPDATE {$table} SET " . implode(', ', $setClauses) . " WHERE id IN ({$idsStr})"; // 执行预处理语句,提高安全性和性能 Db::execute($sql, $params); } // 数据库重连方法,用于长时间运行的任务,避免连接超时 private function reconnectDatabase() { try { $connection = \think\facade\Db::connect(); $connection->close(); // 关闭当前连接 $connection->connect(); // 重新建立连接 } catch (\Exception $e) { // 记录重连失败日志,但不中断程序执行 Log::error('【菜鸟非销售出库费用核对数据库重连失败】' . $e->getMessage()); } } /** * @notes 根据年月比如2025-05获取当月时间范围 精确到秒 * @param string $yearMonth * @return array * @author 胡军 * @date 2025/06/25 */ private function getMonthTimeRange(string $yearMonth): array { // 验证输入格式 (YYYY-MM) if (!preg_match('/^\d{4}-(0[1-9]|1[0-2])$/', $yearMonth)) { throw new InvalidArgumentException('输入格式不正确,必须为YYYY-MM格式'); } list($year, $month) = explode('-', $yearMonth); // 构建开始时间 $startTime = "{$year}-{$month}-01 00:00:00"; // 使用DateTime类计算当月最后一天 $lastDay = (new \DateTime("{$year}-{$month}-01")) ->modify('last day of this month') ->format('d'); // 构建结束时间 $endTime = "{$year}-{$month}-{$lastDay} 23:59:59"; return [ 'startTime' => $startTime, 'endTime' => $endTime ]; }
07-09
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值