// +---------------------------------------------------------------------- declare (strict_types=1); namespace app\store\controller; use think\db\exception\DbException; use think\response\Json; use cores\exception\BaseException; use app\store\model\Goods as GoodsModel; use app\store\model\goods\Import as ImportModel; use app\common\model\GoodsCategoryRel; /** * 商品管理控制器 * Class Goods * @package app\store\controller */ class Goods extends Controller { private static $show_content = "***"; /** * 商品列表 * @return Json * @throws DbException */ public function list(): Json { // 获取列表记录 $model = new GoodsModel; $params = $this->request->param(); //$params['channel'] = 'zy'; if (!isset($params['merchant_id']) || !$params['merchant_id']) { $params['merchant_id'] = $this->merchantId; } $list = $model->getList($params, (int)$this->request->param('pageSize', 15)); if ($list->isEmpty()) { return $this->renderSuccess(compact('list')); } $list = $list->toArray(); foreach ($list['data'] as &$value) { if (!in_array($value['channel'], ['zy'])) { // $value['goods_price_min'] = self::$show_content; // $value['goods_price_max'] = self::$show_content; // $value['line_price_max'] = self::$show_content; // $value['line_price_min'] = self::$show_content; // $value['cost_price_min'] = self::$show_content; $value['goods_no'] = self::$show_content; } } 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); if (!in_array($goodsInfo['channel'], ['zy'])) { // $goodsInfo['goods_price_min'] = self::$show_content; // $goodsInfo['goods_price_max'] = self::$show_content; // $goodsInfo['line_price_max'] = self::$show_content; // $goodsInfo['line_price_min'] = self::$show_content; // $goodsInfo['cost_price_min'] = self::$show_content; $goodsInfo['goods_no'] = self::$show_content; // foreach ($goodsInfo['skuList'] as $key => &$value) { // $value['goods_price'] = self::$show_content; // $value['cost_price'] = self::$show_content; // } } 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; $postform = $this->postForm(); $postform['merchant_id'] = $this->merchantId; if ($model->add($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); $params = $this->postForm(); if ($model->isEmpty()) { return $this->renderError("该商品已不存在!"); } if (!in_array($model->channel, ['zy'])) { $params['goods_price'] = $model->goods_price_min; $params['cost_price'] = $model->cost_price_min; $params['goods_no'] = $model->goods_no; } // echo "
";
        // print_r($params);
        // print_r($model->toArray());
        // exit();
        // 更新记录
        if ($model->edit($params)) {
            return $this->renderSuccess('更新成功');
        }
        return $this->renderError($model->getError() ?: '更新失败');
    }
    /**
     * 商品入池、出池
     * @param array $goodsIds 商品id集
     * @param bool $state 为true表示上架
     * @return Json
     */
    public function pool(array $goodsIds, bool $state): Json
    {
        $model = new GoodsModel;
        //过滤其他渠道的商品,只能把自营商品加入商品池
        $list = GoodsModel::whereIn('goods_id', $goodsIds)->where('channel','zy')->select();
        if (!$list) {
            return $this->renderError("没有需要加入商品池的自营商品");
        }
        $goodsIds = array_column($list->toArray(), "goods_id");
        $state = $state ? 1 : 2;
        if (!$model->setIsPool($goodsIds, $state)) {
            return $this->renderError($model->getError() ?: '操作失败');
        }
        return $this->renderSuccess('操作成功');
    }
    /**
     * 修改商品状态(上下架)
     * @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 商品id集
     * @param bool $state 为true表示上架
     * @return Json
     */
    public function setBrand(array $goodsIds, bool $is_brand): Json
    {
        $model = new GoodsModel;
        if (!$model->setBrand($goodsIds, $is_brand)) {
            return $this->renderError($model->getError() ?: '操作失败');
        }
        return $this->renderSuccess('操作成功');
    }
    /**
     * 修改商品状态(上下架)
     * @param array $goodsIds 商品id集
     * @param bool $state 为true表示上架
     * @return Json
     */
    public function setNew(array $goodsIds, bool $is_new): Json
    {
        $model = new GoodsModel;
        if (!$model->setNew($goodsIds, $is_new)) {
            return $this->renderError($model->getError() ?: '操作失败');
        }
        return $this->renderSuccess('操作成功');
    }
    /**
     * 修改商品状态(上下架)
     * @param array $goodsIds 商品id集
     * @param bool $state 为true表示上架
     * @return Json
     */
    public function setInstore(array $goodsIds, bool $is_in_store): Json
    {
        $model = new GoodsModel;
        if (!$model->setInstore($goodsIds, $is_in_store)) {
            return $this->renderError($model->getError() ?: '操作失败');
        }
        return $this->renderSuccess('操作成功');
    }
    /**
     * 修改商品状态(上下架)
     * @param array $goodsIds 商品id集
     * @param bool $state 为true表示上架
     * @return Json
     */
    public function setRank(array $goodsIds, int $paihang): Json
    {
        $model = new GoodsModel;
        if (!$model->setRank($goodsIds, $paihang)) {
            return $this->renderError($model->getError() ?: '操作失败');
        }
        return $this->renderSuccess('操作成功');
    }
    /**
     * 商品归类
     * @param array $goodsIds 商品id集
     * @param bool $state 为true表示上架
     * @return Json
     */
    public function category(array $goodsIds, array $categoryIds): Json
    {
        $model = new GoodsModel;

        foreach ($goodsIds as $key => $goodsId) {
            $categoryIds = $model->dealCategory($categoryIds);
            \app\store\model\GoodsCategoryRel::updates($goodsId, $categoryIds);
        }
        $model->whereIn('goods_id', $goodsIds)->update(['cate_status'=>1,'update_time' => time()]);
        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('删除成功');
    }
    public function export(){
        ini_set('memory_limit', '2024M');
        set_time_limit(0);
        $model = new GoodsModel;
        $params = $this->request->param();
        $params['store_id'] = $this->storeId;
        $perSize = 10000;
        $params['page'] = 1;
        if ($this->merchantId) {
            $params['merchantId'] = $this->merchantId;
        }
        //$params['channels'] = ['zy'];
        // echo "
";
        // print_r($params);
        //exit();
        $data = $model->getAdminListExport($params, $perSize)->toArray();
        // echo "
";
        // print_r($data['data']);
        // exit();
        if ($data['data']) {
            foreach ($data['data'] as &$value) {
                if (!in_array($value['channel'], ['zy'])) {
                    $value['goods_price_min'] = self::$show_content;
                    $value['goods_price_max'] = self::$show_content;
                    $value['line_price_max'] = self::$show_content;
                    $value['line_price_min'] = self::$show_content;
                    $value['cost_price_min'] = self::$show_content;
                    $value['goods_no'] = self::$show_content;
                }
                $cates = GoodsCategoryRel::where('goods_id', $value['goods_id'])->select()->toArray();
                $value['category_id'] = $cates ? implode(",", array_column($cates, "category_id")) : "";
            }
        }
        
        // echo "
";
        // print_r($data);
        // exit();
        $titles = [
            ['goods_id'=>'系统编码(禁止修改)'],
            ['goods_name'=>'标题'],
            ['cmmdty_model'=>'商品型号'],
            ['goods_source'=>'商品来源(GC:工厂 CC:仓储 ZC:自采 填其中一项)'],
            ['goods_no'=>'商品编码(电商平台同款编码)'],
            ['category_id'=>'分类(填写1级到现有分类代码,中间用英文小写的逗号分开)'],
            ['delivery_time'=>'发货时效(0:24小时 1:48小时 3:72小时 4:7天内 5:15天内 6:30天内 7:45天内 填写其中一项)'],
            ['is_check'=>'是否审单(是填1 否填0)'],
            ['delivery_id'=>'运费模板ID'],
            ['status'=>'商品状态 (下架填写20 上架填写10)'],
            ['cost_price_min'=>'成本价'],
            ['goods_price_min'=>'前台价'],
            ['stock_total'=>'库存量'],
            // ['stock_total'=>'库存量'],
            ['remark'=>'下单须知'],
            ['is_use_jd_stock'=>'是否使用京东库存(是填1 否填0)'],
        ];
        
        downLoadExcel('导出数据-'.date('Y-m-d', time()),$titles,$data['data']);
    }

    public function import(){
        // 新增记录
        $model = new ImportModel;
        $params = $this->postData();
        $params['channel'] = "zy";
        if ($model->storeGoodsUpdateBatch($params)) {
            return $this->renderSuccess('批量修改成功');
        }
        return $this->renderError($model->getError() ?: '操作失败');

    }
    /**
     * 商品抓取
     * [collector description]
     * @return [type] [description]
     */
    public function collector(string $sku): Json{
        $res = getJdGoodsBySku([$sku]);
        // $collector = new \app\job\service\goods\Collector;
        // $url = "https://item.jd.com/{$sku}.html";
        // $res1 = $collector->collector($url, 10048);
        
        return $this->renderSuccess($res[$sku] ?? []);
    }


    //商品一键上架
    public function oneClickShelf(): Json{

        $model = new GoodsModel;
        GoodsModel::update(['status'=>10]);
        return $this->renderSuccess('操作成功');
    }
    public function oneClickOffTheShelf(): Json{
        $model = new GoodsModel;
        GoodsModel::update(['status'=>20]);
        return $this->renderSuccess('操作成功');
    }


}