<?php

declare (strict_types=1);

namespace app\command;

use app\api\model\dealer\User as DealerUserModel;
use app\api\model\User;
use app\common\enum\user\UserTypeEnum;
use app\common\model\wholesaler\Apply;
use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;


class CalDealerTime extends Command
{
    protected function configure()
    {
        // 指令配置
        $this->setName('CalDealerTime')
            ->setDescription('会员身份以及分销身份检测');
    }

    /**
     * @notes:执行
     * @param Input $input
     * @param Output $output
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     * @author: wanghousheng
     */
    protected function execute(Input $input, Output $output)
    {
        $nowDay = date('Y-m-d');
        $this->dealerUser($nowDay);
        $this->plusUser($nowDay);
    }

    /**
     * @notes:分销商用户
     * @param $nowDay
     * @return void
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     * @author: wanghousheng
     */
    private function dealerUser($nowDay)
    {
        $dealerUser = (new User)->where(['user_type' => UserTypeEnum::DEALER])
            ->whereDay('fx_effective_time', $nowDay)
            ->field('user_id,user_type,fx_effective_time,effective_time')
            ->select();
        $plusUserIds = [];
        $normalUserIds = [];
        if (!$dealerUser->isEmpty()) {
            $dealerUser = $dealerUser->toArray();
            foreach ($dealerUser as $value) {
                //分销商到期 会员未到期
                if (!empty($value['effective_time'])) {
                    $effective_time = strtotime($value['effective_time']);
                    if (strtotime($nowDay) < $effective_time) {
                        $plusUserIds[] = $value['user_id'];
                    } else {
                        $normalUserIds[] = $value['user_id'];//会员已到期
                    }
                } else {
                    //普通会员
                    $normalUserIds[] = $value['user_id'];
                }
            }
            if ($plusUserIds) {
                (new User)->whereIn('user_id', $plusUserIds)->update(['user_type' => UserTypeEnum::MEMBER]);
            }
            if ($normalUserIds) {
                $normalUserIds = array_unique($normalUserIds);
                (new User)->whereIn('user_id', $normalUserIds)->update(['user_type' => UserTypeEnum::NORMAL]);
                //删除采购商申请
                Apply::destroy(function ($query) use ($normalUserIds) {
                    $query->whereIn('user_id', $normalUserIds);
                });
            }
            $dealerUserIds = array_unique(array_merge($normalUserIds, $plusUserIds));
            //删除分销商(软)
            if ($dealerUserIds) {
//                foreach ($dealerUserIds as $value) {
//                    UserModel::detail($value)->delete();
//                }
                (new DealerUserModel())->whereIn('user_id', $dealerUserIds)->update(['is_delete' => 1]);
            }
        }
    }

    /**
     * @notes:plus会员
     * @param $nowDay
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     * @author: wanghousheng
     */
    private function plusUser($nowDay)
    {
        $normalUserIds = [];
        $list = (new User)->where(['user_type' => UserTypeEnum::MEMBER])
            ->whereDay('effective_time', $nowDay)
            ->field('user_id,user_type,effective_time')
            ->select();
        if (!$list->isEmpty()) {
            $list = $list->toArray();
            foreach ($list as $value) {
                $normalUserIds[] = $value['user_id'];
            }
            if ($normalUserIds) {
                (new User)->whereIn('user_id', $normalUserIds)->update(['user_type' => UserTypeEnum::NORMAL]);
                //删除采购商申请
                Apply::destroy(function ($query) use ($normalUserIds) {
                    $query->whereIn('user_id', $normalUserIds);
                });
            }

        }
    }
}