<?php

namespace app\api\controller;

use addons\unishop\model\Order;
use app\admin\model\unishop\OrderProduct;
use app\admin\model\unishop\Product;
use app\common\controller\Api;
use think\Exception;

/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 首页
     *
     */
    public function index()
    {
        $this->success('请求成功');
    }

    public function cancel(){
        $token = $this->request->request("token");
        if (md5("ikea")!=$token){
            return;
        }
        $model=new Order();
        $order = $model->field("GROUP_CONCAT(id) ids")->where([
            "createtime"=>["<",time()-1800],
            "have_paid"=>0,
            "status"=>1,
        ])->find();
        echo $order;die;
        if (!$order){
            return;
        }
        $ids=explode(",",$order->ids);
        $orderProduct = new OrderProduct();
        $list =$orderProduct->where([
            'order_id' => ["in",$ids],
        ])->select();
        $list = collection($list)->toArray();
        $product = new \app\admin\model\unishop\Product();
        foreach ($list as $val){
            try {
                $product->where(["id"=>$val['product_id']])->setInc("stock",$val["number"]);
                $product->where(["id"=>$val['product_id']])->setInc("real_sales",-$val["number"]);
            }catch (Exception $e){}
        }
        $model->save(["status"=>0],["id"=>["in",$ids]]);
    }
}