Goods.php 26 KB

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