Goods.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  1. <?php
  2. namespace app\controller\merchant\gong;
  3. use app\common\repositories\store\product\ProductRepository;
  4. use app\validate\merchant\GongProductValidate;
  5. use crmeb\basic\BaseController;
  6. use think\Exception;
  7. use think\facade\Cache;
  8. use think\facade\Db;
  9. use think\facade\Log;
  10. class Goods extends BaseController
  11. {
  12. //特殊商户ID 正式测试321
  13. private $specialMerId = 321;
  14. private $signKey = 'tdba2023!@#';
  15. // 商品类表
  16. public function lst()
  17. {
  18. [$page, $limit] = $this->getPage();
  19. $params = $this->request->params(['cate_ids','title', 'type', 'profit', 'price']);
  20. $product_model = Db::name('douhuomall')->whereIn('status', [0, 1]);
  21. $gongProduct = $this->getJoinProduct();
  22. if ($params['type'] == 1) {
  23. // 已加入商品管理
  24. $product_model->whereIn('spu_id', $gongProduct);
  25. } elseif ($params['type'] == 2) {
  26. // 未加入商品管理
  27. $product_model->whereNotIn('spu_id', $gongProduct);
  28. }
  29. if($this->request->merId()==496){
  30. $product_model->whereIn('spu_id', $this->getJoinProduct439());
  31. }else{
  32. $product_model->whereNotIn('spu_id', $this->getJoinProduct439());
  33. }
  34. // SQL条件拼装
  35. $product_model->whereIn('status', [0, 1])
  36. ->when(!empty($params['title']), function($query) use ($params) {
  37. $query->whereLike('title','%'.$params['title'].'%');
  38. })
  39. ->when(!empty($params['cate_ids']), function ($query) use ($params) {
  40. $query->where(function($que) use ($params) {
  41. $cateIds = explode(',', $params['cate_ids']);
  42. foreach ($cateIds as $key => $value) {
  43. $que->whereFindInSet('cate_ids', $value, 'OR');
  44. }
  45. });
  46. })
  47. ->field('spu_id,title,cost_price,market_price,profit,spuId_info,status,ot_price,mer_profit,pension');
  48. // 获取总数
  49. $count=$product_model->count('spu_id');
  50. // 按创客利润排序
  51. if (!empty($params['profit'])) {
  52. if ($params['profit'] == 'up') {
  53. $product_model->order('mer_profit', 'asc');
  54. }elseif ($params['profit'] == 'down') {
  55. $product_model->order('mer_profit', 'desc');
  56. }
  57. }
  58. // 按销售价排序
  59. if (!empty($params['price'])) {
  60. if ($params['price'] == 'up') {
  61. $product_model->order('market_price', 'asc');
  62. }elseif ($params['price'] == 'down') {
  63. $product_model->order('market_price', 'desc');
  64. }
  65. }
  66. if (empty($params['price']) && empty($params['profit'])) {
  67. $product_model->order('create_time', 'desc');
  68. }
  69. // 获取分页数据
  70. $product_lst=$product_model->page($page,$limit)->select()->toArray();
  71. foreach ($product_lst as $k=>&$v){
  72. //商品图片处理
  73. $v['spuId_info']=json_decode($v['spuId_info'],true);
  74. $v['img']=$v['spuId_info']['cover_url'];
  75. if(in_array($v['spu_id'], $gongProduct)){
  76. $v['is_join']=1;
  77. }else{
  78. $v['is_join']=0;
  79. }
  80. unset($v['spuId_info']);
  81. }
  82. $retrun_data=[
  83. 'count'=>$count,
  84. 'data'=>$product_lst
  85. ];
  86. return app('json')->success($retrun_data);
  87. }
  88. // 获取已加入选品库的商品
  89. public function getJoinProduct()
  90. {
  91. $mer_id = $this->request->merId();
  92. $gongProduct = Db::name('store_product')
  93. ->where('mer_id', $mer_id)
  94. ->where('is_del', 0)
  95. ->whereNotNull('spu_id')
  96. ->column('spu_id');
  97. return $gongProduct;
  98. }
  99. // 获取439已加入选品库的商品
  100. public function getJoinProduct439()
  101. {
  102. $mer_id = $this->request->merId();
  103. if($mer_id==439){
  104. return [];
  105. }
  106. $gongProduct = Db::name('store_product')
  107. ->where('mer_id', 439)
  108. ->where('is_del', 0)
  109. ->whereNotNull('spu_id')
  110. ->column('spu_id');
  111. return $gongProduct;
  112. }
  113. // 获取选品库中全部、已加入商品库、未加入商品库的数量
  114. public function getStatusFilter()
  115. {
  116. $gongProduct = $this->getJoinProduct();
  117. $gongProduct439=$this->getJoinProduct439();
  118. if($this->request->merId()==496){
  119. // 获取全部数据
  120. $allNum = Db::name('douhuomall')->whereIn('spu_id',$gongProduct439 )->whereIn('status', [0, 1])->count();
  121. // 获取已加入选品库的数据
  122. $joinNum = Db::name('douhuomall')->whereIn('spu_id',$gongProduct439 )->whereIn('spu_id', $gongProduct)->whereIn('status', [0,1])->count();
  123. // 获取未加入选品库的数据
  124. $notJoinNum = Db::name('douhuomall')->whereIn('spu_id',$gongProduct439 )->whereNotIn('spu_id', $gongProduct)->whereIn('status', [0,1])->count();
  125. }else{
  126. // 获取全部数据
  127. $allNum = Db::name('douhuomall')->whereNotIn('spu_id',$gongProduct439 )->whereIn('status', [0, 1])->count();
  128. // 获取已加入选品库的数据
  129. $joinNum = Db::name('douhuomall')->whereNotIn('spu_id',$gongProduct439 )->whereIn('spu_id', $gongProduct)->whereIn('status', [0,1])->count();
  130. // 获取未加入选品库的数据
  131. $notJoinNum = Db::name('douhuomall')->whereNotIn('spu_id',$gongProduct439 )->whereNotIn('spu_id', $gongProduct)->whereIn('status', [0,1])->count();
  132. }
  133. $res = [
  134. [
  135. 'type' => 0,
  136. 'title' => '全部',
  137. 'count' => $allNum
  138. ],
  139. [
  140. 'type' => 1,
  141. 'title' => '已加入商品管理',
  142. 'count' => $joinNum
  143. ],
  144. [
  145. 'type' => 2,
  146. 'title' => '未加入商品管理',
  147. 'count' => $notJoinNum
  148. ]
  149. ];
  150. return app('json')->success($res);
  151. }
  152. //商品详情
  153. public function product_details()
  154. {
  155. $spu_id = $this->request->param('spu_id');
  156. $product_data=Db::name('douhuomall')->where('spu_id',$spu_id)->find();
  157. if (empty($product_data))
  158. return app('json')->success([]);
  159. $product_data['spuId_info']=json_decode($product_data['spuId_info'],true);
  160. $product_data['skuId_info']=json_decode($product_data['skuId_info'],true);
  161. foreach ($product_data['skuId_info'] as &$skuId){
  162. $name='';
  163. foreach($skuId['attribute_json'] as $k=>$v){
  164. $name.=$v['val'];
  165. }
  166. $skuId['attribute_json']=$name;
  167. }
  168. return app('json')->success($product_data);
  169. }
  170. // 加入选品
  171. public function join_selection_library()
  172. {
  173. $data = $this->request->params(['spu_id']);
  174. $spu_id = $data['spu_id'];
  175. $mer_id=$this->request->merId();
  176. $store_product_data=Db::name('store_product')->where('mer_id',$mer_id)->where('spu_id',$spu_id)->where('is_del', 0)->value('product_id');
  177. if($store_product_data)
  178. return app('json')->fail('此商品已加入选品库');
  179. $res = $this->insert_product($spu_id, $mer_id);
  180. if($res) {
  181. return app('json')->success('加入成功');
  182. }
  183. return app('json')->fail('加入失败');
  184. }
  185. // 批量加入选品
  186. public function batch_join_selection_library()
  187. {
  188. $data = $this->request->params(['spu_ids']);
  189. if (empty($data['spu_ids'])) {
  190. return app('json')->fail('参数错误');
  191. }
  192. $mer_id=$this->request->merId();
  193. $joinNum = 0;
  194. $successJoin = 0;
  195. $failJoin = 0;
  196. $spuIds = explode(',', $data['spu_ids']);
  197. foreach ($spuIds as $spu_id) {
  198. $store_product_data=Db::name('store_product')->where('mer_id',$mer_id)->where('spu_id',$spu_id)->where('is_del', 0)->value('product_id');
  199. if(!empty($store_product_data)) {
  200. ++$joinNum;
  201. continue;
  202. }
  203. $res = $this->insert_product($spu_id, $mer_id);
  204. $res ? ++$successJoin : ++$failJoin;
  205. }
  206. return app('json')->success($successJoin . '个商品加入成功,' . $failJoin . '个商品加入失败,' . $joinNum . '个商品已经加入');
  207. }
  208. /**
  209. * 将 微唯宝商品 选品(加入) 到系统内部商户商品表
  210. * @param $spu_id
  211. * @param $mer_id
  212. * @return bool
  213. */
  214. public function insert_product($spu_id, $mer_id)
  215. {
  216. try {
  217. // 1、获取商品信息
  218. $data = Db::name('douhuomall')->where('spu_id', $spu_id)->find();
  219. // 2、是否为多规格
  220. $sku_data = json_decode($data['skuId_info'], true);
  221. $spec_type = count($sku_data);
  222. if ($spec_type > 1) {
  223. $spec_type = 1;
  224. } else {
  225. $spec_type = 0;
  226. }
  227. // 3、商品主图
  228. $spuId_info = json_decode($data['spuId_info'], true);
  229. $slider_image = $spuId_info['detail_img_list'] ?? '';
  230. $image = $spuId_info['cover_url'] ?? '';
  231. if (empty($slider_image)) {
  232. $slider_image = $image;
  233. } else if (is_string($slider_image)) {
  234. $slider_image = implode(',', json_decode($slider_image, true));
  235. } else if (is_array($slider_image)) {
  236. $slider_image = implode(',', $slider_image);
  237. }
  238. $yanglaojin_scale = (!empty($data['pension']) && !empty($data['market_price'])) ? bcdiv($data['pension'], $data['market_price'], 2) : 0;
  239. // 4、商品入库数据
  240. $product_insert_data = [
  241. 'mer_id' => $mer_id,
  242. 'image' => $image,
  243. 'slider_image' => $slider_image,
  244. 'store_name' => $data['title'],
  245. 'store_info' => 1,
  246. 'keyword' => mb_substr($data['title'], 0, 3),
  247. 'is_show' => ($mer_id == $this->specialMerId) ? 1 : 0,
  248. 'status' => 1,
  249. 'cate_id' => ($mer_id == $this->specialMerId) ? $data['cxb_cate_id'] : $data['cate_ids'],
  250. 'unit_name' => '个',
  251. 'price' => $data['market_price'],
  252. 'cost' => $data['cost_price'],
  253. 'ot_price' => $data['ot_price'],
  254. 'stock' => '1000',
  255. 'spec_type' => $spec_type,
  256. 'extension_type' => 1,
  257. 'mer_status' => 1,
  258. 'is_used' => 1,
  259. 'old_product_id' => $data['id'],
  260. 'volunteer' => 0,
  261. 'type' => ($mer_id == $this->specialMerId) ? 5 : 1,
  262. 'pension' => '0.00',
  263. 'commission' => 5,
  264. 'spu_id' => $data['spu_id'],
  265. 'temp_id' => 105,
  266. 'plate_mer_profit' => $data['mer_profit'],
  267. 'concession_pri' => $data['mer_profit'],
  268. 'yanglaojin_scale' => $yanglaojin_scale
  269. ];
  270. $insert_id = Db::name('store_product')->insertGetId($product_insert_data);
  271. // 5、更新商品详情表
  272. $detail = $spuId_info['detail'];
  273. if (!is_null(json_decode($detail))) {
  274. $detail_list = json_decode($detail);
  275. $detail = '';
  276. foreach ($detail_list as $value) {
  277. $detail .= '<img src="' . $value . '"></img>';
  278. }
  279. }
  280. Db::name('store_product_content')->insert(['content' => $detail, 'product_id' => $insert_id]);
  281. // 6、
  282. $this->insert_sku($insert_id, $sku_data);
  283. return true;
  284. } catch (Exception $e) {
  285. Log::error($spu_id . "加入选品失败:" . $e->getMessage());
  286. return false;
  287. }
  288. }
  289. public function insert_sku($id, $data)
  290. {
  291. // 1、获取 所有SKU的 规格属性值
  292. $sku_attr_data = array_column($data, 'attribute_json');
  293. $name = '';
  294. // 2、写入 商品属性表
  295. $ProductRepository = app()->make(ProductRepository::class);
  296. if (empty($sku_attr_data[0][0])) {
  297. // 写入商品属性表
  298. Db::name('store_product_attr')->insert(['product_id' => $id, 'attr_name' => '规格', 'attr_values' => $name]);
  299. $key = '';
  300. $num = 1;
  301. // 写入 SKU 商品属性值表
  302. foreach ($data as $k => $v) {
  303. Db::name('store_product_attr_value')->insert([
  304. 'product_id' => $id,
  305. 'detail' => json_encode(['规格' => $key]),
  306. 'sku' => $key,
  307. 'image' => $v['img_url'],
  308. 'cost' => $v['cost_price'],
  309. 'ot_price' => $v['retail_price'],
  310. 'price' => $v['market_price'],
  311. 'unique' => $ProductRepository->setUnique($id, $v['sku_id'], 0),
  312. 'stock' => 100,
  313. 'cost_price' => $v['cost_price'],
  314. 'extension_one' => 5,
  315. 'gong_sku_id' => $v['sku_id'],
  316. 'gong_mer_profit' => $v['mer_profit'],
  317. 'gong_pension' => $v['yanglaojin'],
  318. 'gong_market_price' => $v['market_price'],
  319. 'plate_mer_profit' => $v['mer_profit']
  320. ]);
  321. $key = '';
  322. $num++;
  323. }
  324. } else {
  325. foreach ($sku_attr_data as $kk => $vv) {
  326. foreach ($vv as $k1 => $v1) {
  327. $name .= $v1['val'];
  328. }
  329. $name .= '-!-';
  330. }
  331. $name = rtrim($name, '-!-');
  332. Db::name('store_product_attr')->insert(['product_id' => $id, 'attr_name' => '规格', 'attr_values' => $name]);
  333. $key = '';
  334. $num = 1;
  335. foreach ($data as $k => $v) {
  336. foreach ($v['attribute_json'] as $k2 => $v2) {
  337. $key .= $v2['val'];
  338. }
  339. Db::name('store_product_attr_value')->insert([
  340. 'product_id' => $id,
  341. 'detail' => json_encode(['规格' => $key]),
  342. 'sku' => $key,
  343. 'image' => $v['img_url'],
  344. 'cost' => $v['cost_price'],
  345. 'ot_price' => $v['retail_price'],
  346. 'price' => $v['market_price'],
  347. 'unique' => $ProductRepository->setUnique($id, $v['sku_id'], 0),
  348. 'stock' => 100,
  349. 'cost_price' => $v['cost_price'],
  350. 'extension_one' => 5,
  351. 'gong_sku_id' => $v['sku_id'],
  352. 'gong_mer_profit' => $v['mer_profit'],
  353. 'gong_pension' => $v['yanglaojin'],
  354. 'gong_market_price' => $v['market_price'],
  355. 'plate_mer_profit' => $v['mer_profit']
  356. ]);
  357. $key = '';
  358. $num++;
  359. }
  360. }
  361. }
  362. public function add(GongProductValidate $validate)
  363. {
  364. $data = $this->request->params(['spu_id', 'skuId_info', 'sys_id', 'goods_id', 'spuId_info', 'sku_id', 'cate_ids', 'title', ['cxb_cate_id', 0], 'sign']);
  365. Log::channel("gong")->info("加入3号库供应链数据:" . json_encode($data));
  366. // 校验sign
  367. $checkSign = $this->checkSign($data);
  368. if (!$checkSign) {
  369. Log::channel("gong")->error('sign校验失败');
  370. return app('json')->fail('sign校验失败');
  371. }
  372. unset($data['sign']);
  373. $validate->check($data);
  374. $findData = Db::name('douhuomall')->field('id,spu_id,status')->where('spu_id', $data['spu_id'])->find();
  375. if (!empty($findData) && $findData['status'] == 1)
  376. return app('json')->success('商品已存在');
  377. try {
  378. $data['create_time'] = date('Y-m-d H:i:s');
  379. $data['status'] = 1;
  380. $data['update_time'] = date('Y-m-d H:i:s');
  381. $skuInfo = $this->arrSort(json_decode($data['skuId_info'], true), 'cost_price', SORT_ASC);
  382. $data['cost_price'] = isset($skuInfo[0]['cost_price']) ? $skuInfo[0]['cost_price'] : 0;
  383. $data['market_price'] = isset($skuInfo[0]['market_price']) ? $skuInfo[0]['market_price'] : 0;
  384. $data['ot_price'] = isset($skuInfo[0]['retail_price']) ? $skuInfo[0]['retail_price'] : 0;
  385. $data['mer_profit'] = isset($skuInfo[0]['profit']) ? $skuInfo[0]['profit'] : 0;
  386. $data['pension'] = isset($skuInfo[0]['yanglaojin']) ? $skuInfo[0]['yanglaojin'] : 0;
  387. $data['profit'] = empty($data['cost_price']) || empty($data['market_price']) ? 0 : bcdiv($data['cost_price'], $data['market_price'], 2);
  388. if (empty($findData)) {
  389. Db::name('douhuomall')->insert($data);
  390. } else {
  391. Db::name('douhuomall')->where('spu_id', $findData['spu_id'])->update([
  392. 'status' => 1,
  393. 'create_time' => date('Y-m-d H:i:s'),
  394. 'update_time' => date('Y-m-d H:i:s'),
  395. 'cost_price' => $data['cost_price'],
  396. 'market_price' => $data['market_price'],
  397. 'ot_price' => $data['ot_price'],
  398. 'mer_profit' => $data['mer_profit'],
  399. 'pension' => $data['pension'],
  400. 'profit' => $data['profit'],
  401. 'skuId_info' => $data['skuId_info'],
  402. 'spuId_info' => $data['spuId_info'],
  403. 'title' => $data['title'],
  404. 'cate_ids' => $data['cate_ids'],
  405. 'cxb_cate_id' => $data['cxb_cate_id']
  406. ]);
  407. }
  408. // 特殊商户直接加入商品库
  409. $is_exit = Db::name('store_product')
  410. ->field('product_id')
  411. ->where('spu_id', $data['spu_id'])
  412. ->where('mer_id', $this->specialMerId)
  413. ->where('is_del', 0)
  414. ->find();
  415. if (empty($is_exit)) {
  416. $this->insert_product($data['spu_id'], $this->specialMerId);
  417. }
  418. return app('json')->success('添加成功');
  419. } catch (Exception $e) {
  420. Log::channel("gong")->error("获取供应链数据失败:" . $e->getMessage());
  421. return app('json')->fail($e->getMessage());
  422. }
  423. }
  424. public function remove()
  425. {
  426. $data = $this->request->params(['token', 'sign']);
  427. Log::channel("gong")->info("供应链取消商品数据:" . json_encode($data));
  428. // 校验sign
  429. $checkSign = $this->checkSign($data);
  430. if (!$checkSign) {
  431. Log::channel("gong")->error('sign校验失败');
  432. return app('json')->fail('sign校验失败');
  433. }
  434. unset($data['sign']);
  435. if (empty($data['token'])) {
  436. return app('json')->fail('参数为空');
  437. }
  438. $spuIds = explode(',', $data['token']);
  439. Db::startTrans();
  440. try{
  441. // 删除选品库对应的数据
  442. Db::name('douhuomall')->whereIn('spu_id', $spuIds)->update([
  443. 'status' => 9,
  444. 'update_time' => date('Y-m-d H:i:s')
  445. ]);
  446. // 删除商品管理对应的数据
  447. Db::name('store_product')->whereIn('spu_id', $spuIds)->update([
  448. 'is_del' => 1,
  449. 'is_show' => 0
  450. ]);
  451. Db::commit();
  452. return app('json')->success('操作成功');
  453. } catch (Exception $e) {
  454. Db::rollback();
  455. Log::channel("gong")->error('供应链取消商品失败:' . $e->getMessage());
  456. return app('json')->fail($e->getMessage());
  457. }
  458. }
  459. public function edit()
  460. {
  461. $params = $this->request->params(['spu_id', 'sku_info', 'cxb_cate_id', 'sign']);
  462. Log::channel("gong")->info('供应链修改商品数据:' . json_encode($params));
  463. // 校验sign
  464. $checkSign = $this->checkSign($params);
  465. if (!$checkSign) {
  466. Log::channel("gong")->error('sign校验失败');
  467. return app('json')->fail('sign校验失败');
  468. }
  469. unset($params['sign']);
  470. $spu_id = $params['spu_id'];
  471. $sku_info = $params['sku_info'];
  472. $cxb_cate_id = $params['cxb_cate_id'];
  473. if (!empty($cxb_cate_id) && !empty($spu_id) && empty($sku_info)) {
  474. try{
  475. Db::startTrans();
  476. Db::name('douhuomall')->where('spu_id', $spu_id)->update([
  477. 'cxb_cate_id' => $cxb_cate_id,
  478. 'update_time' => date('Y-m-d H:i:s')
  479. ]);
  480. Db::name('store_product')->where('spu_id', $spu_id)->where('mer_id', $this->specialMerId)->update(['cate_id' => $cxb_cate_id]);
  481. Db::commit();
  482. return app('json')->success('操作成功');
  483. } catch (Exception $e) {
  484. Db::rollback();
  485. Log::channel("gong")->error('供应链修改商品分类失败:' . $e->getMessage());
  486. return app('json')->fail($e->getMessage());
  487. }
  488. }
  489. if (!empty($spu_id) && !empty($sku_info)) {
  490. try {
  491. // 修改选品库中的sku信息
  492. $data['skuId_info'] = $sku_info;
  493. $sku_info = json_decode($sku_info, true);
  494. $skuInfo = $this->arrSort($sku_info, 'cost_price', SORT_ASC);
  495. $data['cost_price'] = isset($skuInfo[0]['cost_price']) ? $skuInfo[0]['cost_price'] : 0;
  496. $data['market_price'] = isset($skuInfo[0]['market_price']) ? $skuInfo[0]['market_price'] : 0;
  497. $data['ot_price'] = isset($skuInfo[0]['retail_price']) ? $skuInfo[0]['retail_price'] : 0;
  498. $data['mer_profit'] = isset($skuInfo[0]['profit']) ? $skuInfo[0]['profit'] : 0;
  499. $data['pension'] = isset($skuInfo[0]['yanglaojin']) ? $skuInfo[0]['yanglaojin'] : 0;
  500. $data['profit'] = empty($data['cost_price']) || empty($data['market_price']) ? 0 : bcdiv($data['cost_price'], $data['market_price'], 2);
  501. $data['update_time'] = date('Y-m-d H:i:s');
  502. Db::startTrans();
  503. Db::name('douhuomall')->where('spu_id', $spu_id)->update($data);
  504. $productModel = Db::name('store_product')->where('spu_id', $spu_id)->where('is_del', 0);
  505. $productIds = $productModel->column('product_id');
  506. // 修改商品库中的sku信息
  507. foreach ($sku_info as $value) {
  508. $skuModel = Db::name('store_product_attr_value')
  509. ->where('gong_sku_id', $value['sku_sn'])
  510. ->whereIn('product_id', $productIds);
  511. $skuOld = $skuModel->select()->toArray();
  512. Log::channel("gong")->info('供应链修改之前的sku信息数据:' . json_encode($skuOld));
  513. $skuModel->update([
  514. 'price' => $value['market_price'],
  515. 'gong_mer_profit' => $value['profit'],
  516. 'gong_pension' => $value['yanglaojin'],
  517. 'gong_market_price' => $value['market_price'],
  518. 'plate_mer_profit' => $value['profit'],
  519. 'ot_price' => $value['retail_price']
  520. ]);
  521. }
  522. $productOld = $productModel->select()->toArray();
  523. Log::channel("gong")->info('供应链修改之前的商品基本信息数据:' . json_encode($productOld));
  524. $yanglaojin_scale = (!empty($data['market_price']) && !empty($data['pension'])) ? bcdiv($data['pension'], $data['market_price'], 2) : 0;
  525. $productModel->where('mer_id', '<>', $this->specialMerId)->update([
  526. 'is_show' => 0,
  527. 'price'=>$data['market_price'],
  528. 'cost'=>$data['cost_price'],
  529. 'ot_price'=>$data['ot_price'],
  530. 'plate_mer_profit' => $data['mer_profit'],
  531. 'yanglaojin_scale' => $yanglaojin_scale
  532. ]);
  533. Db::name('store_product')
  534. ->where('mer_id', $this->specialMerId)
  535. ->where('spu_id', $spu_id)
  536. ->where('is_del', 0)
  537. ->update([
  538. 'price'=>$data['market_price'],
  539. 'cost'=>$data['cost_price'],
  540. 'ot_price'=>$data['ot_price'],
  541. 'plate_mer_profit' => $data['mer_profit'],
  542. 'yanglaojin_scale' => $yanglaojin_scale
  543. ]);
  544. Db::commit();
  545. return app('json')->success('操作成功');
  546. } catch (Exception $e) {
  547. Db::rollback();
  548. Log::channel("gong")->error('供应链修改商品销售价失败:' . $e->getMessage());
  549. return app('json')->fail($e->getMessage());
  550. }
  551. }
  552. return app('json')->fail('数据错误');
  553. }
  554. public function category()
  555. {
  556. $url = "https://dc-api.hxxfb.com/douhuomallCategory/getCateAll";
  557. $result = curlGet($url);
  558. if (!isset($result['status']) || $result['status'] != 200) {
  559. return app('json')->success([]);
  560. }
  561. return app('json')->success($result);
  562. }
  563. /**
  564. * 二维数组根据某个字段排序
  565. * @param array $array 要排序的数组
  566. * @param string $keys 要排序的键字段
  567. * @param string $sort 排序类型 SORT_ASC SORT_DESC
  568. * @return array 排序后的数组
  569. */
  570. function arrSort($array, $keys, $sort = SORT_DESC) {
  571. $keysValue = [];
  572. foreach ($array as $k => $v) {
  573. $keysValue[$k] = $v[$keys];
  574. }
  575. array_multisort($keysValue, $sort, $array);
  576. return $array;
  577. }
  578. /** 校验sign
  579. * @param $data
  580. * @return false
  581. */
  582. function checkSign($data) {
  583. if (empty($data['sign'])) {
  584. return false;
  585. }
  586. $originalSign = $data['sign'];
  587. unset($data['sign']);
  588. ksort($data);
  589. $params = '';
  590. foreach($data as $key => $value) {
  591. $params .= '&' . $key . '=' . $value;
  592. }
  593. $params = ltrim($params, '&');
  594. $sign = md5($params . '&signKey=' . $this->signKey);
  595. Log::channel("gong")->info("参数sign:" .$originalSign . ',校验生成sign' . $sign);
  596. if ($originalSign != $sign) {
  597. return false;
  598. }
  599. return true;
  600. }
  601. }