<?php
// +----------------------------------------------------------------------
// | likeadmin快速开发前后端分离管理后台（PHP版）
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码，建议反馈是我们前进的动力
// | 开源版本可自由商用，可去除界面版权logo
// | gitee下载：https://gitee.com/likeshop_gitee/likeadmin
// | github下载：https://github.com/likeshop-github/likeadmin
// | 访问官网：https://www.likeadmin.cn
// | likeadmin团队 版权所有 拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeadminTeam
// +----------------------------------------------------------------------

namespace app\adminapi\lists\fault_type;


use app\adminapi\lists\BaseAdminDataLists;
use app\common\model\fault_type\FaultType;
use app\common\lists\ListsSearchInterface;


/**
 * FaultType列表
 * Class FaultTypeLists
 * @package app\adminapi\lists
 */
class FaultTypeLists extends BaseAdminDataLists implements ListsSearchInterface
{


    /**
     * @notes 设置搜索条件
     * @return \string[][]
     * @author likeadmin
     * @date 2024/10/18 15:09
     */
    public function setSearch(): array
    {
        return [
            '=' => ['pid', 'sort', 'level', 'goods_category_id'],
        ];
    }

    public function queryWhere()
    {
        $where = [];
        if (isset($this->params['status']) && $this->params['status'] != '') {
            $where[] = ['a.status', '=', $this->params['status']];
        }
        if (!empty($this->params['name'])) {
            $where[] = ['a.name', 'like', '%' . $this->params['name'] . '%'];
        }
        if (!empty($this->params['category_name'])) {
            $where[] = ['b.name', 'like', '%' . $this->params['category_name'] . '%'];
        }
        if (!empty($this->params['category_type'])) {
            $where[] = ['a.category_type', '=', $this->params['category_type']];
        }
        $where[] = ['a.pid', '=', 0];
        return $where;
    }


    /**
     * @notes 获取列表
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @author likeadmin
     * @date 2024/10/18 15:09
     */
    public function lists(): array
    {
        $lists = FaultType::alias("a")
            ->with(['children'])
            ->leftJoin("goods_category b", "a.goods_category_id = b.id")
            ->where($this->searchWhere)
            ->where($this->queryWhere())
            ->field("a.*,b.name as category_name")
            ->limit($this->limitOffset, $this->limitLength)
            ->append(['status_desc'])
            ->order(['a.sort' => 'desc', 'a.id' => 'desc'])
            ->select()
            ->toArray();
        return $lists;
    }

    /**
     * @notes 获取数量
     * @return int
     * @author likeadmin
     * @date 2024/10/18 15:09
     */
    public function count(): int
    {
        return FaultType::alias("a")
            ->where($this->searchWhere)
            ->where($this->queryWhere())
            ->leftJoin("goods_category b", "a.goods_category_id = b.id")
            ->count();
    }

}