<?php
namespace app\model;

/**
 * 产品快链管理
 *
 * Class LinkProduct
 * @package app\model
 */
class LinkProduct extends Base
{
    public static function delByIds($ids)
    {
        return self::whereIn('id', $ids)->delete();
    }

    //获取列表
    public static function getList($limit = 10, $where = [], $orders = ['sort'=>'asc'])
    {
        return self::order($orders)
            ->when(!empty($where), function ($q) use ($where) {
                $q->where($where);
            })
            ->when($limit > 0, function ($q) use ($limit) {
                $q->limit($limit);
            })
            ->select()
            ->toArray();
    }

    public static function onAfterInsert($item)
    {
        $item->sort  = $item->id;
        $item->save();
    }

    //获取涉及到的文件
    public static function getFilesInUse()
    {
        $items = self::select()->toArray();
        $data = [];
        foreach($items as $item){
            $src = trim($item['src']);
            if(!empty($src)){
                $key = getKeyByPath($src);
                $data[$key] = $src;
            }
        }
        return $data;
    }
}