// +---------------------------------------------------------------------- declare (strict_types=1); namespace app\admin\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\GoodsCategoryRel; use app\store\model\GoodsSku; use app\store\model\goods\Import as ImportModel; use app\job\controller\goods\GoodsAddPrice as GoodsAddPriceJob; use app\job\controller\goods\GoodsOffline as GoodsOfflineJob; use app\job\controller\goods\GoodsOnline as GoodsOnlineJob; use app\job\controller\goods\GoodsDelete as GoodsDeleteJob; /** * 商品管理控制器 * Class Goods * @package app\store\controller */ class Goods extends Controller { /** * 强制验证当前访问的控制器方法method * @var array */ protected array $allowAllAction = [ //"goods/export", ]; /** * 商品列表 * @return Json * @throws DbException */ public function list(int $pageSize = 15): Json { // 获取列表记录 $model = new GoodsModel; $params = $this->request->param(); $params['store_id'] = 0; //$params['role'] = $this->admin['user']['role']; $platform = $this->getUserPlatform(); $params['channels'] = $this->admin['user']['role'] == 0 ? [] :($platform ? array_column($platform->toArray(), "code") : []); $list= $model->getAdminList($params, $pageSize); 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; $params = $this->postForm(); $params['channel'] = $this->getUserAddPlatform(); if ($model->add($params)) { 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); // echo "
";
        // print_r($model);
        // exit();
        // 更新记录
        if ($model->edit($this->postForm(), $model)) {
            //更新来源数据为当前数据的商品
            

            $list = GoodsModel::where('origin_goods_id', $goodsId)->field('goods_id')->select();
            if (!$list) {
                return $this->renderSuccess('更新成功');
            }
            $goods_sku = GoodsModel::where('goods_id', $goodsId)->where('store_id', 0)->find();
            // var_dump($goods_sku->cost_price_min);
            // var_dump($goods_sku->markup_rate);
            //成本价加价之后的处理
            list($cost_price, $profit, $profit_rate) = getGoodsCostAndProfitAndProfitRate($goods_sku->goods_price_min, $goods_sku->cost_price_min, $goods_sku->markup_rate);
            $goods_sku->cost_price_min = $cost_price;
            $goods_sku->profit = $profit;
            $goods_sku->profit_rate = $profit_rate;
            
            // var_dump($cost_price);
            // var_dump($profit);
            // var_dump($profit_rate);
            // exit();
            $goods_data = [
                'goods_name' => $goods_sku->name,
                'content' => $goods_sku->content,
                'selling_point' => $goods_sku->selling_point,
                'goods_price_min' => $goods_sku->goods_price_min,
                'goods_price_max' => $goods_sku->goods_price_max,
                'line_price_min' => $goods_sku->line_price_min,
                'line_price_max' => $goods_sku->line_price_max,
                'cost_price_min' => $goods_sku->cost_price_min,
                'profit_rate' => $goods_sku->profit_rate,
                'profit' => $goods_sku->profit,
                'goods_source' => $goods_sku->goods_source,
                'is_check' => $goods_sku->is_check,
                'delivery_time' => $goods_sku->delivery_time,
                // 'is_pool' => $goods_sku->is_pool,
                // 'is_sale' => $goods_sku->is_sale,
                'update_time' => time(),
            ];
            if ($goods_sku->is_pool != 1 || $goods_sku->is_sale == 0) {
                $goods_data['status'] = 20;
                $goods_data['is_jd_remove'] = 1;
            }
            if ($goods_sku->is_pool == 1 && $goods_sku->is_sale == 1) {
                $goods_data['status'] = 10;
                $goods_data['is_jd_remove'] = 0;
            }
            GoodsModel::where('origin_goods_id', $goodsId)->update($goods_data);
            $goods_sku_data = [
                'goods_price' => $goods_sku->goods_price_min,
                'cost_price' => $goods_sku->cost_price_min,
                'update_time' => time(),
            ];

            
            GoodsSku::whereIn('goods_id', array_column($list->toArray(), "goods_id"))->update($goods_sku_data);
            return $this->renderSuccess('更新成功');
        }
        return $this->renderError($model->getError() ?: '更新失败');
    }

    /**
     * 修改商品状态(上下架)
     * @param array $goodsIds 商品id集
     * @param bool $state 为true表示上架
     * @return Json
     */
    public function state(array $goodsIds, int $is_pool): Json
    {
        $model = new GoodsModel;
        if (!$model->setIsPool($goodsIds, $is_pool)) {
            return $this->renderError($model->getError() ?: '操作失败');
        }
        // 分批每次导入20条
        $limit = 20;
        // 根据商品总数量计算需要的队列任务数量
        $jobCount = \count($goodsIds) / $limit;
        // 逐次发布队列任务
        for ($i = 0; $i < $jobCount; $i++) {
            $data = array_slice($goodsIds, $i * $limit, $limit);
            if ($is_pool == 2) {
                GoodsOfflineJob::dispatch([
                        'list' => $data,
                    ]);
                
            } elseif ($is_pool == 1) {
                GoodsOnlineJob::dispatch([
                        'list' => $data,
                    ]);
            }
        }
        return $this->renderSuccess('操作成功');
    }
    /**
     * 修改商品状态(在售、停售)
     * @param array $goodsIds 商品id集
     * @param bool $state 为true表示上架
     * @return Json
     */
    public function sale(array $goodsIds, int $is_sale): Json
    {
        
        $model = new GoodsModel;
        if (!$model->setIsSale($goodsIds, $is_sale)) {
            return $this->renderError($model->getError() ?: '操作失败');
        }
        // 分批每次导入20条
        $limit = 20;
        // 根据商品总数量计算需要的队列任务数量
        $jobCount = \count($goodsIds) / $limit;
        // 逐次发布队列任务
        for ($i = 0; $i < $jobCount; $i++) {
            $data = array_slice($goodsIds, $i * $limit, $limit);
            if ($is_sale == 0) {
                GoodsOfflineJob::dispatch([
                        'list' => $data,
                    ]);
                
            } elseif ($is_sale == 1) {
                GoodsOnlineJob::dispatch([
                        'list' => $data,
                    ]);
            }
        }
        return $this->renderSuccess('操作成功');
    }
    /**
     * 修改商品状态(上下架)
     * @param array $goodsIds 商品id集
     * @param bool $state 为true表示上架
     * @return Json
     */
    public function jingpin(array $goodsIds, int $is_jingpin): Json
    {
        $model = new GoodsModel;
        if (!$model->setIsJingpin($goodsIds, $is_jingpin)) {
            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() ?: '删除失败');
        }
        // 分批每次导入20条
        $limit = 20;
        // 根据商品总数量计算需要的队列任务数量
        $jobCount = \count($goodsIds) / $limit;
        // 逐次发布队列任务
        for ($i = 0; $i < $jobCount; $i++) {
            $data = array_slice($goodsIds, $i * $limit, $limit);
            GoodsDeleteJob::dispatch([
                    'list' => $data,
                ]);
        }
        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'] = 0;
        $perSize = 100000;
        $params['page'] = 1;
        $platform = $this->getUserPlatform();
        $params['channels'] = $platform ? array_column($platform->toArray(), "code") : [];
        $data = $model->getAdminListExport($params, $perSize)->toArray();
        // echo "
";
        // print_r($data);
        // exit();
        $titles = [
            ['goods_id'=>'系统编码'],
            ['goods_name'=>'标题'],
            ['cmmdty_model'=>'型号'],
            ['link'=>'该商品苏宁的链接'],
            ['cost_price_min'=>'成本价'],
            ['stock_total'=>'库存'],
            ['link_other'=>'京东的价拖链接'],
            ['goods_price_min'=>'前台价'],
            ['goods_no'=>'sku'],
            ['goods_source'=>'商品来源      (GC:工厂 CC:仓储 ZC:自采 填其中一项)'],
            ['delivery_time'=>'发货时效(0:24小时 1:48小时 2:72小时 3:7天内 4:15天内 5:30天内 6:45天内 填写其中一项)'],
            ['is_check'=>'是否审单(是填1 否填0)'],
        ];
        
        downLoadExcel('导出数据-'.date('Y-m-d', time()),$titles,$data['data']);
    }
    
    public function export1(){
        
        $model = new GoodsModel;
        $params = $this->request->param();
        $params['store_id'] = 0;
        $list = $model->getAdminListExport($params)->toArray();
        $accessNum = $list['total'];
        // echo $accessNum;
        // exit();
        set_time_limit(0);
        ini_set('memory_limit', '1024M');        
        $columns = ['系统编码','标题','型号','该商品苏宁的链接','成本价','库存','京东的价拖链接','前台价'];        //设置好告诉浏览器要下载excel文件的headers
        header('Content-Encoding: UTF-8');
        header('Content-Type: application/vnd.ms-excel;charset=UTF-8');
        header('Content-Disposition: attachment; filename="导出数据-'.date('Y-m-d', time()).'.csv"');

        $fp = fopen('php://output', 'a');//打开output流
        //添加BOM头,以UTF8编码导出CSV文件,如果文件头未添加BOM头,打开会出现乱码。
        fwrite($fp, chr(0xEF).chr(0xBB).chr(0xBF));

        //mb_convert_variables('GBK', 'UTF-8', $columns);
        fputcsv($fp, $columns);//将数据格式化为CSV格式并写入到output流中

        //获取总数,分页循环处理
        $accessNum = $list['total'];

        $perSize = 10000;
        $pages = ceil($accessNum / $perSize);
        // echo $pages;
        // exit();
        for($i = 1; $i <= $pages; $i++) {
            $params['page'] = $i;
            $db_data = $model->getAdminListExport($params, $perSize)->toArray();
            // echo "
";
            // print_r($db_data);
            // exit;
            foreach($db_data['data'] as $key => $value) {
                unset($value['goods_sales']);
                //$rowData = [];                //获取每列数据,转换处理成需要导出的数据
                //需要格式转换,否则会乱码
                //mb_convert_variables('GBK', 'UTF-8', $value);
                fputcsv($fp, $value);
            }
            unset($db_data);//刷新输出缓冲到浏览器
            ob_flush();//必须同时使用 ob_flush() 和flush() 函数来刷新输出缓冲。
            flush();
        }
        fclose($fp);
        exit();

        //return $this->renderSuccess(compact('list'));
    }
    /**
     * 多个商品批量加价
     * [addPrice description]
     * @param array $categoryIds [description]
     * @param int   $rate        [description]
     */
    public function batchAddPrice(array $goodsIds, int $rate){
        // ini_set('memory_limit', '1024M');
        // set_time_limit(0);
        $platform = $this->getUserPlatform();
        $channels = $platform ? array_column($platform->toArray(), "code") : [];
        $goodsList = GoodsModel::alias('g')
                    ->where('g.store_id',0)
                    ->where('g.is_delete',0)
                    ->whereIn('g.goods_id',$goodsIds)
                    ->whereIn('g.channel', $channels)
                    ->field(['g.goods_id','g.cost_price_min'])
                    ->group('g.goods_id')
                    ->order("g.goods_id asc")
                    ->select()
                    ->toArray();
        if (!$goodsList) {
            return $this->renderSuccess('没有需要加价的商品');
        }
        // 分批每次导入20条
        $limit = 20;
        // 根据商品总数量计算需要的队列任务数量
        $jobCount = \count($goodsList) / $limit;
        // 逐次发布队列任务
        for ($i = 0; $i < $jobCount; $i++) {
            $data = array_slice($goodsList, $i * $limit, $limit);
            GoodsAddPriceJob::dispatch([
                'list' => $data,
                'rate' => $rate,
            ]);
        }
        return $this->renderSuccess('加价成功');
    }

    /**
     * 加价
     * [addPrice description]
     * @param array $categoryIds [description]
     * @param int   $rate        [description]
     */
    public function addPrice(array $categoryIds, int $rate){
        // ini_set('memory_limit', '1024M');
        // set_time_limit(0);
        $platform = $this->getUserPlatform();
        $channels = $platform ? array_column($platform->toArray(), "code") : [];
        $goodsList = GoodsModel::alias('g')
                    ->join('goods_category_rel c', 'g.goods_id = c.goods_id')
                    ->where('c.store_id',0)
                    ->where('g.is_delete',0)
                    ->whereIn('c.category_id',$categoryIds)
                    ->whereIn('g.channel', $channels)
                    ->field(['g.goods_id','g.cost_price_min'])
                    ->group('g.goods_id')
                    ->order("g.goods_id asc")
                    // ->limit(100)
                    ->select()
                    ->toArray();
        if (!$goodsList) {
            return $this->renderSuccess('没有需要加价的商品');
        }
        // 分批每次导入20条
        $limit = 50;
        // 根据商品总数量计算需要的队列任务数量
        $jobCount = \count($goodsList) / $limit;
        // 逐次发布队列任务
        for ($i = 0; $i < $jobCount; $i++) {
            $data = array_slice($goodsList, $i * $limit, $limit);
            GoodsAddPriceJob::dispatch([
                'list' => $data,
                'rate' => $rate,
            ]);
        }
        
        return $this->renderSuccess('加价成功');
    }

    public function import(){
        // 新增记录
        $model = new ImportModel;
        $params = $this->postData();
        $params['channel'] = $this->admin['user']['channel'];
        if ($model->goodsUpdateBatch($params)) {
            return $this->renderSuccess('已添加到导入任务中,请在历史导入记录中查看结果');
        }
        return $this->renderError($model->getError() ?: '操作失败');

    }








}