UserForm.php 25 KB

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