123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187 |
- <?php
- declare (strict_types = 1);
- namespace app\model\api;
- use library\traits\JwtAuthModelTrait;
- use library\traits\ModelTrait;
- use think\Exception;
- use think\Model;
- /**
- * @mixin \think\Model
- */
- class SiteProduct extends Model
- {
- use ModelTrait;
- use JwtAuthModelTrait;
- private $sassid;
- public function setSass($sassid) {
- $this->sassid = $sassid;
- }
- /**
- * 获取排行榜
- * @param $page
- * @param array $where
- * @param int $pageCount
- */
- public function SortList($page = 1,$where = [],$pageCount = 6){
- 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")
- ->alias("sp")
- ->join("product p","p.id = sp.p_id","left")
- ->where('sp.status',1)
- ->where('sp.sassid',$this->sassid)
- ->order("p.seq","desc")
- ->page($page,$pageCount)
- ->select()
- ->toArray();
- }
- /**
- * 新品上市
- * @param $page
- * @param array $where
- * @param int $pageCount
- */
- public function NewsList($page = 1,$where = [],$pageCount = 12){
- return $this->field("sp.id,p.title,img,sp.price,sp.sales,sp.ver_bug_count,p.count,p.wget")
- ->alias("sp")
- ->join("product p","p.id = sp.p_id","left")
- ->where('sp.status',1)
- ->where('sp.is_new',1)
- ->where('sp.sassid',$this->sassid)
- ->order("sp.id","desc")
- ->page($page,$pageCount)
- ->select()
- ->toArray();
- }
- /**
- * 获取列表数据
- * @param $page
- * @param $where
- * @param $pageCount
- * @param $desc
- */
- public function getList($page = 1,$where = [],$pageCount = 20){
- $data =
- $this->field("sp.id,p.title,img,sp.price,sp.sales,sp.ver_bug_count,p.count,p.wget,p.warehouse_ids")
- ->alias("sp")
- ->join("product p","p.id = sp.p_id","left")
- ->where('sp.status',1)
- ->when(1 == 1,function ($query) use($where){
- if($where['type'] == 'all') {
- $query->order("sp.price");
- }
- if($where['countType'] == 1) {
- $query->where('p.count','>',0);
- }
- if($where['type'] == 'price') {
- $query->order("sp.price");
- }
- if($where['type'] == 'wget') {
- $query->order("p.wget");
- }
- if(!empty($where['warehouse'])) {
- $wIds = [];
- try{
- $wIds = explode(',',$where['warehouse']);
- } catch (\Throwable $e) {
- $wIds[] = $where['warehouse'];
- }
- $rawAr = [];
- foreach ($wIds as $k=>$v) {
- $rawAr[] = "FIND_IN_SET('".$v."',p.warehouse_ids)";
- }
- $query->whereRaw(join(' OR ',$rawAr));
- }
- })
- ->where('sp.sassid',$this->sassid)
- ->paginate(['list_rows'=>$pageCount,'page'=>$page])
- ->toArray();
- return [$data['total'],$data['data']];
- }
- /**
- * 基础配置信息
- * @param $id
- */
- public function getItem($id) {
- $data = $this
- ->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")
- ->alias("sp")
- ->join("product p","p.id = sp.p_id","left")
- ->where('sp.id',$id)
- ->find();
- if(empty($data)) {
- return null;
- }
- $tAr = $data->getData();
- if(!empty($data['warehouse_ids'])) {
- $warehouse = (new Warehouse)->whereIn('id',$data['warehouse_ids'])->column('name');
- $tAr['warehouse'] = $warehouse;
- } else {
- $tAr['warehouse'] = [];
- }
- $tAr['img'] = explode(',',$tAr['img']);
- return $tAr;
- }
- /**
- * 基础配置信息
- * @param $id
- */
- public function getProItem($id) {
- $data = $this
- ->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")
- ->alias("sp")
- ->join("product p","p.id = sp.p_id","left")
- ->where('p.id',$id)
- ->find();
- if(empty($data)) {
- return null;
- }
- $tAr = $data->getData();
- if(!empty($data['warehouse_ids'])) {
- $warehouse = (new Warehouse)->whereIn('id',$data['warehouse_ids'])->column('name');
- $tAr['warehouse'] = $warehouse;
- } else {
- $tAr['warehouse'] = [];
- }
- $tAr['img'] = explode(',',$tAr['img']);
- return $tAr;
- }
- /**
- * 随机数据
- * @param $size
- */
- public function getRand($size) {
- $data =
- $this->field("sp.id,p.title,img,sp.price,sp.sales,sp.ver_bug_count,p.count,p.wget")
- ->alias("sp")
- ->join("product p","p.id = sp.p_id","left")
- ->where('sp.status',1)
- ->where('sp.sassid',$this->sassid)
- ->whereRaw("sp.id >= (SELECT floor(RAND() * (SELECT MAX(id) FROM `table_site_product` where status = 1 and sassid =" . $this->sassid . " )) )")
- ->limit($size)
- ->select()
- ->toArray();
- return $data;
- }
- }
|