AdminAddUserForm.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. <?php
  2. namespace common\models\forms;
  3. use backendApi\modules\v1\models\Admin;
  4. use common\components\Model;
  5. use common\helpers\Cache;
  6. use common\helpers\Date;
  7. use common\helpers\Form;
  8. use common\helpers\Tool;
  9. use common\helpers\user\Balance;
  10. use common\helpers\user\Info;
  11. use common\libs\logging\operate\AdminOperate;
  12. use common\models\FlowBonus;
  13. use common\models\Period;
  14. use common\models\DeclarationLevel;
  15. use common\models\DecOrder;
  16. use common\models\EmployLevel;
  17. use common\models\User;
  18. use common\models\UserBind;
  19. use common\models\UserBonus;
  20. use common\models\UserInfo;
  21. use common\models\UserMove;
  22. use common\models\UserNetwork;
  23. use common\models\UserRelation;
  24. use common\models\UserSystem;
  25. use common\models\UserTeamwork;
  26. use yii\base\Exception;
  27. /**
  28. * Login form
  29. */
  30. class AdminAddUserForm extends Model {
  31. public $userName;
  32. public $nation;
  33. public $realName;
  34. public $idCard;
  35. public $mobile;
  36. public $address;
  37. public $openBank;
  38. public $bankAddress;
  39. public $bankNo;
  40. public $bankAreaSelected;
  41. public $decLv;
  42. public $areaSelected;
  43. public $tel;
  44. public $isDec;
  45. public $decUserName;
  46. public $birthday;
  47. public $decRoleId;
  48. public $conUserName;
  49. public $recUserName;
  50. public $location;
  51. public $delUserName;
  52. public $countryId;
  53. public $languageId;
  54. private $_decId;
  55. private $_delUserId;
  56. public function init() {
  57. parent::init();
  58. $this->adminOperateLogger = new AdminOperate([
  59. 'fetchClass' => User::class,
  60. ]);
  61. }
  62. /**
  63. * @inheritdoc
  64. */
  65. public function rules() {
  66. return [
  67. [['userName', 'nation', 'realName', /*'idCard',*/ 'mobile', 'address', 'openBank', 'bankAddress', 'bankNo', 'bankAreaSelected', 'decLv', 'areaSelected', 'tel', 'isDec', 'decUserName', 'birthday', 'decRoleId', 'conUserName', 'recUserName', 'location', 'delUserName'], 'trim'],
  68. [['userName', 'realName', /*'idCard',*/ 'decLv', 'decUserName', 'conUserName', 'recUserName', 'location', 'countryId', 'languageId'], 'required'],
  69. [['userName'], 'unique', 'targetClass' => UserInfo::class, 'targetAttribute' => 'USER_NAME'],
  70. [['conUserName'], 'exist', 'targetClass' => UserInfo::class, 'targetAttribute' => 'USER_NAME'],
  71. [['recUserName'], 'exist', 'targetClass' => UserInfo::class, 'targetAttribute' => 'USER_NAME'],
  72. [['decLv'], 'exist', 'targetClass' => DeclarationLevel::class, 'targetAttribute' => 'ID'],
  73. [['mobile'], 'mobile'],
  74. // [['idCard'], 'simpleIdCard'],
  75. [['bankNo'], 'string', 'max' => 19],
  76. [['decUserName'], 'issetDec'],
  77. [['isDec'], 'issetDecRoleId'],
  78. [['location'], 'isLocation'],
  79. [['delUserName'], 'isDelUserName'],
  80. //[['userName'], 'userName'],
  81. [['realName', 'address', 'bankAddress'], 'realName'],
  82. ];
  83. }
  84. /**
  85. * 指定校验场景
  86. * @return array
  87. */
  88. public function scenarios() {
  89. $parentScenarios = parent::scenarios();
  90. $customScenarios = [
  91. 'add' => ['userName', 'nation', 'realName', /*'idCard',*/ 'mobile', 'address', 'openBank', 'bankAddress', 'bankNo', 'bankAreaSelected', 'decLv', 'areaSelected', 'tel', 'isDec', 'decUserName', 'birthday', 'decRoleId', 'conUserName', 'recUserName', 'location', 'countryId', 'languageId'],
  92. 'userDel' => ['delUserName'],
  93. ];
  94. return array_merge($parentScenarios, $customScenarios);
  95. }
  96. public function attributeLabels() {
  97. return [
  98. 'userName' => 'Member Code',//会员编号
  99. 'nation' => 'Nation',//民族
  100. 'realName' => 'Member Name',//会员姓名
  101. // 'idCard' => 'ID',//身份证号
  102. 'mobile' => 'Phone Number',//手机号
  103. 'address' => 'Address',//地址
  104. 'openBank' => 'Bank Name',//开户行
  105. 'bankAddress' => 'Account opening sub branch',//开户支行
  106. 'bankNo' => 'bank account',//银行账号
  107. 'bankAreaSelected' => 'Banking region',//银行地区
  108. 'decLv' => 'Real time declaration level',//实时报单级别
  109. 'areaSelected' => 'Location',//所在地区
  110. 'tel' => 'Telephone',//电话
  111. 'isDec' => 'Stockist or not',//是否报单中心
  112. 'decUserName' => 'Stockist No', // 报单中心编号
  113. 'birthday' => 'birthday',//生日
  114. 'decRoleId' => 'Stockist Level',//报单中心级别
  115. 'conUserName' => 'Contact person Member No',//接点人会员编号
  116. 'recUserName' => 'Sponsor Member No',
  117. 'location' => 'Resettlement area',//安置区域
  118. 'countryId' => 'Country',//安置区域
  119. 'languageId' => 'Language',//安置区域
  120. ];
  121. }
  122. /**
  123. * 是否存在报单中心
  124. * @param $attribute
  125. */
  126. public function issetDec($attribute) {
  127. if (!$decUser = User::find()->select('ID')->where('IS_DEC=1 AND USER_NAME=:USER_NAME', [':USER_NAME' => $this->decUserName])->asArray()->one()) {
  128. $this->addError($attribute, 'Stockist does not exist');//报单中心不存在
  129. } else {
  130. $this->_decId = $decUser['ID'];
  131. }
  132. }
  133. /**
  134. * 校验是否填写报单中心级别
  135. * @param $attribute
  136. */
  137. public function issetDecRoleId($attribute) {
  138. if ($this->isDec && !$this->decRoleId) {
  139. $this->addError($attribute, 'Stockist level cannot be empty');//报单中心级别不能为空
  140. }
  141. }
  142. public function isLocation($attribute) {
  143. if (!in_array($this->location, ['1', '2', '3'])) {
  144. $this->addError($attribute, 'Location error');//区位错误
  145. }
  146. }
  147. /**
  148. * 校验删除的用户是否符合条件
  149. * @param $attribute
  150. */
  151. public function isDelUserName($attribute) {
  152. if (!$this->_delUserId = Info::getUserIdByUserName($this->delUserName)) {
  153. $this->addError($attribute, 'user does not exist');//用户不存在
  154. }
  155. if (UserInfo::findOneAsArray('REC_UID=:REC_UID AND DELETED=0', [':REC_UID' => $this->_delUserId], 'USER_ID')) {
  156. $this->addError($attribute, 'This member has developed other members and cannot be deleted');//此会员已开拓其它会员不可以删除
  157. }
  158. if (UserInfo::findOneAsArray('CON_UID=:CON_UID AND DELETED=0', [':CON_UID' => $this->_delUserId], 'USER_ID')) {
  159. $this->addError($attribute, 'This member has been placed with other members and cannot be deleted');//此会员已安置其它会员不可以删除
  160. }
  161. if (User::findOneAsArray('DEC_ID=:DEC_ID AND DELETED=0', [':DEC_ID' => $this->_delUserId], 'ID')) {
  162. $this->addError($attribute, 'This member has submitted a bill and other members cannot delete it');//此会员已报单其它会员不可以删除
  163. }
  164. if (DecOrder::findOneAsArray('USER_ID=:TO_USER_ID OR TO_USER_ID=:TO_USER_ID', [':TO_USER_ID' => $this->_delUserId], 'ID')) {
  165. $this->addError($attribute, 'This member has submitted a bill and cannot be deleted');//此会员已报单不可以删除
  166. }
  167. $info = Info::baseInfoWithNet($this->_delUserId);
  168. if ($info['REG_FROM'] == 1) {
  169. $this->addError($attribute, 'This member is not empty and cannot be deleted');//此会员不是空单无法删除
  170. }
  171. $sentMaxPeriodNum = Period::sentMaxPeriodNum();
  172. if ($info['PERIOD_AT'] <= $sentMaxPeriodNum) {
  173. $this->addError($attribute, 'The number of enrollment periods of this member is less than or equal to the number of online periods');//此会员加入期数小于等于已挂网期数
  174. }
  175. if (User::find()->where('ID=:ID AND VERIFIED=1', [':ID' => $this->_delUserId])->exists()) {
  176. $this->addError($attribute, 'This member has been authenticated by real name and cannot be deleted');//此会员已实名认证无法删除
  177. }
  178. if (User::find()->where('ID=:ID AND SUB_COM_LEADER=1', [':ID' => $this->_delUserId])->exists()) {
  179. $this->addError($attribute, 'This member is a branch leader and cannot be deleted');//此会员是分公司领导人无法删除
  180. }
  181. if (UserInfo::find()->where('USER_ID=:USER_ID AND (IS_GROUP_LEADER=1 OR IS_SYSTEM_LEADER=1 OR IS_BIND=1 OR IS_TEAM=1)', [':USER_ID' => $this->_delUserId])->exists()) {
  182. $this->addError($attribute, 'This member cannot be deleted due to the following conditions: Team Leader / system leader / point binding / point cooperation');//此会员存在以下情况无法删除,团队领导人/体系领导人/点位绑定/点位合作
  183. }
  184. if (Balance::hasBonus($this->_delUserId)) {
  185. $this->addError($attribute, 'This member has balance and cannot be deleted');//此会员有余额无法删除
  186. }
  187. if (FlowBonus::findOneAsArray('USER_ID=:USER_ID', [':USER_ID' => $this->_delUserId], 'ID')) {
  188. $this->addError($attribute, 'This member has generated a balance flow, which cannot be deleted');//该会员产生过余额流水无法删除
  189. }
  190. if (UserMove::findOneAsArray('USER_ID=:USER_ID', [':USER_ID' => $this->_delUserId], 'ID')) {
  191. $this->addError($attribute, 'The member cannot be deleted due to network migration');//该会员发生过移网无法删除
  192. }
  193. }
  194. /**
  195. * 添加
  196. * @return User|null
  197. * @throws \yii\db\Exception
  198. */
  199. public function edit() {
  200. $this->userName = strtoupper($this->userName);
  201. if (!$this->validate()) {
  202. return null;
  203. }
  204. $transactionShop = \Yii::$app->db->beginTransaction();
  205. try {
  206. $period = Period::instance();
  207. // 商城表增加会员
  208. $user = new User();
  209. $user->USER_NAME = $this->userName;
  210. $pwd = Info::passwordGenerator($this->idCard,$user->USER_NAME);
  211. $user->PASSWORD_HASH = \Yii::$app->security->generatePasswordHash($pwd);
  212. $pwd = Info::passwordGenerator($this->idCard,$user->USER_NAME,true);
  213. $user->PAY_PASSWORD = \Yii::$app->security->generatePasswordHash($pwd);
  214. $user->NATION = 0;
  215. $user->REAL_NAME = $this->realName;
  216. // $user->ID_CARD = $this->idCard;
  217. $user->MOBILE = $this->mobile;
  218. $user->ADDRESS = $this->address ? $this->address : 'nothing';//无
  219. $user->OPEN_BANK = $this->openBank;
  220. $user->BANK_ADDRESS = $this->bankAddress;
  221. $user->BANK_NO = $this->bankNo;
  222. $user->BANK_PROVINCE = $this->bankAreaSelected[0] ?? '0';
  223. $user->BANK_CITY = $this->bankAreaSelected[1] ?? '0';
  224. $user->BANK_COUNTY = $this->bankAreaSelected[2] ?? '0';
  225. $user->CREATED_AT = Date::nowTime();
  226. $user->STATUS = 1;
  227. $user->DEC_LV = $this->decLv;
  228. $user->LAST_DEC_LV = $this->decLv;
  229. $user->EMP_LV = EmployLevel::getDefaultLevelId();
  230. $user->PROVINCE = $this->areaSelected[0] ?? '0';
  231. $user->CITY = $this->areaSelected[1] ?? '0';
  232. $user->COUNTY = $this->areaSelected[2] ?? '0';
  233. //$user->TEL = $this->tel;
  234. //$user->REG_IS_COMPLETED = 1;
  235. $user->AVATAR = 'avatar/1.png';
  236. $user->IS_DEC = $this->isDec ? $this->isDec : 0;
  237. $user->DEC_ID = $this->_decId ?? null;
  238. //$user->BIRTHDAY = Date::ociToDate($this->birthday, Date::OCI_TIME_FORMAT_DATE);
  239. $user->DEC_ROLE_ID = $this->decRoleId ?? null;
  240. $user->PERIOD_AT = $period->getNowPeriodNum();
  241. $user->REG_FROM = 0;//空单标识
  242. $user->IS_DIRECT_SELLER = 0;
  243. $user->VERIFIED = 1;
  244. $user->VERIFIED_AT = Date::nowTime();
  245. $user->USER_CREATOR = Admin::getAdminNameById(\Yii::$app->user->id);
  246. $user->IS_RECHARGE = 1;
  247. $user->COUNTRY_ID = $this->countryId;
  248. $user->LANGUAGE_ID = $this->languageId;
  249. if (!$user->save()) {
  250. throw new Exception(Form::formatErrorsForApi($user->getErrors()));
  251. }
  252. // 获取报单级别的设置
  253. $decLevelConfig = Cache::getDecLevelConfig();
  254. $decLevelPv = $decLevelConfig[$this->decLv]['PERF'];
  255. $userForm = new UserForm();
  256. $userForm->scenario = 'addByAdmin';
  257. $userForm->userId = $user->ID;
  258. $userForm->userName = $this->userName;
  259. $userForm->zcAmount = $decLevelPv;
  260. $userForm->zcPv = $decLevelPv;
  261. $userForm->conUserName = $this->conUserName;
  262. $userForm->recUserName = $this->recUserName;
  263. $userForm->location = $this->location;
  264. if (!$userForm->add()) {
  265. throw new Exception(Form::formatErrorsForApi($userForm->getErrors()));
  266. }
  267. $transactionShop->commit();
  268. } catch (Exception $e) {
  269. $transactionShop->rollBack();
  270. $this->addError('add', $e->getMessage());
  271. return null;
  272. }
  273. $select = new \yii\db\Expression('ID,REAL_NAME,ID_CARD,MOBILE,ADDRESS,OPEN_BANK,BANK_ADDRESS,BANK_NO,BANK_PROVINCE,BANK_CITY,BANK_COUNTY,CREATED_AT,DEC_LV,EMP_LV,PROVINCE,CITY,COUNTY,TEL,SUB_COM_ID,IS_DEC,DEC_ID,DEC_ROLE_ID,PERIOD_AT,REG_FROM');
  274. $this->adminOperateLogger->afterInsert($user->ID, 'ID', ['select'=>$select]);
  275. $userData = $this->adminOperateLogger->saveAfterContent;
  276. $this->adminOperateLogger->fetchClass = UserInfo::class;
  277. $this->adminOperateLogger->afterInsert($user->ID, 'USER_ID', ['select'=>'USER_ID,CON_UID,REC_UID']);
  278. $infoData = $this->adminOperateLogger->saveAfterContent;
  279. $this->adminOperateLogger->saveAfterContent=array_merge($userData,$infoData);
  280. $this->adminOperateLogger->clean()->save([
  281. 'optType' => 'Blank document entry',//空单录入
  282. 'userId' => $user->ID,
  283. 'userName' => $user->USER_NAME,
  284. ]);
  285. return $user;
  286. }
  287. /**
  288. * 删除会员
  289. * @return null
  290. * @throws Exception
  291. * @throws \yii\db\Exception
  292. */
  293. public function userDel() {
  294. if (!$this->validate()) {
  295. return null;
  296. }
  297. $select = new \yii\db\Expression('ID,REAL_NAME,ID_CARD,MOBILE,ADDRESS,OPEN_BANK,BANK_ADDRESS,BANK_NO,BANK_PROVINCE,BANK_CITY,BANK_COUNTY,CREATED_AT,DEC_LV,EMP_LV,PROVINCE,CITY,COUNTY,TEL,SUB_COM_ID,IS_DEC,DEC_ID,DEC_ROLE_ID,PERIOD_AT,REG_FROM');
  298. $this->adminOperateLogger->beforeDelete($this->_delUserId, 'ID', ['select'=>$select]);
  299. $userData = $this->adminOperateLogger->saveBeforeContent;
  300. $this->adminOperateLogger->fetchClass = UserInfo::class;
  301. $this->adminOperateLogger->beforeDelete($this->_delUserId, 'USER_ID', ['select'=>'USER_ID,CON_UID,REC_UID']);
  302. $infoData = $this->adminOperateLogger->saveBeforeContent;
  303. $this->adminOperateLogger->saveBeforeContent=array_merge($userData,$infoData);
  304. $transaction = \Yii::$app->db->beginTransaction();
  305. try {
  306. /*$user=User::findOne(['ID'=>$this->_delUserId]);
  307. $user->DELETED=1;
  308. $user->DELETED_AT=Date::nowTime();
  309. if(!$user->save()){
  310. throw new Exception(Form::formatErrorsForApi($user->getErrors()));
  311. }
  312. $userInfo=UserInfo::findOne(['USER_ID'=>$this->_delUserId]);
  313. $userInfo->DELETED=1;
  314. $userInfo->DELETED_AT=Date::nowTime();
  315. if(!$userInfo->save()){
  316. throw new Exception(Form::formatErrorsForApi($userInfo->getErrors()));
  317. }*/
  318. //为释放用户名资源,彻底删除@190606
  319. User::deleteAll('ID=:USER_ID', [':USER_ID' => $this->_delUserId]);
  320. // 获取会员信息
  321. $userInfo = UserInfo::findOneAsArray('USER_ID=:USER_ID', [':USER_ID'=>$this->_delUserId]);
  322. // 调整上级会员的CON_NUM和REC_NUM
  323. if(!UserInfo::updateAllCounters(['CON_NUM'=> -1], "USER_ID=:USER_ID", [':USER_ID'=>$userInfo['CON_UID']])){
  324. throw new Exception('Failed to reduce the number of superior member contacts');//减少上级会员接点数量失败
  325. }
  326. if(!UserInfo::updateAllCounters(['REC_NUM'=> -1], "USER_ID=:USER_ID", [':USER_ID'=>$userInfo['REC_UID']])){
  327. throw new Exception('Failed to reduce the number of recommended Sponsor');//减少开拓人推荐数量失败
  328. }
  329. // 删除会员信息
  330. if(!UserInfo::deleteAll('USER_ID=:USER_ID', [':USER_ID' => $this->_delUserId])){
  331. throw new Exception('Failed to delete member information');//删除会员信息失败
  332. }
  333. //删除安置
  334. if(!UserNetwork::deleteAll('USER_ID=:USER_ID', [':USER_ID' => $this->_delUserId])){
  335. throw new Exception('Failed to delete member placement data');//删除会员安置数据失败
  336. }
  337. //删除开拓
  338. if(!UserRelation::deleteAll('USER_ID=:USER_ID', [':USER_ID' => $this->_delUserId])){
  339. throw new Exception('Failed to delete member development data');//删除会员开拓数据失败
  340. }
  341. //删除点位绑定
  342. UserBind::deleteAll('USER_ID=:USER_ID', [':USER_ID' => $this->_delUserId]);
  343. //删除合作
  344. UserTeamwork::deleteAll('USER_ID=:USER_ID', [':USER_ID' => $this->_delUserId]);
  345. //删除奖金
  346. UserBonus::deleteAll('USER_ID=:USER_ID', [':USER_ID' => $this->_delUserId]);
  347. $transaction->commit();
  348. } catch (Exception $e) {
  349. $transaction->rollBack();
  350. throw new Exception($e->getMessage());
  351. }
  352. $this->adminOperateLogger->clean()->save([
  353. 'optType' => 'Delete empty member',//删除空单会员
  354. ]);
  355. return $this->_delUserId;
  356. }
  357. }