PurchaseManager.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. namespace addons\Purchase\common\models;
  3. use addons\Store\common\models\Store;
  4. use common\enums\StatusEnum;
  5. use common\models\user\User;
  6. use Yii;
  7. use yii\db\Exception;
  8. /**
  9. * This is the model class for table "{{%addons_purchase_manager}}".
  10. *
  11. * @property int $id
  12. * @property int|null $mall_id 商城ID
  13. * @property int|null $user_id 会员ID
  14. * @property int|null $mobile 手机号码
  15. * @property string|null $license 营业执照
  16. * @property string|null $store_info 店铺抬头
  17. * @property string|null $idcart_front 身份证照(正)
  18. * @property string|null $idcart_back 身份证照(反)
  19. * @property string|null $idcart_hands 身份证照(手持)
  20. * @property int $is_poss 审核状态[0审核中 1审核通过 -1拒绝]
  21. * @property string|null $reason 失败原因
  22. * @property int $status 状态[0禁用 1启用 -1删除]
  23. * @property int $created_at 创建时间
  24. * @property int $updated_at 修改时间
  25. */
  26. class PurchaseManager extends \common\models\base\BaseActiveRecord
  27. {
  28. /**
  29. * {@inheritdoc}
  30. */
  31. public static function tableName()
  32. {
  33. return '{{%addons_purchase_manager}}';
  34. }
  35. /**
  36. * {@inheritdoc}
  37. */
  38. public function rules()
  39. {
  40. return [
  41. [['mall_id', 'user_id', 'mobile', 'is_poss', 'status', 'created_at', 'updated_at'], 'integer'],
  42. [['license', 'store_info', 'idcart_front', 'idcart_back', 'idcart_hands', 'reason'], 'string'],
  43. ];
  44. }
  45. /**
  46. * {@inheritdoc}
  47. */
  48. public function attributeLabels()
  49. {
  50. return [
  51. 'id' => 'ID',
  52. 'mall_id' => '商城ID',
  53. 'user_id' => '会员ID',
  54. 'mobile' => '手机号码',
  55. 'license' => '营业执照',
  56. 'store_info' => '店铺抬头',
  57. 'idcart_front' => '身份证照(正)',
  58. 'idcart_back' => '身份证照(反)',
  59. 'idcart_hands' => '身份证照(手持)',
  60. 'is_poss' => '审核状态[0审核中 1审核通过 -1拒绝]',
  61. 'reason' => '失败原因',
  62. 'status' => '状态[0禁用 1启用 -1删除]',
  63. 'created_at' => '创建时间',
  64. 'updated_at' => '修改时间',
  65. ];
  66. }
  67. /**
  68. * @notes:关联用户
  69. * @return \yii\db\ActiveQuery
  70. * @author: lun
  71. * @Time: 2022/10/22 17:45
  72. */
  73. public function getUser()
  74. {
  75. return $this->hasOne(User::class, ['id' => 'user_id'])->select('id,nickname,mobile,avatar_url');
  76. }
  77. /**
  78. * @notes:数据保存
  79. * @param $mall_id
  80. * @param $params
  81. * @return bool
  82. * @author: lun
  83. * @Time: 2022/10/22 18:28
  84. */
  85. public static function setData($mall_id, $params)
  86. {
  87. $trans = Yii::$app->db->beginTransaction();
  88. try{
  89. $model = self::find()->where(['mall_id' => $mall_id, 'user_id' => $params['user_id']])->andWhere(['<>','status',StatusEnum::DELETE])->one();
  90. if (empty($model)) {
  91. $model = new self();
  92. $model->mall_id = $mall_id;
  93. $model->is_poss = StatusEnum::DISABLED;
  94. $model->loadDefaultValues();
  95. unset($params['is_poss']);
  96. } else {
  97. // 非首次审核处理
  98. }
  99. if (!$model->load($params, '') || !$model->save()) throw new Exception($model->getErrorMessage());
  100. $trans->commit();
  101. return $model->toArray();
  102. }catch(\Exception $e){
  103. $trans->rollBack();
  104. self::$static_error = $e->getMessage();
  105. return false;
  106. }
  107. }
  108. /**
  109. * @notes:驳回,同意,删除处理
  110. * @param $mall_id
  111. * @param $params
  112. * @return bool
  113. * @author: lun
  114. * @Time: 2022/10/22 18:27
  115. */
  116. public static function handle($mall_id, $params)
  117. {
  118. $trans = Yii::$app->db->beginTransaction();
  119. try{
  120. $model = self::findOne(['mall_id' => $mall_id, 'id' => $params['id']]);
  121. if (empty($model)) throw new \Exception('该用户数据不存在');
  122. if (!empty($params['is_poss'])) {
  123. $model->is_poss = $params['is_poss'];
  124. $params['is_poss'] == StatusEnum::ENABLED && $model->reason = '';
  125. $params['is_poss'] == StatusEnum::DELETE && $params['reason'] && $model->reason = $params['reason'];
  126. }
  127. if (!empty($params['status'])) $model->status = $params['status'];
  128. if (!$model->save()) throw new Exception($model->getErrorMessage());
  129. $trans->commit();
  130. return true;
  131. }catch(\Exception $e){
  132. $trans->rollBack();
  133. self::$static_error = $e->getMessage();
  134. return false;
  135. }
  136. }
  137. /**
  138. * @notes:检查用户是否审核通过
  139. * @param $user_id
  140. * @return bool
  141. * @author: lun
  142. * @Time: 2022/10/31 11:32
  143. */
  144. public static function check($user_id)
  145. {
  146. $data = self::find()->where(['user_id' => $user_id, 'status' => StatusEnum::ENABLED])->asArray()->one();
  147. if (empty($data)) return false;
  148. if ($data['is_poss'] != 1) return false;
  149. return true;
  150. }
  151. }