AddonsSudoku.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. namespace addons\Sudoku\common\models;
  3. use addons\Coupon\common\models\AddonsCoupon;
  4. use common\enums\StatusEnum;
  5. use common\models\goods\Goods;
  6. use Yii;
  7. use yii\db\Exception;
  8. /**
  9. * This is the model class for table "qimall_addons_sudoku".
  10. *
  11. * @property int $id
  12. * @property int $mall_id 商城ID
  13. * @property string|null $name 别名
  14. * @property int|null $type 1.红包2.优惠券3.积分4.实物.5.无
  15. * @property int|null $goods_id 商品
  16. * @property int|null $score 积分
  17. * @property float|null $red_packet_amount 红包价格
  18. * @property string|null $image_url 图片
  19. * @property int|null $coupon_id 优惠券
  20. * @property int|null $stock 库存
  21. * @property float|null $probability 概率
  22. * @property int|null $prize_id 奖品ID
  23. * @property int $created_at 创建时间
  24. * @property int|null $updated_at 更新时间
  25. */
  26. class AddonsSudoku extends \common\models\base\BaseActiveRecord
  27. {
  28. /**
  29. * {@inheritdoc}
  30. */
  31. public static function tableName()
  32. {
  33. return '{{%addons_sudoku}}';
  34. }
  35. /**
  36. * {@inheritdoc}
  37. */
  38. public function rules()
  39. {
  40. return [
  41. [['mall_id', 'created_at'], 'required'],
  42. [['mall_id', 'type', 'goods_id', 'coupon_id', 'stock', 'prize_id', 'created_at', 'updated_at'], 'integer'],
  43. [['red_packet_amount','score', 'probability'], 'number'],
  44. [['name', 'image_url'], 'string', 'max' => 255],
  45. ];
  46. }
  47. /**
  48. * {@inheritdoc}
  49. */
  50. public function attributeLabels()
  51. {
  52. return [
  53. 'id' => 'ID',
  54. 'mall_id' => '商城ID',
  55. 'name' => '别名',
  56. 'type' => '1.红包2.优惠卷3.积分4.实物.5.无',
  57. 'goods_id' => '商品',
  58. 'score' => '积分',
  59. 'red_packet_amount' => '红包价格',
  60. 'image_url' => '图片',
  61. 'coupon_id' => '优惠券',
  62. 'stock' => '库存',
  63. 'probability' => '概率',
  64. 'prize_id' => '奖品ID',
  65. 'created_at' => '创建时间',
  66. 'updated_at' => '更新时间',
  67. ];
  68. }
  69. //管理优惠卷
  70. public function getCoupon()
  71. {
  72. return $this->hasOne(AddonsCoupon::class, ['id' => 'coupon_id'])->select('id,name,discount,min_price,sub_price,total_count,receive_count,status');
  73. }
  74. //管理商品
  75. public function getGoods()
  76. {
  77. return $this->hasOne(Goods::class, ['id' => 'goods_id'])->select('id,goods_name,price,cover_pic,status,stock,is_on_sale');
  78. }
  79. /**
  80. * 保存数据
  81. * @Author: ltm
  82. * @DateTime: 2021/12/18 0022 17:13
  83. * @Copyright: copyright (c) 2021 广东七件事集团
  84. * @param array $params
  85. * @return AddonsMaterial|bool
  86. */
  87. public static function setData(array $params){
  88. try{
  89. if (!empty($params['id'])) {
  90. $model = self::findOne(['id'=>$params['id'],'mall_id'=>$params['mall_id']]);
  91. if (empty($model)) throw new Exception('记录不存在或已删除');
  92. }else{
  93. $model = new self();
  94. $model->loadDefaultValues();
  95. $model->mall_id = $params['mall_id'];
  96. }
  97. if(!$model->load($params,'') || !$model->save()){
  98. throw new Exception($model->getErrorMessage());
  99. }
  100. return $model;
  101. }catch(\Exception $e){
  102. self::$static_error = $e->getMessage();
  103. return false;
  104. }
  105. }
  106. /**
  107. * 保存数据
  108. * @Author: ltm
  109. * @DateTime: 2021/12/27 0022 14:12
  110. * @Copyright: copyright (c) 2021 广东七件事集团
  111. * @param array $params
  112. * @return AddonsMaterial|bool
  113. */
  114. public static function getPrizeName($list){
  115. if($list){
  116. foreach ($list as $k=>$v){
  117. switch ($v['type']){
  118. case '1': //奖励金额
  119. $list[$k]['prize_name'] = '余额 +'.$v['red_packet_amount'].'';
  120. break;
  121. case '2': //奖励优惠卷
  122. $list[$k]['prize_name'] = '优惠券 '.$v['coupon']['name'];
  123. break;
  124. case '3': //奖励积分
  125. $list[$k]['prize_name'] = '积分 +'.$v['score'];
  126. break;
  127. case '4': //奖励商品
  128. $list[$k]['prize_name'] = '商品:'.$v['goods']['goods_name'].'商品一件';
  129. break;
  130. case '5': //谢谢参与
  131. $list[$k]['prize_name'] = '谢谢参与';
  132. break;
  133. }
  134. }
  135. return $list;
  136. }
  137. }
  138. }