<?php
/*
* use for to transfer card serial from a to b,but the serial a have is like this ab-cde-000000001 to ab-cde-000000009
* when i want transfer (ab-cde-000000001 to ab-cde-000000003) or (ab-cde-000000003 to ab-cde-000000005) or (ab-cde-000000006 to ab-cde-000000009) to b
* so there is a algorithm,see the function transCardById
* db struct:id aid startSerial endSerial time
* copy right http://qqmaster.info
*/
function transCardById($db, $startSerial,$endSerial,$serialId,$accountId, $newAccountId)
{
$result = false;
$serialInfo = test::getSerialById2($db,$serialId);
if(!empty($serialInfo))
{
//echo '1';echo '<br />';
$num = '%1$09d';
$prefixe = test::getSerialPrefix($startSerial);
$startSerialSuffix = test::getSerialSuffix($startSerial);
$endSerialSuffix = test::getSerialSuffix($endSerial);
$serialInfoStartSerialSuffix = test::getSerialSuffix($serialInfo['serialNumStart']);
$serialInfoEndSerialSuffix = test::getSerialSuffix($serialInfo['serialNumEnd']);
//echo $startSerialSuffix.'--'.$endSerialSuffix.'--'.$serialInfoStartSerialSuffix.'--'.$serialInfoEndSerialSuffix.'--';
// 1,5
if($serialInfoStartSerialSuffix==$startSerialSuffix)
{
//echo '2';echo '<br />';
if($serialInfoEndSerialSuffix==$endSerialSuffix)
{
//echo '3';echo '<br />';
// 1,5
// update accountId To newAccountId
$result = test::updateSerialById($db,$serialId,$newAccountId);
}
elseif($serialInfoEndSerialSuffix>$endSerialSuffix)
{
//echo '4';echo '<br />';
// 1,3
// update start serial to 4
// add a record with newAccountId,serial from 1 to 3
$result = test::updateSerialById($db,$serialId,'',$prefixe.sprintf($num,($endSerialSuffix+1)));
if($result==true)
{
//echo '5';echo '<br />';
$result = test::addCard($db, $startSerial, $endSerial, $newAccountId);
}
}
}
elseif($serialInfoStartSerialSuffix<$startSerialSuffix)
{
//echo '6';echo '<br />';
if($serialInfoEndSerialSuffix==$endSerialSuffix)
{
//echo '7';echo '<br />';
// 3,5
// update end serial to 2
// add a record with newAccountId,serial from 3 to 5
$result = test::updateSerialById($db,$serialId,'','',$prefixe.sprintf($num,($startSerialSuffix-1)));
if($result==true)
{
//echo '8';echo '<br />';
$result = test::addCard($db, $startSerial, $endSerial, $newAccountId);
}
}
elseif($serialInfoEndSerialSuffix>$endSerialSuffix)
{
//echo '9';echo '<br />';
// 3,4
// update end serial to 2
// add a record with accountId ,serial from 5 to 5
// add a record with newAccountId ,serial from 3 to 4
$result = test::updateSerialById($db,$serialId,'','',$prefixe.sprintf($num,($startSerialSuffix-1)));
if($result==true)
{
//echo '10';echo '<br />';
$result = test::addCard($db, $prefixe.sprintf($num,($endSerialSuffix+1)), $serialInfo['serialNumEnd'], $accountId);
if($result==true)
{
//echo '11';echo '<br />';
$result = test::addCard($db, $startSerial, $endSerial, $newAccountId);
}
}
}
}
}
return $result;
}
class test
{
static public function checkSerialMatch($startSerial,$endSerial)
{
$result = false;
$checkStartSerialRight = self::checkSerialFormat($startSerial);
$checkendSerialRight = self::checkSerialFormat($endSerial);
if($checkStartSerialRight==true&&$checkendSerialRight==true)
{
//echo '30';
$startSerialPrefix = self::getSerialPrefix($startSerial);
$endSerialPrefix = self::getSerialPrefix($endSerial);
if($startSerialPrefix==$endSerialPrefix)
{
$startSerial = preg_replace('/^'.$startSerialPrefix.'/','',$startSerial);
$endSerial = preg_replace('/^'.$endSerialPrefix.'/','',$endSerial);
$startSerialNum = preg_replace('/^0*/','',$startSerial);
$endSerialNum = preg_replace('/^0*/','',$endSerial);
if($startSerialNum<=$endSerialNum)
{
$result = true;
}
}
}
return $result;
}
static public function checkSerialFormat($serial)
{
if(strlen($serial)!=16)
{
//echo '310';
return false;
}
// $serialPrefix = substr($serial,0,7);
// $serialPrefixArr= explode('|', SERIAL_PREFIX);
// if(!in_array($serialPrefix, $serialPrefixArr))
// {
// //echo '311';
// return false;
// }
$serialNum = substr($serial,-9);
if(!is_numeric($serialNum))
{
//echo '312';
return false;
}
return true;
}
static public function getSerialPrefix($serial)
{
if(strlen($serial)!=16)
{
return false;
}
$serialPrefix = substr($serial,0,7);
return $serialPrefix;
}
static public function getSerialSuffix($serial,$removeZeroFlag=true)
{
$return = false;
if(strlen($serial)==16)
{
$serialSuffix = substr($serial,-9);
if($removeZeroFlag=true)
{
$serialSuffix = preg_replace('/^0*/', '', $serialSuffix);
}
$return = $serialSuffix;
}
return $return;
}
}