<?php
declare (strict_types = 1);

namespace app\model\api;

use think\Model;

/**
 * @mixin \think\Model
 */
class ShowTemplate extends Model
{
    
    public function getDataList($post,$field="*",$is_admin=0){
        $post["pageSize"] = $post["pageSize"]>50 ? 50 : (int)$post["pageSize"];
        $post["page"]     = $post["page"]<=0     ? 1  : (int)$post["page"];
        $where=[];
        if(isset($post["status"]) && in_array((string)$post["status"], ["0","1"])){
            $where[]=["status","=",(int)$post["status"]];
        }
        if(!empty($post["title"])){
            $where[]=["title","like","%{$post["title"]}%"];
        }
        if(isset($post["is_hot"]) && in_array((string)$post["is_hot"], ["0","1"])){
            $where[]=["is_hot","=",(int)$post["is_hot"]];
        }
        if(isset($post["is_recommend"]) && in_array((string)$post["is_recommend"], ["0","1"])){
            $where[]=["is_recommend","=",(int)$post["is_recommend"]];
        }
        $totalCount = $this->where($where)->count();
        $data=null;
        if($totalCount>0){
            $data = $this
                ->field($field)
                ->where($where)
                ->order("id", "desc")
                ->page($post["page"], $post["pageSize"])
                ->select();
            if(!empty($data)){
                $data = $data->toArray();
            }
        }
        $data = empty($data)?[]:$data;
        
        foreach($data as $k=>$v){
            $data[$k]["is_use"]      = 0;//是否已经购买或者可以使用
            $data[$k]["imgs"]        = getImageAr($v["imgs"]);
            $data[$k]["img"]         = empty($data[$k]["imgs"]) ? "" : $data[$k]["imgs"][0];
            $data[$k]["sales_count"] = $data[$k]["real_sales"] + $data[$k]["unreal_sales"];
            if($is_admin==0){
                unset($data[$k]["real_sales"]);
                unset($data[$k]["unreal_sales"]);
            }else{
                $data[$k]["time"]       = date("Y-m-d H:i:s",$v["time"]);
                $data[$k]["admin_time"] = date("Y-m-d H:i:s",$v["admin_time"]);
            }
        }
        
        
        return ["list" => $data, "pageSize" => $post["pageSize"],"page"=>$post["page"],"totalCount"=>$totalCount];
    }
    
    
}
