UserForm.php 26 KB

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