| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- <?php
- namespace addons\ScoreMall\common\models;
- use common\models\base\BaseActiveRecord;
- use common\models\goods\GoodsAttr;
- use common\enums\StatusEnum;
- use Exception;
- use Yii;
- /**
- * This is the model class for table "{{%addons_score_mall_goods_attr}}".
- * @property int $id
- * @property int $mall_id
- * @property int $goods_id 商品ID
- * @property int $goods_attr_id 属性ID
- * @property string $score 积分
- * @property float $money 金额
- * @property float $price 商品售价
- * @property int $num 已兑换数量
- * @property int $type 类型
- * @property int $use_coupon 是否使用
- * @property int $status
- * @property int $created_at 创建时间
- * @property int $updated_at 修改时间
- */
- class ScoreMallGoodsAttr extends BaseActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%addons_score_mall_goods_attr}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules(){
- return [
- [['goods_id', 'goods_attr_id'], 'required'],
- [[ 'status', 'created_at', 'updated_at', 'goods_id', 'goods_attr_id'], 'integer'],
- [['deduction_limit'], 'string'],
- [['money', 'price', 'score'], 'number'],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels(){
- return [
- 'id' => '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;
- }
- }
- }
|