CloudStockUpgradeBag.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. namespace addons\CloudStock\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. * @property string $level_reward
  20. * @property int $is_percent
  21. */
  22. class CloudStockUpgradeBag extends BaseActiveRecord
  23. {
  24. /**
  25. * {@inheritdoc}
  26. */
  27. public static function tableName()
  28. {
  29. return '{{%addons_cloud_stock_upgrade_bag}}';
  30. }
  31. /**
  32. * {@inheritdoc}
  33. */
  34. public function rules()
  35. {
  36. return [
  37. [['mall_id', 'name', 'level', 'goods_id'], 'required'],
  38. [['mall_id', 'level', 'status', 'created_at', 'updated_at','is_percent'], 'integer'],
  39. [['name'], 'string', 'max' => 255],
  40. [['level_reward'], 'string'],
  41. [['goods_id', 'give_goods_id','give_goods_num'], 'string'],
  42. ];
  43. }
  44. /**
  45. * {@inheritdoc}
  46. */
  47. public function attributeLabels()
  48. {
  49. return [
  50. 'id' => 'ID',
  51. 'mall_id' => '商城id',
  52. 'name' => '升级礼包名称',
  53. 'level' => '升级等级权重',
  54. 'goods_id' => '升级商品id',
  55. 'is_percent' => 'is_percent',
  56. 'give_goods_num' => '赠送商品数量',
  57. 'give_goods_id' => '赠送商品id',
  58. 'status' => '状态[-1:删除;0:禁用;1启用]',
  59. 'created_at' => 'Created At',
  60. 'updated_at' => 'Updated At',
  61. 'level_reward' => '直推、间推奖励',
  62. ];
  63. }
  64. public static function setData(array $params)
  65. {
  66. try {
  67. $query = self::find()->where(['mall_id'=>$params['mall_id'],'status' => [StatusEnum::ENABLED,StatusEnum::DISABLED]]);
  68. if(!empty($params['id'])){
  69. $query->andWhere(['<>','id',$params['id']]);
  70. $model = self::findOne(['mall_id' => $params['mall_id'], 'id' => $params['id'], 'status' => [StatusEnum::ENABLED,StatusEnum::DISABLED]]);
  71. if (empty($model)) throw new \Exception('服务不存在或已删除');
  72. }else{
  73. // $res = self::findOne(['mall_id'=>$params['mall_id'],'level'=>$params['level'],'status'=>StatusEnum::ENABLED]);
  74. // if($res) throw new \Exception('该等级已经存在有升级礼包');
  75. $model = new self();
  76. $model->loadDefaultValues();
  77. }
  78. // $res = $query->one();
  79. // if ($res) throw new \Exception('该商品已经被添加过');
  80. if (!$model->load($params, '')) throw new \Exception($model->getErrorMessage());
  81. if (!$model->save()) throw new \Exception($model->getErrorMessage());
  82. return $model;
  83. } catch (\Exception $e) {
  84. self::$static_error = $e->getMessage();
  85. return false;
  86. }
  87. }
  88. public static function getGoodsIds($mall_id)
  89. {
  90. $goods_ids = CloudStockUpgradeBag::find()->where(['mall_id' => $mall_id, 'status' => StatusEnum::ENABLED, 'is_enable' => StatusEnum::ENABLED])->select('goods_id')->column();
  91. $goods_id_arr = [];
  92. foreach ($goods_ids as $item) {
  93. $temp_goods_id_arr = json_decode($item, true);
  94. if (!empty($temp_goods_id_arr)) {
  95. $goods_id_arr = array_merge($goods_id_arr, $temp_goods_id_arr);
  96. } else {
  97. $goods_id_arr[] = $item;
  98. }
  99. }
  100. return $goods_id_arr;
  101. }
  102. }