<?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\shops;


use app\adminapi\lists\BaseAdminDataLists;
use app\common\model\shops\ShopCategory;
use app\common\model\shops\ShopGoods;
use app\common\lists\ListsSearchInterface;
use think\db\Query;


/**
 * ShopGoods列表
 * Class ShopGoodsLists
 * @package app\adminapi\listsshops
 */
class ShopGoodsLists extends BaseAdminDataLists implements ListsSearchInterface
{


    /**
     * @notes 设置搜索条件
     * @return \string[][]
     * @author likeadmin
     * @date 2024/08/04 11:07
     */
    public function setSearch(): array
    {
        return [
            '=' => ['delivery_type', 'shop_goods_type',  'goods_name', 'company_name', 'goods_status', 'is_recommend'],

        ];
    }

    public function queryWhere()
    {
        $where = [];
        if (!empty($this->params['goods_category_id'])) {
            $goodsCategoryId = end($this->params['goods_category_id']);
            $goodsCategoryData = ShopCategory::where(['status'=>1])->order(['pid' => 'asc','weigh' => 'desc', 'id' => 'desc'])
                ->select()->toArray();
            $ids =get_tree_ids($goodsCategoryData,$goodsCategoryId);
            $ids[] = $goodsCategoryId;
            $where[] = ['goods_category_id','in' ,$ids];
        }
        return $where;
    }
    /**
     * @notes 获取列表
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @author likeadmin
     * @date 2024/08/04 11:07
     */
    public function lists(): array
    {
        $lists =  ShopGoods::with(['goodsCategory','goodSpecsInventory'=>function (Query $query) {
                $query->field(['specs', 'inventory','shop_goods_id','remaining_inventory','service_fee','service_total']);
             }])
            ->where($this->searchWhere)
            ->where($this->queryWhere())
            ->field(['id', 'delivery_type', 'shop_goods_type', 'goods_category_ids', 'goods_category_id', 'goods_name', 'company_name',  'goods_image', 'goods_banners', 'description', 'goods_status', 'is_recommend', 'recommend_weight','specs_type','custom_attribute_items'])
            ->limit($this->limitOffset, $this->limitLength)
            ->order(['id' => 'desc'])
            ->select()
            ->toArray();
        foreach ($lists as &$item) {
            $item['goods_category_ids'] = array_map("intval",$item['goods_category_ids']);
        }
        return $lists;
    }


    /**
     * @notes 获取数量
     * @return int
     * @author likeadmin
     * @date 2024/08/04 11:07
     */
    public function count(): int
    {
        return ShopGoods::where($this->searchWhere)->where($this->queryWhere())->count();
    }

}