<?php

namespace app\workerapi\lists\shops;


use app\common\lists\ListsSearchInterface;
use app\common\model\shops\ShopCart;
use app\workerapi\lists\BaseWorkerDataLists;
use think\db\Query;

/**
 * ShopCartLists列表
 * Class ShopCartLists
 * @package app\api\lists\goods_category
 */
class ShopCartLists extends BaseWorkerDataLists implements ListsSearchInterface
{


    /**
     *
     * @return array[]
     */
    public function setSearch(): array
    {
        return [
            '=' => ['shop_goods_id', 'goods_specs_inventory_id'],

        ];
    }

    public function querySearch(): array
    {
        $where = [];
        $where[] = ['worker_id', '=', $this->userId];
        return $where;
    }

    /**
     * @notes 获取列表
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @author whitef
     * @date 2024/07/07 18:23
     */
    public function lists(): array
    {
        $lists = ShopCart::with([
            'goods' => function (Query $query) {
                $query->field(['id', 'goods_name', 'delivery_type', 'shop_goods_type', 'goods_image', 'goods_status', 'specs_type', 'custom_attribute_items', 'company_name',])->append(['delivery_type_text', 'shop_goods_type_text']);
            },
            'goodSpecsInventory' => function (Query $query) {
                $query->field(['id', 'remaining_inventory', 'specs', 'service_fee', 'service_total']);
            }
        ])->when(!empty($this->params['goods_name']), function ($query) {
            $query->hasWhere('goods', function (Query $query) {
                $query->where('goods_name', 'like', '%' . $this->params['goods_name'] . '%');
            });
        })
            ->where($this->searchWhere)
            ->where($this->querySearch())
            ->visible(['id', 'shop_goods_id', 'goods_specs_inventory_id', 'number'])
            ->limit($this->limitOffset, $this->limitLength)
            ->select()
            ->toArray();

        return $lists;
    }


    /**
     * @notes 获取数量
     * @return int
     * @author whitef
     * @date 2024/07/07 18:23
     */
    public function count(): int
    {

        return ShopCart::where($this->searchWhere)->where($this->querySearch())
            ->when(!empty($this->params['goods_name']), function (Query $query) {
                $query->hasWhere('goods', function (Query $query) {
                    $query->where('goods_name', 'like', '%' . $this->params['goods_name'] . '%');
                });
            })->count();
    }

}