CloudStockTwoUpgradeBag.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. namespace addons\CloudStockTwo\common\models;
  3. use common\enums\StatusEnum;
  4. use common\models\base\BaseActiveRecord;
  5. use Yii;
  6. /**
  7. * This is the model class for table "{{%addons_cloud_stock_upgrade_bag".
  8. *
  9. * @property int $id
  10. * @property int $mall_id 商城id
  11. * @property string $name 升级礼包名称
  12. * @property int $level 升级等级权重
  13. * @property int $goods_id 升级商品id
  14. * @property string $give_goods_num 赠送商品数量
  15. * @property string $give_goods_id 赠送商品id
  16. * @property int $status 状态[-1:删除;0:禁用;1启用]
  17. * @property int $created_at
  18. * @property int $updated_at
  19. */
  20. class CloudStockTwoUpgradeBag extends BaseActiveRecord
  21. {
  22. /**
  23. * {@inheritdoc}
  24. */
  25. public static function tableName()
  26. {
  27. return '{{%addons_cloud_stock_two_upgrade_bag}}';
  28. }
  29. /**
  30. * {@inheritdoc}
  31. */
  32. public function rules()
  33. {
  34. return [
  35. [['mall_id', 'name', 'level', 'goods_id'], 'required'],
  36. [['mall_id', 'level', 'goods_id', 'status', 'created_at', 'updated_at'], 'integer'],
  37. [['name'], 'string', 'max' => 255],
  38. [['give_goods_num', 'give_goods_id'], 'string', 'max' => 1000],
  39. [['give_goods_info'],'string'],
  40. ];
  41. }
  42. /**
  43. * {@inheritdoc}
  44. */
  45. public function attributeLabels()
  46. {
  47. return [
  48. 'id' => 'ID',
  49. 'mall_id' => '商城id',
  50. 'name' => '升级礼包名称',
  51. 'level' => '升级等级权重',
  52. 'goods_id' => '升级商品id',
  53. 'give_goods_num' => '赠送商品数量',
  54. 'give_goods_id' => '赠送商品id',
  55. 'status' => '状态[-1:删除;0:禁用;1启用]',
  56. 'created_at' => 'Created At',
  57. 'updated_at' => 'Updated At',
  58. 'give_goods_info' => 'Give Goods Info'
  59. ];
  60. }
  61. public static function setData(array $params)
  62. {
  63. try {
  64. $query = self::find()->where(['mall_id'=>$params['mall_id'],'goods_id'=>$params['goods_id'],'status' => [StatusEnum::ENABLED,StatusEnum::DISABLED]]);
  65. if(!empty($params['id'])){
  66. $query->andWhere(['<>','id',$params['id']]);
  67. $model = self::findOne(['mall_id' => $params['mall_id'], 'id' => $params['id'], 'status' => [StatusEnum::ENABLED,StatusEnum::DISABLED]]);
  68. if (empty($model)) throw new \Exception('服务不存在或已删除');
  69. }else{
  70. // $res = self::findOne(['mall_id'=>$params['mall_id'],'level'=>$params['level'],'status'=>StatusEnum::ENABLED]);
  71. // if($res) throw new \Exception('该等级已经存在有升级礼包');
  72. $model = new self();
  73. $model->loadDefaultValues();
  74. }
  75. $res = $query->one();
  76. if ($res) throw new \Exception('该商品已经被添加过');
  77. if (!$model->load($params, '')) throw new \Exception($model->getErrorMessage());
  78. if (!$model->save()) throw new \Exception($model->getErrorMessage());
  79. return $model;
  80. } catch (\Exception $e) {
  81. self::$static_error = $e->getMessage();
  82. return false;
  83. }
  84. }
  85. }