keepSnapshots(true); $this->addBehavior( new SoftDelete([ 'field' => 'deleted', 'value' => 1, ]) ); } public function beforeCreate() { $this->sn = $this->getOrderSn(); $this->create_time = time(); } public function beforeUpdate() { $this->update_time = time(); } public function beforeSave() { if (is_array($this->item_info) || is_object($this->item_info)) { $this->item_info = kg_json_encode($this->item_info); } if (is_array($this->promotion_info) || is_object($this->promotion_info)) { $this->promotion_info = kg_json_encode($this->promotion_info); } } public function afterSave() { if ($this->hasUpdated('status')) { $orderStatus = new OrderStatus(); $orderStatus->order_id = $this->id; $orderStatus->status = $this->getSnapshotData()['status']; $orderStatus->create(); } } public function afterFetch() { $this->amount = (float)$this->amount; if (is_string($this->item_info)) { $this->item_info = json_decode($this->item_info, true); } if (is_string($this->promotion_info)) { $this->promotion_info = json_decode($this->promotion_info, true); } } public static function itemTypes() { return [ self::ITEM_COURSE => '课程', self::ITEM_PACKAGE => '套餐', self::ITEM_REWARD => '赞赏', self::ITEM_VIP => '会员', self::ITEM_TEST => '测试', ]; } public static function promotionTypes() { return [ self::PROMOTION_FLASH_SALE => '限时秒杀', self::PROMOTION_DISCOUNT => '限时折扣', ]; } public static function statusTypes() { return [ self::STATUS_PENDING => '待支付', self::STATUS_DELIVERING => '发货中', self::STATUS_FINISHED => '已完成', self::STATUS_CLOSED => '已关闭', self::STATUS_REFUNDED => '已退款', ]; } protected function getOrderSn() { $sn = date('YmdHis') . rand(1000, 9999); $order = self::findFirst([ 'conditions' => 'sn = :sn:', 'bind' => ['sn' => $sn], ]); if (!$order) return $sn; $this->getOrderSn(); } }