Goods.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  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. $slider_image = empty($slider_image) ? $image : implode(',', json_decode($slider_image, true));
  232. $yanglaojin_scale = (!empty($data['pension']) && !empty($data['market_price'])) ? bcdiv($data['pension'], $data['market_price'], 2) : 0;
  233. // 4、商品入库数据
  234. $product_insert_data = [
  235. 'mer_id' => $mer_id,
  236. 'image' => $image,
  237. 'slider_image' => $slider_image,
  238. 'store_name' => $data['title'],
  239. 'store_info' => 1,
  240. 'keyword' => mb_substr($data['title'], 0, 3),
  241. 'is_show' => ($mer_id == $this->specialMerId) ? 1 : 0,
  242. 'status' => 1,
  243. 'cate_id' => ($mer_id == $this->specialMerId) ? $data['cxb_cate_id'] : $data['cate_ids'],
  244. 'unit_name' => '个',
  245. 'price' => $data['market_price'],
  246. 'cost' => $data['cost_price'],
  247. 'ot_price' => $data['ot_price'],
  248. 'stock' => '1000',
  249. 'spec_type' => $spec_type,
  250. 'extension_type' => 1,
  251. 'mer_status' => 1,
  252. 'is_used' => 1,
  253. 'old_product_id' => $data['id'],
  254. 'volunteer' => 0,
  255. 'type' => ($mer_id == $this->specialMerId) ? 5 : 1,
  256. 'pension' => '0.00',
  257. 'commission' => 5,
  258. 'spu_id' => $data['spu_id'],
  259. 'temp_id' => 105,
  260. 'plate_mer_profit' => $data['mer_profit'],
  261. 'concession_pri' => $data['mer_profit'],
  262. 'yanglaojin_scale' => $yanglaojin_scale
  263. ];
  264. $insert_id = Db::name('store_product')->insertGetId($product_insert_data);
  265. // 5、更新商品详情表
  266. $detail = $spuId_info['detail'];
  267. if (!is_null(json_decode($detail))) {
  268. $detail_list = json_decode($detail);
  269. $detail = '';
  270. foreach ($detail_list as $value) {
  271. $detail .= '<img src="' . $value . '"></img>';
  272. }
  273. }
  274. Db::name('store_product_content')->insert(['content' => $detail, 'product_id' => $insert_id]);
  275. // 6、
  276. $this->insert_sku($insert_id, $sku_data);
  277. return true;
  278. } catch (Exception $e) {
  279. Log::error($spu_id . "加入选品失败:" . $e->getMessage());
  280. return false;
  281. }
  282. }
  283. public function insert_sku($id, $data)
  284. {
  285. // 1、获取 所有SKU的 规格属性值
  286. $sku_attr_data = array_column($data, 'attribute_json');
  287. $name = '';
  288. // 2、写入 商品属性表
  289. $ProductRepository = app()->make(ProductRepository::class);
  290. if (empty($sku_attr_data[0][0])) {
  291. // 写入商品属性表
  292. Db::name('store_product_attr')->insert(['product_id' => $id, 'attr_name' => '规格', 'attr_values' => $name]);
  293. $key = '';
  294. $num = 1;
  295. // 写入 SKU 商品属性值表
  296. foreach ($data as $k => $v) {
  297. Db::name('store_product_attr_value')->insert([
  298. 'product_id' => $id,
  299. 'detail' => json_encode(['规格' => $key]),
  300. 'sku' => $key,
  301. 'image' => $v['img_url'],
  302. 'cost' => $v['cost_price'],
  303. 'ot_price' => $v['retail_price'],
  304. 'price' => $v['market_price'],
  305. 'unique' => $ProductRepository->setUnique($id, $v['sku_id'], 0),
  306. 'stock' => 100,
  307. 'cost_price' => $v['cost_price'],
  308. 'extension_one' => 5,
  309. 'gong_sku_id' => $v['sku_id'],
  310. 'gong_mer_profit' => $v['mer_profit'],
  311. 'gong_pension' => $v['yanglaojin'],
  312. 'gong_market_price' => $v['market_price'],
  313. 'plate_mer_profit' => $v['mer_profit']
  314. ]);
  315. $key = '';
  316. $num++;
  317. }
  318. } else {
  319. foreach ($sku_attr_data as $kk => $vv) {
  320. foreach ($vv as $k1 => $v1) {
  321. $name .= $v1['val'];
  322. }
  323. $name .= '-!-';
  324. }
  325. $name = rtrim($name, '-!-');
  326. Db::name('store_product_attr')->insert(['product_id' => $id, 'attr_name' => '规格', 'attr_values' => $name]);
  327. $key = '';
  328. $num = 1;
  329. foreach ($data as $k => $v) {
  330. foreach ($v['attribute_json'] as $k2 => $v2) {
  331. $key .= $v2['val'];
  332. }
  333. Db::name('store_product_attr_value')->insert([
  334. 'product_id' => $id,
  335. 'detail' => json_encode(['规格' => $key]),
  336. 'sku' => $key,
  337. 'image' => $v['img_url'],
  338. 'cost' => $v['cost_price'],
  339. 'ot_price' => $v['retail_price'],
  340. 'price' => $v['market_price'],
  341. 'unique' => $ProductRepository->setUnique($id, $v['sku_id'], 0),
  342. 'stock' => 100,
  343. 'cost_price' => $v['cost_price'],
  344. 'extension_one' => 5,
  345. 'gong_sku_id' => $v['sku_id'],
  346. 'gong_mer_profit' => $v['mer_profit'],
  347. 'gong_pension' => $v['yanglaojin'],
  348. 'gong_market_price' => $v['market_price'],
  349. 'plate_mer_profit' => $v['mer_profit']
  350. ]);
  351. $key = '';
  352. $num++;
  353. }
  354. }
  355. }
  356. public function add(GongProductValidate $validate)
  357. {
  358. $data = $this->request->params(['spu_id', 'skuId_info', 'sys_id', 'goods_id', 'spuId_info', 'sku_id', 'cate_ids', 'title', ['cxb_cate_id', 0], 'sign']);
  359. Log::channel("gong")->info("加入3号库供应链数据:" . json_encode($data));
  360. // 校验sign
  361. $checkSign = $this->checkSign($data);
  362. if (!$checkSign) {
  363. Log::channel("gong")->error('sign校验失败');
  364. return app('json')->fail('sign校验失败');
  365. }
  366. unset($data['sign']);
  367. $validate->check($data);
  368. $findData = Db::name('douhuomall')->field('id,spu_id,status')->where('spu_id', $data['spu_id'])->find();
  369. if (!empty($findData) && $findData['status'] == 1)
  370. return app('json')->success('商品已存在');
  371. try {
  372. $data['create_time'] = date('Y-m-d H:i:s');
  373. $data['status'] = 1;
  374. $data['update_time'] = date('Y-m-d H:i:s');
  375. $skuInfo = $this->arrSort(json_decode($data['skuId_info'], true), 'cost_price', SORT_ASC);
  376. $data['cost_price'] = isset($skuInfo[0]['cost_price']) ? $skuInfo[0]['cost_price'] : 0;
  377. $data['market_price'] = isset($skuInfo[0]['market_price']) ? $skuInfo[0]['market_price'] : 0;
  378. $data['ot_price'] = isset($skuInfo[0]['retail_price']) ? $skuInfo[0]['retail_price'] : 0;
  379. $data['mer_profit'] = isset($skuInfo[0]['profit']) ? $skuInfo[0]['profit'] : 0;
  380. $data['pension'] = isset($skuInfo[0]['yanglaojin']) ? $skuInfo[0]['yanglaojin'] : 0;
  381. $data['profit'] = empty($data['cost_price']) || empty($data['market_price']) ? 0 : bcdiv($data['cost_price'], $data['market_price'], 2);
  382. if (empty($findData)) {
  383. Db::name('douhuomall')->insert($data);
  384. } else {
  385. Db::name('douhuomall')->where('spu_id', $findData['spu_id'])->update([
  386. 'status' => 1,
  387. 'create_time' => date('Y-m-d H:i:s'),
  388. 'update_time' => date('Y-m-d H:i:s'),
  389. 'cost_price' => $data['cost_price'],
  390. 'market_price' => $data['market_price'],
  391. 'ot_price' => $data['ot_price'],
  392. 'mer_profit' => $data['mer_profit'],
  393. 'pension' => $data['pension'],
  394. 'profit' => $data['profit'],
  395. 'skuId_info' => $data['skuId_info'],
  396. 'spuId_info' => $data['spuId_info'],
  397. 'title' => $data['title'],
  398. 'cate_ids' => $data['cate_ids'],
  399. 'cxb_cate_id' => $data['cxb_cate_id']
  400. ]);
  401. }
  402. // 特殊商户直接加入商品库
  403. $is_exit = Db::name('store_product')
  404. ->field('product_id')
  405. ->where('spu_id', $data['spu_id'])
  406. ->where('mer_id', $this->specialMerId)
  407. ->where('is_del', 0)
  408. ->find();
  409. if (empty($is_exit)) {
  410. $this->insert_product($data['spu_id'], $this->specialMerId);
  411. }
  412. return app('json')->success('添加成功');
  413. } catch (Exception $e) {
  414. Log::channel("gong")->error("获取供应链数据失败:" . $e->getMessage());
  415. return app('json')->fail($e->getMessage());
  416. }
  417. }
  418. public function remove()
  419. {
  420. $data = $this->request->params(['token', 'sign']);
  421. Log::channel("gong")->info("供应链取消商品数据:" . json_encode($data));
  422. // 校验sign
  423. $checkSign = $this->checkSign($data);
  424. if (!$checkSign) {
  425. Log::channel("gong")->error('sign校验失败');
  426. return app('json')->fail('sign校验失败');
  427. }
  428. unset($data['sign']);
  429. if (empty($data['token'])) {
  430. return app('json')->fail('参数为空');
  431. }
  432. $spuIds = explode(',', $data['token']);
  433. Db::startTrans();
  434. try{
  435. // 删除选品库对应的数据
  436. Db::name('douhuomall')->whereIn('spu_id', $spuIds)->update([
  437. 'status' => 9,
  438. 'update_time' => date('Y-m-d H:i:s')
  439. ]);
  440. // 删除商品管理对应的数据
  441. Db::name('store_product')->whereIn('spu_id', $spuIds)->update([
  442. 'is_del' => 1,
  443. 'is_show' => 0
  444. ]);
  445. Db::commit();
  446. return app('json')->success('操作成功');
  447. } catch (Exception $e) {
  448. Db::rollback();
  449. Log::channel("gong")->error('供应链取消商品失败:' . $e->getMessage());
  450. return app('json')->fail($e->getMessage());
  451. }
  452. }
  453. public function edit()
  454. {
  455. $params = $this->request->params(['spu_id', 'sku_info', 'cxb_cate_id', 'sign']);
  456. Log::channel("gong")->info('供应链修改商品数据:' . json_encode($params));
  457. // 校验sign
  458. $checkSign = $this->checkSign($params);
  459. if (!$checkSign) {
  460. Log::channel("gong")->error('sign校验失败');
  461. return app('json')->fail('sign校验失败');
  462. }
  463. unset($params['sign']);
  464. $spu_id = $params['spu_id'];
  465. $sku_info = $params['sku_info'];
  466. $cxb_cate_id = $params['cxb_cate_id'];
  467. if (!empty($cxb_cate_id) && !empty($spu_id) && empty($sku_info)) {
  468. try{
  469. Db::startTrans();
  470. Db::name('douhuomall')->where('spu_id', $spu_id)->update([
  471. 'cxb_cate_id' => $cxb_cate_id,
  472. 'update_time' => date('Y-m-d H:i:s')
  473. ]);
  474. Db::name('store_product')->where('spu_id', $spu_id)->where('mer_id', $this->specialMerId)->update(['cate_id' => $cxb_cate_id]);
  475. Db::commit();
  476. return app('json')->success('操作成功');
  477. } catch (Exception $e) {
  478. Db::rollback();
  479. Log::channel("gong")->error('供应链修改商品分类失败:' . $e->getMessage());
  480. return app('json')->fail($e->getMessage());
  481. }
  482. }
  483. if (!empty($spu_id) && !empty($sku_info)) {
  484. try {
  485. // 修改选品库中的sku信息
  486. $data['skuId_info'] = $sku_info;
  487. $sku_info = json_decode($sku_info, true);
  488. $skuInfo = $this->arrSort($sku_info, 'cost_price', SORT_ASC);
  489. $data['cost_price'] = isset($skuInfo[0]['cost_price']) ? $skuInfo[0]['cost_price'] : 0;
  490. $data['market_price'] = isset($skuInfo[0]['market_price']) ? $skuInfo[0]['market_price'] : 0;
  491. $data['ot_price'] = isset($skuInfo[0]['retail_price']) ? $skuInfo[0]['retail_price'] : 0;
  492. $data['mer_profit'] = isset($skuInfo[0]['profit']) ? $skuInfo[0]['profit'] : 0;
  493. $data['pension'] = isset($skuInfo[0]['yanglaojin']) ? $skuInfo[0]['yanglaojin'] : 0;
  494. $data['profit'] = empty($data['cost_price']) || empty($data['market_price']) ? 0 : bcdiv($data['cost_price'], $data['market_price'], 2);
  495. $data['update_time'] = date('Y-m-d H:i:s');
  496. Db::startTrans();
  497. Db::name('douhuomall')->where('spu_id', $spu_id)->update($data);
  498. $productModel = Db::name('store_product')->where('spu_id', $spu_id)->where('is_del', 0);
  499. $productIds = $productModel->column('product_id');
  500. // 修改商品库中的sku信息
  501. foreach ($sku_info as $value) {
  502. $skuModel = Db::name('store_product_attr_value')
  503. ->where('gong_sku_id', $value['sku_sn'])
  504. ->whereIn('product_id', $productIds);
  505. $skuOld = $skuModel->select()->toArray();
  506. Log::channel("gong")->info('供应链修改之前的sku信息数据:' . json_encode($skuOld));
  507. $skuModel->update([
  508. 'price' => $value['market_price'],
  509. 'gong_mer_profit' => $value['profit'],
  510. 'gong_pension' => $value['yanglaojin'],
  511. 'gong_market_price' => $value['market_price'],
  512. 'plate_mer_profit' => $value['profit'],
  513. 'ot_price' => $value['retail_price']
  514. ]);
  515. }
  516. $productOld = $productModel->select()->toArray();
  517. Log::channel("gong")->info('供应链修改之前的商品基本信息数据:' . json_encode($productOld));
  518. $yanglaojin_scale = (!empty($data['market_price']) && !empty($data['pension'])) ? bcdiv($data['pension'], $data['market_price'], 2) : 0;
  519. $productModel->where('mer_id', '<>', $this->specialMerId)->update([
  520. 'is_show' => 0,
  521. 'price'=>$data['market_price'],
  522. 'cost'=>$data['cost_price'],
  523. 'ot_price'=>$data['ot_price'],
  524. 'plate_mer_profit' => $data['mer_profit'],
  525. 'yanglaojin_scale' => $yanglaojin_scale
  526. ]);
  527. Db::name('store_product')
  528. ->where('mer_id', $this->specialMerId)
  529. ->where('spu_id', $spu_id)
  530. ->where('is_del', 0)
  531. ->update([
  532. 'price'=>$data['market_price'],
  533. 'cost'=>$data['cost_price'],
  534. 'ot_price'=>$data['ot_price'],
  535. 'plate_mer_profit' => $data['mer_profit'],
  536. 'yanglaojin_scale' => $yanglaojin_scale
  537. ]);
  538. Db::commit();
  539. return app('json')->success('操作成功');
  540. } catch (Exception $e) {
  541. Db::rollback();
  542. Log::channel("gong")->error('供应链修改商品销售价失败:' . $e->getMessage());
  543. return app('json')->fail($e->getMessage());
  544. }
  545. }
  546. return app('json')->fail('数据错误');
  547. }
  548. public function category()
  549. {
  550. $url = "https://dc-api.hxxfb.com/douhuomallCategory/getCateAll";
  551. $result = curlGet($url);
  552. if (!isset($result['status']) || $result['status'] != 200) {
  553. return app('json')->success([]);
  554. }
  555. return app('json')->success($result);
  556. }
  557. /**
  558. * 二维数组根据某个字段排序
  559. * @param array $array 要排序的数组
  560. * @param string $keys 要排序的键字段
  561. * @param string $sort 排序类型 SORT_ASC SORT_DESC
  562. * @return array 排序后的数组
  563. */
  564. function arrSort($array, $keys, $sort = SORT_DESC) {
  565. $keysValue = [];
  566. foreach ($array as $k => $v) {
  567. $keysValue[$k] = $v[$keys];
  568. }
  569. array_multisort($keysValue, $sort, $array);
  570. return $array;
  571. }
  572. /** 校验sign
  573. * @param $data
  574. * @return false
  575. */
  576. function checkSign($data) {
  577. if (empty($data['sign'])) {
  578. return false;
  579. }
  580. $originalSign = $data['sign'];
  581. unset($data['sign']);
  582. ksort($data);
  583. $params = '';
  584. foreach($data as $key => $value) {
  585. $params .= '&' . $key . '=' . $value;
  586. }
  587. $params = ltrim($params, '&');
  588. $sign = md5($params . '&signKey=' . $this->signKey);
  589. Log::channel("gong")->info("参数sign:" .$originalSign . ',校验生成sign' . $sign);
  590. if ($originalSign != $sign) {
  591. return false;
  592. }
  593. return true;
  594. }
  595. }