<?php
/**
 * @Created by PhpStorm
 * @author: Kirin
 * @day: 2024/11/20
 * @time: 11:24
 */

namespace app\common;


use app\Request;
use qiniu\basic\BaseController;
use qiniu\exceptions\AdminException;

abstract class StoreBaseController extends BaseController
{


    /**
     * 当前登陆管理员信息
     * @var
     */
    protected $adminInfo;

    /**
     * 当前登陆管理员ID
     * @var
     */
    protected $adminId;

    protected $storeId;

    protected $storeInfo;

    protected $auth = [];

    // 搜索条件
    protected $searchable = [];
    // 搜索处理
    protected $searchDeal = null;

    // 创建参数
    protected $createParams = [];
    // 新增处理
    protected $saveDeal = null;
    // 更新处理
    protected $updateDeal = null;

    protected $validate = null;

    protected $service = null;

    protected $with = [];

    protected $storeSearch = false;

    /**
     * 初始化
     */

    public function __construct(Request $request)
    {
        parent::__construct($request);
        $this->initialize();
    }

    protected function initialize()
    {
        $this->adminId = $this->request->adminId();
        $this->adminInfo = $this->request->adminInfo();
        $this->storeId = $this->request->adminStoreId();
        $this->storeInfo = $this->request->adminStoreInfo();
        $this->auth = $this->adminInfo['rule'] ?? [];
    }


    public function index()
    {
        if (!$this->service) {
            throw new AdminException('接口不存在');
        }
        $where = $this->request->getMore($this->searchable, false, $this->searchDeal);
        if ($this->storeSearch) $where['store_id'] = $this->storeId;
        list($page, $limit) = $this->service->getPageValue();
        $list = $this->service->getList($where, '*', $page, $limit, $this->with);
        $count = $this->service->getCount($where);
        return $this->success(compact('list', 'count'));
    }


    public function read($id)
    {
        if (!$this->service) {
            throw new AdminException('接口不存在');
        }
        if ($this->storeSearch) {
            $info = $this->service->getOne(['id' => $id, 'store_id' => $this->storeId], '*', $this->with);
        } else {
            $info = $this->service->get($id, '*', $this->with);
        }
        if (!$info)
            return $this->error('数据不存在');
        return $this->success('ok', $info->toArray());
    }


    public function save()
    {
        if (!$this->service) {
            throw new AdminException('接口不存在');
        }
        $data = $this->request->postMore($this->createParams, false, $this->saveDeal);
        if ($this->validate) {
            $this->validate($data, $this->validate, 'save');
        }
        $res = $this->service->create($data);
        if ($res) return $this->success('添加成功');
        return $this->error('添加失败');
    }

    public function validate($data, $validate, $s = '')
    {
        $scene = method_exists($validate, 'allScene') ? ($validate->allScene() ?? []) : [];
        if (in_array($s, $scene))
            $res = $validate->scene($s)->check($data);
        else
            $res = $validate->check($data);
        if (!$res) throw new AdminException($validate->getError());
    }


    public function update($id)
    {
        if (!$this->service) {
            throw new AdminException('接口不存在');
        }
        if ($this->storeSearch) {
            $info = $this->service->getOne(['id' => $id, 'store_id' => $this->storeId], '*', $this->with);
        } else {
            $info = $this->service->get($id);
        }
        if (!$info)
            return $this->error('数据不存在');
        $data = $this->request->postMore($this->createParams, false, $this->updateDeal, $id);
        if ($this->validate) {
            $this->validate($data, $this->validate, 'update');
        }
        $res = $this->service->update($id, $data);
        if ($res) return $this->success('修改成功');
        return $this->error('修改失败');
    }


    public function delete($id)
    {
        if (!$this->service) {
            throw new AdminException('接口不存在');
        }
        if ($this->storeSearch) {
            $info = $this->service->getOne(['id' => $id, 'store_id' => $this->storeId], '*', $this->with);
        } else {
            $info = $this->service->get($id);
        }
        if (!$info)
            return $this->error('数据不存在');
        $res = $this->service->delete($id);
        if ($res) return $this->success('已删除');
        return $this->error('删除失败');
    }

    public function export()
    {
        if (!$this->service) {
            throw new AdminException('接口不存在');
        }
        $where = $this->request->getMore($this->searchable, false, $this->searchDeal);
        if ($this->storeSearch) $where['store_id'] = $this->storeId;
        $export_type = sys_config('export_type', 1);
        return $this->success($this->service->export($where, $export_type));
    }
}