UserForm.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652
  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->bottomUserName]); //3790 (2026/1/21 14:59 Ryan)
  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. $recUid = $this->_recUid; // 推荐人
  288. // 实际推荐人
  289. $recUidActual = User::findOneAsArray('USER_NAME=:USER_NAME', [':USER_NAME' => $this->recUserName]);
  290. // $location = $this->location;
  291. // $db = \Yii::$app->db;
  292. // $transaction = $db->beginTransaction();
  293. try{
  294. $periodObj = Period::instance();
  295. $nowPeriodNum = $periodObj->getNowPeriodNum();
  296. // 查找接点人的体系信息
  297. $conUserInfo = UserInfo::find()->where('USER_ID=:USER_ID', [':USER_ID'=>$conUid])->asArray()->one();
  298. // 加入会员信息
  299. $userInfoModel = new UserInfo();
  300. $userInfoModel->USER_ID = $insertUid;
  301. $userInfoModel->USER_NAME = $insertUserName;
  302. $userInfoModel->ZC_PV = $zcPv;
  303. $userInfoModel->ZC_AMOUNT = $this->zcAmount;
  304. $userInfoModel->CON_UID = $conUid;
  305. $userInfoModel->REC_UID = $recUid;
  306. $userInfoModel->REC_UID_ACTUAL = $recUidActual['ID'];
  307. $userInfoModel->SYSTEM_ID = $conUserInfo['SYSTEM_ID'];
  308. //$userInfoModel->PERIOD_NUM = $nowPeriodNum;
  309. $userInfoModel->CREATED_AT = Date::nowTime();
  310. $userInfoModel->HIGHEST_EMP_LV = EmployLevel::getDefaultLevelId();
  311. $userInfoModel->ALLOW_TRANSFER = 1;
  312. $userInfoModel->HIGHEST_EMP_LV_PERIOD = $nowPeriodNum;
  313. $userInfoModel->TRANSFER_PROP = 100.00;
  314. $userInfoModel->IS_GROUP_LEADER = 0;
  315. $userInfoModel->GROUP_LEADER_AT = 0;
  316. if(!$userInfoModel->save()){
  317. throw new Exception('会员信息更新失败');
  318. }
  319. // 安置关系
  320. // $this->_addToNetwork();
  321. // 开拓关系
  322. $this->_addToRelation();
  323. // 把会员的网络深度也追加上
  324. $userInfoModel->NETWORK_DEEP = $this->_recTopDeep;
  325. $userInfoModel->RELATION_DEEP = $this->_recTopDeep;
  326. if(!$userInfoModel->save()){
  327. throw new Exception('会员网络深度更新失败');
  328. }
  329. // 清空安置网络和开拓网络的顶点和顶点深度的数值
  330. $this->_conTopUid = null;
  331. $this->_conTopDeep = null;
  332. $this->_recTopUid = null;
  333. $this->_recTopDeep = null;
  334. // $transaction->commit();
  335. return $userInfoModel;
  336. } catch (Exception $e){
  337. // $transaction->rollBack();
  338. $this->addError('userId', $e->getMessage());
  339. //echo $e->getMessage().PHP_EOL;
  340. return null;
  341. }
  342. }
  343. /**
  344. * 添加会员
  345. * @param $nowPeriodNum
  346. * @return boolean
  347. */
  348. public function addForImport($nowPeriodNum){
  349. $insertUid = $this->userId;
  350. $insertUserName = $this->userName;
  351. $zcPv = $this->zcPv;
  352. $conUid = $this->conUid;
  353. $recUid = $this->recUid;
  354. try{
  355. // 查找接点人的体系信息
  356. // 加入会员信息
  357. $insertData = [
  358. 'USER_ID' => $insertUid,
  359. 'USER_NAME' => $insertUserName,
  360. 'ZC_PV' => $zcPv,
  361. 'ZC_AMOUNT' => $this->zcAmount,
  362. 'CON_UID' => $conUid,
  363. 'REC_UID' => $recUid,
  364. 'SYSTEM_ID' => "",
  365. 'CREATED_AT' => time(),
  366. 'HIGHEST_EMP_LV' => "",
  367. 'HIGHEST_EMP_LV_PERIOD' => $nowPeriodNum,
  368. 'TRANSFER_PROP' => 100.00,
  369. 'IS_GROUP_LEADER' => 0,
  370. 'GROUP_LEADER_AT' => 0,
  371. ];
  372. UserInfo::insertOne($insertData);
  373. unset($insertData);
  374. // 安置关系
  375. // $this->_addToNetworkForImport();
  376. // 开拓关系
  377. //$this->_addToRelationForImport();
  378. // 把会员的网络深度也追加上
  379. UserInfo::updateAll([
  380. "NETWORK_DEEP" => $this->_conTopDeep,
  381. //"RELATION_DEEP" => $this->_recTopDeep,
  382. ], "USER_ID='{$insertUid}'");
  383. // 清空安置网络和开拓网络的顶点和顶点深度的数值
  384. $this->_conTopUid = null;
  385. $this->_conTopDeep = null;
  386. $this->_recTopUid = null;
  387. $this->_recTopDeep = null;
  388. unset($insertUid, $insertUserName, $conUid, $recUid);
  389. return true;
  390. } catch (Exception $e){
  391. $this->addError('userId', $e->getMessage());
  392. return false;
  393. }
  394. }
  395. /**
  396. * 加入安置网络
  397. * @throws Exception
  398. */
  399. private function _addToNetworkForImport(){
  400. //获取上级的点位信息
  401. $conNetInfo = UserNetwork::findOneAsArray(['USER_ID'=>$this->conUid]);
  402. if( !$conNetInfo ) {
  403. throw new Exception('接点人信息不存在');
  404. }
  405. $this->_conTopUid = $conNetInfo['TOP_UID'];
  406. $this->_conTopDeep = $conNetInfo['TOP_DEEP'] + 1;
  407. // 加入安置网络关系
  408. $insertData = [
  409. 'ID' => SnowFake::instance()->generateId(),
  410. "USER_ID" => $this->userId,
  411. "PARENT_UID" => $this->conUid,
  412. "LOCATION_TAG" => $conNetInfo['LOCATION_TAG'] . $this->location,
  413. "RELATIVE_LOCATION" => $this->location,
  414. "TOP_UID" => $conNetInfo['TOP_UID'],
  415. "TOP_DEEP" => $conNetInfo['TOP_DEEP'] + 1,
  416. "PARENT_UIDS" => $conNetInfo['PARENT_UIDS'] . ',' . $this->conUid,
  417. "CREATED_AT" => time(),
  418. ];
  419. UserNetwork::insertOne($insertData);
  420. if(!UserInfo::updateAllCounters(['CON_NUM'=>1], "USER_ID=:USER_ID", [':USER_ID'=>$this->conUid])){
  421. throw new Exception('会员安置关系下级节点数量更新失败');
  422. }
  423. }
  424. /**
  425. * 加入安置网络
  426. * @throws Exception
  427. */
  428. private function _addToNetwork(){
  429. //获取上级的点位信息
  430. $conNetInfo = UserNetwork::findOneAsArray(['USER_ID'=>$this->_conUid]);
  431. if( !$conNetInfo ) {
  432. throw new Exception('接点人信息不存在');
  433. }
  434. $this->_conTopUid = $conNetInfo['TOP_UID'];
  435. $this->_conTopDeep = $conNetInfo['TOP_DEEP'] + 1;
  436. // 加入安置网络关系
  437. if( $conNetInfo['PARENT_UIDS'] ) {
  438. $parentUids = $conNetInfo['PARENT_UIDS'] . ',' . $this->_conUid;
  439. }else {
  440. $parentUids = $this->_conUid;
  441. }
  442. $insertData = [
  443. 'ID' => SnowFake::instance()->generateId(),
  444. "USER_ID" => $this->userId,
  445. "PARENT_UID" => $this->_conUid,
  446. "LOCATION_TAG" => $conNetInfo['LOCATION_TAG'] . $this->location,
  447. "RELATIVE_LOCATION" => $this->location,
  448. "TOP_UID" => $conNetInfo['TOP_UID'],
  449. "TOP_DEEP" => $conNetInfo['TOP_DEEP'] + 1,
  450. "PARENT_UIDS" => $parentUids,
  451. "CREATED_AT" => time(),
  452. ];
  453. UserNetwork::insertOne($insertData);
  454. if(!UserInfo::updateAllCounters(['CON_NUM'=>1], "USER_ID=:USER_ID", [':USER_ID'=>$this->_conUid])){
  455. throw new Exception('会员安置关系下级节点数量更新失败');
  456. }
  457. }
  458. /**
  459. * 加入开拓网络
  460. * @throws Exception
  461. */
  462. private function _addToRelationForImport(){
  463. //获取上级的推荐信息
  464. $conRelationInfo = UserRelation::findOneAsArray(['USER_ID'=>$this->recUid]);
  465. if( !$conRelationInfo ) {
  466. throw new Exception('推荐人信息不存在');
  467. }
  468. $this->_recTopUid = $conRelationInfo['TOP_UID'];
  469. $this->_recTopDeep = $conRelationInfo['TOP_DEEP'] + 1;
  470. // 加入推荐网络关系
  471. $insertData = [
  472. 'ID' => SnowFake::instance()->generateId(),
  473. "USER_ID" => $this->userId,
  474. "PARENT_UID" => $this->recUid,
  475. "TOP_UID" => $conRelationInfo['TOP_UID'],
  476. "TOP_DEEP" => $conRelationInfo['TOP_DEEP'] + 1,
  477. "PARENT_UIDS" => $conRelationInfo['PARENT_UIDS'] . ',' . $this->recUid,
  478. "CREATED_AT" => time(),
  479. ];
  480. UserRelation::insertOne($insertData);
  481. if(!UserInfo::updateAllCounters(['REC_NUM'=>1], "USER_ID=:USER_ID", [':USER_ID'=>$this->recUid])){
  482. throw new Exception('会员开拓关系下级节点数量更新失败');
  483. }
  484. }
  485. /**
  486. * 加入开拓网络
  487. * @throws Exception
  488. */
  489. private function _addToRelation(){
  490. //获取上级的推荐信息
  491. $conRelationInfo = UserRelation::findOneAsArray(['USER_ID'=>$this->_recUid]);
  492. if( !$conRelationInfo ) {
  493. throw new Exception('推荐人信息不存在');
  494. }
  495. $this->_recTopUid = $conRelationInfo['TOP_UID'];
  496. $this->_recTopDeep = $conRelationInfo['TOP_DEEP'] + 1;
  497. if( $conRelationInfo['PARENT_UIDS'] ) {
  498. $parentUids = $conRelationInfo['PARENT_UIDS'] . ',' . $this->_recUid;
  499. }else {
  500. $parentUids = $this->_recUid;
  501. }
  502. // 加入推荐网络关系
  503. $insertData = [
  504. 'ID' => SnowFake::instance()->generateId(),
  505. "USER_ID" => $this->userId,
  506. "PARENT_UID" => $this->_recUid,
  507. "TOP_UID" => $conRelationInfo['TOP_UID'],
  508. "TOP_DEEP" => $conRelationInfo['TOP_DEEP'] + 1,
  509. "PARENT_UIDS" => $parentUids,
  510. "CREATED_AT" => time(),
  511. ];
  512. UserRelation::insertOne($insertData);
  513. if(!UserInfo::updateAllCounters(['REC_NUM'=>1], "USER_ID=:USER_ID", [':USER_ID'=>$this->_recUid])){
  514. throw new Exception('会员开拓关系下级节点数量更新失败');
  515. }
  516. }
  517. /**
  518. * 修改个人资料
  519. * @return User|null
  520. */
  521. public function modifyProfile(){
  522. if(!$this->validate()){
  523. return null;
  524. }
  525. $this->userOperateLogger->beforeUpdate(\Yii::$app->user->id,'ID',['select'=>'NATION,OPEN_BANK,BANK_NO,BANK_ADDRESS,EMAIL']);
  526. $uid = \Yii::$app->user->id;
  527. $model = User::findOne(['ID'=>$uid]);
  528. // $model->NATION = $this->nation;
  529. //$model->REAL_NAME = $this->realName;
  530. //$model->MOBILE = $this->mobile;
  531. //$model->ID_CARD = $this->idCard;
  532. // $model->OPEN_BANK = $this->openBank;
  533. // $model->BANK_NO = $this->bankNo;
  534. // $model->BANK_ADDRESS = $this->bankAddress;
  535. $model->EMAIL = $this->email;
  536. if(!$model->save()){
  537. $this->addErrors($model->getErrors());
  538. return null;
  539. }
  540. User::updateBaseInfoToRedis($uid);
  541. $this->userOperateLogger->afterUpdate(\Yii::$app->user->id,'ID',['select'=>'NATION,OPEN_BANK,BANK_NO,BANK_ADDRESS']);
  542. $this->userOperateLogger->clean()->save([
  543. 'optType' => '会员修改资料',
  544. 'userId' => \Yii::$app->user->id,
  545. 'userName' => Info::getUserNameByUserId(\Yii::$app->user->id),
  546. ]);
  547. return $model;
  548. }
  549. /**
  550. * 修改密码
  551. */
  552. public function modifyPassword(){
  553. if(!$this->validate()){
  554. return null;
  555. }
  556. $uid = \Yii::$app->user->id;
  557. $model = User::findOne(['ID'=>$uid]);
  558. if ( !$model->validatePassword($this->oldPassword) ) {
  559. $this->addError('modifyPassword', Yii::t('app', 'originalLoginPasswordError'));
  560. return false;
  561. }
  562. $model->PASSWORD_HASH = \Yii::$app->security->generatePasswordHash($this->password);
  563. if(!$model->save()){
  564. $this->addErrors($model->getErrors());
  565. return false;
  566. }
  567. return true;
  568. }
  569. /**
  570. * 修改支付密码
  571. */
  572. public function modifyPasswordPay(){
  573. if(!$this->validate()){
  574. return null;
  575. }
  576. $uid = \Yii::$app->user->id;
  577. $model = User::findOne(['ID'=>$uid]);
  578. if ( !$model->validatePasswordPay($this->oldPassword) ) {
  579. $this->addError('modifyPasswordPay', Yii::t('app', 'originalPaymentPasswordError'));
  580. return false;
  581. }
  582. $model->PAY_PASSWORD = \Yii::$app->security->generatePasswordHash($this->payPassword);
  583. if(!$model->save()){
  584. $this->addErrors($model->getErrors());
  585. return false;
  586. }
  587. return true;
  588. }
  589. /**
  590. * 不登录修改密码
  591. */
  592. public function noLoginModifyPassword(){
  593. if(!$this->validate()){
  594. return null;
  595. }
  596. $model = User::findOne(["USER_NAME"=>$this->userName]);
  597. if( !$model ) {
  598. $this->addError('noLoginModifyPassword', Yii::t('app', 'userNameNotExists'));
  599. return false;
  600. }
  601. if ( !$model->validatePassword($this->oldPassword) ) {
  602. $this->addError('noLoginModifyPassword', Yii::t('app', 'originalLoginPasswordError'));
  603. return false;
  604. }
  605. $model->PASSWORD_HASH = \Yii::$app->security->generatePasswordHash($this->password);
  606. $model->IS_MODIFY_PASSWORD = 0;
  607. if(!$model->save()){
  608. $this->addErrors($model->getErrors());
  609. return false;
  610. }
  611. return true;
  612. }
  613. }