'ID', 'goods_id' => '商品ID', 'goods_attr_id' => '属性ID', 'score' => '积分', 'money' => '金额', 'price' => '商品售价', 'deduction_limit'=> '抵扣限制', 'use_coupon' => '是否不参加优惠券抵扣', 'status' => '状态[-1:删除;0:禁用;1启用]', 'created_at' => '创建时间', 'updated_at' => '修改时间' ]; } public function getAttr() { return $this->hasOne(GoodsAttr::class, ['id' => 'goods_attr_id'])->andWhere(['status' => [StatusEnum::ENABLED]]); } public static function getGoodsAttr($goods_id) { $params = [ 'where' => [['=', 'goods_id', $goods_id], ['<>','status',StatusEnum::DELETE]], 'select' => ['goods_attr_id', 'score', 'money', 'deduction_limit'], 'with' => ['attr' => function($query){ $query->select(['original_price', 'name', 'id']); }] ]; $model = self::find(); self::paramPack($params, $model); $list = $model->asArray()->all(); if (!empty($list)) { foreach ($list as &$val) { $val['original_price'] = $val['attr']['original_price']; $val['name'] = $val['attr']['name']; unset($val['attr']); } } return $list; } /** * 保存数据(多条) * @param array $params * @param string $action */ public static function setData($params, $action) { try { if ($action == 'add') { Yii::$app->db->createCommand()->batchInsert(self::tableName(), ['goods_id', 'goods_attr_id', 'price', 'score', 'money', 'created_at', 'updated_at', 'deduction_limit'], $params)->execute(); } else { $attrList = array_column($params, null, 'goods_attr_id'); $goods_attr_ids = array_keys($attrList); $newAttrList = self::find()->where(['goods_attr_id' => $goods_attr_ids])->all(); foreach ($newAttrList as $val) { if (isset($attrList[$val->goods_attr_id])) { $val->price = $attrList[$val->goods_attr_id]['price']; $val->score = $attrList[$val->goods_attr_id]['score']; $val->money = $attrList[$val->goods_attr_id]['money']; $val->deduction_limit = $attrList[$val->goods_attr_id]['deduction_limit']; $val->updated_at = time(); if (!$val->save()) throw new Exception($val->getErrorMessage()); } } } return true; } catch (Exception $e) { self::$static_error = $e->getMessage(); return false; } } }