| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621 |
- <?php
- namespace app\controller\admin\gong;
- use app\common\repositories\store\product\ProductRepository;
- use app\validate\merchant\GongProductValidate;
- use crmeb\basic\BaseController;
- use think\Exception;
- use think\facade\Cache;
- use think\facade\Db;
- use think\facade\Log;
- class Goods extends BaseController
- {
- //特殊商户ID 正式测试321
- private $specialMerId = 321;
- private $signKey = 'tdba2023!@#';
- // 商品类表
- public function lst()
- {
- [$page, $limit] = $this->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->where('status', 1);
- } elseif ($params['type'] == 2) {
- // 未加入商品管理
- $product_model->where('status', 0);
- }
- // 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()
- {
- $gongProduct = Db::name('store_product')
- ->where('status', 1)
- ->whereNotNull('spu_id')
- ->column('spu_id');
- return $gongProduct;
- }
- // 获取选品库中全部、已加入3号库、未加入3号库的数量
- public function getStatusFilter()
- {
- // 获取全部数据
- $allNum = Db::name('douhuomall')->whereIn('status', [0, 1])->count();
- // 获取已加入3号库的数据
- $joinNum = Db::name('douhuomall')->where('status', 1)->count();
- // 获取未加入3号库的数据
- $notJoinNum = Db::name('douhuomall')->where('status', 0)->count();
- $res = [
- // [
- // 'type' => 0,
- // 'title' => '全部',
- // 'count' => $allNum
- // ],
- // [
- // 'type' => 1,
- // 'title' => '已加入3号库',
- // 'count' => $joinNum
- // ],
- // [
- // 'type' => 2,
- // 'title' => '未加入3号库',
- // '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 update_Price()
- {
- $data = $this->request->param();
- $spu_id = $data['spu_id'];
-
- $res = Db::name('douhuomall')->where('spu_id', $spu_id)->update([
- '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'],
- 'title' => $data['title'],
- ]);
- if($res) {
- return app('json')->success('修改成功');
- }
- return app('json')->fail('修改失败');
- }
- // 上下架
- public function join_selection_library()
- {
- $data = $this->request->params(['spu_id']);
- $spu_id = $data['spu_id'];
- $product_data=Db::name('douhuomall')->where('spu_id',$spu_id)->find();
- if (empty($product_data)) return app('json')->fail('设置失败,没有找到该商品');
- if ($product_data['status'] == 1) {
- $res = Db::name('douhuomall')->where('spu_id', $spu_id)->update([
- 'update_time' => date('Y-m-d H:i:s'),
- 'status' => 0,
- ]);
- }else{
- $res = Db::name('douhuomall')->where('spu_id', $spu_id)->update([
- 'update_time' => date('Y-m-d H:i:s'),
- 'status' => 1,
- ]);
- }
- 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 . '个商品已经加入');
- }
- public function insert_product($spu_id, $mer_id) {
- try {
- $data=Db::name('douhuomall')->where('spu_id',$spu_id)->find();
- // 是否为多规格
- $sku_data=json_decode($data['skuId_info'],true);
- $spec_type=count($sku_data);
- if($spec_type>1){
- $spec_type=1;
- }else{
- $spec_type=0;
- }
- //商品主图
- $spuId_info=json_decode($data['spuId_info'],true);
- $slider_image=$spuId_info['detail_img_list']??'';
- $image=$spuId_info['cover_url']??'';
- $slider_image = empty($slider_image) ? $image : implode(',',json_decode($slider_image,true));
- $yanglaojin_scale = (!empty($data['pension']) && !empty($data['market_price'])) ? bcdiv($data['pension'], $data['market_price'], 2) : 0;
- //商品入库数据
- $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);
- $detail = $spuId_info['detail'];
- if(!is_null(json_decode($detail))){
- $detail_list = json_decode($detail);
- $detail = '';
- foreach($detail_list as $value){
- $detail .= '<img src="'.$value.'"></img>';
- }
- }
- Db::name('store_product_content')->insert(['content'=>$detail,'product_id'=>$insert_id]);
- $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)
- {
- $sku_attr_data=array_column($data,'attribute_json');
- $name='';
- $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;
- 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_sn'],0),
- 'stock'=>100,
- 'cost_price'=>$v['cost_price'],
- 'extension_one'=>5,
- 'gong_sku_id'=>$v['sku_id'],
- 'gong_mer_profit' => $v['profit'],
- 'gong_pension' => $v['yanglaojin'],
- 'gong_market_price' => $v['market_price'],
- 'plate_mer_profit' => $v['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_sn'],0),
- 'stock'=>100,
- 'cost_price'=>$v['cost_price'],
- 'extension_one'=>5,
- 'gong_sku_id'=>$v['sku_id'],
- 'gong_mer_profit' => $v['profit'],
- 'gong_pension' => $v['yanglaojin'],
- 'gong_market_price' => $v['market_price'],
- 'plate_mer_profit' => $v['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;
- }
- }
|