<?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;
    }

}
