| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 |
- <?php
- namespace addons\Purchase\common\models;
- use common\enums\StatusEnum;
- use Yii;
- use yii\db\Exception;
- /**
- * This is the model class for table "{{%addons_purchase_goods_attr}}".
- *
- * @property int $id
- * @property int $goods_id 商品ID
- * @property int $goods_attr_id 规格ID
- * @property float|null $min_score 最低抵扣积分
- * @property int|null $min_limit 是否限制[0=不限制,1=限制]
- * @property float|null $max_score 最高抵扣积分
- * @property int|null $max_limit 是否限制[0=不限制,1=限制]
- * @property int $status 状态[-1:删除;0:禁用;1启用]
- * @property int $created_at 创建时间
- * @property int $updated_at 更新时间
- */
- class PurchaseGoodsAttr extends \common\models\base\BaseActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%addons_purchase_goods_attr}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['goods_id', 'goods_attr_id', 'min_limit', 'max_limit', 'status', 'created_at', 'updated_at'], 'integer'],
- [['min_score', 'max_score'], 'number'],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'goods_id' => '商品ID',
- 'goods_attr_id' => '规格ID',
- 'min_score' => '最低抵扣积分',
- 'min_limit' => '是否限制[0=不限制,1=限制]',
- 'max_score' => '最高抵扣积分',
- 'max_limit' => '是否限制[0=不限制,1=限制]',
- 'status' => '状态[-1:删除;0:禁用;1启用]',
- 'created_at' => '创建时间',
- 'updated_at' => '更新时间',
- ];
- }
- /**
- * @notes:数据保存
- * @param $params
- * @return bool|void
- * @author: lun
- * @Time: 2022/10/22 15:59
- */
- public static function setData($params)
- {
- try {
- foreach ($params as $attr) {
- $attr['min_limit'] = $attr['min_limit'] === 'true' ? 1 : 0;
- $attr['max_limit'] = $attr['max_limit'] === 'true' ? 1 : 0;
- $model = self::findOne(['goods_id' => $attr['goods_id'], 'goods_attr_id' => $attr['goods_attr_id'], 'status' => StatusEnum::ENABLED]);
- if (empty($model)) {
- $model = new self();
- $model->loadDefaultValues();
- }
- if (!$model->load($attr, '') || !$model->save()) throw new Exception($model->getErrorMessage());
- }
- return true;
- } catch (\Exception $e) {
- self::setStaticError($e->getMessage());
- return false;
- }
- }
- /**
- * @notes:批量添加规格
- * @param $attrs
- * @param $setting
- * @param $score_deduct_ratio
- * @return bool
- * @author: lun
- * @Time: 2022/11/15 15:37
- */
- public static function percentSetData($attrs, $setting, $score_deduct_ratio)
- {
- try {
- foreach ($attrs as $attr) {
- $attr['min_limit'] = $setting['min_limit'] === 'true' ? 1 : 0;
- $attr['max_limit'] = $setting['max_limit'] === 'true' ? 1 : 0;
- $attr['min_score'] = ceil($setting['min_score'] * $score_deduct_ratio * $attr['price']) / 100;
- $attr['max_score'] = ceil($setting['max_score'] * $score_deduct_ratio * $attr['price']) / 100;
- $model = self::findOne(['goods_id' => $attr['goods_id'], 'goods_attr_id' => $attr['goods_attr_id'], 'status' => StatusEnum::ENABLED]);
- if (empty($model)) {
- $model = new self();
- $model->loadDefaultValues();
- }
- if (!$model->load($attr, '') || !$model->save()) throw new Exception($model->getErrorMessage());
- }
- return true;
- } catch (\Exception $e) {
- self::setStaticError($e->getMessage());
- return false;
- }
- }
- }
|