<?php
// +----------------------------------------------------------------------
// | onezan-admin 快速开发的管理平台
// +----------------------------------------------------------------------
// | 官方网址：https://www.onezan.cn
// +----------------------------------------------------------------------
// | onezan团队 版权所有 开源版本可自由商用
// +----------------------------------------------------------------------
// | Author: 543885307@qq.com
// +----------------------------------------------------------------------
namespace Applications\sample\plugins\notice\service\admin;

use app\service\admin\BaseAdminService;
use Applications\sample\plugins\notice\repository\NoticeRepository;
use think\Request;

/**
 * 示例应用公告插件后台服务
 * Class NoticeService
 * @package Applications\sample\plugins\notice\service\admin
 */
class NoticeService extends BaseAdminService
{
    /**
     * 获取公告分页列表
     * @param Request $request
     * @return array
     */
    public function list(Request $request): array
    {
        return [
            'code' => 0,
            'data' => $this->repository()->paginateForAdmin([
                'keyword' => $request->get('keyword', ''),
                'status' => $request->get('status'),
            ], (int)$request->get('page', 1), (int)$request->get('limit', 10)),
        ];
    }

    /**
     * 保存公告
     * @param Request $request
     * @return array
     */
    public function save(Request $request): array
    {
        $payload = [
            'id' => $request->post('id'),
            'title' => trim((string)$request->post('title', '')),
            'code' => trim((string)$request->post('code', '')),
            'content' => trim((string)$request->post('content', '')),
            'sort' => (int)$request->post('sort', 0),
            'status' => (int)$request->post('status', 1),
        ];
        $id = isset($payload['id']) && is_numeric($payload['id']) ? (int)$payload['id'] : 0;
        unset($payload['id']);

        if ($id > 0) {
            $notice = $this->repository()->findById($id);
            if (!$notice) {
                return ['code' => 1, 'msg' => '记录不存在'];
            }
            $this->repository()->save($notice, $payload);
            $saved = $this->repository()->findById($id);
        } else {
            $saved = $this->repository()->create($payload);
        }

        return [
            'code' => 0,
            'msg' => '保存成功',
            'data' => $saved ? $saved->toArray() : null,
        ];
    }

    /**
     * 删除公告
     * @param Request $request
     * @return array
     */
    public function delete(Request $request): array
    {
        $id = (int)$request->post('id', 0);
        if (!$this->repository()->deleteById($id)) {
            return ['code' => 1, 'msg' => '记录不存在'];
        }

        return ['code' => 0, 'msg' => '删除成功'];
    }

    /**
     * 获取仓储实例
     * @return NoticeRepository
     */
    private function repository(): NoticeRepository
    {
        return $this->app->make(NoticeRepository::class);
    }
}
