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]); } }*/ }