<?php

namespace app\common\model\hu;

use think\Exception;
use think\Model;
use traits\model\SoftDelete;
use think\Db;
class OrderGoods extends Model
{

    use SoftDelete;

    

    // 表名
    protected $name = 'hu_order_goods';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [

    ];

    /**
     * @param array $goodArr 商品信息
     * @param int $user_id   用户ID
     * @param int $order_id  订单ID
     * @return bool
     */
    public function add(array $goodArr,int $user_id,int $order_id){
        try {
            $Goods = new Goods();
            foreach ($goodArr as $key=>$value){
                $good_info = Db::name("hu_goods")->find($value['goods_id']);
                if(!empty($good_info)){
                    $data['user_id'] = $user_id;
                    $data['order_id'] = $order_id;
                    $data['goods_id'] = $value['goods_id'];
                    $data['goods_name'] = $good_info['goods_name'];
                    $data['cover_image'] = $good_info['cover_image'];
                    $data['details'] = $good_info['details'];
                    $data['price'] = $good_info['price'];
                    $data['buy_number'] = $value['buy_number'];
                    $data['amounts'] = $Goods->computePrice($value['goods_id'],$value['buy_number']);
                    $data['createtime'] = time();

                    self::insert($data);

                }
            }
            return  true;
        }catch (Exception $e) {
            return false;
        }

    }
    

}
