UserForm.php 26 KB

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