<?php

namespace app\command;
// +----------------------------------------------------------------------
// | [ WE CAN DO IT MORE SIMPLE  ]
// +----------------------------------------------------------------------
// | Copyright (c) 2018-2020 rights reserved.
// +----------------------------------------------------------------------
// | Author: TABLE ME
// +----------------------------------------------------------------------
// | Date: 2020-09-08 16:19
// +----------------------------------------------------------------------
use app\model\api\Member;
use app\model\api\Order;
use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\facade\Db;

/**
 * 会员打上标签
 * @author mouxinfen
 */
class MemberTag extends Command
{
    protected function configure()
    {
        $this
            ->setName('MemberTag')
            ->setDescription('每日筛选实力商家和潜力商家！')
            ->addArgument('action', null, "");
    }


    protected function execute(Input $input, Output $output)
    {
        $time = strtotime(date('Y-m-d', strtotime('-1 day')));
        $this->expData($time);
    }


    /**
     * 统计数据
     * @param $time
     */
    private function expData($time)
    {
        $startTime = $time - 13 * 24 * 3600;
        $endTime = $time + 3600 * 24;
        $user = Db::name("order")
            ->field('uid,SUM(all_price) as number')
            ->where('status', '>', 0)
            ->where('pay_time', 'between', [$startTime, $endTime])
            ->group("uid")
            ->having('SUM(all_price)>=200')
            ->column('uid');
        $order = Db::name("order")
            ->field("uid,from_unixtime(pay_time,'%Y_%m_%d') as _pay_time")
            ->where('status', '>', 0)
            ->where('pay_time', 'between', [$startTime, $endTime])
            ->where('uid', 'not in', $user)
            ->group("uid,_pay_time")
            ->select()->toArray();
        $member = [];
        foreach ($order as $k => $v) {
            $member[] = $v['uid'];
        }
        $member2 = (array_count_values($member));
        $userList = Member::where('uid', 'in', $member)->column('regtime', 'uid');
        foreach ($userList as $k => $v) {
            if($endTime - $v > 4 * 24 * 3600){
                if ($endTime - $v < 14 * 24 * 3600) {
                    $day = ceil(($time - $v) / (24 * 3600)) + 1;
                } else {
                    $day = 14;
                }
                if ($member2[$k] / $day >= 0.4) {
                    $user[] = $k;
                }
            }
        }
        $res1 = Member::where('uid', 'in', $user)->update(['label' => 1,'admin_id' => 100]);
        $power = Member::where('label', 1)->column('uid');
        $diff = array_diff($user, $power);
        $res2 = implode(',',$diff);
        $potential = array_diff($power, $user);
        $res4 = implode(',',$potential);
        $res3 = Member::where('uid', 'in', $potential)->update(['label' => 2]);

        echo "时间：" . date('Y-m-d', $time) ." ". $res1 . "人成为实力商家:" . $res2 .
            "; " . $res3 . "人成为潜力商家:" . $res4 . PHP_EOL;

    }


}
