getPage(); $params = $this->request->params(['cate_ids','title', 'type', 'profit', 'price']); $product_model = Db::name('douhuomall')->whereIn('status', [0, 1]); $gongProduct = $this->getJoinProduct(); if ($params['type'] == 1) { // 已加入商品管理 $product_model->whereIn('spu_id', $gongProduct); } elseif ($params['type'] == 2) { // 未加入商品管理 $product_model->whereNotIn('spu_id', $gongProduct); } if($this->request->merId()==496){ $product_model->whereIn('spu_id', $this->getJoinProduct439()); }else{ $product_model->whereNotIn('spu_id', $this->getJoinProduct439()); } // SQL条件拼装 $product_model->whereIn('status', [0, 1]) ->when(!empty($params['title']), function($query) use ($params) { $query->whereLike('title','%'.$params['title'].'%'); }) ->when(!empty($params['cate_ids']), function ($query) use ($params) { $query->where(function($que) use ($params) { $cateIds = explode(',', $params['cate_ids']); foreach ($cateIds as $key => $value) { $que->whereFindInSet('cate_ids', $value, 'OR'); } }); }) ->field('spu_id,title,cost_price,market_price,profit,spuId_info,status,ot_price,mer_profit,pension'); // 获取总数 $count=$product_model->count('spu_id'); // 按创客利润排序 if (!empty($params['profit'])) { if ($params['profit'] == 'up') { $product_model->order('mer_profit', 'asc'); }elseif ($params['profit'] == 'down') { $product_model->order('mer_profit', 'desc'); } } // 按销售价排序 if (!empty($params['price'])) { if ($params['price'] == 'up') { $product_model->order('market_price', 'asc'); }elseif ($params['price'] == 'down') { $product_model->order('market_price', 'desc'); } } if (empty($params['price']) && empty($params['profit'])) { $product_model->order('create_time', 'desc'); } // 获取分页数据 $product_lst=$product_model->page($page,$limit)->select()->toArray(); foreach ($product_lst as $k=>&$v){ //商品图片处理 $v['spuId_info']=json_decode($v['spuId_info'],true); $v['img']=$v['spuId_info']['cover_url']; if(in_array($v['spu_id'], $gongProduct)){ $v['is_join']=1; }else{ $v['is_join']=0; } unset($v['spuId_info']); } $retrun_data=[ 'count'=>$count, 'data'=>$product_lst ]; return app('json')->success($retrun_data); } // 获取已加入选品库的商品 public function getJoinProduct() { $mer_id = $this->request->merId(); $gongProduct = Db::name('store_product') ->where('mer_id', $mer_id) ->where('is_del', 0) ->whereNotNull('spu_id') ->column('spu_id'); return $gongProduct; } // 获取439已加入选品库的商品 public function getJoinProduct439() { $mer_id = $this->request->merId(); if($mer_id==439){ return []; } $gongProduct = Db::name('store_product') ->where('mer_id', 439) ->where('is_del', 0) ->whereNotNull('spu_id') ->column('spu_id'); return $gongProduct; } // 获取选品库中全部、已加入商品库、未加入商品库的数量 public function getStatusFilter() { $gongProduct = $this->getJoinProduct(); $gongProduct439=$this->getJoinProduct439(); if($this->request->merId()==496){ // 获取全部数据 $allNum = Db::name('douhuomall')->whereIn('spu_id',$gongProduct439 )->whereIn('status', [0, 1])->count(); // 获取已加入选品库的数据 $joinNum = Db::name('douhuomall')->whereIn('spu_id',$gongProduct439 )->whereIn('spu_id', $gongProduct)->whereIn('status', [0,1])->count(); // 获取未加入选品库的数据 $notJoinNum = Db::name('douhuomall')->whereIn('spu_id',$gongProduct439 )->whereNotIn('spu_id', $gongProduct)->whereIn('status', [0,1])->count(); }else{ // 获取全部数据 $allNum = Db::name('douhuomall')->whereNotIn('spu_id',$gongProduct439 )->whereIn('status', [0, 1])->count(); // 获取已加入选品库的数据 $joinNum = Db::name('douhuomall')->whereNotIn('spu_id',$gongProduct439 )->whereIn('spu_id', $gongProduct)->whereIn('status', [0,1])->count(); // 获取未加入选品库的数据 $notJoinNum = Db::name('douhuomall')->whereNotIn('spu_id',$gongProduct439 )->whereNotIn('spu_id', $gongProduct)->whereIn('status', [0,1])->count(); } $res = [ [ 'type' => 0, 'title' => '全部', 'count' => $allNum ], [ 'type' => 1, 'title' => '已加入商品管理', 'count' => $joinNum ], [ 'type' => 2, 'title' => '未加入商品管理', 'count' => $notJoinNum ] ]; return app('json')->success($res); } //商品详情 public function product_details() { $spu_id = $this->request->param('spu_id'); $product_data=Db::name('douhuomall')->where('spu_id',$spu_id)->find(); if (empty($product_data)) return app('json')->success([]); $product_data['spuId_info']=json_decode($product_data['spuId_info'],true); $product_data['skuId_info']=json_decode($product_data['skuId_info'],true); foreach ($product_data['skuId_info'] as &$skuId){ $name=''; foreach($skuId['attribute_json'] as $k=>$v){ $name.=$v['val']; } $skuId['attribute_json']=$name; } return app('json')->success($product_data); } // 加入选品 public function join_selection_library() { $data = $this->request->params(['spu_id']); $spu_id = $data['spu_id']; $mer_id=$this->request->merId(); $store_product_data=Db::name('store_product')->where('mer_id',$mer_id)->where('spu_id',$spu_id)->where('is_del', 0)->value('product_id'); if($store_product_data) return app('json')->fail('此商品已加入选品库'); $res = $this->insert_product($spu_id, $mer_id); if($res) { return app('json')->success('加入成功'); } return app('json')->fail('加入失败'); } // 批量加入选品 public function batch_join_selection_library() { $data = $this->request->params(['spu_ids']); if (empty($data['spu_ids'])) { return app('json')->fail('参数错误'); } $mer_id=$this->request->merId(); $joinNum = 0; $successJoin = 0; $failJoin = 0; $spuIds = explode(',', $data['spu_ids']); foreach ($spuIds as $spu_id) { $store_product_data=Db::name('store_product')->where('mer_id',$mer_id)->where('spu_id',$spu_id)->where('is_del', 0)->value('product_id'); if(!empty($store_product_data)) { ++$joinNum; continue; } $res = $this->insert_product($spu_id, $mer_id); $res ? ++$successJoin : ++$failJoin; } return app('json')->success($successJoin . '个商品加入成功,' . $failJoin . '个商品加入失败,' . $joinNum . '个商品已经加入'); } /** * 将 微唯宝商品 选品(加入) 到系统内部商户商品表 * @param $spu_id * @param $mer_id * @return bool */ public function insert_product($spu_id, $mer_id) { try { // 1、获取商品信息 $data = Db::name('douhuomall')->where('spu_id', $spu_id)->find(); // 2、是否为多规格 $sku_data = json_decode($data['skuId_info'], true); $spec_type = count($sku_data); if ($spec_type > 1) { $spec_type = 1; } else { $spec_type = 0; } // 3、商品主图 $spuId_info = json_decode($data['spuId_info'], true); $slider_image = $spuId_info['detail_img_list'] ?? ''; $image = $spuId_info['cover_url'] ?? ''; if (empty($slider_image)) { $slider_image = $image; } else if (is_string($slider_image)) { $slider_image = implode(',', json_decode($slider_image, true)); } else if (is_array($slider_image)) { $slider_image = implode(',', $slider_image); } $yanglaojin_scale = (!empty($data['pension']) && !empty($data['market_price'])) ? bcdiv($data['pension'], $data['market_price'], 2) : 0; // 4、商品入库数据 $product_insert_data = [ 'mer_id' => $mer_id, 'image' => $image, 'slider_image' => $slider_image, 'store_name' => $data['title'], 'store_info' => 1, 'keyword' => mb_substr($data['title'], 0, 3), 'is_show' => ($mer_id == $this->specialMerId) ? 1 : 0, 'status' => 1, 'cate_id' => ($mer_id == $this->specialMerId) ? $data['cxb_cate_id'] : $data['cate_ids'], 'unit_name' => '个', 'price' => $data['market_price'], 'cost' => $data['cost_price'], 'ot_price' => $data['ot_price'], 'stock' => '1000', 'spec_type' => $spec_type, 'extension_type' => 1, 'mer_status' => 1, 'is_used' => 1, 'old_product_id' => $data['id'], 'volunteer' => 0, 'type' => ($mer_id == $this->specialMerId) ? 5 : 1, 'pension' => '0.00', 'commission' => 5, 'spu_id' => $data['spu_id'], 'temp_id' => 105, 'plate_mer_profit' => $data['mer_profit'], 'concession_pri' => $data['mer_profit'], 'yanglaojin_scale' => $yanglaojin_scale ]; $insert_id = Db::name('store_product')->insertGetId($product_insert_data); // 5、更新商品详情表 $detail = $spuId_info['detail']; if (!is_null(json_decode($detail))) { $detail_list = json_decode($detail); $detail = ''; foreach ($detail_list as $value) { $detail .= ''; } } Db::name('store_product_content')->insert(['content' => $detail, 'product_id' => $insert_id]); // 6、 $this->insert_sku($insert_id, $sku_data); return true; } catch (Exception $e) { Log::error($spu_id . "加入选品失败:" . $e->getMessage()); return false; } } public function insert_sku($id, $data) { // 1、获取 所有SKU的 规格属性值 $sku_attr_data = array_column($data, 'attribute_json'); $name = ''; // 2、写入 商品属性表 $ProductRepository = app()->make(ProductRepository::class); if (empty($sku_attr_data[0][0])) { // 写入商品属性表 Db::name('store_product_attr')->insert(['product_id' => $id, 'attr_name' => '规格', 'attr_values' => $name]); $key = ''; $num = 1; // 写入 SKU 商品属性值表 foreach ($data as $k => $v) { Db::name('store_product_attr_value')->insert([ 'product_id' => $id, 'detail' => json_encode(['规格' => $key]), 'sku' => $key, 'image' => $v['img_url'], 'cost' => $v['cost_price'], 'ot_price' => $v['retail_price'], 'price' => $v['market_price'], 'unique' => $ProductRepository->setUnique($id, $v['sku_id'], 0), 'stock' => 100, 'cost_price' => $v['cost_price'], 'extension_one' => 5, 'gong_sku_id' => $v['sku_id'], 'gong_mer_profit' => $v['mer_profit'], 'gong_pension' => $v['yanglaojin'], 'gong_market_price' => $v['market_price'], 'plate_mer_profit' => $v['mer_profit'] ]); $key = ''; $num++; } } else { foreach ($sku_attr_data as $kk => $vv) { foreach ($vv as $k1 => $v1) { $name .= $v1['val']; } $name .= '-!-'; } $name = rtrim($name, '-!-'); Db::name('store_product_attr')->insert(['product_id' => $id, 'attr_name' => '规格', 'attr_values' => $name]); $key = ''; $num = 1; foreach ($data as $k => $v) { foreach ($v['attribute_json'] as $k2 => $v2) { $key .= $v2['val']; } Db::name('store_product_attr_value')->insert([ 'product_id' => $id, 'detail' => json_encode(['规格' => $key]), 'sku' => $key, 'image' => $v['img_url'], 'cost' => $v['cost_price'], 'ot_price' => $v['retail_price'], 'price' => $v['market_price'], 'unique' => $ProductRepository->setUnique($id, $v['sku_id'], 0), 'stock' => 100, 'cost_price' => $v['cost_price'], 'extension_one' => 5, 'gong_sku_id' => $v['sku_id'], 'gong_mer_profit' => $v['mer_profit'], 'gong_pension' => $v['yanglaojin'], 'gong_market_price' => $v['market_price'], 'plate_mer_profit' => $v['mer_profit'] ]); $key = ''; $num++; } } } public function add(GongProductValidate $validate) { $data = $this->request->params(['spu_id', 'skuId_info', 'sys_id', 'goods_id', 'spuId_info', 'sku_id', 'cate_ids', 'title', ['cxb_cate_id', 0], 'sign']); Log::channel("gong")->info("加入3号库供应链数据:" . json_encode($data)); // 校验sign $checkSign = $this->checkSign($data); if (!$checkSign) { Log::channel("gong")->error('sign校验失败'); return app('json')->fail('sign校验失败'); } unset($data['sign']); $validate->check($data); $findData = Db::name('douhuomall')->field('id,spu_id,status')->where('spu_id', $data['spu_id'])->find(); if (!empty($findData) && $findData['status'] == 1) return app('json')->success('商品已存在'); try { $data['create_time'] = date('Y-m-d H:i:s'); $data['status'] = 1; $data['update_time'] = date('Y-m-d H:i:s'); $skuInfo = $this->arrSort(json_decode($data['skuId_info'], true), 'cost_price', SORT_ASC); $data['cost_price'] = isset($skuInfo[0]['cost_price']) ? $skuInfo[0]['cost_price'] : 0; $data['market_price'] = isset($skuInfo[0]['market_price']) ? $skuInfo[0]['market_price'] : 0; $data['ot_price'] = isset($skuInfo[0]['retail_price']) ? $skuInfo[0]['retail_price'] : 0; $data['mer_profit'] = isset($skuInfo[0]['profit']) ? $skuInfo[0]['profit'] : 0; $data['pension'] = isset($skuInfo[0]['yanglaojin']) ? $skuInfo[0]['yanglaojin'] : 0; $data['profit'] = empty($data['cost_price']) || empty($data['market_price']) ? 0 : bcdiv($data['cost_price'], $data['market_price'], 2); if (empty($findData)) { Db::name('douhuomall')->insert($data); } else { Db::name('douhuomall')->where('spu_id', $findData['spu_id'])->update([ 'status' => 1, 'create_time' => date('Y-m-d H:i:s'), 'update_time' => date('Y-m-d H:i:s'), 'cost_price' => $data['cost_price'], 'market_price' => $data['market_price'], 'ot_price' => $data['ot_price'], 'mer_profit' => $data['mer_profit'], 'pension' => $data['pension'], 'profit' => $data['profit'], 'skuId_info' => $data['skuId_info'], 'spuId_info' => $data['spuId_info'], 'title' => $data['title'], 'cate_ids' => $data['cate_ids'], 'cxb_cate_id' => $data['cxb_cate_id'] ]); } // 特殊商户直接加入商品库 $is_exit = Db::name('store_product') ->field('product_id') ->where('spu_id', $data['spu_id']) ->where('mer_id', $this->specialMerId) ->where('is_del', 0) ->find(); if (empty($is_exit)) { $this->insert_product($data['spu_id'], $this->specialMerId); } return app('json')->success('添加成功'); } catch (Exception $e) { Log::channel("gong")->error("获取供应链数据失败:" . $e->getMessage()); return app('json')->fail($e->getMessage()); } } public function remove() { $data = $this->request->params(['token', 'sign']); Log::channel("gong")->info("供应链取消商品数据:" . json_encode($data)); // 校验sign $checkSign = $this->checkSign($data); if (!$checkSign) { Log::channel("gong")->error('sign校验失败'); return app('json')->fail('sign校验失败'); } unset($data['sign']); if (empty($data['token'])) { return app('json')->fail('参数为空'); } $spuIds = explode(',', $data['token']); Db::startTrans(); try{ // 删除选品库对应的数据 Db::name('douhuomall')->whereIn('spu_id', $spuIds)->update([ 'status' => 9, 'update_time' => date('Y-m-d H:i:s') ]); // 删除商品管理对应的数据 Db::name('store_product')->whereIn('spu_id', $spuIds)->update([ 'is_del' => 1, 'is_show' => 0 ]); Db::commit(); return app('json')->success('操作成功'); } catch (Exception $e) { Db::rollback(); Log::channel("gong")->error('供应链取消商品失败:' . $e->getMessage()); return app('json')->fail($e->getMessage()); } } public function edit() { $params = $this->request->params(['spu_id', 'sku_info', 'cxb_cate_id', 'sign']); Log::channel("gong")->info('供应链修改商品数据:' . json_encode($params)); // 校验sign $checkSign = $this->checkSign($params); if (!$checkSign) { Log::channel("gong")->error('sign校验失败'); return app('json')->fail('sign校验失败'); } unset($params['sign']); $spu_id = $params['spu_id']; $sku_info = $params['sku_info']; $cxb_cate_id = $params['cxb_cate_id']; if (!empty($cxb_cate_id) && !empty($spu_id) && empty($sku_info)) { try{ Db::startTrans(); Db::name('douhuomall')->where('spu_id', $spu_id)->update([ 'cxb_cate_id' => $cxb_cate_id, 'update_time' => date('Y-m-d H:i:s') ]); Db::name('store_product')->where('spu_id', $spu_id)->where('mer_id', $this->specialMerId)->update(['cate_id' => $cxb_cate_id]); Db::commit(); return app('json')->success('操作成功'); } catch (Exception $e) { Db::rollback(); Log::channel("gong")->error('供应链修改商品分类失败:' . $e->getMessage()); return app('json')->fail($e->getMessage()); } } if (!empty($spu_id) && !empty($sku_info)) { try { // 修改选品库中的sku信息 $data['skuId_info'] = $sku_info; $sku_info = json_decode($sku_info, true); $skuInfo = $this->arrSort($sku_info, 'cost_price', SORT_ASC); $data['cost_price'] = isset($skuInfo[0]['cost_price']) ? $skuInfo[0]['cost_price'] : 0; $data['market_price'] = isset($skuInfo[0]['market_price']) ? $skuInfo[0]['market_price'] : 0; $data['ot_price'] = isset($skuInfo[0]['retail_price']) ? $skuInfo[0]['retail_price'] : 0; $data['mer_profit'] = isset($skuInfo[0]['profit']) ? $skuInfo[0]['profit'] : 0; $data['pension'] = isset($skuInfo[0]['yanglaojin']) ? $skuInfo[0]['yanglaojin'] : 0; $data['profit'] = empty($data['cost_price']) || empty($data['market_price']) ? 0 : bcdiv($data['cost_price'], $data['market_price'], 2); $data['update_time'] = date('Y-m-d H:i:s'); Db::startTrans(); Db::name('douhuomall')->where('spu_id', $spu_id)->update($data); $productModel = Db::name('store_product')->where('spu_id', $spu_id)->where('is_del', 0); $productIds = $productModel->column('product_id'); // 修改商品库中的sku信息 foreach ($sku_info as $value) { $skuModel = Db::name('store_product_attr_value') ->where('gong_sku_id', $value['sku_sn']) ->whereIn('product_id', $productIds); $skuOld = $skuModel->select()->toArray(); Log::channel("gong")->info('供应链修改之前的sku信息数据:' . json_encode($skuOld)); $skuModel->update([ 'price' => $value['market_price'], 'gong_mer_profit' => $value['profit'], 'gong_pension' => $value['yanglaojin'], 'gong_market_price' => $value['market_price'], 'plate_mer_profit' => $value['profit'], 'ot_price' => $value['retail_price'] ]); } $productOld = $productModel->select()->toArray(); Log::channel("gong")->info('供应链修改之前的商品基本信息数据:' . json_encode($productOld)); $yanglaojin_scale = (!empty($data['market_price']) && !empty($data['pension'])) ? bcdiv($data['pension'], $data['market_price'], 2) : 0; $productModel->where('mer_id', '<>', $this->specialMerId)->update([ 'is_show' => 0, 'price'=>$data['market_price'], 'cost'=>$data['cost_price'], 'ot_price'=>$data['ot_price'], 'plate_mer_profit' => $data['mer_profit'], 'yanglaojin_scale' => $yanglaojin_scale ]); Db::name('store_product') ->where('mer_id', $this->specialMerId) ->where('spu_id', $spu_id) ->where('is_del', 0) ->update([ 'price'=>$data['market_price'], 'cost'=>$data['cost_price'], 'ot_price'=>$data['ot_price'], 'plate_mer_profit' => $data['mer_profit'], 'yanglaojin_scale' => $yanglaojin_scale ]); Db::commit(); return app('json')->success('操作成功'); } catch (Exception $e) { Db::rollback(); Log::channel("gong")->error('供应链修改商品销售价失败:' . $e->getMessage()); return app('json')->fail($e->getMessage()); } } return app('json')->fail('数据错误'); } public function category() { $url = "https://dc-api.hxxfb.com/douhuomallCategory/getCateAll"; $result = curlGet($url); if (!isset($result['status']) || $result['status'] != 200) { return app('json')->success([]); } return app('json')->success($result); } /** * 二维数组根据某个字段排序 * @param array $array 要排序的数组 * @param string $keys 要排序的键字段 * @param string $sort 排序类型 SORT_ASC SORT_DESC * @return array 排序后的数组 */ function arrSort($array, $keys, $sort = SORT_DESC) { $keysValue = []; foreach ($array as $k => $v) { $keysValue[$k] = $v[$keys]; } array_multisort($keysValue, $sort, $array); return $array; } /** 校验sign * @param $data * @return false */ function checkSign($data) { if (empty($data['sign'])) { return false; } $originalSign = $data['sign']; unset($data['sign']); ksort($data); $params = ''; foreach($data as $key => $value) { $params .= '&' . $key . '=' . $value; } $params = ltrim($params, '&'); $sign = md5($params . '&signKey=' . $this->signKey); Log::channel("gong")->info("参数sign:" .$originalSign . ',校验生成sign' . $sign); if ($originalSign != $sign) { return false; } return true; } }