UserForm.php 26 KB

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