UserAccount.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: qisse
  5. * Date: 2017/6/27
  6. * Time: 20:02
  7. */
  8. namespace app\core\model;
  9. use app\core\model\UserFinance;
  10. use think\Config;
  11. use think\Db;
  12. use think\Exception;
  13. use think\Log;
  14. use think\Model;
  15. use tool\Common;
  16. class UserAccount extends Model
  17. {
  18. protected $pk = 'userAccountID';
  19. protected $resultSetType = 'collection';
  20. protected $updateTime = 'updateTime';
  21. protected $createTime = 'updateTime';
  22. protected $autoWriteTimestamp = true;
  23. public function setMoneyAttr($value)
  24. {
  25. return $value * 100;
  26. }
  27. public function getMoneyTotalAttr($value, $data)
  28. {
  29. return Common::price2(($data['waitBen'] + $data['waitInterest'] + $data['money']) / 100);
  30. }
  31. public function getMoneyAttr($value)
  32. {
  33. return Common::price2($value / 100);
  34. }
  35. public function setMoneyFrozenAttr($value)
  36. {
  37. return $value * 100;
  38. }
  39. public function getMoneyFrozenAttr($value)
  40. {
  41. return Common::price2($value / 100);
  42. }
  43. public function setMoneyAccAttr($value)
  44. {
  45. return $value * 100;
  46. }
  47. public function getMoneyAccAttr($value)
  48. {
  49. return Common::price2($value / 100);
  50. }
  51. /*public function setMoneyYesterdayAttr($value)
  52. {
  53. return $value * 100;
  54. }
  55. public function getMoneyTodayAccAttr($value)
  56. {
  57. return Common::price2($value / 100);
  58. }
  59. public function setMoneyTodayAttr($value)
  60. {
  61. return $value * 100;
  62. }
  63. public function getMoneyTodayAttr($value)
  64. {
  65. return Common::price2($value / 100);
  66. }*/
  67. public function setHasInvestBenTotalAttr($value)
  68. {
  69. return $value * 100;
  70. }
  71. public function getHasInvestBenTotalAttr($value)
  72. {
  73. return Common::price2($value / 100);
  74. }
  75. public function setHasInvestMoneyTotalAttr($value)
  76. {
  77. return $value * 100;
  78. }
  79. public function getHasInvestMoneyTotalAttr($value)
  80. {
  81. return Common::price2($value / 100);
  82. }
  83. public function setHasRepayBenTotalAttr($value)
  84. {
  85. return $value * 100;
  86. }
  87. public function gethasRepayBenTotalAttr($value)
  88. {
  89. return Common::price2($value / 100);
  90. }
  91. public function setHasRepayInterestTotalAttr($value)
  92. {
  93. return $value * 100;
  94. }
  95. public function getHasRepayInterestTotalAttr($value)
  96. {
  97. return Common::price2($value / 100);
  98. }
  99. public function setWaitBenAttr($value)
  100. {
  101. return $value * 100;
  102. }
  103. public function getWaitBenAttr($value)
  104. {
  105. return Common::price2($value / 100);
  106. }
  107. public function setWaitInterestAttr($value)
  108. {
  109. return $value * 100;
  110. }
  111. public function getWaitInterestAttr($value)
  112. {
  113. return Common::price2($value / 100);
  114. }
  115. public function setIntroduceMoneyAttr($value)
  116. {
  117. return $value * 100;
  118. }
  119. public function getIntroduceMoneyAttr($value)
  120. {
  121. return Common::price2($value / 100);
  122. }
  123. public function updateProfit($cang = false) {
  124. $todayTime = Common::datetotime(Common::timetodate(THINK_START_TIME, 0));
  125. if($this['todayTime'] != $todayTime) {
  126. //更新昨天收益
  127. //$this['moneyYesterday'] = $this['moneyToday'];
  128. //更新今日收益
  129. $cangList = Cang::where([
  130. 'isForged'=>0,
  131. 'status'=>['in', [Cang::STATUS_INTEREST]],
  132. 'userID'=>$this['userID']
  133. ])->select();
  134. $money = 0;
  135. $moneyAcc = 0;
  136. if(!$cangList->isEmpty()) {
  137. foreach ($cangList as $k=>$item) {
  138. $moneyOneDay = round($item['interest'] / $item['investDay'], 6);
  139. $money += $moneyOneDay;
  140. if(!$this['todayTime']) {
  141. $moneyAcc = $moneyAcc + $moneyOneDay + (($todayTime - Common::datetotime(Common::timetodate($item->getData('interestBeginTime')))) / 86400) * $moneyOneDay;
  142. //Log::info('day:'.(($todayTime - Common::datetotime(Common::timetodate($item->getData('interestBeginTime')))) / 86400));
  143. }
  144. }
  145. }
  146. $money = round($money, 4);
  147. $this['moneyToday'] = $money;
  148. $this['moneyAcc'] = round($this['moneyAcc'] + $moneyAcc, 4);
  149. $this['todayTime'] = $todayTime;
  150. $this->save();
  151. //$this->userAccount['moneyAcc'] =
  152. }
  153. else {
  154. if($cang) {
  155. $this['moneyAcc'] = $this['moneyAcc'] + round($cang['interest'] / $cang['investDay'], 2);
  156. $this['moneyToday'] = $this['moneyToday'] + round($cang['interest'] / $cang['investDay'], 4);
  157. $this->save();
  158. }
  159. }
  160. return $this;
  161. }
  162. /**
  163. * 充值时,更新money,并生成账务流水,该方法只做充值时余额增加时使用
  164. * @param $recharge 充值对象
  165. * @param $user 用户对象
  166. * @param $reachTime 第三方生成流水时间
  167. */
  168. public function MODE_recharge_addMoney(UserRecharge $recharge, User $user, $reachTime)
  169. {
  170. //1、更新账户资产
  171. $userAccount = self::get([
  172. 'userID' => $user['userID']
  173. ]);
  174. $moneyPre = $userAccount['money'];
  175. $moneyNow = $userAccount['money'] + $recharge['money'];
  176. $userAccount['money'] = $userAccount['money'] + $recharge['money'];
  177. $userAccount->save();
  178. //2、更新用户流水
  179. UserFinance::create([
  180. 'mode' => UserFinance::MODE_RECHARGE,
  181. 'modeID' => $recharge['userRechargeID'],
  182. 'money' => $recharge['money'],
  183. 'userID' => $user['userID'],
  184. 'status' => UserFinance::STATUS_OK,
  185. 'moneyPre' => $moneyPre,
  186. 'moneyNow' => $moneyNow
  187. ]);
  188. //3、更新充值记录
  189. UserRecharge::update([
  190. 'status' => UserRecharge::STATUS_PAY,
  191. 'outerReachTime' => $reachTime
  192. ], [
  193. 'userRechargeID' => $recharge['userRechargeID']
  194. ]);
  195. return $userAccount;
  196. }
  197. /**
  198. * 发起提现到零钱的请求
  199. * @param $userDrawcash
  200. * @param $user - 发起申请人
  201. * @param $submitTime - 提交时间
  202. */
  203. public function MODE_drawcash_decMoney_submit(&$msg, UserDrawcash $userDrawcash, User $user, $submitTime)
  204. {
  205. //1、更新账户资产,余额减少,冻结金额增多
  206. $msg = '操作成功';
  207. $userAccount = self::get([
  208. 'userID' => $user['userID']
  209. ]);
  210. $moneyPre = $userAccount['money'];
  211. $moneyNow = $userAccount['money'] - $userDrawcash['money'];
  212. if ($moneyNow < 0) {
  213. $msg = '余额不足';
  214. return false;
  215. }
  216. $userAccount['money'] = $userAccount['money'] - $userDrawcash['money'];
  217. $userAccount['moneyFrozen'] = $userAccount['moneyFrozen'] + $userDrawcash['money'];
  218. $userAccount->save();
  219. //2、生成一个提现流水,处于冻结状态,什么时候收到异步通知,我们在改变该流水
  220. UserFinance::create([
  221. 'mode' => UserFinance::MODE_DRAWCASH,
  222. 'modeID' => $userDrawcash['userDrawcashID'],
  223. 'money' => 0 - $userDrawcash['money'],
  224. 'userID' => $user['userID'],
  225. 'status' => UserFinance::STATUS_FROZEN,
  226. 'moneyPre' => $moneyPre,
  227. 'moneyNow' => $moneyNow
  228. ]);
  229. //3、更新提现记录
  230. UserDrawcash::update([
  231. 'status' => UserDrawcash::STATUS_ING,
  232. 'submitTime' => $submitTime
  233. ], [
  234. 'userDrawcashID' => $userDrawcash['userDrawcashID']
  235. ]);
  236. return $userAccount;
  237. }
  238. /**
  239. * 持仓时,更新money,并生成账务流水,该方法只做持仓时余额减少时使用
  240. * @param $money 更新金钱
  241. * @param $rechargeID 充值ID
  242. * @param $userID 用户ID
  243. * @param $outer 第三方支付等
  244. */
  245. public function MODE_cang_decMoney($cang, $user, $interest)
  246. {
  247. //1、更新账户资产
  248. $userAccount = self::get([
  249. 'userID' => $user['userID']
  250. ]);
  251. $moneyPre = $userAccount['money'];
  252. $moneyNow = $userAccount['money'] - $cang['money'];
  253. $userAccount['waitBen'] = $userAccount['waitBen'] + $cang['ben'];
  254. $userAccount['waitInterest'] = $userAccount['waitInterest'] + $interest;
  255. $userAccount['hasInvestBenTotal'] = $userAccount['hasInvestBenTotal'] + $cang['ben'];
  256. $userAccount['money'] = $moneyNow;
  257. $userAccount->save();
  258. //2、更新用户流水
  259. UserFinance::create([
  260. 'mode' => UserFinance::MODE_CANG,
  261. 'modeID' => $cang['cangID'],
  262. 'money' => 0 - $cang['money'],
  263. 'userID' => $user['userID'],
  264. 'status' => UserFinance::STATUS_OK,
  265. 'moneyPre' => $moneyPre,
  266. 'moneyNow' => $moneyNow
  267. ]);
  268. return $userAccount;
  269. }
  270. /**
  271. * 返息,更新money,并生成账务流水,该方法只做持返息余额增加时使用
  272. * @param $money 更新金钱
  273. * @param $cangID 持仓ID
  274. * @param $userID 用户ID
  275. */
  276. public function MODE_repayInterest_addMoney($cangRepay, $user)
  277. {
  278. //1、更新账户资产
  279. $userAccount = self::get([
  280. 'userID' => $user['userID']
  281. ]);
  282. $moneyPre = $userAccount['money'];
  283. $moneyNow = $userAccount['money'] + $cangRepay['money'];
  284. $userAccount['money'] = $moneyNow;
  285. $userAccount['waitInterest'] = $userAccount['waitInterest'] - $cangRepay['money'];
  286. $userAccount['moneyAcc'] = $userAccount['moneyAcc'] + $cangRepay['money'];
  287. $userAccount->save();
  288. //2、更新用户流水
  289. UserFinance::create([
  290. 'mode' => UserFinance::MODE_REPAY_INTEREST,
  291. 'modeID' => $cangRepay['cangRepayID'],
  292. 'money' => $cangRepay['money'],
  293. 'userID' => $user['userID'],
  294. 'status' => UserFinance::STATUS_OK,
  295. 'moneyPre' => $moneyPre,
  296. 'moneyNow' => $moneyNow
  297. ]);
  298. return $userAccount;
  299. }
  300. /**
  301. * 返本,更新money,并生成账务流水,该方法只做返本时余额增加时使用
  302. * @param $money 更新金钱
  303. * @param $cangID 持仓ID
  304. * @param $userID 用户ID
  305. */
  306. public function MODE_repayBen_addMoney($cangRepay, $user)
  307. {
  308. //1、更新账户资产
  309. $userAccount = self::get([
  310. 'userID' => $user['userID']
  311. ]);
  312. $moneyPre = $userAccount['money'];
  313. $moneyNow = $userAccount['money'] + $cangRepay['money'];
  314. $userAccount['money'] = $moneyNow;
  315. $userAccount['waitBen'] = $userAccount['waitBen'] - $cangRepay['money'];
  316. $userAccount->save();
  317. //2、更新用户流水
  318. UserFinance::create([
  319. 'mode' => UserFinance::MODE_REPAY_BEN,
  320. 'modeID' => $cangRepay['cangRepayID'],
  321. 'money' => $cangRepay['money'],
  322. 'userID' => $user['userID'],
  323. 'status' => UserFinance::STATUS_OK,
  324. 'moneyPre' => $moneyPre,
  325. 'moneyNow' => $moneyNow
  326. ]);
  327. return $userAccount;
  328. }
  329. /**
  330. * 返本,送现金
  331. * @param $cash 现金
  332. * @param $user 用户
  333. * @param $userID 用户ID
  334. */
  335. public function MODE_cashsend_addMoney(Cash $cash, User $user) {
  336. //1、更新账户资产
  337. $userAccount = self::get([
  338. 'userID' => $user['userID']
  339. ]);
  340. $moneyPre = $userAccount['money'];
  341. $moneyNow = $userAccount['money'] + $cash['money'];
  342. $userAccount['money'] = $userAccount['money'] + $cash['money'];
  343. $userAccount->save();
  344. //2、更新用户流水
  345. UserFinance::create([
  346. 'mode' => UserFinance::MODE_CASHSEND,
  347. 'modeID' => $cash['cashID'],
  348. 'money' => $cash['money'],
  349. 'userID' => $user['userID'],
  350. 'status' => UserFinance::STATUS_OK,
  351. 'moneyPre' => $moneyPre,
  352. 'moneyNow' => $moneyNow
  353. ]);
  354. return $userAccount;
  355. }
  356. }