暂无描述

GoodsController.php 3.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php namespace App\Http\Controllers\Api;
  2. use App\Goods;
  3. use App\GoodsSkus;
  4. use App\Http\Controllers\Controller;
  5. use Illuminate\Http\Request;
  6. class GoodsController extends Controller {
  7. public function index()
  8. {
  9. $result = Goods::select('id as goods_id', 'name','picUrl','goodsCategoryName')->where('is_del', 0)->orderBy('goods.id', 'desc')->get();
  10. $result = json_decode(json_encode($result), true);
  11. $data = array();
  12. foreach($result as $value){
  13. # 获取sku
  14. $skuList = GoodsSkus::select('id as sku_id','propsName','price','is_white','quantity','is_weigh')->where('goodsCode', $value['goods_id'])->where('is_del', 0)->get();
  15. $skuList = json_decode(json_encode($skuList),true);
  16. $sku = array();
  17. foreach($skuList as $v){
  18. $quantity = GoodsSkus::actualQuantityByRedis($v['sku_id']);
  19. if($v['is_white'] == 0 && $quantity['quantity'] <= 0){
  20. $v['stock'] = 0;
  21. } else {
  22. $v['stock'] = 1;
  23. }
  24. unset($v['is_white']);
  25. unset($v['quantity']);
  26. $sku[] = $v;
  27. }
  28. $value['sku_num'] = count($sku);
  29. if(1 == $value['sku_num']){
  30. $value['stock'] = $sku[0]['stock'];
  31. $value['is_weigh'] = $sku[0]['is_weigh'];
  32. }
  33. $value['sku_list'] = $sku;
  34. if('0' == $value['goodsCategoryName']) {
  35. $data['其它'][] = $value;
  36. } else {
  37. $data[$value['goodsCategoryName']][] = $value;
  38. }
  39. }
  40. return self::returnValue($data,200);
  41. }
  42. public function searchGoods(Request $request)
  43. {
  44. $name = trim($request->input('name'));
  45. $result = Goods::select('id as goods_id', 'name','picUrl','goodsCategoryName')->where('name', 'like', '%'.$name.'%')->where('is_del', 0)->orderBy('id', 'desc')->get();
  46. $result = json_decode(json_encode($result), true);
  47. $data = array();
  48. foreach($result as $value){
  49. # 获取sku
  50. $skuList = GoodsSkus::select('id as sku_id','propsName','price','is_white','quantity','is_weigh')->where('goodsCode', $value['goods_id'])->where('is_del', 0)->get();
  51. $skuList = json_decode(json_encode($skuList),true);
  52. $sku = array();
  53. foreach($skuList as $v){
  54. $quantity = GoodsSkus::actualQuantityByRedis($v['sku_id']);
  55. if($v['is_white'] == 0 && $quantity['quantity'] <= 0){
  56. $v['stock'] = 0;
  57. } else {
  58. $v['stock'] = 1;
  59. }
  60. unset($v['is_white']);
  61. unset($v['quantity']);
  62. $sku[] = $v;
  63. }
  64. $value['sku_num'] = count($sku);
  65. if(1 == $value['sku_num']){
  66. $value['stock'] = $sku[0]['stock'];
  67. $value['is_weigh'] = $sku[0]['is_weigh'];
  68. }
  69. $value['sku_list'] = $sku;
  70. $data[] = $value;
  71. }
  72. return self::returnValue($data);
  73. }
  74. }