Commit dc27d38c by 杨树贤

Merge branch 'dev/ver/1.0.0' of http://git.ichunt.net/semour/semour_web into dev/ver/1.0.0

parents ecd19adc f218d094
......@@ -44,6 +44,7 @@ class CartApiController extends Controller
//购物车列表
public function cartLists(Request $request){
$result = CartService::cartLists($request->user->id);
return $this->setSuccessData($result);
}
......
......@@ -14,6 +14,9 @@ use App\Models\CartModel;
//购物车服务器层
class CartService
{
static $day_filter = ["工作日","个工作日","日"];
static $week_filter = ["周"];
//添加或者更新购物车
public static function saveCart($data, $user_id)
{
......@@ -22,17 +25,18 @@ class CartService
$con = DB::connection();
$con->beginTransaction();
$items = json_decode(\Arr::get($data, 'items', []),true);
$vs = json_decode(\Arr::get($data, 'items', []),true);
$redis = Redis::connection();
$goodsInfoArr = ThirdService::getGoodsInfo(array_column($items,"goods_id"));
$goodsInfoArr = ThirdService::getGoodsInfo(array_column($vs,"goods_id"));
foreach ($vs as $v) {
foreach ($items as $item) {
$goods_id = $item['goods_id'];
$goods_id = $v['goods_id'];
$temp = [
'user_id' => $user_id,
'goods_id' => $item['goods_id'],
'goods_id' => $v['goods_id'],
];
$skuInfo = $goodsInfoArr[$goods_id]; //sku库存
......@@ -44,17 +48,33 @@ class CartService
$temp["raw_brand_name"]= $digikeyArr["raw_brand_name"];
}
//购买时价格
$buy_price = 0;
$ladder_price = data_get($skuInfo,"ladder_price");
if ($ladder_price){ //购买价格
foreach ($ladder_price as $a=>$b){
if ($v["buy_number"] >= $b["purchases"]){
$buy_price = $b["price_us"];
}else{
break;
}
}
}
$temp["buy_price"] = $buy_price;
$checkHas = CartModel::where(["user_id"=>$user_id,"goods_id"=>$goods_id,"status"=>1])->first();
if ($checkHas){ //存在累计库存
$temp["buy_number"] = $skuInfo["stock"] > ($item['buy_number']+$checkHas["buy_number"]) ? $item['buy_number']+$checkHas["buy_number"] : $skuInfo["stock"];
$temp["buy_number"] = $skuInfo["stock"] > ($v['buy_number']+$checkHas["buy_number"]) ? $v['buy_number']+$checkHas["buy_number"] : $skuInfo["stock"];
$temp["update_time"] = time();
$temp["status"] = $v["buy_number"] > 0 ? CartModel::STATUS_YES:CartModel::STATUS_NO;
$flag = CartModel::where(["cart_id"=>$checkHas["cart_id"]])->update($temp);
if (!$flag){
return false;
}
}else{ //不存在插入购物车
$temp["buy_number"] = $skuInfo["stock"] > $item['buy_number'] ? $item['buy_number'] : $skuInfo["stock"];
$temp["buy_number"] = $skuInfo["stock"] > $v['buy_number'] ? $v['buy_number'] : $skuInfo["stock"];
$temp["status"] = $v["buy_number"] > 0 ? CartModel::STATUS_YES:CartModel::STATUS_NO;
$temp["create_time"] = time();
$temp["update_time"] = time();
......@@ -93,12 +113,12 @@ class CartService
$goodsInfoArr = ThirdService::getGoodsInfo(array_column($result,"goods_id"));
$cartList = [];
foreach ($result as $item) {
$goods_id = $item['goods_id'];
foreach ($result as $v) {
$goods_id = $v['goods_id'];
$temp = [
'user_id' => $user_id,
'goods_id' => $item['goods_id'],
'goods_id' => $v['goods_id'],
];
$skuInfo = $goodsInfoArr[$goods_id]; //sku库存
......@@ -110,25 +130,141 @@ class CartService
$temp["raw_brand_name"]= $digikeyArr["raw_brand_name"];
}
$temp["buy_number"] = $skuInfo["stock"] > $item['buy_number'] ? $item['buy_number'] : $skuInfo["stock"];
if ($temp["buy_number"] == 0){
$temp["status"] = CartModel::STATUS_NO ;
$temp["buy_number"] = $skuInfo["stock"] > $v['buy_number'] ? $v['buy_number'] : $skuInfo["stock"]; //购买数量
$buy_price = 0;
$ladder_price = \Arr::get($skuInfo,"ladder_price");
if ($ladder_price){ //购买价格
foreach ($ladder_price as $a=>$b){
if ($temp["buy_number"] >= $b["purchases"]){
$buy_price = $b["price_us"];
}else{
break;
}
}
}
$temp["status"] = $temp["buy_number"] > 0 ? CartModel::STATUS_YES:CartModel::STATUS_NO;
$temp["buy_price"] = $buy_price;
$temp["update_time"] = time();
$flag = CartModel::where(["cart_id"=>$item["cart_id"]])->update($temp);
$temp["update_time"] = time();
$flag = CartModel::where(["cart_id"=>$v["cart_id"]])->update($temp);
if (!$flag){
return false;
continue;
}
//格式化数据
$skuInfo = self::dullDataInfo($skuInfo);
$cartList[] = [
"goods_id"=>$skuInfo["goods_id"], //sku_id
"goods_name"=>$skuInfo["goods_name"], //型号名称
"supplier_id"=>$skuInfo["supplier_id"], //供应商id
"supplier_name"=>$skuInfo["supplier_name"], //供应商id
"buy_number"=>$temp["buy_number"], //购买数量
"encap"=>$skuInfo["encap"], //标准包装数
"pdf"=>$skuInfo["pdf"], //型号pdf
"brand_id"=>$skuInfo["brand_id"], //品牌id
"brand_name"=>$skuInfo["brand_name"], //品牌名称
"stock"=>$skuInfo["stock"], //库存
"stock_format"=>numberToHtml($skuInfo["stock"]), //库存加密
"max_buy_stock"=>$skuInfo["max_buy_stock"], //最大可购买数量
"moq"=>numberToHtml($skuInfo["moq"]), //最少起订量
"mpq"=>numberToHtml($skuInfo["mpq"]), //标准包装量
"mult"=>numberToHtml($skuInfo["multiple"]), //递增量
"hk_delivery_time"=>$skuInfo["hk_delivery_time"], //香港交期
"ladder_price"=>$skuInfo["ladder_price"], //价格阶梯
"is_buy"=>$skuInfo["is_buy"], //是否能购买
];
}
$con->commit();
return true;
return $cartList;
}catch (\Exception $e){
$con->rollback();
throw new InvalidRequestException($e->getMessage().$e->getLine());
return false;
}
}
/**
*格式化商品服务数据
*/
public static function dullDataInfo($info,$keyword = ""){
$redis = Redis::connection();
$info['pdf'] = empty($info['pdf']) ? '':$info['pdf'];
$info['encap'] = empty($info['encap']) ? '':$info['encap'];
$info['spu_brief'] = empty($info['spu_brief']) ? '':$info['spu_brief'];
$info['mpq'] = $info['mpq']>0? $info['mpq'] : 1;
$info['goods_name'] = empty($info['goods_name'])? $info['spu_name']:$info['goods_name'];
if(!empty ($keyword)){
$info['goods_name_temp'] = str_ireplace($keyword, "<b class='f-red'>".strtoupper($keyword)."</b>",$info['goods_name']);
}else{
$info['goods_name_temp'] = $info['goods_name'];
}
$info['multiple'] = $info['mpq']>$info['moq'] ? $info['moq'] : $info['mpq'];//那个小用那个
//todo 2022.9.27 计算最大购买数量
$info["max_buy_stock"] = getMaxBuyStock($info["stock"],$info["multiple"],$info["ladder_price"]);
$info['page_flag'] = 2;
//猎芯联营采购员 ,添加供应商采购员渠道不得为空
$info['purchase_name'] = '';
if ($info['canal'] != ""){
$pur_name = $redis->hget('search_supplier_canaltopurchase',$info['canal']);
$info['purchase_name'] = $pur_name;
}
//货区去掉工作日
$info['hk_delivery_time_origin'] = $info['hk_delivery_time'];
$info['cn_delivery_time'] = str_replace("工作日","",$info['cn_delivery_time']);
$info['cn_delivery_time'] = str_replace("个","",$info['cn_delivery_time']);
$info['hk_delivery_time'] = str_replace("工作日","",$info['hk_delivery_time']);
$info['hk_delivery_time'] = str_replace("个","",$info['hk_delivery_time']);
#查询英文品牌缩写
$standard_brand_id = $info["standard_brand"]["standard_brand_id"];
$standardBrandInfo = $redis->hget("standard_brand",$standard_brand_id);
if ($standardBrandInfo){
$standardBrandInfoArr = json_decode($standardBrandInfo,true);
#深贸商城的品牌字段需展示该SKU对应基石标准品牌列表的英文简称,无则取英文名称、都无则取品牌名称
$brandName = $info["brand_name"];
if ($standardBrandInfoArr["brand_short_name_en"]){
$brandName = $standardBrandInfoArr["brand_short_name_en"];
}else if ($standardBrandInfoArr["brand_name_en"]){
$brandName = $standardBrandInfoArr["brand_name_en"];
}
$info["brand_name"] = $brandName;
}
$original_price = $info["original_price"];
#梯度价转换
foreach ($original_price as $e=>&$f){
$f["purchases_format"] = numberToHtml($f["purchases"]);
}
usort($original_price, function ($current, $next) { //原始价格倒叙
return $current['purchases'] < $next['purchases'];
});
$info["ladder_price"] = $original_price;
#工作日转换
foreach (self::$day_filter as $d){
if (strpos($info["hk_delivery_time_origin"],$d) !== false){
$info["hk_delivery_time"] = $info["hk_delivery_time"]." workdays";
break;
}
}
#周转换
foreach (self::$week_filter as $d){
if (strpos($info["hk_delivery_time_origin"],$d) !== false){
$info["hk_delivery_time"] = $info["hk_delivery_time"]." weeks";
break;
}
}
return $info;
}
......
......@@ -61,3 +61,40 @@ function numberToHtml($number){
return $number;
}
}
/**
* 生成纯小写字母的字符串
*
*/
function strRandom($class='',$len_num=3){
$randstr = 'asdwpkxmiqplmzacbmeruwulurjlauejrifkfghjklzxcvbnmqwwertyuiopkdsieurnvewjeilweiskvnx';
$len = strlen($randstr)-9;
$res = '';
for($i=0;$i<$len_num;$i++){
$start = rand(0,$len);
$str = substr($randstr, $start,9);
$res .= ' '.$str;
}
return $res;
}
/*
* 计算最大购买数量
*/
function getMaxBuyStock($stock,$multiple,$ladder_price){
//todo 2022.9.27 计算最大购买数量
$max_buy_stock = 0;
if ($stock > 0 && $multiple >0){
$max_buy_stock1 = intval($stock/$multiple)*$multiple; //库存最大购买数量
if ($ladder_price){ //梯度价库存最大购买数量
$max_pur_num = @$ladder_price[count($ladder_price)-1]["purchases"];
$max_buy_stock2 = intval($max_pur_num/$multiple)*$multiple; //库存最大购买数量
$max_buy_stock = $max_buy_stock1 > $max_buy_stock2 ? $max_buy_stock2:$max_buy_stock1;
}else{
$max_buy_stock = $max_buy_stock1;
}
}
return $max_buy_stock;
}
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 sign in to comment