PurchaseGoodsAttr.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. namespace addons\Purchase\common\models;
  3. use common\enums\StatusEnum;
  4. use Yii;
  5. use yii\db\Exception;
  6. /**
  7. * This is the model class for table "{{%addons_purchase_goods_attr}}".
  8. *
  9. * @property int $id
  10. * @property int $goods_id 商品ID
  11. * @property int $goods_attr_id 规格ID
  12. * @property float|null $min_score 最低抵扣积分
  13. * @property int|null $min_limit 是否限制[0=不限制,1=限制]
  14. * @property float|null $max_score 最高抵扣积分
  15. * @property int|null $max_limit 是否限制[0=不限制,1=限制]
  16. * @property int $status 状态[-1:删除;0:禁用;1启用]
  17. * @property int $created_at 创建时间
  18. * @property int $updated_at 更新时间
  19. */
  20. class PurchaseGoodsAttr extends \common\models\base\BaseActiveRecord
  21. {
  22. /**
  23. * {@inheritdoc}
  24. */
  25. public static function tableName()
  26. {
  27. return '{{%addons_purchase_goods_attr}}';
  28. }
  29. /**
  30. * {@inheritdoc}
  31. */
  32. public function rules()
  33. {
  34. return [
  35. [['goods_id', 'goods_attr_id', 'min_limit', 'max_limit', 'status', 'created_at', 'updated_at'], 'integer'],
  36. [['min_score', 'max_score'], 'number'],
  37. ];
  38. }
  39. /**
  40. * {@inheritdoc}
  41. */
  42. public function attributeLabels()
  43. {
  44. return [
  45. 'id' => 'ID',
  46. 'goods_id' => '商品ID',
  47. 'goods_attr_id' => '规格ID',
  48. 'min_score' => '最低抵扣积分',
  49. 'min_limit' => '是否限制[0=不限制,1=限制]',
  50. 'max_score' => '最高抵扣积分',
  51. 'max_limit' => '是否限制[0=不限制,1=限制]',
  52. 'status' => '状态[-1:删除;0:禁用;1启用]',
  53. 'created_at' => '创建时间',
  54. 'updated_at' => '更新时间',
  55. ];
  56. }
  57. /**
  58. * @notes:数据保存
  59. * @param $params
  60. * @return bool|void
  61. * @author: lun
  62. * @Time: 2022/10/22 15:59
  63. */
  64. public static function setData($params)
  65. {
  66. try {
  67. foreach ($params as $attr) {
  68. $attr['min_limit'] = $attr['min_limit'] === 'true' ? 1 : 0;
  69. $attr['max_limit'] = $attr['max_limit'] === 'true' ? 1 : 0;
  70. $model = self::findOne(['goods_id' => $attr['goods_id'], 'goods_attr_id' => $attr['goods_attr_id'], 'status' => StatusEnum::ENABLED]);
  71. if (empty($model)) {
  72. $model = new self();
  73. $model->loadDefaultValues();
  74. }
  75. if (!$model->load($attr, '') || !$model->save()) throw new Exception($model->getErrorMessage());
  76. }
  77. return true;
  78. } catch (\Exception $e) {
  79. self::setStaticError($e->getMessage());
  80. return false;
  81. }
  82. }
  83. /**
  84. * @notes:批量添加规格
  85. * @param $attrs
  86. * @param $setting
  87. * @param $score_deduct_ratio
  88. * @return bool
  89. * @author: lun
  90. * @Time: 2022/11/15 15:37
  91. */
  92. public static function percentSetData($attrs, $setting, $score_deduct_ratio)
  93. {
  94. try {
  95. foreach ($attrs as $attr) {
  96. $attr['min_limit'] = $setting['min_limit'] === 'true' ? 1 : 0;
  97. $attr['max_limit'] = $setting['max_limit'] === 'true' ? 1 : 0;
  98. $attr['min_score'] = ceil($setting['min_score'] * $score_deduct_ratio * $attr['price']) / 100;
  99. $attr['max_score'] = ceil($setting['max_score'] * $score_deduct_ratio * $attr['price']) / 100;
  100. $model = self::findOne(['goods_id' => $attr['goods_id'], 'goods_attr_id' => $attr['goods_attr_id'], 'status' => StatusEnum::ENABLED]);
  101. if (empty($model)) {
  102. $model = new self();
  103. $model->loadDefaultValues();
  104. }
  105. if (!$model->load($attr, '') || !$model->save()) throw new Exception($model->getErrorMessage());
  106. }
  107. return true;
  108. } catch (\Exception $e) {
  109. self::setStaticError($e->getMessage());
  110. return false;
  111. }
  112. }
  113. }