<?php

$is_script = 1;
include_once __DIR__ . '/../index.php';

use app\manager\model\FollowRecord;
use app\manager\model\ProductOrder;
use app\manager\model\User;
use enum\user\User as UserEnum;

$user_model = new User;
$product_order_model = new ProductOrder;
$follow_record_model = new FollowRecord;
$user_enum = new UserEnum;

$date1 = date("Y-m-01", time());
$date2 = date("Y-m-d H:i:s", time());
$flag = date("d", time()) == date("t", time());
$date3 = date("Y-m-01", strtotime("-1 month"));
$date4 = $flag ? date("Y-m-t H:i:s", strtotime("-1 month")) : date("Y-m-d H:i:s", strtotime("-1 month"));

$time_where1 = '';
$time_where2 = '';

$user_id_list = [];
$register_id_list = [];
$cost_user_id_list = [];
$uncost_user_id_list = [];
$uncost_overtime_user_id_list = [];
$cost_overtime_user_id_list = [];
$finish_record_data = [];
$unfollow_record_data = [];
$last_cost_list = [];

//本月
$time_where1 .= " UpdateTime>='" . $date1 . "' and UpdateTime<='" . $date2 . "' ";
$time_where2 .= " UpdateTime>='" . $date3 . "' and UpdateTime<='" . $date4 . "' ";

//获取本月注册的用户
$register_user_list = $user_model->getAllManagerUser([]);

foreach ($register_user_list as $info) {

    //本月注册用户
    if (strtotime($info['CreateTime']) >= strtotime($date1) && strtotime($info['CreateTime']) <= strtotime($date2)) {
        $register_id_list[] = $info['Id'];
    } else {
        $user_id_list[] = $info['Id'];
    }
}
//本月注册用户消费
$user_month_cost_list = $product_order_model->getPreMonthCost($register_id_list, $time_where1);

foreach ($user_month_cost_list as $info) {
    $cost_user_id_list[] = $info['UserId'];
}

//老用户本月消费
$user_now_cost_list = $product_order_model->getPreMonthCost($user_id_list, $time_where1);
//老用户上月消费
$user_last_cost_list = $product_order_model->getPreMonthCost($user_id_list, $time_where2);

foreach ($user_last_cost_list as $cost) {
    $last_cost_list[$cost['UserId']] = $cost;
}

foreach ($user_now_cost_list as $cost) {
    $user_follow_status = $user_model->getOne(['Id' => $cost['UserId']], 'follow_status')['follow_status'];
    if (in_array($user_follow_status, $user_enum::$Lost)) {
        continue;
    }
    if (isset($last_cost_list[$cost['UserId']])) {
        if ($last_cost_list[$cost['UserId']]['money'] > $cost['money']) {
            $uncost_user_id_list[] = $cost['UserId'];
        } else {
            //已完成
            $cost_user_id_list[] = $cost['UserId'];
        }
    } else {
        //已完成
        $cost_user_id_list[] = $cost['UserId'];
    }
}

//获取待跟进状态超过15天
$over_15_list = $follow_record_model->getOverTime();
foreach ($over_15_list as $info) {
    switch ($info['follow_status']) {
        case $user_enum::$UnfollowNotBuy:
            $uncost_overtime_user_id_list[] = $info['user_id'];
            break;
        case $user_enum::$UnfollowBuyReduce:
            $cost_overtime_user_id_list[] = $info['user_id'];
            break;
    }
}

$user_model->updateOne(['Id' => ['in', $cost_user_id_list]], ['follow_status' => $user_enum::$Finish]);
$user_model->updateOne(['Id' => ['in', $uncost_user_id_list]], ['follow_status' => $user_enum::$UnfollowBuyReduce]);
$user_model->updateOne(['Id' => ['in', $uncost_overtime_user_id_list]], ['follow_status' => $user_enum::$FollowOverTimeNotBuy]);
$user_model->updateOne(['Id' => ['in', $cost_overtime_user_id_list]], ['follow_status' => $user_enum::$FollowOverTimeBuyReduce]);

//获取所有用户的最后支付时间
$all_last_cost = $product_order_model->getUserCostTime();
$user_last_cost_id = [];
$all_last_cost_list = [];
foreach ($all_last_cost as $info) {
    $all_last_cost_list[$info['UserId']] = $info;
    $user_last_cost_id[] = $info['UserId'];
}
//更新用户最后的支付时间
$all_user_list = $user_model->getList(['Id' => ['in', $user_last_cost_id]], 'Id,pay_time');

foreach ($all_user_list as $info) {
    if ($info['pay_time'] != $all_last_cost_list[$info['Id']]['last_time']) {
        $user_model->updateOne(['Id' => $info['Id']], ['pay_time' => $all_last_cost_list[$info['Id']]['last_time']]);
    }
}
