where(['id' => $this->id, 'is_delete' => 0, 'status' => 1])->one(); if (!$integral_goods) { return [ 'code' => 1, 'msg' => '未找到该商品' ]; } $attr = json_decode($integral_goods['attr'], true); $count = count($attr); $str = ''; foreach ($attr as $key => &$value) { $attr_group = GoodsForm::getAttrGroupByAttId($value['attr_id']); $t = $value['attr_name']; unset($value['attr_name']); $value['attr_group_name'] = $attr_group ? $attr_group->attr_group_name : null; $value['attr_name'] = $t; $str .= $value['attr_group_name'] . ':' . $t; if ($key < $count - 1) { $str .= ','; } } $goods = Goods::findOne($integral_goods->goods_id); $result = [ 'id' => $integral_goods->id, 'goods_id' => $integral_goods->goods_id, 'name' => !empty($integral_goods->name) ? $integral_goods->name : $goods->name, 'pic' => $goods->cover_pic, 'integral' => $integral_goods->integral, 'sales' => $integral_goods->sales, 'detail' => $integral_goods->detail, 'attr_str' => $str ]; return [ 'code' => 0, 'msg' => 'success', 'data' => $result ]; } public function pay() { $integral_goods = SaasIntegralGoods::find()->where(['id' => $this->id, 'is_delete' => 0, 'status' => 1])->one(); if (!$integral_goods) { return [ 'code' => 1, 'msg' => '未找到该商品' ]; } if ($this->user->integral < $integral_goods->integral) { return [ 'code' => 1, 'msg' => '积分不足以兑换商品' ]; } if ($integral_goods->goods_count < 1) { return [ 'code' => 1, 'msg' => '积分商品库存不足' ]; } $count = SaasIntegralOrder::find()->where(['store_id' => $integral_goods->store_id, 'integral_goods_id' => $integral_goods->id, 'user_id' => $this->user->id])->count(); if ($count >= $integral_goods->user_num) { return [ 'code' => 1, 'msg' => '超出本商品限制每人兑换数量' ]; } $name = Goods::findOne($integral_goods->goods_id)->name; $name = mb_substr($name, 0, 16, 'utf-8') . '...'; $t = \Yii::$app->db->beginTransaction(); $before = $this->user->integral; $this->user->integral -= $integral_goods->integral; if (!$this->user->save()) { $t->rollBack(); return [ 'code' => 1, 'msg' => $this->user->errors[0] ]; } $order = new SaasIntegralOrder(); $order->store_id = $integral_goods->store_id; $order->goods_id = $integral_goods->goods_id; $order->integral_goods_id = $integral_goods->id; $order->cost = $integral_goods->integral; $order->order_no = OrderNo::getOrderNo(OrderNo::ORDER_SAAS_INTEGRAL); $order->user_id = $this->user->id; $order->created_at = time(); if (!$order->save()) { $t->rollBack(); return [ 'code' => 1, 'msg' => $order->errors[0] ]; } $log = new AccountLog(); $log->store_id = $integral_goods->store_id; $log->user_id = $this->user->id; $log->type = AccountLog::TYPE_INTEGRAL; $log->amount = $order->cost; $log->log_type = AccountLog::LOG_TYPE_EXPEND; $log->desc = '兑换积分商品: ' . $name; $log->before = $before; $log->after = $this->user->integral; $log->operator = ''; $log->operator_id = 0; $log->operator_type = AccountLog::TYPE_OPERATOR_NORMAL; $log->created_at = time(); $log->order_id = $order->id; $log->order_type = AccountLog::TYPE_SAAS_INTEGRAL_ORDER; if (!$log->save()) { $t->rollBack(); return [ 'code' => 1, 'msg' => $log->errors[0] ]; } $t->commit(); return [ 'code' => 0, 'msg' => '兑换成功' ]; } }