UserBindForm.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. <?php
  2. namespace common\models\forms;
  3. use common\helpers\Date;
  4. use common\helpers\user\Info;
  5. use common\helpers\user\Status;
  6. use common\components\Model;
  7. use common\helpers\Form;
  8. use common\libs\logging\operate\AdminOperate;
  9. use common\libs\logging\operate\UserOperate;
  10. use common\models\Period;
  11. use common\models\User;
  12. use common\models\UserBind;
  13. use common\models\UserInfo;
  14. use yii\base\Exception;
  15. /**
  16. * 点位绑定表单
  17. */
  18. class UserBindForm extends Model {
  19. public $id;
  20. public $userId;
  21. public $mainUid;
  22. public $userName;
  23. public $mainUserName;
  24. private $_uid;
  25. private $_mainUid;
  26. private $_model;
  27. private $_userIdcard;
  28. public function init() {
  29. parent::init();
  30. $this->adminOperateLogger = new AdminOperate([
  31. 'fetchClass' => UserBind::class,
  32. ]);
  33. $this->userOperateLogger = new UserOperate([
  34. 'fetchClass' => UserBind::class,
  35. ]);
  36. }
  37. /**
  38. * @inheritdoc
  39. */
  40. public function rules() {
  41. return [
  42. [['id', 'userName', 'mainUserName', 'mainUid', 'userId'], 'trim'],
  43. [['id', 'userName', 'mainUserName'], 'required'],
  44. [['userName'], 'isUserName'],
  45. [['mainUserName'], 'isMainUserName'],
  46. //[['mainUid'], 'isMainUid'],
  47. //[['userId'], 'isUserId'],
  48. [['id'], 'initModel'],
  49. ];
  50. }
  51. /**
  52. * 指定校验场景
  53. * @return array
  54. */
  55. public function scenarios() {
  56. $parentScenarios = parent::scenarios();
  57. $customScenarios = [
  58. 'add' => ['userName', 'mainUserName'],
  59. 'edit' => ['id', 'mainUid'],
  60. 'frontEdit' => ['id', 'mainUid'],
  61. 'autoBind' => ['userId', 'mainUid'],
  62. ];
  63. return array_merge($parentScenarios, $customScenarios);
  64. }
  65. public function attributeLabels() {
  66. return [
  67. 'userName' => '会员编号',
  68. 'mainUserName' => '主会员编号',
  69. ];
  70. }
  71. /**
  72. * 初始化model
  73. * @param $attributes
  74. */
  75. public function initModel($attributes) {
  76. $this->_model = UserBind::findOne(['ID' => $this->id]);
  77. if (!$this->_model) {
  78. $this->addError($attributes, 'The data does not exist'); // 数据不存在
  79. }
  80. if($this->scenario=='frontEdit'){
  81. if (Info::getIdCardByUserId(\Yii::$app->user->id) != Info::getIdCardByUserId($this->_model['MAIN_UID'])) {
  82. $this->addError($attributes, '无权限编辑');
  83. }
  84. }
  85. }
  86. /**
  87. * 用户名是否正确
  88. * @param $attributes
  89. */
  90. public function isUserName($attributes) {
  91. if ($this->userName == $this->mainUserName) {
  92. $this->addError($attributes, '同一会员不能绑定');
  93. }
  94. $userInfo = UserInfo::findOneAsArray(['USER_NAME' => $this->userName]);
  95. if ($userInfo && Info::isVerified(($userInfo['USER_ID']))) {
  96. if (UserBind::find()->where('USER_ID=:USER_ID AND IS_DEL=0', [':USER_ID' => $userInfo['USER_ID']])->exists()) {
  97. $this->addError($attributes, '填写的会员已有绑定关系');
  98. }
  99. $this->_uid = $userInfo['USER_ID'];
  100. } else {
  101. $this->addError($attributes, '会员不存在或未完成实名认证');
  102. }
  103. }
  104. /**
  105. * 主会员编号
  106. * @param $attributes
  107. */
  108. public function isMainUserName($attributes) {
  109. $userInfo = UserInfo::findOneAsArray(['USER_NAME' => $this->mainUserName]);
  110. if ($userInfo && Info::isVerified(($userInfo['USER_ID']))) {
  111. if (UserBind::find()->where('USER_ID=:USER_ID AND IS_DEL=0 AND MAIN_UID!=:USER_ID', [':USER_ID' => $userInfo['USER_ID']])->exists()) {
  112. $this->addError($attributes, '填写的主会员已被其他主会员绑定');
  113. }
  114. $this->_mainUid = $userInfo['USER_ID'];
  115. } else {
  116. $this->addError($attributes, '主会员不存在或未完成实名认证');
  117. return false;
  118. }
  119. if (Info::getIdCardByUserId($this->_uid) != Info::getIdCardByUserId($this->_mainUid)) {
  120. $this->addError($attributes, '两个会员身份证号不一致无法绑定');
  121. }
  122. $idCard = Info::getIdCardByUserId($this->_mainUid);
  123. //是否有其他绑定关系
  124. $hasUsers = User::find()->select('ID')->where('ID!=:ID AND ID!=:IDU AND VERIFIED=1 AND ID_CARD=:ID_CARD', [':ID' => $this->_mainUid, ':IDU'=>$this->_uid, ':ID_CARD' => $idCard])->orderBy('CREATED_AT ASC')->asArray()->all();
  125. if($hasUsers){
  126. foreach ($hasUsers as $u){
  127. if (UserBind::find()->where('USER_ID=:USER_ID AND IS_DEL=0 AND MAIN_UID!=:MAIN_UID', [':USER_ID' => $u['ID'], ':MAIN_UID'=>$this->_mainUid])->exists()) {
  128. $this->addError($attributes, '身份证'.$idCard.'有其他的绑定关系,请查证');
  129. break;
  130. }
  131. }
  132. }
  133. }
  134. public function isMainUid($attributes) {
  135. if(!Info::isVerified($this->mainUid)){
  136. $this->addError($attributes, '主会员未完成实名认证');
  137. return false;
  138. }
  139. }
  140. public function isUserId($attributes) {
  141. if(!Info::isVerified($this->userId)){
  142. $this->addError($attributes, '会员未完成实名认证');
  143. return false;
  144. }
  145. }
  146. /**
  147. * 添加
  148. * @return UserBind|null
  149. * @throws \yii\base\Exception
  150. */
  151. public function add() {
  152. if (!$this->validate()) {
  153. return null;
  154. }
  155. $transaction = \Yii::$app->db->beginTransaction();
  156. try {
  157. $period = Period::instance();
  158. // 查看主点位是否已经存在
  159. if (!UserBind::find()->where('USER_ID=:USER_ID AND MAIN_UID=:MAIN_UID AND IS_DEL=0', [':USER_ID' => $this->_mainUid, ':MAIN_UID' => $this->_mainUid])->exists()) {
  160. UserBind::insertOne([
  161. 'USER_ID' => $this->_mainUid,
  162. 'MAIN_UID' => $this->_mainUid,
  163. 'PERIOD_NUM' => $period->getNowPeriodNum(),
  164. 'IS_DEL' => 0,
  165. 'CREATE_ADMIN' => \Yii::$app->user->id,
  166. 'CREATED_AT' => Date::nowTime(),
  167. ]);
  168. }
  169. // 会员信息表添加点位合作标识
  170. UserInfo::updateAll(['IS_BIND' => 1], 'USER_ID=:USER_ID', [':USER_ID' => $this->_uid]);
  171. UserInfo::updateAll(['IS_BIND' => 1, 'IS_BIND_MAIN' => 1], 'USER_ID=:USER_ID', [':USER_ID' => $this->_mainUid]);
  172. // 添加会员合作点位
  173. $model = new UserBind();
  174. $model->USER_ID = $this->_uid;
  175. $model->MAIN_UID = $this->_mainUid;
  176. $model->PERIOD_NUM = $period->getNowPeriodNum();
  177. $model->IS_DEL = 0;
  178. $model->CREATE_ADMIN = \Yii::$app->user->id;
  179. $model->CREATED_AT = Date::nowTime();
  180. if (!$model->save()) {
  181. throw new Exception(Form::formatErrorsForApi($model->getErrors()));
  182. }
  183. $transaction->commit();
  184. } catch (Exception $e) {
  185. $transaction->rollBack();
  186. $this->addError('add', $e->getMessage());
  187. return null;
  188. }
  189. $this->adminOperateLogger->afterInsert($model);
  190. $this->adminOperateLogger->clean()->save([
  191. 'optType' => '添加点位绑定',
  192. 'userId' => $model->USER_ID,
  193. 'userName' => Info::getUserNameByUserId($model->USER_ID),
  194. ]);
  195. return $model;
  196. }
  197. /**
  198. * 前台编辑主点位
  199. * @return null
  200. * @throws \yii\db\Exception
  201. */
  202. public function frontEdit() {
  203. if (!$this->validate()) {
  204. return null;
  205. }
  206. $this->userOperateLogger->beforeUpdate($this->_model->ID,'ID',['select'=>'USER_ID,MAIN_UID']);
  207. $transaction = \Yii::$app->db->beginTransaction();
  208. try {
  209. $model = $this->_model;
  210. if ($model->MAIN_UID == $this->mainUid) {
  211. throw new Exception('未做任何修改');
  212. }
  213. //撤销主会员
  214. UserInfo::updateAll(['IS_BIND_MAIN' => 0], 'USER_ID=:USER_ID', [':USER_ID' => $model->MAIN_UID]);
  215. //设置主会员
  216. UserInfo::updateAll(['IS_BIND_MAIN' => 1], 'USER_ID=:USER_ID', [':USER_ID' => $this->mainUid]);
  217. //更新绑定表的主会员
  218. UserBind::updateAll(['MAIN_UID' => $this->mainUid, 'UPDATED_AT' => Date::nowTime(), 'UPDATE_ADMIN' => \Yii::$app->user->id], 'MAIN_UID=:MAIN_UID AND IS_DEL=0', [':MAIN_UID' => $model->MAIN_UID]);
  219. $transaction->commit();
  220. } catch (Exception $e) {
  221. $transaction->rollBack();
  222. $this->addError('frontEdit', $e->getMessage());
  223. return null;
  224. }
  225. $this->userOperateLogger->afterUpdate($this->_model->ID,'ID',['select'=>'USER_ID,MAIN_UID']);
  226. $this->userOperateLogger->clean()->save([
  227. 'optType' => '修改主点位',
  228. 'userId' => $model->USER_ID,
  229. 'userName' => Info::getUserNameByUserId($model->USER_ID),
  230. ]);
  231. return $model;
  232. }
  233. /**
  234. * 编辑主点位
  235. * @return null
  236. * @throws \yii\db\Exception
  237. */
  238. public function edit() {
  239. if (!$this->validate()) {
  240. return null;
  241. }
  242. $this->adminOperateLogger->beforeUpdate($this->_model);
  243. $transaction = \Yii::$app->db->beginTransaction();
  244. try {
  245. $model = $this->_model;
  246. if ($model->MAIN_UID == $this->mainUid) {
  247. throw new Exception('未做任何修改');
  248. }
  249. //撤销主会员
  250. UserInfo::updateAll(['IS_BIND_MAIN' => 0], 'USER_ID=:USER_ID', [':USER_ID' => $model->MAIN_UID]);
  251. //设置主会员
  252. UserInfo::updateAll(['IS_BIND_MAIN' => 1], 'USER_ID=:USER_ID', [':USER_ID' => $this->mainUid]);
  253. //更新绑定表的主会员
  254. UserBind::updateAll(['MAIN_UID' => $this->mainUid, 'UPDATED_AT' => Date::nowTime(), 'UPDATE_ADMIN' => \Yii::$app->user->id], 'MAIN_UID=:MAIN_UID AND IS_DEL=0', [':MAIN_UID' => $model->MAIN_UID]);
  255. $transaction->commit();
  256. } catch (Exception $e) {
  257. $transaction->rollBack();
  258. $this->addError('edit', $e->getMessage());
  259. return null;
  260. }
  261. $this->adminOperateLogger->afterUpdate(UserBind::findOne(['ID' => $this->id]));
  262. $this->adminOperateLogger->clean()->save([
  263. 'optType' => '编辑点位绑定',
  264. 'userId' => $model->USER_ID,
  265. 'userName' => Info::getUserNameByUserId($model->USER_ID),
  266. ]);
  267. return $model;
  268. }
  269. public function beforeDelete($selected){
  270. $this->adminOperateLogger->setIsBatch(true)->beforeDelete($selected, 'ID');
  271. }
  272. /**
  273. * 删除
  274. * @param $selected
  275. * @throws Exception
  276. */
  277. public function delete($selected) {
  278. foreach ($selected as $select) {
  279. $userBind = UserBind::findOneAsArray('ID=:ID', [':ID' => $select], 'USER_ID,MAIN_UID');
  280. //无法删除有子会员的
  281. if (UserBind::find()->where('MAIN_UID=:USER_ID AND IS_DEL=0 AND USER_ID!=:USER_ID', [':USER_ID' => $userBind['USER_ID']])->exists()) {
  282. throw new Exception('该主会员有子会员,无法撤销');
  283. }
  284. UserInfo::updateAll(['IS_BIND' => 0, 'IS_BIND_MAIN' => 0], 'USER_ID=:USER_ID', [':USER_ID' => $userBind['USER_ID']]);
  285. User::updateAll(['VERIFIED' => 0], 'ID=:USER_ID', [':USER_ID' => $userBind['USER_ID']]);
  286. $bindNum = UserBind::findOneAsArray('IS_DEL=0 AND MAIN_UID=:MAIN_UID', [':MAIN_UID' => $userBind['MAIN_UID']],'COUNT(*) AS NUM');
  287. if (isset($bindNum['NUM']) && $bindNum['NUM'] < 2) {
  288. UserBind::updateAll(['IS_DEL' => 1], 'USER_ID=:USER_ID', [':USER_ID' => $userBind['MAIN_UID']]);
  289. UserInfo::updateAll(['IS_BIND' => 0, 'IS_BIND_MAIN' => 0], 'USER_ID=:USER_ID', [':USER_ID' => $userBind['MAIN_UID']]);
  290. }
  291. }
  292. $this->adminOperateLogger->clean()->save([
  293. 'optType' => '撤销点位绑定',
  294. ]);
  295. }
  296. /**
  297. * 自动绑定点位
  298. * @return UserBind|null
  299. * @throws \yii\db\Exception
  300. */
  301. public function autoBind() {
  302. if (!$this->validate()) {
  303. return null;
  304. }
  305. $transaction = \Yii::$app->db->beginTransaction();
  306. try {
  307. //判断hz
  308. if (Info::isUnion($this->mainUid) && !Info::isUnion($this->userId)) {
  309. $userId = $this->mainUid;
  310. $this->mainUid = $this->userId;
  311. $this->userId = $userId;
  312. }
  313. $period = Period::instance();
  314. // 查看主点位是否已经存在
  315. if (!UserBind::find()->where('USER_ID=:USER_ID AND MAIN_UID=:MAIN_UID AND IS_DEL=0', [':USER_ID' => $this->mainUid, ':MAIN_UID' => $this->mainUid])->exists()) {
  316. UserBind::insertOne([
  317. 'USER_ID' => $this->mainUid,
  318. 'MAIN_UID' => $this->mainUid,
  319. 'PERIOD_NUM' => $period->getNowPeriodNum(),
  320. 'IS_DEL' => 0,
  321. 'CREATE_ADMIN' => \Yii::$app->id == 'app-backendApi' ? \Yii::$app->user->id : null,
  322. 'CREATED_AT' => Date::nowTime(),
  323. ]);
  324. }
  325. // 会员信息表添加点位合作标识
  326. UserInfo::updateAll(['IS_BIND' => 1], 'USER_ID=:USER_ID', [':USER_ID' => $this->userId]);
  327. UserInfo::updateAll(['IS_BIND' => 1, 'IS_BIND_MAIN' => 1], 'USER_ID=:USER_ID', [':USER_ID' => $this->mainUid]);
  328. // 添加会员合作点位
  329. $model = new UserBind();
  330. $model->USER_ID = $this->userId;
  331. $model->MAIN_UID = $this->mainUid;
  332. $model->PERIOD_NUM = $period->getNowPeriodNum();
  333. $model->IS_DEL = 0;
  334. $model->CREATE_USER = $this->userId;
  335. $model->CREATED_AT = Date::nowTime();
  336. if (!$model->save()) {
  337. throw new Exception(Form::formatErrorsForApi($model->getErrors()));
  338. }
  339. $transaction->commit();
  340. } catch (Exception $e) {
  341. $transaction->rollBack();
  342. $this->addError('autoAdd', $e->getMessage());
  343. return null;
  344. }
  345. return $model;
  346. }
  347. }