<?php
declare(strict_types=1);

namespace app\common\model;

use app\common\model\base\BaseModel;

class StockDetail extends BaseModel
{
      // 自动写入时间戳字段
      protected $autoWriteTimestamp = true;
      protected $createTime = 'createtime';
      protected $updateTime = 'updatetime';
      protected $deleteTime = false;

      protected $type = [
            'createtime'     =>  'timestamp:Y-m-d H:i',
            'updatetime'     =>  'timestamp:Y-m-d H:i',
     
      ];




      /**
       * 设置库存配置，根据操作类型对指定库存进行增加或减少操作
       * @param int $key 库存配置的标识键，通常为库存项的 ID
       * @param float $num 要操作的数量，正数表示增加，负数表示减少
       */
      public static function setStockConfigNum(int $key, string $num, string $type)
      {     
            $row = StockDetail::where('key', $key)->where('type_id', $type)->find();
            $stock = $num; //库存
            if(empty($row)){
                  self::create(['key' => $key,'num' => $num, 'type_id'=> $type]);
            }else{
                  $stock      = bcadd($row->num, $num, 2);
                  $row->num   = $stock;
                  $row->save()?true:false;
            }
            return $stock;
      }     
      


   

}
