SiteProduct.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\model\api;
  4. use library\traits\JwtAuthModelTrait;
  5. use library\traits\ModelTrait;
  6. use think\Exception;
  7. use think\Model;
  8. /**
  9. * @mixin \think\Model
  10. */
  11. class SiteProduct extends Model
  12. {
  13. use ModelTrait;
  14. use JwtAuthModelTrait;
  15. private $sassid;
  16. public function setSass($sassid) {
  17. $this->sassid = $sassid;
  18. }
  19. /**
  20. * 获取排行榜
  21. * @param $page
  22. * @param array $where
  23. * @param int $pageCount
  24. */
  25. public function SortList($page = 1,$where = [],$pageCount = 6){
  26. return $this->field("sp.id,p.is_host,p.title,img,p.code,sp.price,sp.sales,sp.ver_bug_count,p.count,p.wget")
  27. ->alias("sp")
  28. ->join("product p","p.id = sp.p_id","left")
  29. ->where('sp.status',1)
  30. ->where('sp.sassid',$this->sassid)
  31. ->order("p.seq","desc")
  32. ->page($page,$pageCount)
  33. ->select()
  34. ->toArray();
  35. }
  36. /**
  37. * 新品上市
  38. * @param $page
  39. * @param array $where
  40. * @param int $pageCount
  41. */
  42. public function NewsList($page = 1,$where = [],$pageCount = 12){
  43. return $this->field("sp.id,p.title,img,sp.price,sp.sales,sp.ver_bug_count,p.count,p.wget")
  44. ->alias("sp")
  45. ->join("product p","p.id = sp.p_id","left")
  46. ->where('sp.status',1)
  47. ->where('sp.is_new',1)
  48. ->where('sp.sassid',$this->sassid)
  49. ->order("sp.id","desc")
  50. ->page($page,$pageCount)
  51. ->select()
  52. ->toArray();
  53. }
  54. /**
  55. * 获取列表数据
  56. * @param $page
  57. * @param $where
  58. * @param $pageCount
  59. * @param $desc
  60. */
  61. public function getList($page = 1,$where = [],$pageCount = 20){
  62. $data =
  63. $this->field("sp.id,p.title,img,sp.price,sp.sales,sp.ver_bug_count,p.count,p.wget,p.warehouse_ids")
  64. ->alias("sp")
  65. ->join("product p","p.id = sp.p_id","left")
  66. ->where('sp.status',1)
  67. ->when(1 == 1,function ($query) use($where){
  68. if($where['type'] == 'all') {
  69. $query->order("sp.price");
  70. }
  71. if($where['countType'] == 1) {
  72. $query->where('p.count','>',0);
  73. }
  74. if($where['type'] == 'price') {
  75. $query->order("sp.price");
  76. }
  77. if($where['type'] == 'wget') {
  78. $query->order("p.wget");
  79. }
  80. if(!empty($where['warehouse'])) {
  81. $wIds = [];
  82. try{
  83. $wIds = explode(',',$where['warehouse']);
  84. } catch (\Throwable $e) {
  85. $wIds[] = $where['warehouse'];
  86. }
  87. $rawAr = [];
  88. foreach ($wIds as $k=>$v) {
  89. $rawAr[] = "FIND_IN_SET('".$v."',p.warehouse_ids)";
  90. }
  91. $query->whereRaw(join(' OR ',$rawAr));
  92. }
  93. })
  94. ->where('sp.sassid',$this->sassid)
  95. ->paginate(['list_rows'=>$pageCount,'page'=>$page])
  96. ->toArray();
  97. return [$data['total'],$data['data']];
  98. }
  99. /**
  100. * 基础配置信息
  101. * @param $id
  102. */
  103. public function getItem($id) {
  104. $data = $this
  105. ->field("p.id,p.title,img,sp.price,p.commission,sp.sales,sp.ver_bug_count,p.count,p.wget,p.warehouse_ids,p.desc")
  106. ->alias("sp")
  107. ->join("product p","p.id = sp.p_id","left")
  108. ->where('sp.id',$id)
  109. ->find();
  110. if(empty($data)) {
  111. return null;
  112. }
  113. $tAr = $data->getData();
  114. if(!empty($data['warehouse_ids'])) {
  115. $warehouse = (new Warehouse)->whereIn('id',$data['warehouse_ids'])->column('name');
  116. $tAr['warehouse'] = $warehouse;
  117. } else {
  118. $tAr['warehouse'] = [];
  119. }
  120. $tAr['img'] = explode(',',$tAr['img']);
  121. return $tAr;
  122. }
  123. /**
  124. * 基础配置信息
  125. * @param $id
  126. */
  127. public function getProItem($id) {
  128. $data = $this
  129. ->field("p.id,p.csno,p.title,img,p.code,sp.price,sp.sales,sp.ver_bug_count,p.count,p.wget,p.warehouse_ids,p.desc")
  130. ->alias("sp")
  131. ->join("product p","p.id = sp.p_id","left")
  132. ->where('p.id',$id)
  133. ->find();
  134. if(empty($data)) {
  135. return null;
  136. }
  137. $tAr = $data->getData();
  138. if(!empty($data['warehouse_ids'])) {
  139. $warehouse = (new Warehouse)->whereIn('id',$data['warehouse_ids'])->column('name');
  140. $tAr['warehouse'] = $warehouse;
  141. } else {
  142. $tAr['warehouse'] = [];
  143. }
  144. $tAr['img'] = explode(',',$tAr['img']);
  145. return $tAr;
  146. }
  147. /**
  148. * 随机数据
  149. * @param $size
  150. */
  151. public function getRand($size) {
  152. $data =
  153. $this->field("sp.id,p.title,img,sp.price,sp.sales,sp.ver_bug_count,p.count,p.wget")
  154. ->alias("sp")
  155. ->join("product p","p.id = sp.p_id","left")
  156. ->where('sp.status',1)
  157. ->where('sp.sassid',$this->sassid)
  158. ->whereRaw("sp.id >= (SELECT floor(RAND() * (SELECT MAX(id) FROM `table_site_product` where status = 1 and sassid =" . $this->sassid . " )) )")
  159. ->limit($size)
  160. ->select()
  161. ->toArray();
  162. return $data;
  163. }
  164. }