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

namespace app\api\service;

use app\api\model\Store as StoreModel;
use \app\common\model\Store as StoreInfoModel;
use app\api\service\Client as ClientService;
use app\api\service\Setting as SettingService;
use app\api\model\store\Module as StoreModuleModel;
use app\api\model\dealer\Setting as DealerSettingModel;
use app\common\model\store\StoreSettle;
use app\common\service\BaseService;
use think\facade\Db;
use think\Exception;

/**
 * 商城基础信息
 * Class Store
 * @package app\api\service
 */
class Store extends BaseService
{
    /**
     * 获取商城基础信息
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function data(): array
    {
        return [
            // 店铺基本信息
            'storeInfo' => $this->storeInfo(),
            // 当前客户端名称
            'client' => \getPlatform(),
            // 商城设置
            'setting' => $this->setting(),
            // 分销设置 (仅基础设置)
            'dealer' => $this->dealerData(),
            // 客户端设置
            'clientData' => $this->getClientData(),
            // 开启的功能模块
            'modules' => $this->getModules(),
        ];
    }

    /**
     * 开启的功能模块
     * @return array|mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    private function getModules()
    {
        return StoreModuleModel::getModules($this->storeId);
    }

    /**
     * 客户端公共数据
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    private function getClientData(): array
    {
        $service = new ClientService;
        return $service->getPublic();
    }

    /**
     * 商城设置
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    private function setting(): array
    {
        $service = new SettingService;
        return $service->getPublic();
    }

    /**
     * 分销设置 (仅基础设置)
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    private function dealerData(): array
    {
        return ['setting' => DealerSettingModel::getPublic()['basic']];
    }

    /**
     * 店铺基本信息(名称、简介、logo)
     * @return StoreModel|array|null
     */
    private function storeInfo()
    {
        return StoreModel::getInfo();
    }

    //申请入驻
    public function joinStore($data, $user_id)
    {
        $has = Db::query('select * from yoshop_store where user_id = ' . $user_id . ' and status != 2 and is_recycle = 0 and is_delete= 0');
        if ($has) {
            return ['code' => 1, 'msg' => '请勿重复申请入驻'];
        }
        //商城主体信息
        $addData = [
            'store_name' => $data['store_name'],
            'user_id' => $user_id,
            'sort' => 100,
            'create_time' => time(),
            'update_time' => time(),
            'status' => 0
        ];
        try {
            // 新增记录
            $model = new \app\admin\model\Store;
            $res = $model->insertGetId($addData);
            if (!$res) {
                throw new Exception($model->getError());
            }
            //附加资料
            $settleData = [
                'store_id' => $res,
                'store_cat' => $data['store_cat'],
                'store_address' => $data['store_address'],
                'store_brand' => $data['store_brand'],
                'store_settle_type' => $data['store_settle_type'],
                'authorize' => $data['authorize'],
                'has_tax' => $data['has_tax'],
                'user_name' => $data['user_name'],
                'user_position' => $data['user_position'],
                'user_mobile' => $data['user_mobile'],
                'user_wx' => $data['user_wx'],
                'user_email' => $data['user_email'],
                'store_model' => $data['store_model'],
                'send_type' => $data['send_type'],
                'has_factory' => $data['has_factory'],
                'has_offline' => $data['has_offline'],
                'has_online_shop' => $data['has_online_shop'],
                'created_at' => time(),
                'store_type' => empty($data['has_online_shop']) ? json_encode([]) : json_encode($data['store_type']),
            ];
            $settleModel = new StoreSettle();
            if (!$settleModel->insert($settleData)) {
                throw new Exception($settleModel->getError());
            }
            return ['code' => 0, 'msg' => 'success'];
        } catch (\Exception $e) {
            return ['code' => 1, 'msg' => $e->getMessage()];
        }
    }

    public function joinData($user_id)
    {
        $storeInfo = Db::query('select * from yoshop_store where user_id = ' . $user_id . ' and  and is_recycle = 0 and is_delete= 0');
        $settle = [];
        if (!empty($storeInfo)) {
            $res = Db::query('select * from yoshop_store_settle where store_id = ' . $storeInfo[0]['store_id']);
            $settle = $res ? $res[0] : [];
            $settle['store_type'] = json_decode($settle['store_type']);
        }
        return [
            'info' => $storeInfo,
            'settle' => $settle,
        ];
    }

    public function getStore()
    {
        $storeList = Db::query('select shop_id,shop_name from yoshop_store_shop where status = 1  and is_delete= 0');
        return $storeList;
    }

}