<?php
// +----------------------------------------------------------------------
// | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2023 https://www.yiovo.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
// +----------------------------------------------------------------------
// | Author: 萤火科技 <admin@yiovo.com>
// +----------------------------------------------------------------------
declare (strict_types=1);

namespace app\api\controller;

use app\api\service\User as UserService;
use app\store\model\article\Category as CategoryModel;
use app\store\model\Express as ExpressModel;
use app\store\model\store\Address as AddressModel;
use app\store\model\store\shop\Clerk as ClerkModel;
use app\store\service\order\Delivery as DeliveryService;
use cores\exception\BaseException;
use think\App;
use think\response\Json;
use app\api\service\Store as StoreService;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use app\store\model\Goods as GoodsModel;
use app\store\model\Order as OrderModel;
use app\store\model\OrderRefund as OrderRefundModel;
use app\store\model\dealer\Order as DealerOrderModel;
use app\store\model\Article as ArticleModel;
use app\store\model\goods\GoodsPrice as GoodsPriceModel;

/**
 * 店主管理
 * Class Store
 * @package app\api\controller
 */
class StoreKeeper extends Controller
{
    public function __construct(App $app)
    {
        parent::__construct($app);
        $res = UserService::isStore();
        if ($res === false) {
            print_r(json_encode(['code' => config('status.error'), 'msg' => '无权操作']));
            exit;
        }
    }

    /**
     * 商品列表
     * @return Json
     * @throws DbException
     */
    public function list(): Json
    {
        // 获取列表记录
        $model = new GoodsModel;
        $list = $model->getList($this->request->param());
        return $this->renderSuccess(compact('list'));
    }

    /**
     * 根据商品ID集获取列表记录
     * @param array $goodsIds
     * @return Json
     */
    public function listByIds(array $goodsIds): Json
    {
        // 获取列表记录
        $model = new GoodsModel;
        $list = $model->getListByIds($goodsIds);
        return $this->renderSuccess(compact('list'));
    }

    /**
     * 商品详情(详细信息)
     * @param int $goodsId
     * @return Json
     * @throws BaseException
     * @throws \think\db\exception\DataNotFoundException
     * @throws DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function detail(int $goodsId): Json
    {
        // 获取商品详情
        $model = new GoodsModel;
        $goodsInfo = $model->getDetail($goodsId);
        return $this->renderSuccess(compact('goodsInfo'));
    }

    /**
     * 商品详情(基础信息)
     * @param int $goodsId
     * @return Json
     * @throws BaseException
     * @throws \think\db\exception\DataNotFoundException
     * @throws DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function basic(int $goodsId): Json
    {
        // 获取商品详情
        $model = new GoodsModel;
        $detail = $model->getBasic($goodsId);
        return $this->renderSuccess(compact('detail'));
    }

    /**
     * 添加商品
     * @return Json
     * @throws BaseException
     * @throws \think\db\exception\DataNotFoundException
     * @throws DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function add(): Json
    {
        $model = new GoodsModel;
        if ($model->add($this->postForm())) {
            return $this->renderSuccess('添加成功');
        }
        return $this->renderError($model->getError() ?: '添加失败');
    }

    /**
     * 商品编辑
     * @param int $goodsId
     * @return Json
     * @throws BaseException
     * @throws \think\db\exception\DataNotFoundException
     * @throws DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function edit(int $goodsId): Json
    {
        // 商品详情
        $model = GoodsModel::detail($goodsId);
        // 更新记录
        if ($model->edit($this->postForm())) {
            return $this->renderSuccess('更新成功');
        }
        return $this->renderError($model->getError() ?: '更新失败');
    }

    /**
     * 修改商品状态(上下架)
     * @param array $goodsIds 商品id集
     * @param bool $state 为true表示上架
     * @return Json
     */
    public function state(array $goodsIds, bool $state): Json
    {
        $model = new GoodsModel;
        if (!$model->setStatus($goodsIds, $state)) {
            return $this->renderError($model->getError() ?: '操作失败');
        }
        return $this->renderSuccess('操作成功');
    }

    /**
     * 删除商品
     * @param array $goodsIds
     * @return Json
     */
    public function delete(array $goodsIds): Json
    {
        $model = new GoodsModel;
        if (!$model->setDelete($goodsIds)) {
            return $this->renderError($model->getError() ?: '删除失败');
        }
        return $this->renderSuccess('删除成功');
    }

    //订单相关

    /**
     * 订单列表
     * @param string $dataType
     * @return Json
     */
    public function orderList(string $dataType): Json
    {
        $params = $this->request->param();
        $params['searchType'] = 'all';
        // 订单列表
        if (!empty($params['dataType']) && $params['dataType'] == 'refund') {
            $model = new OrderRefundModel;
            $list = $model->getList($params);
            return $this->renderSuccess(compact('list'));
        } else {
            $model = new OrderModel;
            $list = $model->getList($params);
            return $this->renderSuccess(compact('dataType', 'list'));
        }

    }

    /**
     * 获取全部记录
     * @return Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function expressAll(): Json
    {
        $model = new ExpressModel;
        $list = $model->getAll($this->request->param());
        return $this->renderSuccess(compact('list'));
    }

    /**
     * 修改订单价格
     * @param int $orderId
     * @return Json
     */
    public function updatePrice(int $orderId): Json
    {
        // 订单详情
        $model = OrderModel::detail($orderId);
        if ($model->updatePrice($this->postForm())) {
            return $this->renderSuccess('操作成功');
        }
        return $this->renderError($model->getError() ?: '操作失败');
    }

    /**
     * 修改订单地址
     * @param int $orderId
     * @return Json
     */
    public function updateAddress(int $orderId): Json
    {
        // 订单详情
        $model = OrderModel::detail($orderId);
        if ($model->updateAddress($this->postForm())) {
            return $this->renderSuccess('操作成功');
        }
        return $this->renderError($model->getError() ?: '操作失败');
    }

    /**
     * 审核:用户取消订单
     * @param $orderId
     * @return Json
     */
    public function confirmCancel($orderId): Json
    {
        // 订单详情
        $model = OrderModel::detail($orderId);
        if ($model->confirmCancel($this->postForm())) {
            return $this->renderSuccess('操作成功');
        }
        return $this->renderError($model->getError() ?: '操作失败');
    }

    /**
     * 删除订单记录
     * @param int $orderId
     * @return Json
     */
    public function oderDelete(int $orderId): Json
    {
        // 订单详情
        $model = OrderModel::detail($orderId);
        // 确认核销
        if ($model->setDelete()) {
            return $this->renderSuccess('删除成功');
        }
        return $this->renderError($model->getError() ?: '操作失败');
    }

    /**
     * 门店自提核销
     * @param int $orderId
     * @return Json
     */
    public function extract(int $orderId): Json
    {
        // 订单详情
        $model = OrderModel::detail($orderId);
        if ($model->extractEvent($this->postForm())) {
            return $this->renderSuccess('核销成功');
        }
        return $this->renderError($model->getError() ?: '核销失败');
    }

    public function clerkAll(): Json
    {
        // 店员列表
        $model = new ClerkModel;
        $list = $model->getAll($this->request->param());
        return $this->renderSuccess(compact('list'));
    }

    /**
     * 确认发货 (手动录入)
     * @param int $orderId 订单ID
     * @return Json
     */
    public function delivery(int $orderId): Json
    {
        $service = new DeliveryService;
        if ($service->delivery($orderId, $this->postForm())) {
            return $this->renderSuccess('发货成功');
        }
        return $this->renderError($service->getError() ?: '发货失败');
    }

    /**
     * 商家审核
     * @param int $orderRefundId
     * @return Json
     */
    public function audit(int $orderRefundId): Json
    {
        // 售后单详情
        $model = OrderRefundModel::detail($orderRefundId);
        // 确认审核
        if ($model->audit($this->postForm())) {
            return $this->renderSuccess('操作成功');
        }
        return $this->renderError($model->getError() ?: '操作失败');
    }

    /**
     * 确认收货并退款
     * @param int $orderRefundId
     * @return Json
     */
    public function receipt(int $orderRefundId): Json
    {
        // 售后单详情
        $model = OrderRefundModel::detail($orderRefundId);
        // 确认收货并退款
        if ($model->receipt($this->postForm())) {
            return $this->renderSuccess('操作成功');
        }
        return $this->renderError($model->getError() ?: '操作失败');
    }

    /**
     * 全部记录
     * @return Json
     * @throws \think\db\exception\DbException
     */
    public function storeAddress(): Json
    {
        $model = new AddressModel;
        $list = $model->getAll($this->request->param());
        return $this->renderSuccess(compact('list'));
    }

    //分销

    /**
     * 分销订单列表
     * @return Json
     */
    public function dealerList(): Json
    {
        $model = new DealerOrderModel();
        $list = $model->getList($this->request->param());
        return $this->renderSuccess(compact('list'));
    }


    //文章管理

    /**
     * 文章分类列表
     * @return Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function articleCatList(): Json
    {
        $model = new CategoryModel;
        $list = $model->getList();
        return $this->renderSuccess(compact('list'));
    }

    /**
     * 添加文章分类
     * @return Json
     */
    public function articleCatAdd(): Json
    {
        // 新增记录
        $model = new CategoryModel;
        if ($model->add($this->postForm())) {
            return $this->renderSuccess('添加成功');
        }
        return $this->renderError($model->getError() ?: '添加失败');
    }

    /**
     * 编辑文章分类
     * @param int $categoryId
     * @return Json
     */
    public function articleCatEdit(int $categoryId): Json
    {
        // 分类详情
        $model = CategoryModel::detail($categoryId);
        // 更新记录
        if ($model->edit($this->postForm())) {
            return $this->renderSuccess('更新成功');
        }
        return $this->renderError($model->getError() ?: '更新失败');
    }

    /**
     * 删除文章分类
     * @param int $categoryId
     * @return Json
     */
    public function articleCatDelete(int $categoryId): Json
    {
        $model = CategoryModel::detail($categoryId);
        if (!$model->remove($categoryId)) {
            return $this->renderError($model->getError() ?: '删除失败');
        }
        return $this->renderSuccess('删除成功');
    }

    /**
     * 文章列表
     * @return Json
     * @throws \think\db\exception\DbException
     */
    public function articleList(): Json
    {
        $model = new ArticleModel;
        $list = $model->getList($this->request->param());
        return $this->renderSuccess(compact('list'));
    }


    /**
     * 文章详情
     * @param int $articleId
     * @return Json
     */
    public function articleDetail(int $articleId): Json
    {
        $detail = \app\store\model\Article::detail($articleId);
        // 获取image (这里不能用with因为编辑页需要image对象)
        !empty($detail) && $detail['image'];
        return $this->renderSuccess(compact('detail'));
    }

    /**
     * 添加文章
     * @return Json
     */
    public function articleAdd(): Json
    {
        // 新增记录
        $model = new ArticleModel;
        if ($model->add($this->postForm())) {
            return $this->renderSuccess('添加成功');
        }
        return $this->renderError($model->getError() ?: '添加失败');
    }

    /**
     * 更新文章
     * @param int $articleId
     * @return Json
     */
    public function articleEdit(int $articleId): Json
    {
        // 文章详情
        $model = ArticleModel::detail($articleId);
        // 更新记录
        if ($model->edit($this->postForm())) {
            return $this->renderSuccess('更新成功');
        }
        return $this->renderError($model->getError() ?: '更新失败');
    }

    /**
     * 删除文章
     * @param int $articleId
     * @return Json
     */
    public function articleDelete(int $articleId): Json
    {
        // 文章详情
        $model = ArticleModel::detail($articleId);
        if (!$model->setDelete()) {
            return $this->renderError($model->getError() ?: '删除失败');
        }
        return $this->renderSuccess('删除成功');
    }


    //会员、分销价格配置

    /**
     * 配置列表
     * @return Json
     * @throws \think\db\exception\DbException
     */
    public function priceList(): Json
    {
        $model = new GoodsPriceModel;
        $list = $model->getAll($this->request->param());
        return $this->renderSuccess(compact('list'));
    }

    public function addPrice(): Json
    {
        $model = new GoodsPriceModel;
        if (!$model->add($this->request->param())) {
            return $this->renderError($model->getError() ?: '添加失败');
        }
        return $this->renderSuccess('添加成功');
    }

    public function editPrice(): Json
    {
        $model = new GoodsPriceModel;
        if (!$model->edit($this->request->param())) {
            return $this->renderError($model->getError() ?: '添加失败');
        }
        return $this->renderSuccess('添加成功');
    }


}