Open.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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 function checkIdCard($attribute, $params = [])
  82. {
  83. if (!$this->hasErrors()) {
  84. if (Idcard::getInstance()->isChinaIDCard($this->id_card) == false) {
  85. //$this->addError($attribute, '身份证号码格式错误');
  86. }
  87. $where = [
  88. 'and',
  89. ['=', 'id_card', $this->email],
  90. ['in', 'type', [0, 1, 2]],
  91. ];
  92. $exists = static::find()->where($where)->count();
  93. if (Member::checkIdNoExist($this->id_card)) {
  94. $this->addError($attribute, '身份证' . $this->id_card . '已被占用');
  95. }
  96. }
  97. }
  98. public function checkMobile($attribute, $params = [])
  99. {
  100. if (!$this->hasErrors()) {
  101. if (Utils::checkPhoneNumber($this->mobile) == false) {
  102. $this->addError($attribute, '手机号码格式错误');
  103. }
  104. }
  105. }
  106. /**
  107. * @inheritdoc
  108. */
  109. public function attributeLabels()
  110. {
  111. return [
  112. 'id' => 'ID',
  113. 'open_type' => 'Open Type',
  114. 'type' => 'Type',
  115. 'name' => 'Name',
  116. 'id_card' => 'Id Card',
  117. 'email' => 'Email',
  118. 'mobile' => 'Mobile',
  119. 'id_card_file_path0' => 'Id Card File Path0',
  120. 'id_card_file_path1' => 'Id Card File Path1',
  121. 'in_time' => 'In Time',
  122. 'rid' => 'Rid',
  123. 'mt4_login' => 'Mt4 Login',
  124. 'mt4_login_pwd' => 'Mt4 Login Pwd',
  125. 'mt4_view_pwd' => 'Mt4 View Pwd',
  126. 'member_id' => 'Member ID',
  127. 'address' => 'Address',
  128. 'level' => 'Level',
  129. ];
  130. }
  131. /**
  132. * @param string $email
  133. * @return bool
  134. */
  135. public static function checkEmailExist($email)
  136. {
  137. return static::find()->where(['email' => $email, 'type' => 0])->exists();
  138. }
  139. /**
  140. * @param string $idNo
  141. * @return bool
  142. */
  143. public static function checkIdNoExist($idNo)
  144. {
  145. return static::find()
  146. ->where(['id_card' => $idNo, 'open_type' => 2])
  147. ->andWhere(['<>', 'type', 3])
  148. ->exists();
  149. }
  150. /**
  151. * 开户数量
  152. * @param int $type 0申请,1处理中,2完成,3拒绝
  153. * @return int
  154. */
  155. public static function countByType($type)
  156. {
  157. if (!is_numeric($type)) {
  158. return 0;
  159. }
  160. return static::find()->where(['type' => $type])->count();
  161. }
  162. }