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

namespace app\admin\controller;

use think\facade\Db;
use think\response\Json;

/**
 * 商城管理
 * Class Store
 * @package app\admin\controller
 */
class Goods extends Controller
{
    /**
     * 强制验证当前访问的控制器方法method
     * @var array
     */
    protected array $methodRules = [
        'index' => 'GET',
        'recycle' => 'GET',
        'add' => 'POST',
        'move' => 'POST',
        'delete' => 'POST',
    ];

    /**
     * 商城列表
     * @return Json
     * @throws \think\db\exception\DbException
     */
    public function index(): Json
    {

        $params = $this->request->param();
        $sort = $this->request->param('sort', 'id');
        $order = $this->request->param('order', 'desc');
        $where = [];
        if (isset($params['channel']) && $params['channel']) {
            $where[] = ['channel', '=', $params['channel']];
        }
        if (isset($params['min_profit_rate']) && $params['min_profit_rate']) {
            $where[] = ['profit_rate', '>=', $params['min_profit_rate']];
        }
        if (isset($params['max_profit_rate']) && $params['max_profit_rate']) {
            $where[] = ['profit_rate', '<=', $params['max_profit_rate']];
        }
        if (isset($params['min_price']) && $params['min_price']) {
            $where[] = ['net_price', '>=', $params['min_price']];
        }
        if (isset($params['max_price']) && $params['max_price']) {
            $where[] = ['net_price', '<=', $params['max_price']];
        }
        if (!empty($params['catalog_name'])) {
            $where[] = ['catalog_name', 'like', "%{$params["catalog_name"]}%"];
        }
        $list = Db::connect("dataCenterMysql")->table('goods_sku')
            ->where($where)
            ->order($sort, $order)
            ->paginate($this->request->param('per_page', 15))->each(function ($item, $key) {
                $item['create_time'] = date("Y-m-d H:i:s", $item['create_time']);
                $item['update_time'] = date("Y-m-d H:i:s", $item['update_time']);
                $item['channel'] = config('app.platformList')[$item['channel']] ?? "";
                return $item;
            });
        return $this->renderSuccess(compact('list'));
    }


}