UserBind.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. <?php
  2. namespace common\models;
  3. use backendApi\modules\v1\models\Admin;
  4. use common\helpers\Date;
  5. use common\helpers\Form;
  6. use common\helpers\user\Info;
  7. use common\helpers\user\Status;
  8. use common\models\forms\UserBindForm;
  9. use Yii;
  10. use yii\base\Exception;
  11. use common\libs\logging\operate\valueType\Config as ValueTypeConfig;
  12. /**
  13. * This is the model class for table "{{%USER_BIND}}".
  14. *
  15. * @property string $ID
  16. * @property string $USER_ID 会员ID
  17. * @property string $MAIN_UID 主会员ID
  18. * @property int $PERIOD_NUM 加入时期数
  19. * @property int $IS_DEL 是否删除
  20. * @property int $CREATED_AT 创建时间
  21. * @property int $UPDATED_AT 修改时间
  22. * @property string $CREATE_ADMIN 创建管理员
  23. * @property string $CREATE_USER 创建会员
  24. * @property string $UPDATE_ADMIN 修改管理员
  25. * @property string $UPDATE_USER 修改会员
  26. * @property int $DELETED_AT 已删除
  27. */
  28. class UserBind extends \common\components\ActiveRecord {
  29. /**
  30. * {@inheritdoc}
  31. */
  32. public static function tableName() {
  33. return '{{%USER_BIND}}';
  34. }
  35. /**
  36. * {@inheritdoc}
  37. */
  38. public function rules() {
  39. return [
  40. [['USER_ID', 'MAIN_UID', 'PERIOD_NUM', 'CREATED_AT'], 'required'],
  41. [['PERIOD_NUM', 'IS_DEL', 'CREATED_AT', 'UPDATED_AT', 'DELETED_AT'], 'integer'],
  42. [['ID', 'USER_ID', 'MAIN_UID', 'CREATE_ADMIN', 'CREATE_USER', 'UPDATE_ADMIN', 'UPDATE_USER'], 'string', 'max' => 32],
  43. [['ID'], 'unique'],
  44. ];
  45. }
  46. /**
  47. * {@inheritdoc}
  48. */
  49. public function attributeLabels() {
  50. return [
  51. 'ID' => 'ID',
  52. 'USER_ID' => '会员ID',
  53. 'MAIN_UID' => '主会员ID',
  54. 'PERIOD_NUM' => '加入时期数',
  55. 'IS_DEL' => '是否删除',
  56. 'CREATE_ADMIN' => '创建管理员',
  57. 'CREATE_USER' => '创建会员',
  58. 'UPDATE_ADMIN' => '修改管理员',
  59. 'UPDATE_USER' => '修改会员',
  60. 'CREATED_AT' => '创建时间',
  61. 'UPDATED_AT' => '修改时间',
  62. 'DELETED_AT' => '已删除',
  63. ];
  64. }
  65. /**
  66. * @return \yii\db\ActiveQuery
  67. */
  68. public function getUser() {
  69. return $this->hasOne(User::className(), ['ID' => 'USER_ID']);
  70. }
  71. /**
  72. * 为删除的会员点位体系设置新的主会员
  73. * @param $userId
  74. */
  75. public static function setNewBindWhenUserDel($userId) {
  76. //如果是主点位
  77. if (self::find()->where('USER_ID=:MAIN_UID AND MAIN_UID=:MAIN_UID AND IS_DEL=0', [':MAIN_UID' => $userId])->orderBy('CREATED_AT ASC')->asArray()->one()) {
  78. self::setNewMainBind($userId);
  79. }
  80. self::updateAll(['IS_DEL' => 1, 'DELETED_AT' => Date::nowTime()], 'USER_ID=:USER_ID', [':USER_ID' => $userId]);
  81. }
  82. /**
  83. * 向下找新的会员成为新的主会员,优先找不以hz开头的会员
  84. * @param $userId
  85. * @param null $idCard
  86. * @return bool
  87. * @throws \Exception
  88. */
  89. public static function setNewMainBind($userId,$idCard=null) {
  90. //是否是主会员
  91. if (!self::find()->where('USER_ID=:USER_ID AND MAIN_UID=:USER_ID AND IS_DEL=0', [':USER_ID' => $userId])->exists()) {
  92. return false;
  93. }
  94. //取同一身份证的最近会员
  95. if(!$idCard) $idCard = Info::getIdCardByUserId($userId);
  96. //把所有实名身份证的会员绑到一起
  97. $hasUsers = User::findAllAsArray('ID_CARD=:ID_CARD AND VERIFIED=1 AND STATUS=1 AND DELETED=0',[':ID_CARD'=>$idCard],'ID');
  98. if($hasUsers){
  99. $period = Period::instance();
  100. $periodNum = $period->getNowPeriodNum();
  101. foreach ($hasUsers as $u){
  102. if(!self::find()->where('USER_ID=:USER_ID AND IS_DEL=0',[':USER_ID'=>$u['ID']])->exists()){
  103. UserInfo::updateAll(['IS_BIND' => 1], 'USER_ID=:USER_ID', [':USER_ID' => $u['ID']]);
  104. // 添加会员合作点位
  105. $model = new UserBind();
  106. $model->USER_ID = $u['ID'];
  107. $model->MAIN_UID = $userId;
  108. $model->PERIOD_NUM = $periodNum;
  109. $model->IS_DEL = 0;
  110. $model->CREATE_ADMIN = \Yii::$app->user->id??'';
  111. $model->CREATED_AT = Date::nowTime();
  112. if (!$model->save()) {
  113. throw new \Exception($model->getErrors());
  114. }
  115. }
  116. }
  117. }
  118. $newMainUid = 0;
  119. if ($newMain = User::find()->select('ID')->where("ID!=:ID AND ID_CARD=:ID_CARD AND IS_UNION=0 AND VERIFIED=1 AND STATUS=1 AND DELETED=0", [':ID'=>$userId, ':ID_CARD' => $idCard])->orderBy('CREATED_AT ASC')->asArray()->one()) {
  120. $newMainUid = $newMain['ID'];
  121. } else {
  122. if ($newMain = User::find()->select('ID')->where("ID!=:ID AND ID_CARD=:ID_CARD AND STATUS=1 AND VERIFIED=1 AND DELETED=0", [':ID'=>$userId, ':ID_CARD' => $idCard])->orderBy('CREATED_AT ASC')->asArray()->one()) {
  123. $newMainUid = $newMain['ID'];
  124. }
  125. }
  126. if ($newMainUid) {
  127. //设置主会员
  128. UserInfo::updateAll(['IS_BIND_MAIN' => 1], 'USER_ID=:USER_ID', [':USER_ID' => $newMainUid]);
  129. //更新绑定表的主会员
  130. self::updateAll(['MAIN_UID' => $newMainUid,'UPDATED_AT'=>Date::nowTime()], 'MAIN_UID=:MAIN_UID AND IS_DEL=0', [':MAIN_UID' => $userId]);
  131. //取消原来主会员
  132. UserInfo::updateAll(['IS_BIND_MAIN' => 0], 'USER_ID=:USER_ID', [':USER_ID' => $userId]);
  133. }
  134. }
  135. /**
  136. * 商城报首购单,rpc中绑定
  137. * @param $userIds
  138. * @param $idCard
  139. * @throws Exception
  140. * @throws \yii\db\Exception
  141. */
  142. public static function zcBind($userIds, $idCard){
  143. if(!empty($userIds)){
  144. // 从一批的首购会员中拿出第一个去数据库里面查找
  145. // 拿身份证编号去数据库中查找看看有没有同身份证并实名认证的会员
  146. $sameUsers = User::find()->select('ID')->where('ID!=:ID AND VERIFIED=1 AND ID_CARD=:ID_CARD', [':ID' => $userIds[0], ':ID_CARD' => $idCard])->orderBy('CREATED_AT ASC')->asArray()->all();
  147. // 如果有
  148. if($sameUsers){
  149. // 把这些相同身份证号的会员追加到$userIds的数组中
  150. $mainUid = $sameUsers[0]['ID'];
  151. foreach ($sameUsers as $u) {
  152. //是否有绑定,有则绑定到绑定位置,无则新建绑定
  153. $hasBind = self::find()->select('MAIN_UID')->where('USER_ID=:USER_ID AND IS_DEL=0', [':USER_ID' => $u['ID']])->asArray()->one();
  154. if ($hasBind) {
  155. $mainUid = $hasBind['MAIN_UID'];
  156. break;
  157. }
  158. }
  159. foreach($userIds as $uid){
  160. // 是否有绑定关系没有就新增
  161. if(!self::find()->select('MAIN_UID')->where('USER_ID=:USER_ID AND IS_DEL=0', [':USER_ID' => $uid])->asArray()->exists()){
  162. $userBindForm = new UserBindForm();
  163. $userBindForm->scenario = 'autoBind';
  164. $userBindForm->userId = $uid;
  165. $userBindForm->mainUid = $mainUid;
  166. if (!$userBindForm->autoBind()) {
  167. throw new Exception('与其他同身份证会员点位绑定失败'.Form::formatErrorsForApi($userBindForm->getErrors()));
  168. }
  169. }
  170. }
  171. }
  172. // 如果没有
  173. else {
  174. // 只有当新注册的会员数量大于一个才需要绑定
  175. if(count($userIds) > 1){
  176. $mainUid = $userIds[0];
  177. unset($userIds[0]);
  178. // 把这些人都循环绑定到一起
  179. foreach($userIds as $uid){
  180. $userBindForm = new UserBindForm();
  181. $userBindForm->scenario = 'autoBind';
  182. $userBindForm->userId = $uid;
  183. $userBindForm->mainUid = $mainUid;
  184. if (!$userBindForm->autoBind()) {
  185. throw new Exception('点位绑定失败'.Form::formatErrorsForApi($userBindForm->getErrors()));
  186. }
  187. }
  188. }
  189. }
  190. }
  191. }
  192. /**
  193. * 自动绑定
  194. * @param $userId
  195. * @param null $idCard
  196. * @param int $verified
  197. * @return bool|string
  198. * @throws \yii\db\Exception
  199. */
  200. public static function autoBind($userId,$idCard=null,$verified=1) {
  201. if($verified==0){
  202. if (self::find()->where('USER_ID=:MAIN_UID AND MAIN_UID=:MAIN_UID AND IS_DEL=0',[':MAIN_UID'=>$userId])->exists()) {
  203. self::setNewMainBind($userId);
  204. }
  205. UserInfo::updateAll(['IS_BIND' => 0], 'USER_ID=:USER_ID', [':USER_ID' => $userId]);
  206. self::updateAll(['IS_DEL' => 1, 'DELETED_AT' => Date::nowTime()], 'USER_ID=:USER_ID', [':USER_ID' => $userId]);
  207. return '取消绑定关系成功';
  208. }else {
  209. if (self::find()->where('USER_ID=:MAIN_UID AND MAIN_UID=:MAIN_UID AND IS_DEL=0',[':MAIN_UID'=>$userId])->exists()) {
  210. return '此会员是主会员,绑定关系未改变';
  211. }
  212. //清除原有绑定关系
  213. self::updateAll(['IS_DEL' => 1, 'DELETED_AT' => Date::nowTime()], 'USER_ID=:USER_ID', [':USER_ID' => $userId]);
  214. UserInfo::updateAll(['IS_BIND' => 0, 'IS_BIND_MAIN' => 0], 'USER_ID=:USER_ID', [':USER_ID' => $userId]);
  215. //获取新的绑定关系
  216. if (!$idCard) {
  217. $idCard = Info::getIdCardByUserId($userId);
  218. }
  219. $hasUsers = User::find()->select('ID')->where('ID!=:ID AND VERIFIED=1 AND ID_CARD=:ID_CARD', [':ID' => $userId, ':ID_CARD' => $idCard])->orderBy('CREATED_AT ASC')->asArray()->all();
  220. //如果有同一身份证的其他点位
  221. if ($hasUsers) {
  222. $mainUid = $hasUsers[0]['ID'];
  223. foreach ($hasUsers as $u) {
  224. //是否有绑定,有则绑定到绑定位置,无则新建绑定
  225. $hasBind = self::find()->select('MAIN_UID')->where('USER_ID=:USER_ID AND IS_DEL=0 AND MAIN_UID!=:MAIN_UID', [':USER_ID' => $u['ID'], ':MAIN_UID' => $userId])->asArray()->one();
  226. if ($hasBind) {
  227. $mainUid = $hasBind['MAIN_UID'];
  228. break;
  229. }
  230. }
  231. $userBindForm = new UserBindForm();
  232. $userBindForm->scenario = 'autoBind';
  233. $userBindForm->userId = $userId;
  234. $userBindForm->mainUid = $mainUid;
  235. if ($result = $userBindForm->autoBind()) {
  236. return '实名验证成功,此会员编号为非主会员编号,不可提现,可以转账';
  237. } else {
  238. return false;
  239. }
  240. } else {
  241. /*$userBindForm = new UserBindForm();
  242. $userBindForm->scenario = 'autoBind';
  243. $userBindForm->userId = $userId;
  244. $userBindForm->mainUid = $userId;
  245. if ($result = $userBindForm->autoBind()) {
  246. return '实名验证成功';
  247. } else {
  248. return false;
  249. }*/
  250. return '实名验证成功';
  251. }
  252. }
  253. }
  254. /**
  255. * 是否绑定在一起
  256. * @param $oneId
  257. * @param $twoId
  258. * @return bool
  259. */
  260. public static function sameBind($oneId, $twoId) {
  261. $oneBind = self::findOneAsArray('USER_ID=:USER_ID AND IS_DEL=0', [':USER_ID' => $oneId], 'MAIN_UID');
  262. $twoBind = self::findOneAsArray('USER_ID=:USER_ID AND IS_DEL=0', [':USER_ID' => $twoId], 'MAIN_UID');
  263. if ($oneBind && $twoBind && $oneBind['MAIN_UID'] == $twoBind['MAIN_UID']) {
  264. return true;
  265. }
  266. return false;
  267. }
  268. /**
  269. * 操作日志记录条件
  270. * @return array
  271. */
  272. public function attrLabelsWithLogType(){
  273. return [
  274. 'USER_ID' => '会员ID',
  275. 'MAIN_UID' => [
  276. 'label' => '主会员编号',
  277. 'type' => function($data){
  278. $value = $data['value'];
  279. return Info::getUserNameByUserId($value);
  280. },
  281. ],
  282. 'CREATE_ADMIN' => [
  283. 'label' => '创建管理员',
  284. 'type' => function($data){
  285. $value = is_array($data) && isset($data['value']) ? $data['value'] : '';
  286. $result = Admin::findOneAsArray('ID=:ID', [':ID'=>$value], 'ADMIN_NAME');
  287. return !empty($result) ? $result['ADMIN_NAME'] : '';
  288. },
  289. ],
  290. 'UPDATE_ADMIN' => [
  291. 'label' => '修改管理员',
  292. 'type' => function($data){
  293. $value = is_array($data) && isset($data['value']) ? $data['value'] : '';
  294. $result = Admin::findOneAsArray('ID=:ID', [':ID'=>$value], 'ADMIN_NAME');
  295. return !empty($result) ? $result['ADMIN_NAME'] : '';
  296. },
  297. ],
  298. 'CREATED_AT' => [
  299. 'label' => '创建时间',
  300. 'type' => ValueTypeConfig::DATE_TIME_TYPE,
  301. ],
  302. 'UPDATED_AT' => [
  303. 'label' => '修改时间',
  304. 'type' => ValueTypeConfig::DATE_TIME_TYPE,
  305. ],
  306. ];
  307. }
  308. }