RebateStatistics.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. /*
  3. * @Descripttion:
  4. * @copyright: Copyright (c) 2021 广东七件事集团
  5. * @Author: lun
  6. * @Date: 2022-01-02 16:32:57
  7. */
  8. namespace addons\Rebate\common\models;
  9. use Yii;
  10. use yii\db\Exception;
  11. /**
  12. * This is the model class for table "qimall_addons_rebate_statistics".
  13. *
  14. * @property int $id
  15. * @property int $mall_id 商城id
  16. * @property int $r_id 对应qimall_addons_rebate.id
  17. * @property int|null $join_num 参与人数
  18. * @property int|null $pay_num 已支付订单数量
  19. * @property float $pay_money 实付金额,单位:元
  20. * @property int $status 状态[-1:删除;0:禁用;1启用]
  21. * @property int $created_at 创建时间
  22. * @property int $updated_at 更新时间
  23. */
  24. class RebateStatistics extends \common\models\base\BaseActiveRecord
  25. {
  26. /**
  27. * {@inheritdoc}
  28. */
  29. public static function tableName()
  30. {
  31. return 'qimall_addons_rebate_statistics';
  32. }
  33. /**
  34. * {@inheritdoc}
  35. */
  36. public function rules()
  37. {
  38. return [
  39. [['mall_id'], 'required'],
  40. [['mall_id', 'r_id', 'join_num', 'pay_num', 'status', 'created_at', 'updated_at'], 'integer'],
  41. [['pay_money'], 'number'],
  42. ];
  43. }
  44. /**
  45. * {@inheritdoc}
  46. */
  47. public function attributeLabels()
  48. {
  49. return [
  50. 'id' => 'ID',
  51. 'mall_id' => '商城id',
  52. 'r_id' => '对应qimall_addons_rebate.id',
  53. 'join_num' => '参与人数',
  54. 'pay_num' => '已支付订单数量',
  55. 'pay_money' => '实付金额,单位:元',
  56. 'status' => '状态[-1:删除;0:禁用;1启用]',
  57. 'created_at' => '创建时间',
  58. 'updated_at' => '更新时间',
  59. ];
  60. }
  61. /**
  62. * 保存数据
  63. * @Author: lun
  64. * @DateTime: 2022/1/6 0006 19:36
  65. * @Copyright: copyright (c) 2021 广东七件事集团
  66. * @param $mall_id
  67. * @param array $params
  68. * @return bool
  69. */
  70. public static function setData($mall_id, array $params){
  71. try{
  72. // 统计人数
  73. $rebate_buy_arr = RebateBuy::find()->select('user_id')->where(['mall_id' => $mall_id, 'r_id' => $params['r_id'], 'status' => 1])->groupBy("user_id")->asArray()->all();
  74. $num = count($rebate_buy_arr) ?? 0;
  75. $model = self::find()->where(['mall_id' => $mall_id, 'r_id' => $params['r_id'], 'status' => 1])->one();
  76. if(empty($model)){
  77. $model = new self();
  78. $model->loadDefaultValues();
  79. $model->mall_id = $mall_id;
  80. $model->r_id = $params['r_id'];
  81. }
  82. $model->join_num = $num;
  83. $model->pay_num = $model->pay_num + 1;
  84. !empty($params['pay_money']) && $model->pay_money = $model->pay_money + $params['pay_money'];
  85. if(!$model->save()) throw new Exception($model->getErrorMessage());
  86. return true;
  87. }catch(\Exception $e){
  88. self::setStaticError($e->getMessage());
  89. return false;
  90. }
  91. }
  92. }