RandomRedPacket.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. namespace addons\RandomRedPacket\common\models;
  3. use Yii;
  4. use yii\db\Exception;
  5. use yii\helpers\Json;
  6. /**
  7. * This is the model class for table "qimall_addons_random_red_packet".
  8. *
  9. * @property int $id
  10. * @property int $mall_id 商城id
  11. * @property string $activities_name 活动名称
  12. * @property string $activities_number 活动编号
  13. * @property int $start_at 活动开始时间
  14. * @property int $end_at 活动结束时间
  15. * @property int $reward_type 活动类型 1 激活积分 2 贡献值 3 红包 4 余额
  16. * @property string $min_value 最小发放值
  17. * @property string $max_value 最大发放值
  18. * @property int $sum_total 发放总数
  19. * @property int $surplus_num 剩余个数
  20. * @property int get_number 领取人数
  21. * @property int $detail_show 商品详情页是否展示标识 1是 0否
  22. * @property int $index_show 首页是否展示标识 1是 0否
  23. * @property int $status 状态[-1:删除;0:禁用;1启用]
  24. * @property int $created_at 创建时间
  25. * @property int $updated_at 修改时间
  26. */
  27. class RandomRedPacket extends \common\models\base\BaseActiveRecord
  28. {
  29. const INTEGRAL = 1; //激活积分
  30. const CONTRIBUTION = 2; //贡献值
  31. const RED_PACKET = 3; //红包
  32. const BALANCE = 4; //余额
  33. /**
  34. * {@inheritdoc}
  35. */
  36. public static function tableName()
  37. {
  38. return 'qimall_addons_random_red_packet';
  39. }
  40. /**
  41. * {@inheritdoc}
  42. */
  43. public function rules()
  44. {
  45. return [
  46. [['mall_id', 'activities_name', 'start_at', 'end_at', 'reward_type', 'min_value', 'max_value', 'sum_total', 'surplus_num'], 'required'],
  47. [['mall_id', 'start_at', 'end_at', 'reward_type', 'sum_total', 'get_number', 'surplus_num', 'detail_show', 'index_show', 'status', 'created_at', 'updated_at'], 'integer'],
  48. [['activities_name'], 'string', 'max' => 121],
  49. [['activities_number'], 'string', 'max' => 11],
  50. [['min_value', 'max_value'], 'string', 'max' => 191],
  51. ];
  52. }
  53. /**
  54. * {@inheritdoc}
  55. */
  56. public function attributeLabels()
  57. {
  58. return [
  59. 'id' => 'ID',
  60. 'mall_id' => '商户id',
  61. 'activities_name' => '活动名称',
  62. 'activities_number' => '活动编号',
  63. 'start_at' => '活动开始时间',
  64. 'end_at' => '活动结束时间',
  65. 'reward_type' => '奖品类型',
  66. 'min_value' => '数额最小值',
  67. 'max_value' => '数额最大值',
  68. 'sum_total' => '发放总数',
  69. 'get_number' => '领取人数',
  70. 'surplus_num' => '剩余个数',
  71. 'detail_show' => '商品详情页',
  72. 'index_show' => '首页的商品列表',
  73. 'created_at' => 'Created At',
  74. 'updated_at' => 'Updated At',
  75. ];
  76. }
  77. public static function labelText($type = '')
  78. {
  79. $text = '';
  80. if(!empty($type)){
  81. switch ($type) {
  82. case self::INTEGRAL:
  83. $text = '激活积分';
  84. break;
  85. case self::CONTRIBUTION:
  86. $text = '贡献值';
  87. break;
  88. case self::RED_PACKET:
  89. $text = '红包';
  90. break;
  91. case self::BALANCE:
  92. $text = '余额';
  93. break;
  94. }
  95. }
  96. return $text;
  97. }
  98. public static function setData($mall_id, array $params)
  99. {
  100. try {
  101. if(empty($params['id'])){//新增
  102. $model = new self();
  103. $model->load($params, '');
  104. $model->surplus_num = $model->sum_total;
  105. $model->mall_id = $mall_id;
  106. $goods_ids_arr = array_column($params['goods'],'id');
  107. if(!$model->save()){
  108. throw new Exception($model->getErrorMessage());
  109. }
  110. $model->activities_number = rand(1000,1999).$model->id;
  111. if(!$model->save()){
  112. throw new Exception($model->getErrorMessage());
  113. }
  114. //保存到关系表
  115. foreach ($goods_ids_arr as $v){
  116. $relationModel = new RandomRedPacketRelation();
  117. $relationModel->mall_id = $mall_id;
  118. $relationModel->goods_id = $v;
  119. $relationModel->packet_id = $model->id;
  120. if(!$relationModel->save()){
  121. throw new Exception($relationModel->getErrorMessage());
  122. }
  123. }
  124. return $model;
  125. }else{
  126. $model = self::findOne(['id'=>$params['id']]);
  127. $model->load($params, '');
  128. //根据修改后的红包总数 - 领取人数 = 剩余个数
  129. $surplus_num = $model->sum_total - $model->get_number;
  130. if($surplus_num < 0){
  131. $surplus_num = 0;
  132. }
  133. $model->surplus_num = $surplus_num;
  134. if(!$model->save()){
  135. throw new Exception($model->getErrorMessage());
  136. }
  137. if(!empty($params['goods'])){
  138. $goods_ids_arr = array_column($params['goods'],'id');
  139. //删除
  140. $delRelation = RandomRedPacketRelation::deleteAll(['packet_id'=>$model->id]);
  141. if(!$delRelation){
  142. throw new Exception("修改失败");
  143. }
  144. //保存到关系表
  145. foreach ($goods_ids_arr as $v){
  146. $relationModel = new RandomRedPacketRelation();
  147. $relationModel->mall_id = $mall_id;
  148. $relationModel->goods_id = $v;
  149. $relationModel->packet_id = $model->id;
  150. if(!$relationModel->save()){
  151. throw new Exception($relationModel->getErrorMessage());
  152. }
  153. }
  154. }
  155. return $model;
  156. }
  157. }catch (\Exception $e){
  158. self::setStaticError($e->getMessage());
  159. return false;
  160. }
  161. }
  162. }