Product.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. namespace app\api\controller;
  3. use think\Db;
  4. use think\Loader ;
  5. use app\common\controller\Api;
  6. use app\common\model\ProductLists;
  7. use app\common\model\ProductMarket;
  8. use app\common\model\ProductOrder;
  9. use app\common\model\ProductsModel;
  10. use app\common\model\ProductPopular;
  11. use app\api\logic\OrderLogic;
  12. use app\common\model\UserCollect;
  13. class Product extends Api
  14. {
  15. protected array $noNeedLogin = ['getPopularList'];
  16. protected string $lan = '';
  17. public function _initialize()
  18. {
  19. parent::_initialize();
  20. $this->lan = $this->request->getLan();
  21. }
  22. /**
  23. * 热销列表
  24. */
  25. public function getPopularList(ProductsModel $productsModel, ProductPopular $productPopular, ProductLists $productLists)
  26. {
  27. $item = $productsModel::getProductTypeAll($this->lan);
  28. $resp = array();
  29. foreach ($item as $kk =>$val) {
  30. $list= $productLists->where('type_id', $kk)->field('id,thum as img_url,'.$this->lan.'_name as name')->select();
  31. $i = 0;
  32. $proArr = array();
  33. foreach ($list as &$item) {
  34. $pro =$productPopular
  35. ->field('id,product_id,price,cost_price,stock,(num+init_num) as num,start_time,end_time,status')
  36. ->where('product_id', $item['id'])
  37. ->where('status', '<', $productPopular::Stop)
  38. ->order('start_time')
  39. ->find();
  40. if(!empty($pro)){
  41. $proArr[$i] = $item;
  42. $proArr[$i]['pro'] = $pro;
  43. $i+=1;
  44. }
  45. }
  46. $resp[] = ['type_id'=>$kk,'title'=>$val, 'product'=>$proArr];
  47. }
  48. $this->success('', $resp);
  49. }
  50. /**
  51. * 寄售转让列表
  52. * sort: 0 默认排序 1价格从高到低 2价格从低到高
  53. * type_id 分类
  54. * key_val 商品名称搜索
  55. */
  56. public function getTransferList(ProductMarket $productMarket, OrderLogic $orderLogic)
  57. {
  58. $sort = $this->request->post('sort/d', '');
  59. $type_id = $this->request->post('type_id/s', '');
  60. $key_val = $this->request->post('key_val/s', '');
  61. $order = 'a.price ASC';
  62. if($sort == 1) $order = 'a.price DESC';
  63. $map['product_market.status'] = $productMarket::Normal;
  64. if(!empty($type_id)) $map['product_market.type_id'] = $type_id;
  65. if(!empty($key_val)) $map['product_list.'.$this->lan.'_name'] = ['like', '%'.$key_val.'%'];
  66. $list = $productMarket
  67. ->alias('a')
  68. ->join("product_list b", "a.product_id = b.id", "left")
  69. ->field('a.id,'.'b.'.$this->lan.'_name as name,b.thum as img_url,a.price,a.product_id,a.type_id')
  70. ->where('a.status', $productMarket::Normal)
  71. ->where($map)
  72. ->order($order)
  73. ->paginate($this->pageSize);
  74. $list = $this->getProductOrderList($list, $this->auth->id, true, $orderLogic);
  75. $this->success('', $list);
  76. }
  77. /**
  78. * 寄售转让收藏列表
  79. * sort: 0 默认排序 1价格从高到低 2价格从低到高
  80. * type_id 分类
  81. * key_val 商品名称搜索
  82. */
  83. public function getTransferCollectList(ProductMarket $productMarket, OrderLogic $orderLogic)
  84. {
  85. $sort = $this->request->post('sort/d', '');
  86. $type_id = $this->request->post('type_id/s', '');
  87. $key_val = $this->request->post('key_val/s', '');
  88. $order = 'a.price ASC';
  89. if($sort == 1) $order = 'a.price DESC';
  90. $map['a.status'] = $productMarket::Normal;
  91. if(!empty($type_id)) $map['b.type_id'] = $type_id;
  92. if(!empty($key_val)) $map['b.'.$this->lan.'_name'] = ['like', '%'.$key_val.'%'];
  93. $list = $productMarket->alias('a')
  94. ->join("product_list b", "a.product_id = b.id", "left")
  95. ->join("user_collect c", "c.market_id = a.id", "left")
  96. ->field('a.id,'.'b.'.$this->lan.'_name as name,b.thum as img_url,a.price,a.product_id,a.type_id, count(c.market_id) as collect')
  97. ->where('a.status', $productMarket::Normal)
  98. ->where($map)
  99. ->order($order)
  100. ->paginate($this->pageSize);
  101. $list = $this->getProductOrderList($list, $this->auth->id, false, $orderLogic);
  102. $this->success('', $list);
  103. }
  104. /**
  105. * 寄售转让详情
  106. * sort: 0 默认排序 1价格从高到低 2价格从低到高
  107. * type_id 分类
  108. * key_val 商品名称搜索
  109. */
  110. public function getTransferDetail(ProductMarket $productMarket, OrderLogic $orderLogic, ProductOrder $productOrder)
  111. {
  112. $ids = $this->request->post('ids/d', '');
  113. $list = $productMarket->with('products,producttransfer')
  114. ->where('product_market.id', $ids)
  115. ->where('product_market.status', $productMarket::Normal)
  116. ->find();
  117. if(empty($list)) $this->error('数据不存在');
  118. $list->issue = $orderLogic::getProductIssue($list->product_id); //发行: 是产品的库存总量(卖出和没卖出的都算,最保险的计算方式是剩余库存量+所有用户的持有量;因为空投产品不是从库存出去的
  119. $list->circulation = $orderLogic::getProductCirculation($list->product_id); //流通: 所有用户的持有量
  120. $this->success('', $list);
  121. }
  122. private static function getProductOrderList(object $list, int $uid, bool $isCollect, OrderLogic $orderLogic): object
  123. {
  124. if($list){
  125. $userCollect = Loader::model('UserCollect');
  126. foreach ($list as &$item) {
  127. if($isCollect) $item['collect'] = $userCollect::where('user_id', $uid)->where('market_id', $item->id)->count();
  128. $item['issue'] = $orderLogic::getProductIssue($item->product_id); //发行: 是产品的库存总量(卖出和没卖出的都算,最保险的计算方式是剩余库存量+所有用户的持有量;因为空投产品不是从库存出去的
  129. $item['circulation'] = $orderLogic::getProductCirculation($item->product_id); //流通: 所有用户的持有量
  130. }
  131. }
  132. return $list;
  133. }
  134. //获取分类
  135. public function getPopularType()
  136. {
  137. return $this->success('', ProductsModel::getProductTypeById($this->lan));
  138. }
  139. }