Product.php 6.2 KB

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