Open.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. namespace backend\models;
  3. use common\helpers\Idcard;
  4. use common\helpers\Utils;
  5. use Yii;
  6. /**
  7. * This is the model class for table "crm_open".
  8. *
  9. * @property integer $id
  10. * @property integer $open_type
  11. * @property integer $type
  12. * @property string $name
  13. * @property string $id_card
  14. * @property string $email
  15. * @property string $mobile
  16. * @property string $id_card_file_path0
  17. * @property string $id_card_file_path1
  18. * @property integer $in_time
  19. * @property string $rid
  20. * @property integer $mt4_login
  21. * @property string $mt4_login_pwd
  22. * @property string $mt4_view_pwd
  23. * @property integer $member_id
  24. * @property string $address
  25. * @property integer $level
  26. */
  27. class Open extends \yii\db\ActiveRecord
  28. {
  29. /**
  30. * @var array
  31. */
  32. public static $typeTextMap = [
  33. 0 => '等待',
  34. 1 => '处理中',
  35. 2 => '完成',
  36. 3 => '拒绝',
  37. ];
  38. /**
  39. * @inheritdoc
  40. */
  41. public static function tableName()
  42. {
  43. return 'crm_open';
  44. }
  45. /**
  46. * @return \yii\db\Connection the database connection used by this AR class.
  47. */
  48. public static function getDb()
  49. {
  50. return Yii::$app->get('dbXcrm');
  51. }
  52. /**
  53. * @inheritdoc
  54. */
  55. public function rules()
  56. {
  57. return [
  58. [['open_type', 'type', 'in_time', 'mt4_login', 'member_id', 'level'], 'integer'],
  59. [['name', 'id_card', 'email', 'in_time'], 'required'],
  60. [['name', 'id_card', 'email', 'id_card_file_path0', 'id_card_file_path1', 'rid', 'mt4_login_pwd', 'mt4_view_pwd'], 'string', 'max' => 255],
  61. [['address'], 'string', 'max' => 3000],
  62. ['id_card', 'checkIdCard', 'on' => ['frontend']],
  63. ['email', 'checkEmail', 'on' => ['frontend']],
  64. // ['mobile', 'checkMobile'],
  65. ];
  66. }
  67. public function checkEmail($attribute, $params = [])
  68. {
  69. if (!$this->hasErrors()) {
  70. $where = [
  71. 'and',
  72. ['=', 'email', $this->email],
  73. ['in', 'type', [0, 1, 2]],
  74. ];
  75. $exists = static::find()->where($where)->count();
  76. if (Member::checkEmailExist($this->email)) {
  77. $this->addError($attribute, '邮箱' . $this->email . '已被占用');
  78. }
  79. }
  80. }
  81. public static function findById($id)
  82. {
  83. return static::find()->where(['id'=> $id])->asArray()->limit(1)->one();
  84. }
  85. public function checkIdCard($attribute, $params = [])
  86. {
  87. if (!$this->hasErrors()) {
  88. if (Idcard::getInstance()->isChinaIDCard($this->id_card) == false) {
  89. //$this->addError($attribute, '身份证号码格式错误');
  90. }
  91. $where = [
  92. 'and',
  93. ['=', 'id_card', $this->email],
  94. ['in', 'type', [0, 1, 2]],
  95. ];
  96. $exists = static::find()->where($where)->count();
  97. if (Member::checkIdNoExist($this->id_card)) {
  98. $this->addError($attribute, '身份证' . $this->id_card . '已被占用');
  99. }
  100. }
  101. }
  102. public function checkMobile($attribute, $params = [])
  103. {
  104. if (!$this->hasErrors()) {
  105. if (Utils::checkPhoneNumber($this->mobile) == false) {
  106. $this->addError($attribute, '手机号码格式错误');
  107. }
  108. }
  109. }
  110. /**
  111. * @inheritdoc
  112. */
  113. public function attributeLabels()
  114. {
  115. return [
  116. 'id' => 'ID',
  117. 'open_type' => 'Open Type',
  118. 'type' => 'Type',
  119. 'name' => 'Name',
  120. 'id_card' => 'Id Card',
  121. 'email' => 'Email',
  122. 'mobile' => 'Mobile',
  123. 'id_card_file_path0' => 'Id Card File Path0',
  124. 'id_card_file_path1' => 'Id Card File Path1',
  125. 'in_time' => 'In Time',
  126. 'rid' => 'Rid',
  127. 'mt4_login' => 'Mt4 Login',
  128. 'mt4_login_pwd' => 'Mt4 Login Pwd',
  129. 'mt4_view_pwd' => 'Mt4 View Pwd',
  130. 'member_id' => 'Member ID',
  131. 'address' => 'Address',
  132. 'level' => 'Level',
  133. ];
  134. }
  135. /**
  136. * @param string $email
  137. * @return bool
  138. */
  139. public static function checkEmailExist($email)
  140. {
  141. return static::find()->where(['email' => $email, 'type' => 0])->exists();
  142. }
  143. /**
  144. * @param string $idNo
  145. * @return bool
  146. */
  147. public static function checkIdNoExist($idNo)
  148. {
  149. return static::find()
  150. ->where(['id_card' => $idNo, 'open_type' => 2])
  151. ->andWhere(['<>', 'type', 3])
  152. ->exists();
  153. }
  154. /**
  155. * 开户数量
  156. * @param int $type 0申请,1处理中,2完成,3拒绝
  157. * @return int
  158. */
  159. public static function countByType($type)
  160. {
  161. if (!is_numeric($type)) {
  162. return 0;
  163. }
  164. return static::find()->where(['type' => $type])->count();
  165. }
  166. }