<?php


namespace App\Web\Models;



use App\Models\BaseModel;
use Illuminate\Support\Facades\DB;


class SuccessCaseModel extends BaseModel
{



    /**
     * 状态字段
     */
    const DELETED_AT = 'status';
    /**
     * @var string
     */
    protected $table = 'success_case';


    /**
     * 检查 标签是否唯一
     * */
    public function checkTitleUnique($title,$id=0){
        $where=[];
        if(!empty($id)){
            $where[]=['id', '<>', $id];
        }
        $where[]=['status', '<', 2];
        $where[]=['title', '=', trim($title)];
        return $this->checkFieldUnique('title',$where);
    }

    /**
     * 保存成功案例
     * @param array $data
     * */
    public function saveSuccessCaseData($data){
        if (!empty($data['id'])) {
            $id = $data['id'];
            $this->newInstance()->where('id', $data['id'])->update($data);
        } else {
            $id = $this->newInstance()->insertGetId($data);
        }
        return $id;
    }


    /**
     * 获取成功案例列表
     * */
    public function getSuccessCaseList($params,$fields=''){
        list($pageSize, $page, $skip) = $this->getPaginatorParams($params);
        $where=[];
        if(empty($fields)){
            $fields="a.*,b.name as 'function_name',c.name as 'industry_name' ";
        }
        if(isset($params['status'])){
            $where[]=['a.status','=',$params['status']];
        }else{
            $where[]=['a.status','<',2];
        }
        if(isset($params['is_handpick'])){
            $isHandpick=empty($params['is_handpick'])?0:$params['is_handpick'];
            $where[]=['a.is_handpick','=',$isHandpick];
        }
        if(!empty($params['function_id'])){
            $functionId=empty($params['function_id'])?0:$params['function_id'];
            $where[]=['a.function_id','=',$functionId];
        }
        if(!empty($params['industry_id'])){
            $industryId=empty($params['industry_id'])?0:$params['industry_id'];
            $where[]=['a.industry_id','=',$industryId];
        }
        $query= $this->newInstance()->alias('a')->where($where);
        if (!empty($params['keyword'])) {
            $keyword = $params['keyword'];
            $query->where('a.title', 'like', "%" . $keyword . "%");
        }
        $totalCount = $query->count();
        $list= $query->leftJoin('dict_function as b', 'a.function_id', '=', 'b.id')
            ->leftJoin('dict_industry as c', 'a.industry_id', '=', 'c.id')
            ->skip($skip)
            ->limit($pageSize)
            ->selectRaw($fields)
            ->orderBy('a.sort')
            ->get();
        if(!empty($list)){
            $list=$list->toArray();
        }else{
            $list=[];
        }
        $result = $this->buildPaginator($list, $skip, $page, $pageSize, $totalCount);
        return $result;
    }

    /**
     * 获取成功案例详情
     * @param array $params
     * */
    public function getSuccessCaseInfo($params=[]){
        if(empty($params)){
            return [];
        }
        $where=[];
        $where[]=['a.status','<',2];
        if(!empty($params['id'])){
            $where['a.id']=$params['id'];
        }
        $info= $this->newInstance()->alias('a')
            ->leftJoin('dict_function as b', 'a.function_id', '=', 'b.id')
            ->leftJoin('dict_industry as c', 'a.industry_id', '=', 'c.id')
            ->where($where)
            ->selectRaw("a.*,b.name as 'function_name',c.name as 'industry_name' ")
            ->first();
        if(!empty($info)){
            return $info->toArray();
        }else{
            return [];
        }
    }




}
