ScoreMallGoodsAttr.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. namespace addons\ScoreMall\common\models;
  3. use common\models\base\BaseActiveRecord;
  4. use common\models\goods\GoodsAttr;
  5. use common\enums\StatusEnum;
  6. use Exception;
  7. use Yii;
  8. /**
  9. * This is the model class for table "{{%addons_score_mall_goods_attr}}".
  10. * @property int $id
  11. * @property int $mall_id
  12. * @property int $goods_id 商品ID
  13. * @property int $goods_attr_id 属性ID
  14. * @property string $score 积分
  15. * @property float $money 金额
  16. * @property float $price 商品售价
  17. * @property int $num 已兑换数量
  18. * @property int $type 类型
  19. * @property int $use_coupon 是否使用
  20. * @property int $status
  21. * @property int $created_at 创建时间
  22. * @property int $updated_at 修改时间
  23. */
  24. class ScoreMallGoodsAttr extends BaseActiveRecord
  25. {
  26. /**
  27. * {@inheritdoc}
  28. */
  29. public static function tableName()
  30. {
  31. return '{{%addons_score_mall_goods_attr}}';
  32. }
  33. /**
  34. * {@inheritdoc}
  35. */
  36. public function rules(){
  37. return [
  38. [['goods_id', 'goods_attr_id'], 'required'],
  39. [[ 'status', 'created_at', 'updated_at', 'goods_id', 'goods_attr_id'], 'integer'],
  40. [['deduction_limit'], 'string'],
  41. [['money', 'price', 'score'], 'number'],
  42. ];
  43. }
  44. /**
  45. * {@inheritdoc}
  46. */
  47. public function attributeLabels(){
  48. return [
  49. 'id' => 'ID',
  50. 'goods_id' => '商品ID',
  51. 'goods_attr_id' => '属性ID',
  52. 'score' => '积分',
  53. 'money' => '金额',
  54. 'price' => '商品售价',
  55. 'deduction_limit'=> '抵扣限制',
  56. 'use_coupon' => '是否不参加优惠券抵扣',
  57. 'status' => '状态[-1:删除;0:禁用;1启用]',
  58. 'created_at' => '创建时间',
  59. 'updated_at' => '修改时间'
  60. ];
  61. }
  62. public function getAttr()
  63. {
  64. return $this->hasOne(GoodsAttr::class, ['id' => 'goods_attr_id'])->andWhere(['status' => [StatusEnum::ENABLED]]);
  65. }
  66. public static function getGoodsAttr($goods_id) {
  67. $params = [
  68. 'where' => [['=', 'goods_id', $goods_id], ['<>','status',StatusEnum::DELETE]],
  69. 'select' => ['goods_attr_id', 'score', 'money', 'deduction_limit'],
  70. 'with' => ['attr' => function($query){
  71. $query->select(['original_price', 'name', 'id']);
  72. }]
  73. ];
  74. $model = self::find();
  75. self::paramPack($params, $model);
  76. $list = $model->asArray()->all();
  77. if (!empty($list)) {
  78. foreach ($list as &$val) {
  79. $val['original_price'] = $val['attr']['original_price'];
  80. $val['name'] = $val['attr']['name'];
  81. unset($val['attr']);
  82. }
  83. }
  84. return $list;
  85. }
  86. /**
  87. * 保存数据(多条)
  88. * @param array $params
  89. * @param string $action
  90. */
  91. public static function setData($params, $action) {
  92. try {
  93. if ($action == 'add') {
  94. Yii::$app->db->createCommand()->batchInsert(self::tableName(),
  95. ['goods_id', 'goods_attr_id', 'price', 'score', 'money', 'created_at', 'updated_at', 'deduction_limit'], $params)->execute();
  96. } else {
  97. $attrList = array_column($params, null, 'goods_attr_id');
  98. $goods_attr_ids = array_keys($attrList);
  99. $newAttrList = self::find()->where(['goods_attr_id' => $goods_attr_ids])->all();
  100. foreach ($newAttrList as $val) {
  101. if (isset($attrList[$val->goods_attr_id])) {
  102. $val->price = $attrList[$val->goods_attr_id]['price'];
  103. $val->score = $attrList[$val->goods_attr_id]['score'];
  104. $val->money = $attrList[$val->goods_attr_id]['money'];
  105. $val->deduction_limit = $attrList[$val->goods_attr_id]['deduction_limit'];
  106. $val->updated_at = time();
  107. if (!$val->save()) throw new Exception($val->getErrorMessage());
  108. }
  109. }
  110. }
  111. return true;
  112. } catch (Exception $e) {
  113. self::$static_error = $e->getMessage();
  114. return false;
  115. }
  116. }
  117. }