UserForm.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632
  1. <?php
  2. namespace common\models\forms;
  3. use common\components\Model;
  4. use common\helpers\Date;
  5. use common\helpers\Form;
  6. use common\helpers\snowflake\SnowFake;
  7. use common\helpers\user\Balance;
  8. use common\helpers\user\Info;
  9. use common\libs\logging\operate\UserOperate;
  10. use common\models\Period;
  11. use common\models\DeclarationLevel;
  12. use common\models\EmployLevel;
  13. use common\models\User;
  14. use common\models\UserInfo;
  15. use common\models\UserNetwork;
  16. use common\models\UserRelation;
  17. use yii\db\Exception;
  18. /**
  19. * Login form
  20. */
  21. class UserForm extends Model
  22. {
  23. public $userId;
  24. public $userName;
  25. public $zcPv;
  26. public $zcAmount;
  27. public $conUserName;
  28. public $recUserName;
  29. public $conUid;
  30. public $recUid;
  31. public $location;
  32. public $idCard;
  33. public $allData; // 批量报单的全部报单数据
  34. //个人资料
  35. public $nation;
  36. public $realName;
  37. public $mobile;
  38. public $openBank;
  39. public $bankAddress;
  40. public $bankNo;
  41. //修改密码
  42. public $password;
  43. public $oldPassword;
  44. public $verifyPassword;
  45. public $payPassword;
  46. private $_conUid;
  47. private $_recUid;
  48. private $_limit = 1000;
  49. private $_conTopUid;
  50. private $_conTopDeep;
  51. private $_recTopUid;
  52. private $_recTopDeep;
  53. public function init() {
  54. parent::init();
  55. $this->userOperateLogger = new UserOperate([
  56. 'fetchClass' => User::class,
  57. ]);
  58. }
  59. /**
  60. * @inheritdoc
  61. */
  62. public function rules()
  63. {
  64. return [
  65. [['userId', 'userName', 'zcPv', 'zcAmount', 'conUserName', 'recUserName','conUid', 'recUid', 'location','nation','realName', 'mobile','openBank','bankAddress','bankNo'], 'trim'],
  66. [['userId', 'userName', 'zcPv', 'zcAmount', 'conUid', 'recUid', 'location'], 'required', 'on'=>'addWithUid'],
  67. [['userId', 'userName', 'zcPv', 'zcAmount', 'conUserName', 'recUserName', 'location'], 'required', 'on'=>['addWithUserName', 'addByAdmin']],
  68. [['idCard', 'allData'], 'required', 'on'=>['addWithUserName']],
  69. [['userName'], 'required', 'on'=>['validateUser', 'noLoginModifyPassword']],
  70. [['conUid'], 'required', 'on'=>['validateCon', 'validateAddWithUid']],
  71. [['conUserName'], 'required', 'on'=>['validateConName', 'validateAddWithName']],
  72. [['recUid'], 'required', 'on'=>['validateRec', 'validateAddWithUid']],
  73. [['recUserName'], 'required', 'on'=>['validateRecName', 'validateAddWithName']],
  74. [['conUid', 'location'], 'required', 'on'=>['validateLocation', 'validateAddWithUid']],
  75. [['conUserName', 'location'], 'required', 'on'=>['validateLocationConName', 'validateAddWithName']],
  76. [['nation','realName', 'mobile', 'idCard', 'openBank', 'bankAddress', 'bankNo'], 'required', 'on'=>'modifyProfile'],
  77. [['oldPassword','verifyPassword'], 'required','on' => ['modifyPassword', 'modifyPasswordPay', 'noLoginModifyPassword']],
  78. [['password'], 'required','on' => ['modifyPassword', 'noLoginModifyPassword']],
  79. [['payPassword'], 'required','on' => ['modifyPasswordPay']],
  80. ['verifyPassword', 'compare', 'compareAttribute' => 'password' ,'message'=>'两次登录密码输入不一致' ,'on' => ['modifyPassword', 'noLoginModifyPassword']],
  81. ['verifyPassword', 'compare', 'compareAttribute' => 'payPassword' ,'message'=>'两次支付密码输入不一致' ,'on' => ['modifyPasswordPay']],
  82. [['userId'], 'unique', 'targetClass'=>UserInfo::class, 'targetAttribute'=>'USER_ID', 'on' => ['addWithUserName', 'addWithUid', 'addByAdmin']],
  83. [['userName'], 'unique', 'targetClass'=>UserInfo::class, 'targetAttribute'=>'USER_NAME' , 'on' => ['addWithUserName', 'addWithUid', 'addByAdmin']],
  84. [['conUid'], 'exist', 'targetClass'=>UserInfo::class, 'targetAttribute'=>'USER_ID', 'on' => ['addWithUserName', 'addWithUid', 'addByAdmin']],
  85. [['recUid'], 'exist', 'targetClass'=>UserInfo::class, 'targetAttribute'=>'USER_ID', 'on' => ['addWithUserName', 'addWithUid', 'addByAdmin']],
  86. [['conUid'], 'isConUid', 'on' => ['addWithUserName', 'addWithUid', 'addByAdmin']],
  87. [['recUid'], 'isRecUid', 'on' => ['addWithUserName', 'addWithUid', 'addByAdmin']],
  88. [['conUserName'], 'exist', 'targetClass'=>UserInfo::class, 'targetAttribute'=>'USER_NAME', 'on' => ['addWithUserName', 'addWithUid', 'addByAdmin']],
  89. [['recUserName'], 'exist', 'targetClass'=>UserInfo::class, 'targetAttribute'=>'USER_NAME', 'on' => ['addWithUserName', 'addWithUid', 'addByAdmin']],
  90. [['conUserName'], 'isCon', 'on' => ['addWithUserName', 'addWithUid', 'addByAdmin']],
  91. [['recUserName'], 'isRec', 'on' => ['addWithUserName', 'addWithUid', 'addByAdmin']],
  92. [['location'], 'isLocation', 'on' => ['addWithUserName', 'addWithUid', 'addByAdmin']],
  93. //[['idCard'], 'isSameNetwork'],
  94. [['zcPv', 'zcAmount'], 'price', 'on' => ['addWithUserName', 'addWithUid', 'addByAdmin']],
  95. [['zcPv'], 'isMinDecLevel', 'on' => ['addWithUserName', 'addWithUid', 'addByAdmin']],
  96. ];
  97. }
  98. public function attributeLabels()
  99. {
  100. return [
  101. 'userId' => '会员ID',
  102. 'conUserName' => '接点人账号',
  103. 'recUserName' => '开拓人帐号',
  104. 'location' => '区位',
  105. 'nation' => '民族',
  106. //'realName' => '真实姓名',
  107. 'idCard' => '身份证号',
  108. //'mobile' => '手机号',
  109. 'openBank' => '银行名称',
  110. 'bankAddress' => '开户支行',
  111. 'bankNo' => '银行账号',
  112. 'verifyPassword' => '确认密码',
  113. 'oldPassword' => '原密码',
  114. 'password' => '登录密码',
  115. 'payPassword' => '支付密码',
  116. ];
  117. }
  118. /**
  119. * 接点人是否可以放置
  120. * @param $attribute
  121. * @param $params
  122. */
  123. public function isConUid($attribute, $params){
  124. $this->_conUid = $this->conUid;
  125. // 查看该接点人在安置网络中的推荐人数量是否达到上限
  126. $childNum = UserNetwork::firstFloorChildNum($this->_conUid);
  127. if($childNum >= 3){
  128. $this->addError($attribute, '注册'.$this->userName.'时,接点人'.$this->conUserName.'点位已满无法继续添加');
  129. }
  130. }
  131. /**
  132. * 推荐人是否可用
  133. * @param $attribute
  134. * @param $params
  135. */
  136. public function isRecUid($attribute, $params){
  137. $this->_recUid = $this->recUid;
  138. // 查看该开拓者推荐的第一层人的数量
  139. // $childNum = UserRelation::firstFloorChildNum($this->_recUid);
  140. // if($childNum === 0){
  141. // // 必须放在自己的左区
  142. // if($this->location != 1){
  143. // $this->addError($attribute, '开拓的第一人必须放在自己的一市场');
  144. // }
  145. // }
  146. }
  147. /**
  148. * 接点人是否可以放置
  149. * @param $attribute
  150. * @param $params
  151. */
  152. public function isCon($attribute, $params){
  153. // 根据账号找到接点人
  154. $oneConUserInfo = UserInfo::findOneAsArray('USER_NAME=:USER_NAME', [':USER_NAME'=>$this->conUserName]);
  155. if($oneConUserInfo){
  156. $this->_conUid = $oneConUserInfo['USER_ID'];
  157. // 查看该接点人在安置网络中的推荐人数量是否达到上限
  158. $childNum = UserNetwork::firstFloorChildNum($this->_conUid);
  159. if($childNum >= 3){
  160. $this->addError($attribute, $this->userName.'的接点人'.$this->conUserName.'点位已满无法继续添加');
  161. }
  162. } else {
  163. $this->addError($attribute, '注册'.$this->userName.'时,接点人'.$this->conUserName.'帐号无效');
  164. }
  165. }
  166. /**
  167. * 推荐人是否可用
  168. * @param $attribute
  169. * @param $params
  170. */
  171. public function isRec($attribute, $params){
  172. // 根据账号找到开拓人
  173. $oneRecUserInfo = User::findOneAsArray('USER_NAME=:USER_NAME', [':USER_NAME'=>$this->recUserName]);
  174. if($oneRecUserInfo && $oneRecUserInfo['STATUS']){
  175. $this->_recUid = $oneRecUserInfo['ID'];
  176. // 取消开拓的第一人必须放在自己的安置网下左区的需求
  177. // if($this->scenario != 'addByAdmin'){
  178. // // 查看该开拓者推荐的第一层人的数量
  179. // $childNum = UserRelation::firstFloorChildNum($this->_recUid);
  180. // if($childNum === 0){
  181. // // 必须放在自己的左区
  182. // if($this->location != 1 && $this->scenario != 'validateRecName'){
  183. // $this->addError($attribute, '注册'.$this->userName.'时,开拓人'.$this->recUserName.'开拓的第一人必须放在自己的安置网络下的左区');
  184. // }
  185. // }
  186. // }
  187. } else {
  188. $this->addError($attribute, '注册'.$this->userName.'时,开拓人'.$this->recUserName.'帐号无效或未激活');
  189. }
  190. }
  191. /**
  192. * 区位是否可用
  193. * @param $attribute
  194. * @param $params
  195. */
  196. public function isLocation($attribute, $params){
  197. // 查看该接点人下面所有的子会员
  198. $allChildUser = UserNetwork::getFirstFloorChildren($this->_conUid);
  199. if($allChildUser){
  200. $isLocation = [1=>false, 2=>false, 3=>false];
  201. foreach($allChildUser as $child){
  202. $isLocation[$child['RELATIVE_LOCATION']] = true;
  203. if($child['RELATIVE_LOCATION'] == $this->location){
  204. $this->addError($attribute, '注册'.$this->userName.'时,'.$this->conUserName.'的区位【'.$this->location.'】已经存在会员');
  205. }
  206. }
  207. // 第二区不能空点,必须要有会员才能注册到第三区
  208. // if($this->location == 3 && $isLocation[2] === false){
  209. // $this->addError($attribute, '注册'.$this->userName.'到'.$this->conUserName.'的第三区时,'.$this->conUserName.'的第二区不能空点');
  210. // }
  211. } else {
  212. // if($this->location != 1 && $this->scenario != 'addByAdmin'){
  213. // $this->addError($attribute, '注册'.$this->userName.'时,接点人'.$this->conUserName.'下的第一个区位必须选择左区');
  214. // }
  215. }
  216. }
  217. /**
  218. * 校验注册PV必须要大于最低级别要求
  219. * @param $attribute
  220. * @param $params
  221. */
  222. public function isMinDecLevel($attribute, $params){
  223. // 获取排序为2的报单级别
  224. $decLevel = DeclarationLevel::getLevelFromSort(2);
  225. if($this->zcPv < $decLevel['PERF']){
  226. $this->addError($attribute, '注册'.$this->userName.'时,注册PV低于最低级别要求,不能报首购单');
  227. }
  228. }
  229. /**
  230. * 同一身份证号是否在相同网络
  231. * @param $attribute
  232. */
  233. public function isSameNetwork($attribute){
  234. // 新加入会员的身份证号对应网内的其他会员
  235. $otherUser = User::find()->where("ID_CARD=:ID_CARD AND ID<>:ID AND IS_UNION=0 AND DELETED=0 AND (ID_CARD_PREFIX IS NULL OR ID_CARD_PREFIX='')", [':ID_CARD'=>$this->idCard, ':ID'=>$this->userId])->select('ID')->asArray()->all();
  236. // 拿到所有的批量报单数据
  237. foreach($this->allData as $decData){
  238. if($decData['insertUserIdCard'] == $this->idCard){
  239. $otherUser[] = ['ID'=>$decData['toUserId']];
  240. }
  241. }
  242. // 从多轨网络里面找到这些会员的最顶层会员
  243. if($otherUser){
  244. $otherUserIds = [];
  245. foreach($otherUser as $userId){
  246. $otherUserIds[] = $userId['ID'];
  247. }
  248. $otherUserIdsStr = implode("','", $otherUserIds);
  249. $otherUserIdsStr = "'".$otherUserIdsStr."'";
  250. $sameIdCardOtherTopUser = UserInfo::find()->where('USER_ID IN ('.$otherUserIdsStr.')')->orderBy('NETWORK_DEEP ASC')->asArray()->one();
  251. if($sameIdCardOtherTopUser){
  252. // 通过这个顶层会员查看新报单会员的接点人是不是他的下级
  253. if(!UserNetwork::find()->where('PARENT_UID=:PARENT_UID AND USER_ID=:USER_ID', [':PARENT_UID'=>$sameIdCardOtherTopUser['USER_ID'], ':USER_ID'=>$this->_conUid])->exists()){
  254. // 如果不存在的话,就看这个接点人是不是这个顶层的会员
  255. if($this->_conUid != $sameIdCardOtherTopUser['USER_ID']){
  256. $this->addError($attribute, '注册'.$this->userName.'时,同一身份证号会员必须在同一网体内');
  257. }
  258. }
  259. }
  260. }
  261. }
  262. /**
  263. * 添加会员
  264. * @return UserInfo|null
  265. * @throws Exception
  266. */
  267. public function add(){
  268. if(!$this->validate()){
  269. return null;
  270. }
  271. $insertUid = $this->userId;
  272. $insertUserName = $this->userName;
  273. $zcPv = $this->zcPv;
  274. $conUid = $this->_conUid;
  275. $recUid = $this->_recUid;
  276. $location = $this->location;
  277. // $db = \Yii::$app->db;
  278. // $transaction = $db->beginTransaction();
  279. try{
  280. $periodObj = Period::instance();
  281. $nowPeriodNum = $periodObj->getNowPeriodNum();
  282. // 查找接点人的体系信息
  283. $conUserInfo = UserInfo::find()->where('USER_ID=:USER_ID', [':USER_ID'=>$conUid])->asArray()->one();
  284. // 加入会员信息
  285. $userInfoModel = new UserInfo();
  286. $userInfoModel->USER_ID = $insertUid;
  287. $userInfoModel->USER_NAME = $insertUserName;
  288. $userInfoModel->ZC_PV = $zcPv;
  289. $userInfoModel->ZC_AMOUNT = $this->zcAmount;
  290. $userInfoModel->CON_UID = $conUid;
  291. $userInfoModel->REC_UID = $recUid;
  292. $userInfoModel->SYSTEM_ID = $conUserInfo['SYSTEM_ID'];
  293. //$userInfoModel->PERIOD_NUM = $nowPeriodNum;
  294. $userInfoModel->CREATED_AT = Date::nowTime();
  295. $userInfoModel->HIGHEST_EMP_LV = EmployLevel::getDefaultLevelId();
  296. $userInfoModel->ALLOW_TRANSFER = 1;
  297. $userInfoModel->HIGHEST_EMP_LV_PERIOD = $nowPeriodNum;
  298. $userInfoModel->TRANSFER_PROP = 100.00;
  299. $userInfoModel->IS_GROUP_LEADER = 0;
  300. $userInfoModel->GROUP_LEADER_AT = 0;
  301. if(!$userInfoModel->save()){
  302. throw new Exception('会员信息更新失败');
  303. }
  304. // 安置关系
  305. $this->_addToNetwork();
  306. // 开拓关系
  307. $this->_addToRelation();
  308. // 把会员的网络深度也追加上
  309. $userInfoModel->NETWORK_DEEP = $this->_conTopDeep;
  310. $userInfoModel->RELATION_DEEP = $this->_recTopDeep;
  311. if(!$userInfoModel->save()){
  312. throw new Exception('会员网络深度更新失败');
  313. }
  314. // 清空安置网络和开拓网络的顶点和顶点深度的数值
  315. $this->_conTopUid = null;
  316. $this->_conTopDeep = null;
  317. $this->_recTopUid = null;
  318. $this->_recTopDeep = null;
  319. // $transaction->commit();
  320. return $userInfoModel;
  321. } catch (Exception $e){
  322. // $transaction->rollBack();
  323. $this->addError('userId', $e->getMessage());
  324. //echo $e->getMessage().PHP_EOL;
  325. return null;
  326. }
  327. }
  328. /**
  329. * 添加会员
  330. * @param $nowPeriodNum
  331. * @return boolean
  332. */
  333. public function addForImport($nowPeriodNum){
  334. $insertUid = $this->userId;
  335. $insertUserName = $this->userName;
  336. $zcPv = $this->zcPv;
  337. $conUid = $this->conUid;
  338. $recUid = $this->recUid;
  339. try{
  340. // 查找接点人的体系信息
  341. // 加入会员信息
  342. $insertData = [
  343. 'USER_ID' => $insertUid,
  344. 'USER_NAME' => $insertUserName,
  345. 'ZC_PV' => $zcPv,
  346. 'ZC_AMOUNT' => $this->zcAmount,
  347. 'CON_UID' => $conUid,
  348. 'REC_UID' => $recUid,
  349. 'SYSTEM_ID' => "",
  350. 'CREATED_AT' => time(),
  351. 'HIGHEST_EMP_LV' => "",
  352. 'HIGHEST_EMP_LV_PERIOD' => $nowPeriodNum,
  353. 'TRANSFER_PROP' => 100.00,
  354. 'IS_GROUP_LEADER' => 0,
  355. 'GROUP_LEADER_AT' => 0,
  356. ];
  357. UserInfo::insertOne($insertData);
  358. unset($insertData);
  359. // 安置关系
  360. $this->_addToNetworkForImport();
  361. // 开拓关系
  362. //$this->_addToRelationForImport();
  363. // 把会员的网络深度也追加上
  364. UserInfo::updateAll([
  365. "NETWORK_DEEP" => $this->_conTopDeep,
  366. //"RELATION_DEEP" => $this->_recTopDeep,
  367. ], "USER_ID='{$insertUid}'");
  368. // 清空安置网络和开拓网络的顶点和顶点深度的数值
  369. $this->_conTopUid = null;
  370. $this->_conTopDeep = null;
  371. $this->_recTopUid = null;
  372. $this->_recTopDeep = null;
  373. unset($insertUid, $insertUserName, $conUid, $recUid);
  374. return true;
  375. } catch (Exception $e){
  376. $this->addError('userId', $e->getMessage());
  377. return false;
  378. }
  379. }
  380. /**
  381. * 加入安置网络
  382. * @throws Exception
  383. */
  384. private function _addToNetworkForImport(){
  385. //获取上级的点位信息
  386. $conNetInfo = UserNetwork::findOneAsArray(['USER_ID'=>$this->conUid]);
  387. if( !$conNetInfo ) {
  388. throw new Exception('接点人信息不存在');
  389. }
  390. $this->_conTopUid = $conNetInfo['TOP_UID'];
  391. $this->_conTopDeep = $conNetInfo['TOP_DEEP'] + 1;
  392. // 加入安置网络关系
  393. $insertData = [
  394. 'ID' => SnowFake::instance()->generateId(),
  395. "USER_ID" => $this->userId,
  396. "PARENT_UID" => $this->conUid,
  397. "LOCATION_TAG" => $conNetInfo['LOCATION_TAG'] . $this->location,
  398. "RELATIVE_LOCATION" => $this->location,
  399. "TOP_UID" => $conNetInfo['TOP_UID'],
  400. "TOP_DEEP" => $conNetInfo['TOP_DEEP'] + 1,
  401. "PARENT_UIDS" => $conNetInfo['PARENT_UIDS'] . ',' . $this->conUid,
  402. "CREATED_AT" => time(),
  403. ];
  404. UserNetwork::insertOne($insertData);
  405. if(!UserInfo::updateAllCounters(['CON_NUM'=>1], "USER_ID=:USER_ID", [':USER_ID'=>$this->conUid])){
  406. throw new Exception('会员安置关系下级节点数量更新失败');
  407. }
  408. }
  409. /**
  410. * 加入安置网络
  411. * @throws Exception
  412. */
  413. private function _addToNetwork(){
  414. //获取上级的点位信息
  415. $conNetInfo = UserNetwork::findOneAsArray(['USER_ID'=>$this->_conUid]);
  416. if( !$conNetInfo ) {
  417. throw new Exception('接点人信息不存在');
  418. }
  419. $this->_conTopUid = $conNetInfo['TOP_UID'];
  420. $this->_conTopDeep = $conNetInfo['TOP_DEEP'] + 1;
  421. // 加入安置网络关系
  422. if( $conNetInfo['PARENT_UIDS'] ) {
  423. $parentUids = $conNetInfo['PARENT_UIDS'] . ',' . $this->_conUid;
  424. }else {
  425. $parentUids = $this->_conUid;
  426. }
  427. $insertData = [
  428. 'ID' => SnowFake::instance()->generateId(),
  429. "USER_ID" => $this->userId,
  430. "PARENT_UID" => $this->_conUid,
  431. "LOCATION_TAG" => $conNetInfo['LOCATION_TAG'] . $this->location,
  432. "RELATIVE_LOCATION" => $this->location,
  433. "TOP_UID" => $conNetInfo['TOP_UID'],
  434. "TOP_DEEP" => $conNetInfo['TOP_DEEP'] + 1,
  435. "PARENT_UIDS" => $parentUids,
  436. "CREATED_AT" => time(),
  437. ];
  438. UserNetwork::insertOne($insertData);
  439. if(!UserInfo::updateAllCounters(['CON_NUM'=>1], "USER_ID=:USER_ID", [':USER_ID'=>$this->_conUid])){
  440. throw new Exception('会员安置关系下级节点数量更新失败');
  441. }
  442. }
  443. /**
  444. * 加入开拓网络
  445. * @throws Exception
  446. */
  447. private function _addToRelationForImport(){
  448. //获取上级的推荐信息
  449. $conRelationInfo = UserRelation::findOneAsArray(['USER_ID'=>$this->recUid]);
  450. if( !$conRelationInfo ) {
  451. throw new Exception('推荐人信息不存在');
  452. }
  453. $this->_recTopUid = $conRelationInfo['TOP_UID'];
  454. $this->_recTopDeep = $conRelationInfo['TOP_DEEP'] + 1;
  455. // 加入推荐网络关系
  456. $insertData = [
  457. 'ID' => SnowFake::instance()->generateId(),
  458. "USER_ID" => $this->userId,
  459. "PARENT_UID" => $this->recUid,
  460. "TOP_UID" => $conRelationInfo['TOP_UID'],
  461. "TOP_DEEP" => $conRelationInfo['TOP_DEEP'] + 1,
  462. "PARENT_UIDS" => $conRelationInfo['PARENT_UIDS'] . ',' . $this->recUid,
  463. "CREATED_AT" => time(),
  464. ];
  465. UserRelation::insertOne($insertData);
  466. if(!UserInfo::updateAllCounters(['REC_NUM'=>1], "USER_ID=:USER_ID", [':USER_ID'=>$this->recUid])){
  467. throw new Exception('会员开拓关系下级节点数量更新失败');
  468. }
  469. }
  470. /**
  471. * 加入开拓网络
  472. * @throws Exception
  473. */
  474. private function _addToRelation(){
  475. //获取上级的推荐信息
  476. $conRelationInfo = UserRelation::findOneAsArray(['USER_ID'=>$this->_recUid]);
  477. if( !$conRelationInfo ) {
  478. throw new Exception('推荐人信息不存在');
  479. }
  480. $this->_recTopUid = $conRelationInfo['TOP_UID'];
  481. $this->_recTopDeep = $conRelationInfo['TOP_DEEP'] + 1;
  482. if( $conRelationInfo['PARENT_UIDS'] ) {
  483. $parentUids = $conRelationInfo['PARENT_UIDS'] . ',' . $this->_recUid;
  484. }else {
  485. $parentUids = $this->_recUid;
  486. }
  487. // 加入推荐网络关系
  488. $insertData = [
  489. 'ID' => SnowFake::instance()->generateId(),
  490. "USER_ID" => $this->userId,
  491. "PARENT_UID" => $this->_recUid,
  492. "TOP_UID" => $conRelationInfo['TOP_UID'],
  493. "TOP_DEEP" => $conRelationInfo['TOP_DEEP'] + 1,
  494. "PARENT_UIDS" => $parentUids,
  495. "CREATED_AT" => time(),
  496. ];
  497. UserRelation::insertOne($insertData);
  498. if(!UserInfo::updateAllCounters(['REC_NUM'=>1], "USER_ID=:USER_ID", [':USER_ID'=>$this->_recUid])){
  499. throw new Exception('会员开拓关系下级节点数量更新失败');
  500. }
  501. }
  502. /**
  503. * 修改个人资料
  504. * @return User|null
  505. */
  506. public function modifyProfile(){
  507. if(!$this->validate()){
  508. return null;
  509. }
  510. $this->userOperateLogger->beforeUpdate(\Yii::$app->user->id,'ID',['select'=>'NATION,OPEN_BANK,BANK_NO,BANK_ADDRESS']);
  511. $uid = \Yii::$app->user->id;
  512. $model = User::findOne(['ID'=>$uid]);
  513. $model->NATION = $this->nation;
  514. //$model->REAL_NAME = $this->realName;
  515. //$model->MOBILE = $this->mobile;
  516. //$model->ID_CARD = $this->idCard;
  517. $model->OPEN_BANK = $this->openBank;
  518. $model->BANK_NO = $this->bankNo;
  519. $model->BANK_ADDRESS = $this->bankAddress;
  520. if(!$model->save()){
  521. $this->addErrors($model->getErrors());
  522. return null;
  523. }
  524. User::updateBaseInfoToRedis($uid);
  525. $this->userOperateLogger->afterUpdate(\Yii::$app->user->id,'ID',['select'=>'NATION,OPEN_BANK,BANK_NO,BANK_ADDRESS']);
  526. $this->userOperateLogger->clean()->save([
  527. 'optType' => '会员修改资料',
  528. 'userId' => \Yii::$app->user->id,
  529. 'userName' => Info::getUserNameByUserId(\Yii::$app->user->id),
  530. ]);
  531. return $model;
  532. }
  533. /**
  534. * 修改密码
  535. */
  536. public function modifyPassword(){
  537. if(!$this->validate()){
  538. return null;
  539. }
  540. $uid = \Yii::$app->user->id;
  541. $model = User::findOne(['ID'=>$uid]);
  542. if ( !$model->validatePassword($this->oldPassword) ) {
  543. $this->addError('modifyPassword', '原登录密码错误');
  544. return false;
  545. }
  546. $model->PASSWORD_HASH = \Yii::$app->security->generatePasswordHash($this->password);
  547. if(!$model->save()){
  548. $this->addErrors($model->getErrors());
  549. return false;
  550. }
  551. return true;
  552. }
  553. /**
  554. * 修改支付密码
  555. */
  556. public function modifyPasswordPay(){
  557. if(!$this->validate()){
  558. return null;
  559. }
  560. $uid = \Yii::$app->user->id;
  561. $model = User::findOne(['ID'=>$uid]);
  562. if ( !$model->validatePasswordPay($this->oldPassword) ) {
  563. $this->addError('modifyPasswordPay', '原支付密码错误');
  564. return false;
  565. }
  566. $model->PAY_PASSWORD = \Yii::$app->security->generatePasswordHash($this->payPassword);
  567. if(!$model->save()){
  568. $this->addErrors($model->getErrors());
  569. return false;
  570. }
  571. return true;
  572. }
  573. /**
  574. * 不登录修改密码
  575. */
  576. public function noLoginModifyPassword(){
  577. if(!$this->validate()){
  578. return null;
  579. }
  580. $model = User::findOne(["USER_NAME"=>$this->userName]);
  581. if( !$model ) {
  582. $this->addError('noLoginModifyPassword', '不存在的用户名');
  583. return false;
  584. }
  585. if ( !$model->validatePassword($this->oldPassword) ) {
  586. $this->addError('noLoginModifyPassword', '原登录密码错误');
  587. return false;
  588. }
  589. $model->PASSWORD_HASH = \Yii::$app->security->generatePasswordHash($this->password);
  590. $model->IS_MODIFY_PASSWORD = 0;
  591. if(!$model->save()){
  592. $this->addErrors($model->getErrors());
  593. return false;
  594. }
  595. return true;
  596. }
  597. }