<?php

declare (strict_types=1);

namespace app\command;

use think\console\Command;
use think\console\Output;
use think\console\Input;
use think\facade\Db;
use app\api\model\{Goods as GoodsModel};
use app\api\model\PreSale;
use app\api\model\PreSaleMessage;
use app\api\model\PreSaleLog;

//    /www/server/php/74/bin/php /server/wwwroot/yanzong/think test
class test extends Command
{
    protected function configure()
    {
        // 指令配置
        $this->setName('test')
            ->setDescription('预售到期自动上架');
    }

    protected function execute(Input $input, Output $output)
    {
        //todo wmc这边代码需要优化
        $info = PreSale::where('status', 1)
            ->where('p_time', '<=', date('Y-m-d H:i:s'))
            ->where('is_change', '=', 0)
            ->select();

        foreach ($info as $v) {
            //所有商品直接上架
            if (!empty($v['goods_list'])) {
                $goodslist = explode(',', $v['goods_list']);
                GoodsModel::where('store_id', $v['store_id'])
                    ->whereIn('goods_id', $goodslist)
                    ->update(['status' => 10]);
            }
            //更改预售状态
            PreSale::where('id', $v['id'])
                ->update(['is_change' => 1]);
            //获取预约信息
            $prelist = PreSaleLog::where([
                'store_id' => $v['store_id'],
            ])->whereIn('goods_id', $goodslist)->select();

            //预约的用户推送消息
            foreach ($prelist as $v2) {
                $goodsName = GoodsModel::where([
                    'store_id' => $v2['store_id'],
                    'goods_id' => $v2['goods_id']
                ])->field('goods_name')->find()->goods_name;

                PreSaleMessage::insert([
                    'user_id' => $v2['user_id'],
                    'goods_id' => $v2['goods_id'],
                    'type' => 1,
                    'store_id' => $v['store_id'],
                    'content' => '您预约的商品' . $goodsName . '已经开售啦',
                    'ctime' => date('Y-m-d H:i:s')
                ]);
            }
        }


    }


}