<?php
declare (strict_types=1);

namespace app\command;

use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\facade\Db;

class repairPvLog extends Command
{
    protected function configure()
    {
        // 指令配置
        $this->setName('repair_pv_log')
            ->setDescription('修复老数据pv记录');
    }

    /**
     * @param Input $input
     * @param Output $output
     * @return void
     */
    protected function execute(Input $input, Output $output)
    {
        $user_id_list = Db::name('user')->where([['pv','=',0]])->column('uid');
        if($user_id_list){
            foreach ($user_id_list as $user_id) {
                $order_list = Db::name('store_order')->where(['uid' => $user_id,'paid' => 1])->where('mer_id',496)->select();
                if($order_list) {
                    $pv = 0;
                    foreach ($order_list as $order) {
                        if($order['mer_id'] == 496){
                            $pv_tmp = ($order['total_price']/1180) * 700;
                            $pv_log_data = [
                                'uid' => $user_id,
                                'pv' => $pv_tmp,
                                'status' => 1,
                                'mark' => '会员专区消费获得PV（老数据）',
                                'order_id' => $order['order_sn'],
                                'settlement_time' => '2025-03-01 00:00:00',
                                'addtime' => 1740758400
                            ];
                            Db::name('user_pv_log')->insert($pv_log_data);
                            $pv += $pv_tmp;
                        }
                    }
                    if($pv){
                        Db::name('user')->where(['uid' => $user_id])->update(['pv' => $pv]);
                        $output->writeln('user_id = ' . $user_id . '添加成功 pv = ' . $pv);
                    }
                }
            }
        }
        $output->writeln('执行结束');
    }

}
