UserExchangeServices.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\services\user;
  13. use app\dao\user\UserExchangeDao;
  14. use app\model\user\User;
  15. use app\model\user\UserExchange;
  16. use app\model\user\UserExtract;
  17. use app\services\BaseServices;
  18. use app\dao\user\UserExtractDao;
  19. use app\services\wechat\WechatUserServices;
  20. use crmeb\exceptions\AdminException;
  21. use crmeb\services\FormBuilder as Form;
  22. use crmeb\traits\ServicesTrait;
  23. use FormBuilder\Factory\Iview;
  24. use think\exception\ValidateException;
  25. use think\facade\Route as Url;
  26. /**
  27. *
  28. * Class UserExtractServices
  29. * @package app\services\user
  30. * @mixin UserExtractDao
  31. */
  32. class UserExchangeServices extends BaseServices
  33. {
  34. use ServicesTrait;
  35. /**
  36. * UserExtractServices constructor.
  37. * @param UserExchangeDao $dao
  38. */
  39. public function __construct(UserExchangeDao $dao)
  40. {
  41. $this->dao = $dao;
  42. }
  43. /**
  44. * 获取一条提现记录
  45. * @param int $id
  46. * @param array $field
  47. * @return array|\think\Model|null
  48. */
  49. public function getExtract(int $id, array $field = [])
  50. {
  51. return $this->dao->get($id, $field);
  52. }
  53. /**
  54. * 获取某个用户提现总数
  55. * @param int $uid
  56. * @return float
  57. */
  58. public function getUserExtract(int $uid)
  59. {
  60. return $this->dao->getWhereSum(['uid' => $uid, 'status' => [0, 1]]);
  61. }
  62. /**
  63. * 获取某些用户的提现总数列表
  64. * @param array $uids
  65. */
  66. public function getUsersSumList(array $uids)
  67. {
  68. return $this->dao->getWhereSumList(['uid' => $uids, 'status' => [0, 1]]);
  69. }
  70. public function getCount(array $where = [])
  71. {
  72. return $this->dao->getCount($where);
  73. }
  74. /**
  75. * 获取提现列表
  76. * @param array $where
  77. * @param string $field
  78. * @return array
  79. * @throws \think\db\exception\DataNotFoundException
  80. * @throws \think\db\exception\DbException
  81. * @throws \think\db\exception\ModelNotFoundException
  82. */
  83. public function getUserExtractList(array $where, string $field = '*')
  84. {
  85. [$page, $limit] = $this->getPageValue();
  86. $list = $this->dao->getExtractList($where, $field, $page, $limit);
  87. foreach ($list as &$item) {
  88. $item['nickname'] = $item['user']['nickname'] ?? '';
  89. }
  90. $count = $this->dao->count($where);
  91. return compact('list', 'count');
  92. }
  93. /**
  94. * 获取提现总数
  95. * @param array $where
  96. */
  97. public function getExtractSum(array $where)
  98. {
  99. return $this->dao->getExtractMoneyByWhere($where, 'extract_num');
  100. }
  101. /**
  102. * 拒绝提现申请
  103. * @param $id
  104. * @param $fail_msg
  105. * @return bool
  106. * @throws \think\db\exception\DataNotFoundException
  107. * @throws \think\db\exception\ModelNotFoundException
  108. * @throws \think\exception\DbException
  109. */
  110. public function changeFail(int $id, $userExtract, $message)
  111. {
  112. $fail_time = time();
  113. $extract_number = bcadd((string)$userExtract['extract_num'], (string)$userExtract['extract_fee'], 2);
  114. $mark = '换股份失败,退回能量值' . $extract_number;
  115. $uid = $userExtract['uid'];
  116. $status = -1;
  117. /** @var UserServices $userServices */
  118. $userServices = app()->make(UserServices::class);
  119. $user = $userServices->getUserInfo($uid);
  120. if (!$user) {
  121. throw new ValidateException('用户不存在');
  122. }
  123. /** @var UserBillServices $userBrokerageServices */
  124. $userBrokerageServices = app()->make(UserBillServices::class);
  125. $this->transaction(function () use ($user, $userBrokerageServices, $uid, $id, $extract_number, $message, $userServices, $status, $fail_time) {
  126. $now_brokerage = bcadd((string)$user['energy'], (string)$extract_number, 2);
  127. //增加佣金记录
  128. $userBrokerageServices->income('exchange_fail', $uid, $extract_number, $now_brokerage, $id);
  129. //修改用户佣金
  130. if (!$userServices->update($uid, ['energy' => $now_brokerage], 'uid'))
  131. throw new AdminException('增加用户能量失败');
  132. if (!$this->dao->update($id, ['fail_time' => $fail_time, 'fail_msg' => $message, 'status' => $status])) {
  133. throw new AdminException('修改失败');
  134. }
  135. });
  136. //消息推送
  137. return true;
  138. }
  139. /**
  140. * 通过提现申请
  141. * @param $id
  142. * @return bool
  143. * @throws \think\db\exception\DataNotFoundException
  144. * @throws \think\db\exception\ModelNotFoundException
  145. * @throws \think\exception\DbException
  146. */
  147. public function changeSuccess(int $id, $userExtract)
  148. {
  149. $this->transaction(function () use ($id, $userExtract) {
  150. if (!$this->dao->update($id, ['status' => 1])) {
  151. throw new AdminException('修改失败');
  152. }
  153. });
  154. return true;
  155. }
  156. /**
  157. * 显示资源列表
  158. * @param array $where
  159. * @return array
  160. * @throws \think\db\exception\DataNotFoundException
  161. * @throws \think\db\exception\DbException
  162. * @throws \think\db\exception\ModelNotFoundException
  163. */
  164. public function index(array $where)
  165. {
  166. $list = $this->getUserExtractList($where);
  167. //待提现金额
  168. $where['status'] = 0;
  169. $extract_statistics['price'] = $this->getExtractSum($where);
  170. //已提现金额
  171. $where['status'] = 1;
  172. $extract_statistics['priced'] = $this->getExtractSum($where);
  173. //佣金总金额
  174. return compact('extract_statistics', 'list');
  175. }
  176. /**
  177. * 显示资源列表
  178. * @param array $where
  179. * @return array
  180. * @throws \think\db\exception\DataNotFoundException
  181. * @throws \think\db\exception\DbException
  182. * @throws \think\db\exception\ModelNotFoundException
  183. */
  184. public function getExportList(array $where)
  185. {
  186. $list = $this->dao->getExtractAll($where);
  187. return $list;
  188. }
  189. /**
  190. * 显示编辑资源表单页.
  191. *
  192. * @param int $id
  193. * @return \think\Response
  194. */
  195. public function edit(int $id)
  196. {
  197. $UserExtract = $this->getExtract($id);
  198. if (!$UserExtract) {
  199. throw new AdminException('数据不存在!');
  200. }
  201. $f = array();
  202. $f[] = Form::input('real_name', '姓名', $UserExtract['real_name']);
  203. $f[] = Form::number('extract_num', '转换能量', (float)$UserExtract['extract_num'])->precision(2)->disabled(true);
  204. $f[] = Form::number('extract_price', '股份价格', (float)$UserExtract['extract_price'])->precision(2)->disabled(true);
  205. $f[] = Form::number('exchange_num', '转换股份', (float)$UserExtract['exchange_num'])->precision(2)->disabled(true);
  206. $f[] = Form::input('bank_code', '银行卡号', $UserExtract['bank_code']);
  207. $f[] = Form::input('bank_address', '开户行', $UserExtract['bank_address']);
  208. $f[] = Form::input('mark', '备注', $UserExtract['mark'])->type('textarea');
  209. return create_form('编辑', $f, Url::buildUrl('/finance/exchange/' . $id), 'PUT');
  210. }
  211. public function update(int $id, array $data)
  212. {
  213. if (!$this->dao->update($id, $data))
  214. throw new AdminException('修改失败');
  215. else
  216. return true;
  217. }
  218. /**
  219. * 拒绝
  220. * @param $id
  221. * @return mixed
  222. */
  223. public function refuse(int $id, string $message)
  224. {
  225. $extract = $this->getExtract($id);
  226. if (!$extract) {
  227. throw new AdminException('操作记录不存在!');
  228. }
  229. if ($extract->status == 1) {
  230. throw new AdminException('已经提现,错误操作');
  231. }
  232. if ($extract->status == -1) {
  233. throw new AdminException('您的提现申请已被拒绝,请勿重复操作!');
  234. }
  235. $res = $this->changeFail($id, $extract, $message);
  236. if ($res) {
  237. return true;
  238. } else {
  239. throw new AdminException('操作失败!');
  240. }
  241. }
  242. /**
  243. * 通过
  244. * @param $id
  245. * @return mixed
  246. */
  247. public function adopt(int $id)
  248. {
  249. $extract = $this->getExtract($id);
  250. if (!$extract) {
  251. throw new AdminException('操作记录不存!');
  252. }
  253. if ($extract->status == 1 || $extract->status == 2) {
  254. throw new AdminException('您已提现,请勿重复提现!');
  255. }
  256. if ($extract->status == -1) {
  257. throw new AdminException('您的提现申请已被拒绝!');
  258. }
  259. if ($this->changeSuccess($id, $extract)) {
  260. return true;
  261. } else {
  262. throw new AdminException('操作失败!');
  263. }
  264. }
  265. /**待提现的数量
  266. * @return int
  267. */
  268. public function userExtractCount()
  269. {
  270. return $this->dao->count(['status' => 0]);
  271. }
  272. /**
  273. * 银行卡提现
  274. * @param int $uid
  275. * @return mixed
  276. */
  277. public function bank(int $uid)
  278. {
  279. /** @var UserServices $userService */
  280. $userService = app()->make(UserServices::class);
  281. $user = $userService->getUserInfo($uid);
  282. if (!$user) {
  283. throw new ValidateException('数据不存在');
  284. }
  285. $data['energy'] = $user['energy'];
  286. //可提现佣金
  287. $data['minEnergy'] = sys_config('user_exchange_min_energy');//提现最低金额
  288. $data['exchange_fee'] = sys_config('exchange_fee');//提现手续费
  289. $data['stock_price'] = sys_config('stock_price');//提现手续费
  290. return $data;
  291. }
  292. /**
  293. * 提现申请
  294. * @param int $uid
  295. * @param array $data
  296. */
  297. public function cash(int $uid, array $data)
  298. {
  299. /** @var UserServices $userService */
  300. $userService = app()->make(UserServices::class);
  301. $user = $userService->getUserInfo($uid);
  302. if (!$user) {
  303. throw new ValidateException('数据不存在');
  304. }
  305. if ($user['is_auth'] != 2) throw new ValidateException('请先完成实名认证');
  306. if ($data['name'] != $user['real_name']) throw new ValidateException('转换用户与实名认证不符');
  307. $data['energy'] = $user['energy'];
  308. if ($data['money'] > $data['energy']) {
  309. throw new ValidateException('可转换能量值不足');
  310. }
  311. $extractPrice = $user['energy'];
  312. $userExtractMinPrice = sys_config('user_exchange_min_energy');
  313. if ($data['money'] < $userExtractMinPrice) {
  314. throw new ValidateException('转换能量值不能小于' . $userExtractMinPrice);
  315. }
  316. if ($extractPrice < 0) {
  317. throw new ValidateException('转换能量值不足' . $data['money']);
  318. }
  319. if ($data['money'] > $extractPrice) {
  320. throw new ValidateException('转换能量值不足' . $data['money']);
  321. }
  322. if ($data['money'] <= 0) {
  323. throw new ValidateException('转换能量值大于0');
  324. }
  325. $fee = sys_config('exchange_fee');
  326. $stock_price = sys_config('stock_price');
  327. if ($stock_price <= 0) throw new ValidateException('暂不支持转换股份');
  328. $extract_fee = bcdiv(bcmul($fee, $data['money'], 2), '100', 2);
  329. if ($extract_fee < 0) $extract_fee = 0;
  330. $insertData = [
  331. 'uid' => $user['uid'],
  332. 'extract_num' => bcsub((string)$data['money'], (string)$extract_fee, 2),
  333. 'extract_fee' => $extract_fee,
  334. 'extract_price' => $stock_price,
  335. 'exchange_num' => bcdiv(bcsub((string)$data['money'], (string)$extract_fee, 2), (string)$stock_price, 2),
  336. 'add_time' => time(),
  337. 'balance' => $user['energy'],
  338. 'status' => 0,
  339. ];
  340. $insertData['real_name'] = $data['name'];
  341. $insertData['bank_code'] = $data['cardnum'];
  342. $insertData['bank_address'] = $data['bankname'];
  343. $mark = '转换' . $data['money'] . '能量值,扣除手续费后实际转换' . $insertData['extract_num'] . ',转换时股份价格' . $stock_price . ',共转换' . $insertData['exchange_num'] . '股';
  344. /** @var UserBillServices $userBrokerageServices */
  345. $userBrokerageServices = app()->make(UserBillServices::class);
  346. $res1 = $this->transaction(function () use ($insertData, $data, $uid, $userService, $user, $userBrokerageServices, $mark) {
  347. if (!$res1 = $this->dao->save($insertData)) {
  348. throw new ValidateException('转换失败');
  349. }
  350. //修改用户佣金
  351. $balance = bcsub((string)$user['energy'], (string)$data['money'], 2) ?? 0;
  352. if (!$userService->update($uid, ['energy' => $balance], 'uid')) {
  353. throw new ValidateException('修改用户信息失败');
  354. }
  355. //保存佣金记录
  356. $userBrokerageServices->income('exchange', $uid, ['mark' => $mark, 'number' => $data['money']], $balance, $res1['id']);
  357. return $res1;
  358. });
  359. return true;
  360. }
  361. /**
  362. * @param array $where
  363. * @param string $SumField
  364. * @param string $selectType
  365. * @param string $group
  366. * @return float|mixed
  367. */
  368. public function getOutMoneyByWhere(array $where, string $SumField, string $selectType, string $group = "")
  369. {
  370. switch ($selectType) {
  371. case "sum" :
  372. return $this->dao->getWhereSumField($where, $SumField);
  373. case "group" :
  374. return $this->dao->getGroupField($where, $SumField, $group);
  375. }
  376. }
  377. }