<?php




namespace ln\jobs;

use app\common\repositories\store\product\ProductRepository;
use app\common\repositories\user\UserBillRepository;
use app\common\repositories\user\UserRepository;
use ln\interfaces\JobInterface;
use think\facade\Db;
use think\facade\Log;

class ClearUserIntegralJob implements JobInterface
{

    public function fire($job, $data)
    {
        try {
            $user = app()->make(UserRepository::class)->get($data['uid']);
            if ($user && $user->integral > 0) {
                $validIntegral = app()->make(UserBillRepository::class)->validIntegral($data['uid'], $data['startTime'], $data['endTime']);
                if ($user->integral > $validIntegral) {
                    $clear = bcsub($user->integral, $validIntegral, 0);
                    $user->integral = $validIntegral;
                    app()->make(UserBillRepository::class)->decBill($user->uid, 'integral', 'timeout', [
                        'link_id' => 0,
                        'status' => 1,
                        'title' => '积分过期',
                        'number' => $clear,
                        'mark' => $clear . '积分已过期',
                        'balance' => $user->integral
                    ]);
                    $user->save();
                }
            }
        } catch (\Exception $e) {
            Log::info('用户ID：' . $data['uid'] . '积分清理失败');
        }
        $job->delete();
    }

    public function failed($data)
    {
        // TODO: Implement failed() method.
    }
}
