| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309 |
- <?php
- namespace common\services;
- use common\models\xhGoods;
- use Yii;
- use common\models\xhGoodsPrice;
- use common\components\dict;
- use common\services\xhGoodsSettingService;
- class xhGoodsPriceService {
- public static function add($data, $multi=false)
- {
- if(!$multi){
- $goodsPrice = xhGoodsPrice::add($data);
- self::refreshById($goodsPrice['id']);
- return $goodsPrice;
- }else{
- $goodsPriceIds = '';
- foreach ($data as $item){
- $goodsPrice = xhGoodsPrice::add($item);
- self::refreshById($goodsPrice['id']);
- $goodsPriceIds = $goodsPriceIds . ',' . $goodsPrice['id'];
- }
- $goodsPriceIds = ltrim($goodsPriceIds, ',');
- return $goodsPriceIds;
- }
- }
- public static function updateById($id, $data){
- xhGoodsPrice::updateById($id, $data);
- self::refreshById($id);
- }
- /**
- * 重用软删除的表数据
- * @param $data 商品价格数据(可多个)
- * @param int $num 当商品价格数据为多个时(即二维数组),$num = count($data);
- */
- public static function reuse($data, $num = 1){
- $count = xhGoodsPrice::getCount(['softDel' => 1]);
- if($count >= $num){
- if($num == 1){
- if(count($data) != count($data,1)){
- $data = $data[0];
- }
- $dbData = xhGoodsPrice::getByCondition(['softDel' => 1]);
- $data['softDel'] = 0;//此字段也要更新,勿忘
- $re = xhGoodsPrice::updateById($dbData['id'], $data);
- if(!$re){
- Yii::warning('xhGoodsPriceService reuseSoftDel fail!');
- return false;
- }
- self::refreshById($dbData['id']);
- return $dbData['id'];
- }else{
- $idStr = '';
- foreach($data as $item){
- $item['softDel'] = 0;//此字段也要更新,勿忘
- $dbData = xhGoodsPrice::getByCondition(['softDel' => 1]);
- $re = xhGoodsPrice::updateById($dbData['id'], $item);
- if(!$re){
- Yii::warning('xhGoodsPriceService reuseSoftDel fail:2!');
- return false;
- }
- self::refreshById($dbData['id']);
- $idStr .= $dbData['id'].',';
- }
- return rtrim($idStr, ',');
- }
- }else{
- if($num == 1){
- if(count($data) != count($data,1)){
- $data = $data[0];
- }
- $re = self::add($data);
- if(!$re){
- throw new \Exception('xhGoodsPrice 保存数据失败');
- }
- return $re['id'];
- }else{
- return self::add($data, true);
- }
- }
- }
- public static function goodsMultiPriceAdd($goodTitle, $multiPrice, $multiImg){
- $data = [];
- foreach($goodTitle as $key => $title){
- $data[$key] = ['title' => $title, 'picture' => $multiImg[$key], 'price' => $multiPrice[$key], 'order' => 0];
- }
- //$idStr = self::add($data, true);
- $idStr = self::reuse($data, count($data));
- return $idStr;
- }
- public static function goodsMultiPriceUpdate($ids, $goodTitle, $multiPrice, $multiImg){
- foreach($goodTitle as $key => $title){
- $data = ['title' => $title, 'picture' => $multiImg[$key], 'price' => $multiPrice[$key], 'order' => 0];
- $id = $ids[$key];
- self::updateById($id, $data);
- }
- }
- /**
- * 更新商品的多种价格字段,并返回新ids字符串
- * @param $beforeIdArr
- * @param $post
- * @return bool|string
- */
- public static function updateMultiPrice($beforeIdArr, $post){
- $nowIdArr = $post['priceId'];
- $goodTitle = $post['goodTitle'];
- $multiPrice = $post['multiPrice'];
- $multiImg = $post['multiImg'];
- if(empty($beforeIdArr)){//未设置多价格
- $idStr = self::goodsMultiPriceAdd($goodTitle, $multiPrice, $multiImg);
- return $idStr;
- }else{
- $stillInIdArr = [];
- $notInIdArr = [];
- $newIdStr = '';
- $oldIdStr = '';
- $idStr = '';
- foreach($beforeIdArr as $key => $id){
- if(in_array($id, $nowIdArr)){
- $stillInIdArr[] = $id;
- }else{
- $notInIdArr[] = $id;
- }
- }
- //清未加入的Ids
- if(!empty($notInIdArr)){
- $notInIdStr = implode(',', $notInIdArr);
- self::softDeleteIds($notInIdStr);
- }
- $count = count($nowIdArr);
- $newGoodTitle = array_slice($goodTitle, $count);
- $newMultiPrice = array_slice($multiPrice, $count);
- if(!empty($newGoodTitle) && !empty($newMultiPrice)){
- $newIdStr = self::goodsMultiPriceAdd($newGoodTitle, $newMultiPrice, $multiImg);
- }
- if(!empty($stillInIdArr)){
- //更新
- $oldGoodTitle = array_slice($goodTitle, 0, $count);
- $oldMultiPrice = array_slice($multiPrice, 0, $count);
- if(!empty($oldGoodTitle) && !empty($oldMultiPrice)){
- $oldIdStr = self::goodsMultiPriceUpdate($nowIdArr, $oldGoodTitle, $oldMultiPrice, $multiImg);
- }
- $idStr = implode(',', $stillInIdArr) . ($newIdStr == '' ? '' : ','.$newIdStr);
- }
- return (string)$idStr;
- }
- }
- /**
- * @param $id
- * @param int $goodsId 商品ID (当xhGoodsSettingService::changePrice($goods, $goodPrice['price'])不好取商品详情$goods时,传入商品ID)
- * @return array|null|\yii\db\ActiveRecord
- */
- public static function getById($id, $goodsId = 0)
- {
- $preKey = dict::getCacheKey('xhGoodsPrice');
- $cacheKey = $preKey.$id;
- $cacheData = Yii::$app->redis->executeCommand('HGETALL', [$cacheKey]);
- if(!empty($cacheData)){
- $arr = [];
- $i = 0;
- $x = 0;
- foreach($cacheData as $key => $val){
- $i++;
- if($i%2 == 0){
- $arr[$x]['value'] = $val;
- $x++;
- }else{
- $arr[$x]['key'] = $val;
- }
- }
- $goodsPriceArr = [];
- foreach($arr as $key => $val){
- $goodsPriceArr[$val['key']] = $val['value'];
- }
- unset($goodsPriceArr['info_already_get_by_sql']);
- //设置涨价
- if($goodsId > 0){
- $goods = xhGoodsService::getById($goodsId);
- $goodsPriceArr['price'] = xhGoodsSettingService::changePrice($goods, $goodsPriceArr['price']);//统一对商品进行价格等方面设置
- }
- return $goodsPriceArr;
- }
- $goodsPrice = xhGoodsPrice::find()->where(['id' => $id])->asArray()->one();
- $params = [$cacheKey];
- $params[] = 'info_already_get_by_sql';
- $params[] = 'ok';//redis没有办法设置带空值的键,加此可以数据空时表示取过
- if(!empty($goodsPrice)){
- foreach($goodsPrice as $key => $val){
- $params[] = $key;
- $params[] = $val;
- }
- }
- Yii::$app->redis->executeCommand('HMSET',$params);//将xhGoods信息放入缓存
- //设置涨价
- if($goodsId > 0) {
- $goods = xhGoodsService::getById($goodsId);
- $goodsPrice['price'] = xhGoodsSettingService::changePrice($goods, $goodsPrice['price']);//统一对商品进行价格等方面设置
- }
- return $goodsPrice;
- }
- /**
- * 传入id号(数组 或 字符串(例:1,2,3))
- * @param $ids
- * @return array
- */
- public static function getByIds($ids, $goodsId = 0){
- $data = [];
- if(is_string($ids)){
- $ids = explode(',', $ids);
- }
- if(!is_array($ids)){
- throw new \Exception('传入的参数不是数组');
- }
- foreach ($ids as $id){
- if(empty($id)) continue;
- $data[] = self::getById($id, $goodsId);
- }
- return $data;
- }
- public static function deleteById($id)
- {
- xhGoodsPrice::deleteById($id);
- $cacheKey = dict::getCacheKey('xhGoodsPrice').$id;
- Yii::$app->redis->executeCommand('DEL', [$cacheKey]);
- }
- public static function refreshById($id)
- {
- $cacheKey = dict::getCacheKey('xhGoodsPrice').$id;
- Yii::$app->redis->executeCommand('DEL', [$cacheKey]);
- self::getById($id);
- }
- /**
- * 软删除,并清缓存(表字段:softDel标记为1)
- * @param $id ID
- */
- public static function softDelete($id){
- self::updateById($id, ['softDel' => 1]);
- }
- /**
- * 软删除多个,并清缓存(表字段:softDel标记为1)
- * @param $id ID
- */
- public static function softDeleteIds($ids){
- if(is_string($ids)){
- $ids = explode(',', $ids);
- }
- if(!is_array($ids)){
- throw new \Exception('传入的参数不是数组');
- }
- foreach ($ids as $id){
- self::updateById($id, ['softDel' => 1]);
- }
- }
- /**
- * 通过id清价格缓存
- * @param $id
- */
- /*public static function cleanById($id){
- $cacheKey = dict::getCacheKey('xhGoodsPrice').$id;
- Yii::$app->redis->executeCommand('DEL', [$cacheKey]);
- }*/
- /**
- * 通过多个id 清多个价格缓存
- * @param $ids
- * @throws \Exception
- */
- /*public static function cleanByIds($ids){
- if(is_string($ids)){
- $ids = explode(',', $ids);
- }
- if(!is_array($ids)){
- throw new \Exception('传入的参数不是数组');
- }
- foreach ($ids as $id){
- $cacheKey = dict::getCacheKey('xhGoodsPrice').$id;
- Yii::$app->redis->executeCommand('DEL', [$cacheKey]);
- }
- }*/
- }
|