Tongbu.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * 服务提供者
  5. * User: qissen
  6. * Date: 2017/6/7
  7. * Time: 7:36
  8. * 注意调用顺序,checkClientType,checkData必须先调用,才可以验证其他
  9. */
  10. namespace app\core\service;
  11. use app\core\model\Bank;
  12. use app\core\model\Cang;
  13. use app\core\model\CangRepay;
  14. use app\core\model\Channel;
  15. use app\core\model\HongbaoPlan;
  16. use app\core\model\Subject;
  17. use app\core\model\User;
  18. use app\core\model\UserAccount;
  19. use app\core\model\UserBank;
  20. use app\core\model\UserDrawcash;
  21. use app\core\model\UserFinance;
  22. use app\core\model\UserRecharge;
  23. use think\Db;
  24. use think\Log;
  25. use tool\Common;
  26. class Tongbu
  27. {
  28. private $db_config_from = [
  29. // 数据库类型
  30. 'type' => 'mysql',
  31. // 服务器地址
  32. 'hostname' => 'rm-bp1m6abztwo27gum6.mysql.rds.aliyuncs.com',
  33. // 数据库名
  34. 'database' => 'goldapi',
  35. // 数据库用户名
  36. 'username' => 'sqlyog',
  37. // 数据库密码
  38. 'password' => 'Qissen111111',
  39. // 数据库编码默认采用utf8
  40. 'charset' => 'utf8',
  41. // 数据库表前缀
  42. 'prefix' => 's_',
  43. ];
  44. public function DY_user($mobile) {
  45. $isTongbu = false;
  46. $from_list = Db::connect($this->db_config_from)->query('select * from s_user where card_no_auth=1 and username=\''.$mobile.'\'');
  47. foreach ($from_list as $k=>$item) {
  48. if($item['tongbu'] == 1) {
  49. return false;
  50. }
  51. $isTongbu = true;
  52. /*if($password) {
  53. if($item['password'] != md5($password)) {
  54. return -2;
  55. }
  56. }*/
  57. Log::info('执行了一次同步');
  58. $hasTongbu = true;
  59. //用户
  60. $user = User::get([
  61. 'mobile'=>$item['username']
  62. ]);
  63. if(!$user) {
  64. $user = new User();
  65. }
  66. $user['BACKUPID'] = $item['id'];//BACKUPID
  67. $user['password'] = (new User())->createPassword(THINK_START_TIME);//重新设置新密码
  68. $user['mobile'] = $item['username'];//手机号
  69. $user['trueName'] = $item['real_name'];//真实姓名
  70. $user['isAuthTrueName'] = $item['card_no_auth'];//实名认证
  71. $user['isAuthBank'] = $item['card_no_auth'];//实名认证
  72. $user['passport'] = $item['card_no'];//身份证
  73. $user['email'] = $item['email'];//银行卡认证
  74. $user['osType'] = $item['device_type'];//银行卡认证
  75. $user['isNewInvest'] = 0;
  76. if($item['device_type'] == 4) {
  77. $user['osType'] = 3;
  78. }
  79. if($item['device_type'] == 3) {
  80. $user['osType'] = 4;
  81. }
  82. if($item['level'] == 1) {
  83. $user['isNewInvest'] = 1;
  84. }
  85. $channel = Channel::get([
  86. 'code'=>$item['channel']
  87. ]);
  88. if($channel) {
  89. $user['channelID'] = $channel['channelID'];//渠道名称
  90. if($item['channel_web_id'] == 40) {
  91. $user['channelID'] = 59;
  92. }
  93. }
  94. else {
  95. $item['channelID'] = 0;
  96. }
  97. if($item['add_time']) {
  98. $user['addTime'] = Common::datetotime($item['add_time']);//添加时间
  99. }
  100. else {
  101. $user['addTime'] = 0;//添加时间
  102. }
  103. $user->save();
  104. (new HongbaoPlan())->sendUserOnRegister($user);
  105. //账户
  106. $from_account = Db::connect($this->db_config_from)->query('select * from s_user_account where user_id='.$item['id']);
  107. $account = UserAccount::get([
  108. 'userID'=>$user['userID']
  109. ]);
  110. if(!$account) {
  111. $account = new UserAccount();
  112. }
  113. if(isset($from_account[0])) {
  114. $account['BACKUPID'] = $from_account[0]['id'];
  115. $account['userID'] = $user['userID'];
  116. $account['money'] = $from_account[0]['account_able'];
  117. $account['moneyAcc'] = $from_account[0]['total_invest_interest'];
  118. $account['moneyFrozen'] = $from_account[0]['account_freeze'];
  119. $account['waitBen'] = $from_account[0]['wait_capital'];
  120. $account['waitInterest'] = $from_account[0]['wait_interest'];
  121. $account['hasInvestBenTotal'] = 0;
  122. $account['hasInvestMoneyTotal'] = 0;
  123. $account['hasRepayBenTotal'] = $from_account[0]['total_invest_capital'];
  124. $account['hasRepayInterestTotal'] = $from_account[0]['total_invest_interest'];
  125. $account->save();
  126. }
  127. else {
  128. $account['userID'] = $from_account[0]['user_id'];
  129. $account->save();
  130. }
  131. //新增银行卡
  132. $from_bank = Db::connect($this->db_config_from)->query('select * from s_user_bank where user_id='.$item['id']);
  133. foreach ($from_bank as $_k=>$_item) {
  134. $bank = Bank::where([
  135. 'bankAccountCode'=>$_item['bank_code']
  136. ])->find();
  137. //用户银行卡
  138. $userBank = UserBank::get([
  139. 'BACKUPID'=>$_item['id']
  140. ]);
  141. if(!$userBank) {
  142. $userBank = new UserBank();
  143. }
  144. $userBank['BACKUPID'] = $_item['id'];//BACKUPID
  145. $userBank['userID'] = $user['userID'];//手机号
  146. $userBank['bankNameFull'] = $_item['bank_name'];//
  147. $userBank['bankID'] = isset($bank['bankID']) ? $bank['bankID'] : 1;//实名认证
  148. $userBank['bankAccount'] = $_item['acct_name'];
  149. $userBank['bankNumber'] = $_item['bank_card_no'];
  150. $userBank['trueName'] = $_item['acct_name'];
  151. $userBank['mobile'] = $_item['mobile'];
  152. $userBank['addTime'] = Common::datetotime($_item['add_time']);
  153. $userBank['status'] = 1;
  154. $userBank['isDefault'] = 1;
  155. $userBank->save();
  156. }
  157. //新增交易
  158. $from_cangList = Db::connect($this->db_config_from)->query('select * from s_user_due_detail where user_id='.$item['id']);
  159. foreach ($from_cangList as $_k=>$_item) {
  160. $subject = Subject::get([
  161. 'BACKUPID'=>$_item['project_id']
  162. ]);
  163. $user = User::get([
  164. 'BACKUPID'=>$_item['user_id']
  165. ]);
  166. $status = 0;
  167. if($_item['status'] == 1) {
  168. $status = Cang::STATUS_INTEREST;
  169. }
  170. else if($_item['status'] == 2) {
  171. $status = Cang::STATUS_FINISH;
  172. }
  173. else if($_item['type'] == 3) {
  174. $status = Cang::STATUS_REPAY;
  175. }
  176. //$from_recharge = Db::connect($this->db_config_from)->query('select * from s_recharge_log where recharge_no=\''.$_item['recharge_no'].'\''.' and user_id='.$_item['user_id'].' and project_id='.$_item['project_id']);
  177. //if(!$from_recharge) {
  178. // continue;
  179. //}
  180. //$from_recharge = $from_recharge[0];
  181. $cang = Cang::get([
  182. 'BACKUPID'=>$_item['id']
  183. ]);
  184. if(!$cang) {
  185. $cang = new Cang();
  186. }
  187. $cang['BACKUPID'] = $_item['id'];
  188. $cang['subjectID'] = isset($subject['subjectID']) ? $subject['subjectID'] : 0;
  189. $cang['userID'] = $user['userID'];
  190. $cang['moneySubject'] = $_item['due_capital'];
  191. $cang['status'] = $status;
  192. $cang['addTime'] = Common::datetotime($_item['add_time']);
  193. $cang['payTime'] = Common::datetotime($_item['add_time']);
  194. $cang['updateTime'] = Common::datetotime($_item['add_time']);
  195. $cang['interestBeginTime'] = Common::datetotime($_item['start_time']);
  196. $cang['interestEndTime'] = Common::datetotime($_item['due_time']);
  197. $cang['repayTime'] = Common::datetotime($_item['due_time']);
  198. $cang['year'] = $subject['year'];
  199. $cang['ben'] = $_item['due_capital'];
  200. $cang['yearSystem'] = $subject['yearSystem'];
  201. $cang['interest'] = $_item['due_interest'];
  202. $cang['investDay'] = $_item['duration_day'];
  203. $cang['interestTimeTypeID'] = isset($subject['interestTimeTypeID']) ? $subject['interestTimeTypeID']: 0;
  204. if($_item['ghost_phone']) {
  205. $cang['isForged'] = 1;
  206. $cang['userID'] = 4;
  207. }
  208. $cang['osType'] = $_item['device_type'];
  209. if($_item['device_type'] == 3) {
  210. $cang['osType'] = 4;
  211. }
  212. else if($_item['device_type'] == 4) {
  213. $cang['osType'] = 3;
  214. }
  215. $cang['money'] = $_item['due_capital'];
  216. $cang->save();
  217. CangRepay::where([
  218. 'cangID'=>$cang['cangID']
  219. ])->delete();
  220. //生成本
  221. $cangRepay = new CangRepay();
  222. $cangRepay['money'] = $_item['due_capital'];
  223. $cangRepay['repayTime'] = Common::datetotime($_item['due_time']);
  224. $cangRepay['reachTime'] = Common::datetotime($_item['due_time']) + 86400;
  225. $cangRepay['cangID'] = $cang['cangID'];
  226. $cangRepay['subjectID'] = $subject['subjectID'];
  227. $cangRepay['userID'] = $user['userID'];
  228. $cangRepay['status'] = CangRepay::STATUS_UNREPAY;
  229. if($_item['status'] == 2) {
  230. $cangRepay['status'] = CangRepay::STATUS_REPAY;
  231. }
  232. $cangRepay['repayTypeID'] = 1;
  233. $cangRepay->save();
  234. //生成息
  235. $cangRepay = new CangRepay();
  236. $cangRepay['money'] = $_item['due_interest'];
  237. $cangRepay['repayTime'] = Common::datetotime($_item['due_time']);
  238. $cangRepay['reachTime'] = Common::datetotime($_item['due_time']) + 86400;
  239. $cangRepay['cangID'] = $cang['cangID'];
  240. $cangRepay['subjectID'] = $subject['subjectID'];
  241. $cangRepay['userID'] = $user['userID'];
  242. $cangRepay['status'] = CangRepay::STATUS_UNREPAY;
  243. if($_item['status'] == 2) {
  244. $cangRepay['status'] = CangRepay::STATUS_REPAY;
  245. }
  246. $cangRepay['repayTypeID'] = 2;
  247. $cangRepay->save();
  248. }
  249. //生成充值记录
  250. $from_list = Db::connect($this->db_config_from)->query('select * from s_user_wallet_records where recharge_no<>\'\' and value > 0 and type=1 and status<>3 and user_id='.$item['id']);
  251. foreach ($from_list as $__k=>$__item) {
  252. //用户
  253. $user = User::get([
  254. 'BACKUPID'=>$__item['user_id']
  255. ]);
  256. if(!$user || $__item['user_id'] == 0) {
  257. continue;
  258. }
  259. //银行
  260. $userBank = UserBank::get([
  261. 'BACKUPID'=>$__item['user_bank_id']
  262. ]);
  263. if(!$userBank) {
  264. continue;
  265. }
  266. //余额
  267. $userRecharge = UserRecharge::get([
  268. 'BACKUPID'=>$__item['id']
  269. ]);
  270. if(!$userRecharge) {
  271. $userRecharge = new UserRecharge();
  272. }
  273. $status = 0;
  274. if($__item['status'] == 1) {
  275. $status = UserRecharge::STATUS_PAY;
  276. }
  277. else if($__item['status'] == 2) {
  278. $status = UserRecharge::STATUS_UNPAY;
  279. }
  280. else if($__item['status'] == 3) {
  281. $status = UserRecharge::STATUS_ERROR;
  282. }
  283. $userRecharge['BACKUPID'] = $__item['id'];
  284. $userRecharge['userID'] = $user['userID'];
  285. $userRecharge['money'] = $__item['value'];
  286. $userRecharge['status'] = $status;
  287. $userRecharge['outerNumber'] = $__item['recharge_no'].'-'.$__item['trade_no'];
  288. $userRecharge['type'] = 'TYPE_BANK';
  289. $userRecharge['bankID'] = $userBank['bankID'];
  290. $userRecharge['bankAccount'] = $userBank['bankAccount'];
  291. $userRecharge['bankNumber'] = $userBank['bankNumber'];
  292. $userRecharge['bankName'] = $userBank['bankNameFull'];
  293. $userRecharge['addTime'] = Common::datetotime($__item['add_time']);
  294. $userRecharge['trueName'] = $userBank['trueName'];
  295. $userRecharge['mobile'] = $userBank['mobile'];
  296. $userRecharge['userBankID'] = $userBank['userBankID'];
  297. $userRecharge->save();
  298. UserFinance::create([
  299. 'mode'=>UserFinance::MODE_RECHARGE,
  300. 'modeID'=>$userRecharge['userRechargeID'],
  301. 'money'=>$__item['value'],
  302. 'addTime'=>Common::datetotime($__item['add_time']),
  303. 'userID'=>$user['userID'],
  304. 'status'=>UserFinance::STATUS_OK,
  305. 'updateTime'=>Common::datetotime($__item['add_time'])
  306. ]);
  307. }
  308. //生成提现记录
  309. $from_list = Db::connect($this->db_config_from)->query('select * from s_user_wallet_records where recharge_no<>\'\' and value < 0 and type=2 and status<>3 and user_id='.$item['id']);
  310. foreach ($from_list as $__k=>$__item) {
  311. //用户
  312. $user = User::get([
  313. 'BACKUPID'=>$__item['user_id']
  314. ]);
  315. if(!$user || $__item['user_id'] == 0) {
  316. continue;
  317. }
  318. //银行
  319. $userBank = UserBank::get([
  320. 'BACKUPID'=>$__item['user_bank_id']
  321. ]);
  322. if(!$userBank) {
  323. continue;
  324. }
  325. //余额
  326. $userDrawcash = UserDrawcash::get([
  327. 'BACKUPID'=>$__item['id']
  328. ]);
  329. if(!$userDrawcash) {
  330. $userDrawcash = new UserDrawcash();
  331. }
  332. $status = 0;
  333. if($__item['status'] == 1) {
  334. $status = UserDrawcash::STATUS_OK;
  335. }
  336. else if($__item['status'] == 2) {
  337. $status = UserDrawcash::STATUS_ING;
  338. }
  339. else if($item['status'] == 3) {
  340. $status = UserDrawcash::STATUS_ERROR;
  341. }
  342. $userDrawcash['BACKUPID'] = $__item['id'];
  343. $userDrawcash['userID'] = $user['userID'];
  344. $userDrawcash['money'] = $__item['value'];
  345. $userDrawcash['status'] = $status;
  346. $userDrawcash['outerNumber'] = $__item['recharge_no'].'-'.$__item['trade_no'];
  347. $userDrawcash['type'] = 'TYPE_BANK';
  348. $userDrawcash['bankID'] = $userBank['bankID'];
  349. $userDrawcash['bankAccount'] = $userBank['bankAccount'];
  350. $userDrawcash['bankNumber'] = $userBank['bankNumber'];
  351. $userDrawcash['bankName'] = $userBank['bankNameFull'];
  352. $userDrawcash['applyTime'] = Common::datetotime($__item['add_time']);
  353. $userDrawcash['trueName'] = $userBank['trueName'];
  354. $userDrawcash['mobile'] = $userBank['mobile'];
  355. $userDrawcash['userBankID'] = $userBank['userBankID'];
  356. $userDrawcash->save();
  357. UserFinance::create([
  358. 'mode'=>UserFinance::MODE_DRAWCASH,
  359. 'modeID'=>$userDrawcash['userDrawcashID'],
  360. 'money'=>$__item['value'],
  361. 'addTime'=>Common::datetotime($__item['add_time']),
  362. 'userID'=>$user['userID'],
  363. 'status'=>UserFinance::STATUS_OK,
  364. 'updateTime'=>Common::datetotime($__item['add_time'])
  365. ]);
  366. }
  367. Db::connect($this->db_config_from)->execute('UPDATE s_user SET tongbu=1 WHERE username=\''.$mobile.'\'');
  368. }
  369. return $isTongbu;
  370. }
  371. public function DY_password($mobile, $password = '') {
  372. $from_list = Db::connect($this->db_config_from)->query('select * from s_user where card_no_auth=1 and username=\''.$mobile.'\'');
  373. foreach ($from_list as $k=>$item) {
  374. if($item['tongbu_password'] == 1) {
  375. return false;
  376. }
  377. if ($item['password'] == md5($password)) {
  378. //tongbu_password
  379. Db::connect($this->db_config_from)->execute('UPDATE s_user SET tongbu_password=1 WHERE username=\''.$mobile.'\'');
  380. return true;
  381. }
  382. }
  383. return false;
  384. }
  385. }