<?php
// +----------------------------------------------------------------------
// | likeadmin快速开发前后端分离管理后台（PHP版）
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码，建议反馈是我们前进的动力
// | 开源版本可自由商用，可去除界面版权logo
// | gitee下载：https://gitee.com/likeshop_gitee/likeadmin
// | github下载：https://github.com/likeshop-github/likeadmin
// | 访问官网：https://www.likeadmin.cn
// | likeadmin团队 版权所有 拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeadminTeam
// +----------------------------------------------------------------------


namespace app\adminapi\controller\third;


use app\adminapi\controller\BaseAdminController;
use app\adminapi\lists\third\ThirdGoodsLists;
use app\adminapi\logic\third\ThirdGoodsLogic;
use app\adminapi\validate\third\ThirdGoodsValidate;
use app\common\logic\ThirdOrderLogic;
use app\common\model\third\ThirdGoods;


/**
 * ThirdGoods控制器
 * Class ThirdGoodsController
 * @package app\adminapi\controller\third
 */
class ThirdGoodsController extends BaseAdminController
{


    /**
     * @notes 获取列表
     * @return \think\response\Json
     * @author likeadmin
     * @date 2025/03/06 16:57
     */
    public function lists()
    {
        return $this->dataLists(new ThirdGoodsLists());
    }


    /**
     * @notes 添加
     * @return \think\response\Json
     * @author likeadmin
     * @date 2025/03/06 16:57
     */
    public function add()
    {
        $params = (new ThirdGoodsValidate())->post()->goCheck('add');
        $result = ThirdGoodsLogic::add($params);
        if (true === $result) {
            return $this->success('添加成功', [], 1, 1);
        }
        return $this->fail(ThirdGoodsLogic::getError());
    }


    /**
     * @notes 编辑
     * @return \think\response\Json
     * @author likeadmin
     * @date 2025/03/06 16:57
     */
    public function edit()
    {
        $params = (new ThirdGoodsValidate())->post()->goCheck('edit');
        $result = ThirdGoodsLogic::edit($params);
        if (true === $result) {
            return $this->success('编辑成功', [], 1, 1);
        }
        return $this->fail(ThirdGoodsLogic::getError());
    }


    /**
     * @notes 删除
     * @return \think\response\Json
     * @author likeadmin
     * @date 2025/03/06 16:57
     */
    public function delete()
    {
        $params = (new ThirdGoodsValidate())->post()->goCheck('delete');
        ThirdGoodsLogic::delete($params);
        return $this->success('删除成功', [], 1, 1);
    }


    /**
     * @notes 获取详情
     * @return \think\response\Json
     * @author likeadmin
     * @date 2025/03/06 16:57
     */
    public function detail()
    {
        $params = (new ThirdGoodsValidate())->goCheck('detail');
        $result = ThirdGoodsLogic::detail($params);
        return $this->data($result);
    }

    /**
     * 获取美团商品
     * @return \think\response\Json
     */
    public function getMeituanGoods()
    {
        $data = [];
        $i = 1;
        for ($i; $i < 100; $i++){
            $result = ThirdOrderLogic::queryshopdeal($i);
            if(empty($result['data']['result'])){
                break;
            }
            foreach ($result['data']['result'] as $item){
                $data[] = $item;
            }
        }

        $j = 1;
        for ($j; $j < 100; $j++){
            $result1 = ThirdOrderLogic::queryshopdeal1($j);
            if(empty($result1['data']['result'])){
                break;
            }
            foreach ($result1['data']['result'] as $item1){
                $data[] = $item1;
            }
        }

        if(!empty($data)){
            $good_data = [];
            $dealGroupIds = array_column($data, 'dealGroupId');
            $product_ids = ThirdGoods::whereIn('product_id', $dealGroupIds)->column('product_id');
            foreach ($data as $key=>$items){
                if(in_array($items['dealGroupId'], $product_ids)){
                    unset($data[$key]);
                }else{
                     $good_data[] = [
                        'third_type' => 1,
                        'goods_id' => 0,
                        'product_id' => $items['dealGroupId'],
                        'product_name' => $items['title'],
                        'product_price' => $items['price'],
                        'app_sku_id' =>$items['dealId']
                    ];
                }
            }

            if(!empty($good_data)){
                ThirdGoodsLogic::addMeituanGoods($good_data);
            }
        }
        return $this->success('同步成功', [], 1, 1);
    }


}