<?php

namespace app\common\logic;

use app\common\model\effective\EffectiveCategory;
use app\common\model\effective\EffectiveRules;
use app\common\model\orders\OrderEffectiveLog;
use app\common\model\orders\RechargeOrder;
use app\common\model\recharge\OrderGoods;
use Exception;

class EffectiveLogic extends BaseLogic
{
    public static function  receiveEffective($work)
    {
        $orderGoods = OrderGoods::whereIn('sn',RechargeOrder::where('work_id',$work->id)->column('sn'))->findOrEmpty();
        $effective =  EffectiveCategory::with('effectiveRule')->where('goods_category_id',$orderGoods->goods_category_id)->findOrEmpty();
        if(!$effective->isEmpty()){
            OrderEffectiveLog::create([
                'effective_id' => $effective->effectiveRule->id,
                'sn' => $orderGoods['sn'],
                'user_id' => $work['user_id'],
                'work_id' => $work->id,
                'goods_id' => $orderGoods->goods_id,
                'effective_unit' => $effective->effectiveRule->effective_unit,
                'effective_num' => $effective->effectiveRule->effective_num,
                'remark' => $effective->effectiveRule->remark,
                'end_effective_time' =>  EffectiveRules::formatEndEffectiveTime(time() ,$effective->effectiveRule->effective_num,$effective->effectiveRule->effective_unit),
            ]);
        }

    }
}