<?php
namespace app\repository;

use app\model\SlidePosition;
use Exception;
use think\Model;
use app\model\Slide;
use think\Collection;
use app\service\Repository;
use think\db\exception\DbException;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;

/**
 * 运营领域 相关
 *
 * Class OperationRepository
 * @package app\repository
 * @method self getInstance(Model $model = null) static
 */
class OperationRepository extends Repository
{
    /**
     * 轮播位置
     *
     * @return string[][]
     */
    public function slidePositions(): array
    {
        try {
            return SlidePosition::allPosition()->toArray();
        } catch (Exception $e) {
            return [];
        }
    }

    /**
     * 获取轮播
     *
     * @param  int  $id
     * @return array|Model|null
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function findSlideById(int $id)
    {
        return Slide::findById($id);
    }

    /**
     * 轮播列表
     *
     * @param array $where
     * @param array $fields
     * @param int $page
     * @param int $size
     * @param callable|null $call
     * @param array $orders
     * @return array
     * @throws Exception
     */
    public function slideList(array $where=[],  array $fields=[], int $page=1, int $size=20, callable $call=null, array $orders=[]): array
    {
        return Slide::findList($where, $fields, $page, $size, $call, $orders);
    }

    /**
     * 更新轮播
     *
     * @param  array  $data
     * @param  int  $id
     * @return bool
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function updateSlide(array $data, int $id): bool
    {
        $item = (new Slide())->where('id', $id)->find();
        if ($item) {
            return $item->save($data);
        }
        return false;
    }

    /**
     * 创建轮播
     *
     * @param array $data
     * @return Slide
     */
    public function createSlide(array $data): Slide
    {
        $data['created_at'] = date('y-m-d H:i:s');
        return Slide::create($data);
    }

    /**
     * 删除轮播图
     *
     * @param array $ids
     * @return bool
     */
    public function deleteSlides(array $ids): bool
    {
        return Slide::deleteByIds($ids);
    }

    /**
     * 轮播位置是否存在
     *
     * @param  string  $position
     * @param  int  $exceptId 需要排除的显示位置ID
     * @return bool
     */
    public function slidePositionExists(string $position, int $exceptId = 0): bool
    {
        return (new SlidePosition())->when($exceptId > 0, function ($q) use ($exceptId) {
                $q->where('id', '<>', $exceptId);
            })->where('key', $position)->count() > 0;
    }

    /**
     * 根据显示位置查询相关的轮播图信息
     *
     * @param string $position 轮播图位置标识
     * @param int $size 限制查询数量
     * @throws Exception
     */
    public function slideListByPosition(string $position, int $size=0): ?Collection
    {
        $where[] = ['position', '=', $position];
        $orders = ['sort'=>'asc'];
        return Slide::findList($where, [], 1, $size, null, $orders)['list'];
    }


    /**
     * 轮播显示位置列表
     *
     * @param array $where
     * @param array $fields
     * @param int $page
     * @param int $size
     * @param callable|null $call
     * @param array $orders
     * @return array
     * @throws Exception
     */
    public function slidePositionList(array $where=[],  array $fields=[], int $page=1, int $size=20, callable $call=null, array $orders=[]): array
    {
        return SlidePosition::findList($where, $fields, $page, $size, $call, $orders);
    }

    /**
     * 更新轮播位置
     *
     * @param  array  $data
     * @param  int  $id
     * @return bool
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function updateSlidePosition(array $data, int $id): bool
    {
        $item = (new SlidePosition())->where('id', $id)->find();
        if ($item) {
            $item->save($data);
        }
        return false;
    }

    /**
     * 创建轮播位置
     *
     * @param array $data
     * @return SlidePosition
     */
    public function createSlidePosition(array $data): SlidePosition
    {
        $data['created_at'] = date('y-m-d H:i:s');
        return SlidePosition::create($data);
    }

    /**
     * 删除轮播位置
     *
     * @param array $ids
     * @return bool
     */
    public function deleteSlidePositions(array $ids): bool
    {
        return SlidePosition::deleteByIds($ids);
    }

    /**
     * 获取轮播位置
     *
     * @param  int  $id
     * @return array|Model|null
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function findSlidePositionById(int $id)
    {
        return SlidePosition::findById($id);
    }

}