Goods.php 26 KB

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