<?php
namespace app\cron;

use think\Db;
use yunwuxin\cron\Task;

class PhBalanceWarning extends Task
{
    public function configure()
    {
        $this->dailyAt('00:00'); //每天执行一次
    }

    /**
     * 执行任务
     * @return mixed
     */
    protected function execute()
    {
        try{
            $this->record();
        }catch (\Exception $e){
            trace($e->getMessage());
        }

    }

    //生成预警记录
    public function record(){
        $list = Db::name('ph_orders')
            ->where('status',1)
            ->field('org_id,sn,start,price,pre_money,contact,phone')
            ->order('id desc')
            ->select();
        foreach ($list as $k=>$v){
            $time = strtotime(date('Y-m-d 00:00:00')) - strtotime($v['start']);
            $day =  round($time/(60*60*24),1);
            $costMoney = round($v['price'] * $day,2);
            $list[$k]['cost_money'] = $costMoney;
            $list[$k]['end'] = date('Y-m-d 00:00:00');
            unset($list[$k]['price']);
        }
        foreach ($list as $k=>$v){
            if ($v['cost_money'] > $v['pre_money']){
                $data[] = $v;
            }
        }
        if (!empty($data)){
            Db::name('ph_balance_warning')->insertAll($data);
        }
    }
}