UserForm.php 26 KB

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