有时候在写代码的时候只关注功能的实现,代码的质量很差,这里用一个修改某个表中数值的例子来说明,如何实现代码逐步优化的过程:
实例1:
第一种实现方式:
if($enableQuantity >= $transactionQuantity){ //更新锁定库存的数量 $inventory_locked_data = array( "locked_quantity" => $inventoryItem['locked_quantity'] + $transactionQuantity, "locked_real_quantity" => $inventoryItem['locked_real_quantity'] + $transactionQuantity * $inventoryItem['unit_quantity'] ); //记录库存锁定交易过程 $inventory_locked_transaction_data = array( "inventory_item_id" => $inventoryItem['inventory_item_id'], "quantity" => $transactionQuantity, "real_quantity" => $transactionQuantity * $inventoryItem['unit_quantity'], "created_user" => $params['created_user'] ); $transactionQuantity = 0; }else{ //更新锁定库存的数量 $inventory_locked_data = array( "locked_quantity" => $inventoryItem['locked_quantity'] + $enableQuantity, "locked_real_quantity" => $inventoryItem['locked_real_quantity'] + $enableQuantity * $inventoryItem['unit_quantity'] ); //记录库存锁定交易过程 $inventory_locked_transaction_data = array( "inventory_item_id" => $inventoryItem['inventory_item_id'], "quantity" => $enableQuantity, "real_quantity" => $enableQuantity * $inventoryItem['unit_quantity'], "created_user" => $params['created_user'] ); $transactionQuantity -= $enableQuantity; }
第二种实现方式:
$inventory_locked_transaction_data_list = array(); $inventory_item_ids = explode("," , ModelPalletModel::getInventoryItemIdsByPalletIds($params['pallet_id'])); $transactionQuantity = $params['quantity']; foreach($inventory_item_ids as $inventory_item_id){ $inventoryItem = ModelInventoryItemModel::getInventoryItemById($inventory_item_id); $enableQuantity = $inventoryItem['quantity'] - $inventoryItem['locked_quantity']; if($enableQuantity <= 0){ continue; } if ($transactionQuantity == 0) { break; } //原先使用了if条件进行判断的,导致了下面代码的冗余 $locked_quantity = $enableQuantity >= $transactionQuantity ? $inventoryItem['locked_quantity'] + $transactionQuantity : $inventoryItem['locked_quantity'] + $enableQuantity; $transaction_locked_quantity = $enableQuantity >= $transactionQuantity ? $transactionQuantity : $enableQuantity; //更新锁定库存的数量 $inventory_locked_data = array( "locked_quantity" => $locked_quantity, "locked_real_quantity" => $locked_quantity * $inventoryItem['unit_quantity'] ); //记录库存锁定交易过程 $inventory_locked_transaction_data = array( "inventory_item_id" => $inventoryItem['inventory_item_id'], "quantity" => $transaction_locked_quantity, "real_quantity" => $transaction_locked_quantity * $inventoryItem['unit_quantity'], "created_user" => $params['created_user'] ); $transactionQuantity -= $transaction_locked_quantity; ModelInventoryItemModel::updateInventoryItem($inventoryItem['inventory_item_id'] , $inventory_locked_data); ModelInventoryItemModel::createInventoryLockedTransaction($inventory_locked_transaction_data); array_push($inventory_locked_transaction_data_list , $inventory_locked_transaction_data); }
第三种实现方式:
$inventory_locked_transaction_data_list = array(); $inventory_item_ids = explode("," , ModelPalletModel::getInventoryItemIdsByPalletIds($params['pallet_id'])); $transactionQuantity = $params['quantity']; foreach($inventory_item_ids as $inventory_item_id){ $inventoryItem = ModelInventoryItemModel::getInventoryItemById($inventory_item_id); $enableQuantity = $inventoryItem['quantity'] - $inventoryItem['locked_quantity']; if($enableQuantity <= 0){ continue; } if ($transactionQuantity == 0) { break; } //优化后的代码 $locked_quantity = min($enableQuantity , $transactionQuantity); //更新锁定库存的数量 $inventory_locked_data = array( "locked_quantity" => $locked_quantity + $inventoryItem['locked_quantity'], "locked_real_quantity" => ($locked_quantity + $inventoryItem['locked_quantity']) * $inventoryItem['unit_quantity'] ); //记录库存锁定交易过程 $inventory_locked_transaction_data = array( "inventory_item_id" => $inventoryItem['inventory_item_id'], "quantity" => $locked_quantity, "real_quantity" => $locked_quantity * $inventoryItem['unit_quantity'], "created_user" => $params['created_user'] ); $transactionQuantity -= $locked_quantity; ModelInventoryItemModel::updateInventoryItem($inventoryItem['inventory_item_id'] , $inventory_locked_data); ModelInventoryItemModel::createInventoryLockedTransaction($inventory_locked_transaction_data); array_push($inventory_locked_transaction_data_list , $inventory_locked_transaction_data); } if($transactionQuantity > 0){ Flight::throwException($transactionQuantity , 40002); }
第四种实现方式:
try { $inventory_locked_transaction_data_list = array(); $inventory_item_ids = explode("," , ModelPalletModel::getInventoryItemIdsByPalletIds($params['pallet_id'])); $transactionQuantity = $params['quantity']; foreach($inventory_item_ids as $inventory_item_id){ if ($transactionQuantity == 0) { break; } $inventoryItem = ModelInventoryItemModel::getInventoryItemById($inventory_item_id); $enableQuantity = bcsub($inventoryItem['quantity'] , $inventoryItem['locked_quantity'], 6); if($enableQuantity <= 0){ continue; } $locked_quantity = min($enableQuantity, $transactionQuantity); //更新锁定库存的数量 $inventory_locked_data = array( "locked_quantity" => bcadd($locked_quantity, $inventoryItem['locked_quantity'], 6), "locked_real_quantity" => bcmul(bcadd($locked_quantity, $inventoryItem['locked_quantity'], 6), $inventoryItem['unit_quantity'], 6) ); //记录库存锁定交易过程 $inventory_locked_transaction_data = array( "inventory_item_id" => $inventoryItem['inventory_item_id'], "quantity" => $locked_quantity, "real_quantity" => bcmul($locked_quantity, $inventoryItem['unit_quantity'], 6), "created_user" => $params['created_user'] ); //使用高精度计算 $transactionQuantity = bcsub($transactionQuantity,$locked_quantity,6); ModelInventoryItemModel::updateInventoryItem($inventoryItem['inventory_item_id'] , $inventory_locked_data); ModelInventoryItemModel::createInventoryLockedTransaction($inventory_locked_transaction_data); array_push($inventory_locked_transaction_data_list , $inventory_locked_transaction_data); } if($transactionQuantity > 0){ throw new Exception("inventory quantity not sufficient"); } Flight::db()->commit(); Flight::sendRouteResult(array('inventory_item_list' => $inventory_locked_transaction_data_list)); } catch (Exception $e) { Flight::db()->rollback(); Flignt::sendRouteResult('error_info'=>$e->getMessage(), 'error_code'=>$e->getCode()); }
这四种代码实现方式主要优化过程在于避免代码冗余,代码处理顺序,数值计算使用高精度等;
实例2:下面是实现一个字符串插入数据库,要求每个字符串唯一;
优化前:
$res = true; for($i = 1; $res; $i++){ $inventory_batch_sn = 'F-'.date("YmdHis").'-P-'.$i; if(!empty(InventoryItemModel::getInventoryItemByInventoryBatchSn($inventory_batch_sn))){ $res = true; }else{ $res = false; } } $inventory_item_data['inventory_batch_sn'] = $inventory_batch_sn;
优化后1:
$inventory_batch_sn = $params['facility_id'] . '-' . date("YmdHis") . '-' . $params['product_id'] . '-'; $count = InventoryItemModel::getCountInventoryItemByInventoryBatchSn($inventory_batch_sn); $inventory_item_data['inventory_batch_sn'] = $inventory_batch_sn . ($count+1);
减少了代码量;
优化后2,:
$inventory_batch_sn = $params['facility_id'] . '-' . date("YmdHis") . '-' . $params['product_id'] . '-';
$inventoryItem = InventoryItemModel::getInventoryItemByInventoryBatchSn($inventory_batch_sn . "1");
if(empty($inventoryItem)){
$inventory_item_data['inventory_batch_sn'] = $inventory_batch_sn . "1";
} else {
$count = InventoryItemModel::getCountInventoryItemByInventoryBatchSn($inventory_batch_sn);
$inventory_item_data['inventory_batch_sn'] = $inventory_batch_sn . ($count+1);
}
public static function getCountInventoryItemByInventoryBatchSn($inventory_batch_sn) {
$sql = "select count(distinct inventory_batch_sn) from ws_inventory_item where inventory_batch_sn like '{$inventory_batch_sn}%' limit 1 ";
return Flight::db()->getOne($sql);
}
这种方式保证了查询性能;