Product.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. namespace app\api\controller;
  3. use think\Db;
  4. use think\db\Expression;
  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 = ['*'];
  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, ProductOrder $productOrder)
  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['a.status'] = $productMarket::Normal;
  63. if(!empty($type_id)) $map['b.type_id'] = $type_id;
  64. if(!empty($key_val)) $map['b.'.$this->lan.'_name'] = ['like', '%'.$key_val.'%'];
  65. $list = $productMarket->alias('a')
  66. ->join("product_list b", "a.product_id = b.id", "left")
  67. ->field('a.id,'.'b.'.$this->lan.'_name as name,b.thum as img_url,a.price,a.product_id,a.type_id')
  68. ->where('a.status', $productMarket::Normal)
  69. ->where($map)
  70. ->order($order)
  71. ->paginate($this->pageSize);
  72. foreach ($list as &$item) {
  73. $item['issue'] = $orderLogic::getProductIssue($item->product_id); //发行: 是产品的库存总量(卖出和没卖出的都算,最保险的计算方式是剩余库存量+所有用户的持有量;因为空投产品不是从库存出去的
  74. $item['circulation'] = $productOrder::where('product_id', $item->product_id)->where('status', $productOrder::Paid)->count(); //流通: 所有用户的持有量
  75. }
  76. $this->success('', $list);
  77. }
  78. /**
  79. * 寄售转让详情
  80. * sort: 0 默认排序 1价格从高到低 2价格从低到高
  81. * type_id 分类
  82. * key_val 商品名称搜索
  83. */
  84. public function getTransferDetail(ProductMarket $productMarket, OrderLogic $orderLogic, ProductOrder $productOrder)
  85. {
  86. $ids = $this->request->post('ids/d', '');
  87. $list = $productMarket->with('products,producttransfer')
  88. ->where('product_market.id', $ids)
  89. ->where('product_market.status', $productMarket::Normal)
  90. ->find();
  91. if(empty($list)) $this->error('数据不存在');
  92. $list->issue = $orderLogic::getProductIssue($list->product_id); //发行: 是产品的库存总量(卖出和没卖出的都算,最保险的计算方式是剩余库存量+所有用户的持有量;因为空投产品不是从库存出去的
  93. $list->circulation = $productOrder::where('product_id', $list->product_id)->where('status', $productOrder::Paid)->count(); //流通: 所有用户的持有量
  94. $this->success('', $list);
  95. }
  96. //获取分类
  97. public function getPopularType()
  98. {
  99. return $this->success('', ProductsModel::getProductTypeById($this->lan));
  100. }
  101. }