AgentApply.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. namespace addons\Agent\common\models;
  3. use common\enums\AddonsEnum;
  4. use common\enums\StatusEnum;
  5. use common\helpers\FormatHelper;
  6. use common\logic\common\ConditionUpgrade;
  7. use common\models\base\BaseActiveRecord;
  8. use common\models\user\User;
  9. use PHPUnit\Util\Exception;
  10. use Yii;
  11. /**
  12. * This is the model class for table "qimall_addons_agent_apply".
  13. *
  14. * @property int $id
  15. * @property int $mall_id
  16. * @property int $user_id
  17. * @property string $mobile
  18. * @property string $name
  19. * @property string $remark
  20. * @property string $check_remark
  21. * @property int $status 状态[-1:删除;0:禁用;1启用]
  22. * @property int $check_status 0待审核1审核通过2不通过
  23. * @property int $created_at
  24. * @property int $updated_at
  25. */
  26. class AgentApply extends BaseActiveRecord
  27. {
  28. const CHECK_STATUS_WAIT = 0;
  29. const CHECK_STATUS_SUCCESS = 1;
  30. const CHECK_STATUS_FAIL = 2;
  31. /**
  32. * {@inheritdoc}
  33. */
  34. public static function tableName()
  35. {
  36. return '{{%addons_agent_apply}}';
  37. }
  38. /**
  39. * {@inheritdoc}
  40. */
  41. public function rules()
  42. {
  43. return [
  44. [['mall_id', 'user_id', 'mobile', 'name'], 'required'],
  45. [['mall_id', 'user_id', 'status', 'check_status', 'created_at', 'updated_at'], 'integer'],
  46. [['mobile', 'name'], 'string', 'max' => 45],
  47. [['remark','check_remark'], 'string', 'max' => 1000],
  48. ];
  49. }
  50. /**
  51. * {@inheritdoc}
  52. */
  53. public function attributeLabels()
  54. {
  55. return [
  56. 'id' => 'ID',
  57. 'mall_id' => 'Mall ID',
  58. 'user_id' => 'User ID',
  59. 'mobile' => 'Mobile',
  60. 'name' => 'Name',
  61. 'remark' => 'remark',
  62. 'check_remark' => 'check_remark',
  63. 'status' => 'Status',
  64. 'check_status' => 'Check Status',
  65. 'created_at' => 'Created At',
  66. 'updated_at' => 'Updated At',
  67. ];
  68. }
  69. public function getUser()
  70. {
  71. return $this->hasOne(User::class, ['id' => 'user_id']);
  72. }
  73. public static function setData(array $params)
  74. {
  75. $t = Yii::$app->db->beginTransaction();
  76. try {
  77. if(!empty($params['id'])){
  78. $model = self::findOne(['mall_id' => $params['mall_id'], 'id' => $params['id'], 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]);
  79. if (empty($model)) throw new Exception('服务不存在或已删除');
  80. $params['user_id'] = $model->user_id;
  81. }else{
  82. $model = new self();
  83. $model->loadDefaultValues();
  84. }
  85. if (!$model->load($params, '')) throw new \Exception($model->getErrorMessage());
  86. if (!$model->save()) throw new \Exception($model->getErrorMessage());
  87. if(!empty($params['check_status'])&&$params['check_status']==1){
  88. //不需要审核
  89. $res = Agent::findOne(['mall_id'=>$params['mall_id'],'user_id'=>$params['user_id'],'status'=>[StatusEnum::ENABLED,StatusEnum::DISABLED]]);
  90. if(!empty($res)) throw new Exception('该会员已经开启渠道分红');
  91. // $agent_model = new Agent();
  92. // $agent_model->mall_id = $params['mall_id'];
  93. // $agent_model->user_id = $params['user_id'];
  94. // $agent_model->level = 0;
  95. // $agent_model->upgrade_level_at = 0;
  96. // $agent_model->upgrade_status = 0;
  97. // $res = $agent_model->save();
  98. // if(!$res) throw new \Exception($agent_model->getErrorMessage());
  99. }
  100. $t->commit();
  101. return $model;
  102. } catch (\Exception $e) {
  103. $t->rollBack();
  104. self::$static_error = $e->getMessage();
  105. return false;
  106. }
  107. }
  108. /**
  109. * 申请通过后,升级处理
  110. * @param $params
  111. * @return array|bool
  112. */
  113. public function upgradeLevel($params)
  114. {
  115. try{
  116. $mall_id = $params['mall_id'];
  117. $user_id = $params['user_id'];
  118. $order = [
  119. 'mall_id' => $mall_id,
  120. 'user_id' => $user_id,
  121. 'id' => 0
  122. ];
  123. $cond_upgrade = new \addons\Agent\common\logic\ConditionUpgrade();
  124. $cond_uids = $cond_upgrade->execute(AddonsEnum::ADDONS_NAME_AGENT, (new Agent()), (new AgentLevel()), $order);
  125. return $cond_uids;
  126. }catch(\Exception $e){
  127. $exception = FormatHelper::exception($e, 'AgentApply:upgradeLevel err:' . $e->getMessage());
  128. Yii::error($exception);
  129. return false;
  130. }
  131. }
  132. }