Commit 25e3b8d0 authored by liuyuzhen's avatar liuyuzhen

下单增加商品销量

parent a493902e
......@@ -580,7 +580,7 @@ class OrderConfirmUtil {
*
* @param $storeCarts
*/
public function updateGoodsStorage($storeCartsList){
public function updateGoodsStorage($storeCartsList,$op = \Our\ApiConst::minus,$updateSaleNumFlag= true ){
$goodsCommonIds = array();
$goodsIds = array();
$blIds = array();
......@@ -626,12 +626,23 @@ class OrderConfirmUtil {
}
}
}
$this->updateGoodsStorageToDBAndCache($goodsCommonIds,$goodsIds,$blIds,$goodsNewList);
$this->updateGoodsStorageToDBAndCache($goodsCommonIds,$goodsIds,$blIds,$goodsNewList,$op,$updateSaleNumFlag);
//$goodsStorages = $goodsDao->getOnlineStorageGoodsByGoodsCommonIds(array_keys($goodsCommonIds));
return true;
}
public function updateGoodsStorageToDBAndCache($goodsCommons,$goodsList,$blGoodsList,$goodsNewList){
/**
* @param $goodsCommons
* @param $goodsList
* @param $blGoodsList
* @param $goodsNewList
* @param int $op
* @param bool $updateSaleNumFlag
* @return bool
* @throws \Error\OurExceptionModel
* @throws \Exception
*/
public function updateGoodsStorageToDBAndCache($goodsCommons,$goodsList,$blGoodsList,$goodsNewList,$op=\Our\ApiConst::minus,$updateSaleNumFlag=true){
if($blGoodsList){
$dbName = \Our\DbNameConst::masterDBConnectName;
$pBundlingDao = \DAO\PBundlingModel::getInstance($dbName);
......@@ -644,8 +655,8 @@ class OrderConfirmUtil {
$dbName = \Our\DbNameConst::masterDBConnectName;
$goodsDao = \DAO\GoodsModel::getInstance($dbName);
$goodsCommonDao = \DAO\GoodsCommonModel::getInstance($dbName);
$resultGoodsCommon = $goodsCommonDao->updateGoodsStorage($goodsCommons);
$resultGoods = $goodsDao->updateGoodsStorage($goodsNewList);
$resultGoodsCommon = $goodsCommonDao->updateGoodsStorage($goodsCommons,$op,$updateSaleNumFlag);
$resultGoods = $goodsDao->updateGoodsStorage($goodsNewList,$op);
if(!($resultGoodsCommon&&$resultGoods)){
\Error\ErrorModel::throwException(\Error\CodeConfigModel::goodsStorageToDBFailedForOrder);
}
......@@ -659,7 +670,7 @@ class OrderConfirmUtil {
}
}
if($goodsCommons&&$goodsList){
$resultGoodsCache = $goodsDao->opOnlineStorageGoodsByGoodsCommonIds($goodsList);
$resultGoodsCache = $goodsDao->opOnlineStorageGoodsByGoodsCommonIds($goodsList,$op,$updateSaleNumFlag);
if(!$resultGoodsCache){
\Error\ErrorModel::throwException(\Error\CodeConfigModel::goodsStorageToCacheFailedForOrder);
}
......
......@@ -145,4 +145,10 @@ class ArrayConst
const messageOrderButtonsType=array(
0,1,2
);
const ops = array(
\Our\ApiConst::minus => '-',
\Our\ApiConst::plus => '+',
\Our\ApiConst::set => '='
);
}
\ No newline at end of file
......@@ -125,6 +125,8 @@ class NameConst {
const onlineStorageGoodsPrefix = 'onlineStorageGoods_';
const onlineGoodsCommonSaleNumPrefix = 'onlineGoodsCommonSaleNum';
const onlineStorageBlGoodsPrefix = 'onlineStorageBlGoods';//组合销售库存前缀
const storeCoverPrefix = 'storeCover_';
......
......@@ -25,6 +25,8 @@ class GoodsCommonServiceModel extends \Business\AbstractModel
}
$commonGoodsInstance = \DAO\GoodsCommonModel::getInstance();
$commonInfo = \Our\RedisHelper::cachedFunction(\Redis\Db4\GoodsCommonRedisModel::getInstance(),array(&$commonGoodsInstance, 'getOneById'),array($goodsCommonId),0,array($goodsCommonId));
$saleNum = $commonGoodsInstance->getGoodsSaleNumFromCache($goodsCommonId);
$commonInfo['sale_num'] = $saleNum;
if(!$commonInfo){
\Error\ErrorModel::throwException(\Error\CodeConfigModel::goodsNotExist);
}
......
......@@ -140,16 +140,21 @@ class GoodsModel extends \DAO\AbstractModel {
}
/**
* 更新商品缓存
* 更新商品库存缓存以及更新销量
* @param $goodsList 商品列表
* @param int $op 删减标识符(1:增加,0:减少,2:直接设置)
* @return bool
*/
public function opOnlineStorageGoodsByGoodsCommonIds($goodsList,$op = \Our\ApiConst::minus){
public function opOnlineStorageGoodsByGoodsCommonIds($goodsList,$op = \Our\ApiConst::minus,$updateSaleNumFalg = true){
$goodsRedis = \Redis\Db4\GoodsRedisModel::getInstance();
if($updateSaleNumFalg){
$goodsCommonRedis = \Redis\Db4\GoodsCommonRedisModel::getInstance();
$onlineGoodsCommonSaleNumKey = \Our\NameConst::onlineGoodsCommonSaleNumPrefix;
}
$goodsCommonList = $this->getOnlineStorageGoodsByGoodsCommonIds(array_keys($goodsList));
foreach($goodsCommonList as $goodsCommonId=>$goodsCommonsTemp){
$newGoodsList = array();
$totalGoodsNum = \Our\ApiConst::zero;
foreach($goodsCommonsTemp as $goodsId=>$temp){
if($goodsList[$goodsCommonId][$goodsId]&&$goodsList[$goodsCommonId][$goodsId]['goodsNum']>\Our\ApiConst::zero){
if($op == \Our\ApiConst::minus&&$temp['goodsStorage']>=$goodsList[$goodsCommonId][$goodsId]['goodsNum']){
......@@ -161,6 +166,7 @@ class GoodsModel extends \DAO\AbstractModel {
}else{
return false;
}
$totalGoodsNum += $goodsList[$goodsCommonId][$goodsId]['goodsNum'];
}
$newGoodsList[$goodsId]= serialize($temp);
}
......@@ -170,6 +176,19 @@ class GoodsModel extends \DAO\AbstractModel {
if(!$result){
return false;
}
}
/**
* 需要更新销量时 ,如果已经存在销量对应缓存,对销量对应缓存进行加减操作
*/
if($updateSaleNumFalg&&$op!=\Our\ApiConst::set&&$totalGoodsNum>\Our\ApiConst::zero){
$updateGoodsNum = $totalGoodsNum;
if($op==\Our\ApiConst::plus){
$updateGoodsNum = -$updateGoodsNum;
}
if($goodsCommonRedis->tableHExists($onlineGoodsCommonSaleNumKey,$goodsCommonId)){
$goodsCommonRedis->tableHIncrBy($onlineGoodsCommonSaleNumKey,$goodsCommonId,$updateGoodsNum);
}
}
}
return true;
......@@ -286,7 +305,7 @@ class GoodsModel extends \DAO\AbstractModel {
}
public function updateGoodsStorage($dataList){
public function updateGoodsStorage($dataList,$op=\Our\ApiConst::zero){
$datas = array_values($dataList);
$this->setDb(\Our\DbNameConst::masterDBConnectName);
$error = 0;
......@@ -294,7 +313,7 @@ class GoodsModel extends \DAO\AbstractModel {
$updateAllSql = $baseSql;
$dataIds = array();
for ($i = 0; $i < count($datas); $i++) {
$updateSql = \Our\Common::format(" WHEN {0} THEN goods_storage-{1} ", $datas[$i]['goodsId'], $datas[$i]['goodsNum']);
$updateSql = \Our\Common::format(" WHEN {0} THEN goods_storage{2}{1} ", $datas[$i]['goodsId'], $datas[$i]['goodsNum'],\Our\ArrayConst::ops[$op]);
$dataIds[] = $datas[$i]['goodsId'];
$updateAllSql .= $updateSql;
if ($i % 1000 == 0 && $i != 0) {
......
......@@ -25,9 +25,32 @@ class GoodsCommonModel extends \DAO\AbstractModel {
public function init(){
}
public function getOneById($goodsCommonId){
$this->setDb();
return $this->db->select('*')->from($this->_tableName)->where(array('goods_commonid'=>$goodsCommonId))->fetchOne();
public function getOneById($goodsCommonId,$field = \Our\NameConst::allField){
$this->setDb($this->dbName);
return $this->db->select($field)->from($this->_tableName)->where(array('goods_commonid'=>$goodsCommonId))->fetchOne();
}
public function getGoodsSaleNumFromCache($goodsCommonId){
$goodsCommonRedis = \Redis\Db4\GoodsCommonRedisModel::getInstance();
$onlineGoodsCommonSaleNumKey = \Our\NameConst::onlineGoodsCommonSaleNumPrefix;
if($goodsCommonRedis->tableHExists($onlineGoodsCommonSaleNumKey,$goodsCommonId)){
$saleNum = $goodsCommonRedis->tableHGet($onlineGoodsCommonSaleNumKey,$goodsCommonId);
}else{
$goodsCommon = $this->getOneById($goodsCommonId,'sale_num');
$saleNum = $goodsCommon['sale_num'];
$goodsCommonRedis->tableHIncrBy($onlineGoodsCommonSaleNumKey,$goodsCommonId,$saleNum);
}
return $saleNum;
}
/**
* 删除销量缓存
* @param $goodsCommonId
*/
public function deleteGoodsSaleNumFromCache($goodsCommonId){
$goodsCommonRedis = \Redis\Db4\GoodsCommonRedisModel::getInstance();
$onlineGoodsCommonSaleNumKey = \Our\NameConst::onlineGoodsCommonSaleNumPrefix;
$goodsCommonRedis->tableHDel($onlineGoodsCommonSaleNumKey,$goodsCommonId);
}
/**
......@@ -39,7 +62,7 @@ class GoodsCommonModel extends \DAO\AbstractModel {
* @return mixed
*/
public function getGoodsList($where,$field,$limit=array(),$order=array(),$attrStr=''){
$this->setDb();
$this->setDb($this->dbName);
$str = '$data = $this->db->select($field)->from($this->_tableName)';
if($attrStr){
$str .="->join('han_goods_attribute_value','han_goods_attribute_value.goods_commonid = han_goods_common.goods_commonid')";
......@@ -71,7 +94,7 @@ class GoodsCommonModel extends \DAO\AbstractModel {
* @return mixed
*/
public function getListWithSale($where,$field,$actIds,$limit=array(),$order=array(),$attrStr='',$online = true){
$this->setDb();
$this->setDb($this->dbName);
$str = "select ".$field." FROM ".$this->_tableName." LEFT JOIN (SELECT MIN(discount_price) discount_price,goods_commonid,goods_id FROM han_sale_goods where sale_act_id in(".implode(',',$actIds).") GROUP BY goods_commonid) a on (han_goods_common.goods_commonid = a.goods_commonid)";
if($attrStr){
$str .= "INNER JOIN han_goods_attribute_value ON han_goods_attribute_value.goods_commonid = han_goods_common.goods_commonid ";
......@@ -147,7 +170,7 @@ class GoodsCommonModel extends \DAO\AbstractModel {
}
public function getCount($where,$attrStr=''){
$this->setDb();
$this->setDb($this->dbName);
if($attrStr){
return $this->db->from($this->_tableName)->join('han_goods_attribute_value','han_goods_attribute_value.goods_commonid = han_goods_common.goods_commonid')->where($where)->where($attrStr)->fetchNum();
}
......@@ -181,19 +204,25 @@ class GoodsCommonModel extends \DAO\AbstractModel {
}
public function updateGoodsStorage($dataList){
public function updateGoodsStorage($dataList , $op = \Our\ApiConst::minus,$updateSaleNumFlag = false){
$datas = array_values($dataList);
$this->setDb(\Our\DbNameConst::masterDBConnectName);
$error = 0;
$baseSql = "UPDATE {$this->_tableName} set goods_storage = case goods_commonid ";
$updateAllSql = $baseSql;
$dataIds = array();
if($updateSaleNumFlag&&$op!=\Our\ApiConst::set){
$updateSaleNumOp = $op==\Our\ApiConst::minus?'+':'-';
}
for ($i = 0; $i < count($datas); $i++) {
$updateSql = \Our\Common::format(" WHEN {0} THEN goods_storage-{1} ", $datas[$i]['goodsCommonId'], $datas[$i]['goodsNum']);
$updateSql = \Our\Common::format(" WHEN {0} THEN goods_storage{1}{2} ", $datas[$i]['goodsCommonId'],\Our\ArrayConst::ops[$op], $datas[$i]['goodsNum']);
if($updateSaleNumFlag){
$updateSql = \Our\Common::format(" WHEN {0} THEN goods_storage{1}{2} END,sale_num = case goods_commonid when {0} THEN sale_num {3}{2} END", $datas[$i]['goodsCommonId'],\Our\ArrayConst::ops[$op], $datas[$i]['goodsNum'],$updateSaleNumOp);
}
$dataIds[] = $datas[$i]['goodsCommonId'];
$updateAllSql .= $updateSql;
$updateAllSql .= $updateSql;;
if ($i % 1000 == 0 && $i != 0) {
$updateAllSql .= \Our\Common::format('END where goods_commonid in ({0})',implode(',',$dataIds));
$updateAllSql .= \Our\Common::format(' where goods_commonid in ({0})',implode(',',$dataIds));
$one = $this->db->update($this->_tableName)->query($updateAllSql);
if (!$one) {
$error++;
......@@ -203,7 +232,7 @@ class GoodsCommonModel extends \DAO\AbstractModel {
}
}
if ($updateAllSql != $baseSql) {
$updateAllSql .= \Our\Common::format('END where goods_commonid in ({0})',implode(',',$dataIds));
$updateAllSql .= \Our\Common::format(' where goods_commonid in ({0})',implode(',',$dataIds));
$one = $this->db->update($this->_tableName)->query($updateAllSql);
if (!$one) {
......@@ -228,9 +257,9 @@ class GoodsCommonModel extends \DAO\AbstractModel {
*
* @return \DAO\UserModel
*/
public static function getInstance() {
public static function getInstance($dbName =\Our\DbNameConst::salveDBConnectName) {
if (!(self::$_instance instanceof self)) {
self::$_instance = new self();
self::$_instance = new self($dbName);
}
return self::$_instance;
......
......@@ -99,4 +99,14 @@ class AbstractModel extends \Redis\AbstractModel {
public function tableSRandMember($h,$count = null){
return $this->sRandMember($this->calcKey($h),$count);
}
public function tableHIncrBy($h,$key,$keysvalue){
return $this->hIncrBy($this->calcKey($h),$key,$keysvalue);
}
public function tableHExists($h,$key){
return $this->hExists($this->calcKey($h),$key);
}
public function tableHDel($h,$key){
return $this->hDel($this->calcKey($h),$key);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment