<?php

namespace app\common\api;

use app\common\model\MessageRecord;

class MessageRecordApi {

    public static function getList($params = []) {
        $where = [];
        $name = $params["name"];
        $phone = $params["phone"];
        $type = $params["type"];
        $state = $params["state"];
        $order = $params["order"] ?: "desc";
        $offset = $params["offset"] ?: 0;
        $limit = $params["limit"] ?: 10;
        if ($name) {
            $where[] = ["name", "like", "%{$name}%"];
        }
        if($phone){
            $where[] = ["phone", "like", "%{$phone}%"];
        }
        if($type){
            $where[] = ["type", "=", "{$type}"];
        }
        if($state){
            $where[] = ["type", "=", "{$state}"];
        }
        $count = MessageRecord::whereOr($where)->count();
        $list = MessageRecord::whereOr($where)
            ->order(["createTime" => $order])
            ->limit($offset, $limit)
            ->select()->toArray();
        return ["total" => $count, "rows" => $list];
    }
}

