Record.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. <?php
  2. namespace app\admin\controller\deposit;
  3. use app\admin\model\Admin;
  4. use app\admin\model\profit\Bill;
  5. use app\admin\model\Store;
  6. use app\api\model\system\Message;
  7. use app\api\model\User;
  8. use app\api\model\user\Wallet;
  9. use app\common\controller\Backend;
  10. use redis\RedLock;
  11. use think\Db;
  12. use think\Exception;
  13. use think\Request;
  14. /**
  15. * 提现记录管理
  16. *
  17. * @icon fa fa-circle-o
  18. */
  19. class Record extends Backend
  20. {
  21. /**
  22. * Record模型对象
  23. * @var \app\admin\model\deposit\Record
  24. */
  25. protected $model = null;
  26. private $mBillModel;
  27. private $mWalletModel;
  28. private $userModel;
  29. private $uBillModel;
  30. private $uWalletModel;
  31. private $pBillModel;
  32. public function __construct(Request $request = null)
  33. {
  34. parent::__construct($request);
  35. $this->mBillModel = new \app\api\model\massager\Bill();
  36. $this->mWalletModel = new \app\api\model\massager\Wallet();
  37. $this->userModel = new User();
  38. $this->pBillModel = new Bill();
  39. $this->uBillModel = new \app\api\model\user\Bill();
  40. $this->uWalletModel = new \app\api\model\user\Wallet();
  41. }
  42. public function _initialize()
  43. {
  44. parent::_initialize();
  45. $this->model = new \app\admin\model\deposit\Record;
  46. $this->view->assign("platformList", $this->model->getPlatformList());
  47. $this->view->assign("identityTypeList", $this->model->getIdentityTypeList());
  48. $this->view->assign("applyStatusList", $this->model->getApplyStatusList());
  49. $this->view->assign("depositStatusList", $this->model->getDepositStatusList());
  50. }
  51. /**
  52. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  53. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  54. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  55. */
  56. /**
  57. * 查看
  58. */
  59. public function index()
  60. {
  61. //当前是否为关联查询
  62. $this->relationSearch = true;
  63. //设置过滤方法
  64. $this->request->filter(['strip_tags', 'trim']);
  65. if ($this->request->isAjax()) {
  66. //如果发送的来源是Selectpage,则转发到Selectpage
  67. if ($this->request->request('keyField')) {
  68. return $this->selectpage();
  69. }
  70. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  71. $list = $this->model
  72. ->with(['store', 'massager', 'admin', 'user'])
  73. ->where($where)
  74. ->order($sort, $order)
  75. ->paginate($limit);
  76. foreach ($list as $row) {
  77. $row->visible(["opening_bank_name", "bank_real_name", "bank_no", 'id', 'no', 'platform', 'identity_type', 'deposit_amount', 'service_charge_rate', 'apply_status', 'deposit_status', 'createtime', 'updatetime']);
  78. $row->visible(['store']);
  79. $row->getRelation('store')->visible(['name']);
  80. $row->visible(['massager']);
  81. $row->getRelation('massager')->visible(['name']);
  82. $row->visible(['admin']);
  83. $row->getRelation('admin')->visible(['nickname']);
  84. $row->visible(['user']);
  85. $row->getRelation('user')->visible(['nickname']);
  86. }
  87. $result = array("total" => $list->total(), "rows" => $list->items());
  88. return json($result);
  89. }
  90. return $this->view->fetch();
  91. }
  92. public function check($id = null, $check = null)
  93. {
  94. $is_pass = $check === "pass";
  95. if (!$id || !$check)
  96. $this->error("参数错误!");
  97. $redLock = new RedLock();
  98. $deposit = $this->model->where([
  99. "id" => $id,
  100. "apply_status" => \E_MASSAGER_STATUS::Default,
  101. ])->find();
  102. if (!$deposit)
  103. $this->error("申请记录不存在!");
  104. if (false === $is_pass) {
  105. switch ($deposit["identity_type"]) {
  106. case \E_IDENTITY_TYPE::Massager:
  107. Message::sendSystemMessage(
  108. \E_IDENTITY_TYPE::Massager,
  109. ["to_massager_id" => $deposit["massager_id"]],
  110. "提现审核",
  111. "您的提现申请已被管理员拒绝!"
  112. );
  113. $this->model->update([
  114. "apply_status" => $check,
  115. "updatetime" => time(),
  116. ], ["id" => $deposit["id"]]);
  117. break;
  118. case \E_IDENTITY_TYPE::Agency:
  119. Message::sendSystemMessage(
  120. \E_IDENTITY_TYPE::Agency,
  121. ["to_agency_id" => $deposit->agency_id],
  122. "提现审核",
  123. "您的提现申请已被管理员拒绝!"
  124. );
  125. $this->model->update([
  126. "apply_status" => $check,
  127. "updatetime" => time(),
  128. ], ["id" => $deposit["id"]]);
  129. break;
  130. case \E_IDENTITY_TYPE::Store:
  131. Message::sendSystemMessage(
  132. \E_IDENTITY_TYPE::Agency,
  133. ["to_user_id" => $deposit->operation_id],
  134. "提现审核",
  135. "您的提现申请已被管理员" . ($is_pass ? "通过!" : "拒绝!")
  136. );
  137. $this->model->update([
  138. "apply_status" => $check,
  139. "updatetime" => time(),
  140. ], ["id" => $deposit["id"]]);
  141. break;
  142. case \E_IDENTITY_TYPE::User:
  143. Message::sendSystemMessage(
  144. \E_IDENTITY_TYPE::Agency,
  145. ["to_user_id" => $deposit->user_id],
  146. "提现审核",
  147. "您的提现申请已被管理员" . ($is_pass ? "通过!" : "拒绝!")
  148. );
  149. $this->model->update([
  150. "apply_status" => $check,
  151. "updatetime" => time(),
  152. ], ["id" => $deposit["id"]]);
  153. break;
  154. }
  155. $this->success("驳回申请");
  156. }
  157. switch ($deposit["identity_type"]) {
  158. case \E_IDENTITY_TYPE::Massager:
  159. $mLock = $redLock->lock(\app\api\model\massager\Wallet::MWKey($deposit->massager_id));
  160. $mWallet = $this->mWalletModel->getWallet($deposit->massager_id);
  161. if ($mWallet->profit_amount < $deposit->deposit_amount)
  162. $this->error("账户剩余{$mWallet->profit_amount}!");
  163. if (!is_array($mLock))
  164. $this->error("请稍后再试");
  165. Db::startTrans();
  166. try {
  167. $this->mBillModel->save([
  168. "massager_id" => $deposit["massager_id"],
  169. "currency_type" => \E_USER_BILL_CURRENCY_TYPE::Money,
  170. "change_type" => \E_M_BILL_CHANGE_TYPE::Deposit,
  171. "change" => -$deposit->deposit_amount,
  172. "before" => $mWallet->profit_amount,
  173. "after" => $mWallet->profit_amount - $deposit->deposit_amount,
  174. "reason" => "提现",
  175. "relation_no" => $deposit->no,
  176. "createtime" => time()
  177. ]);
  178. $this->mWalletModel->where("id", $mWallet->id)->setDec("profit_amount", $deposit->deposit_amount);
  179. Message::sendSystemMessage(
  180. \E_IDENTITY_TYPE::Massager,
  181. ["to_massager_id" => $deposit["massager_id"]],
  182. "提现审核",
  183. "您的提现申请已被管理员" . ($is_pass ? "通过!" : "拒绝!")
  184. );
  185. $this->model->update([
  186. "apply_status" => $check,
  187. "updatetime" => time(),
  188. ], ["id" => $deposit["id"]]);
  189. Db::commit();
  190. } catch (Exception $e) {
  191. Db::rollback();
  192. $this->error($e->getMessage());
  193. } finally {
  194. $redLock->unlock($mLock);
  195. }
  196. break;
  197. case \E_IDENTITY_TYPE::Agency:
  198. $aLock = $redLock->lock(Admin::AgencyKey($deposit->agency_id));
  199. $admin = (new \app\admin\model\Admin())->get($deposit->agency_id);
  200. if (!$admin)
  201. $this->error("账户不存在!");
  202. if ($admin->profit_amount < $deposit->deposit_amount)
  203. $this->error("账户剩余{$admin->profit_amount}!");
  204. if (!is_array($aLock))
  205. $this->error("请稍后再试");
  206. Db::startTrans();
  207. try {
  208. $this->pBillModel->save([
  209. "identity_type" => \E_IDENTITY_TYPE::Agency,
  210. "target_id" => $admin->id,
  211. "target_name" => $admin->nickname,
  212. "change_type" => \E_PROFIT_BILL_CHANGE_TYPE::Drawings,
  213. "order_no" => $deposit->no,
  214. "total_amount" => 0,
  215. "rate" => 0,
  216. "change" => -$deposit->deposit_amount,
  217. "before" => $admin->profit_amount,
  218. "after" => fixed2Float($admin->profit_amount - $deposit->deposit_amount),
  219. "createtime" => time(),
  220. "city_code" => 0
  221. ]);
  222. (new Admin())->where("id", 1)->setDec("profit_amount", $deposit->deposit_amount);
  223. Message::sendSystemMessage(
  224. \E_IDENTITY_TYPE::Agency,
  225. ["to_agency_id" => $deposit->agency_id],
  226. "提现审核",
  227. "您的提现申请已被管理员" . ($is_pass ? "通过!" : "拒绝!")
  228. );
  229. $this->model->update([
  230. "apply_status" => $check,
  231. "updatetime" => time(),
  232. ], ["id" => $deposit["id"]]);
  233. Db::commit();
  234. } catch (Exception $e) {
  235. Db::rollback();
  236. $this->error($e->getMessage());
  237. } finally {
  238. $redLock->unlock($aLock);
  239. }
  240. break;
  241. case \E_IDENTITY_TYPE::Store:
  242. $sLock = $redLock->lock(\app\api\model\Store::SWKey($deposit->store_id));
  243. $store = (new Store())->get($deposit->store_id);
  244. if (!$store)
  245. $this->error("账户不存在!");
  246. if ($store->profit_amount < $deposit->deposit_amount)
  247. $this->error("账户剩余{$store->profit_amount}!");
  248. if (!is_array($sLock))
  249. $this->error("请稍后再试");
  250. Db::startTrans();
  251. try {
  252. $this->pBillModel->save([
  253. "identity_type" => \E_IDENTITY_TYPE::Store,
  254. "target_id" => $store->id,
  255. "target_name" => $store->name,
  256. "change_type" => \E_PROFIT_BILL_CHANGE_TYPE::Drawings,
  257. "order_no" => $deposit->no,
  258. "total_amount" => 0,
  259. "rate" => 0,
  260. "change" => -$deposit->deposit_amount,
  261. "before" => $store->profit_amount,
  262. "after" => $store->profit_amount - $deposit->deposit_amount,
  263. "createtime" => time(),
  264. "city_code" => $store->city_code
  265. ]);
  266. (new Store())->where("id", $store->id)->setDec("profit_amount", $deposit->deposit_amount);
  267. $this->model->update([
  268. "apply_status" => $check,
  269. "updatetime" => time(),
  270. ], ["id" => $deposit["id"]]);
  271. Db::commit();
  272. } catch (Exception $e) {
  273. Db::rollback();
  274. $this->error($e->getMessage());
  275. } finally {
  276. $redLock->unlock($sLock);
  277. }
  278. break;
  279. case \E_IDENTITY_TYPE::User:
  280. $uLock = $redLock->lock(Wallet::UWKey($deposit->user_id));
  281. $uWallet = (new Wallet())->get($deposit->user_id);
  282. if (!$uWallet)
  283. $this->error("账户不存在!");
  284. if ($uWallet->money < $deposit->deposit_amount)
  285. $this->error("账户剩余{$uWallet->money}!");
  286. if (!is_array($uLock))
  287. $this->error("请稍后再试");
  288. Db::startTrans();
  289. try {
  290. $this->uBillModel->save([
  291. "user_id" => $deposit["user_id"],
  292. "currency_type" => \E_USER_BILL_CURRENCY_TYPE::Money,
  293. "change_type" => \E_USER_BILL_CHANGE_TYPE::Deposit[0],
  294. "change" => -$deposit->deposit_amount,
  295. "before" => $uWallet->money,
  296. "after" => fixed2Float($uWallet->money - $deposit->deposit_amount),
  297. "reason" => \E_USER_BILL_CHANGE_TYPE::Deposit[1],
  298. "money" => 0,
  299. "give_money" => 0,
  300. "relation_no" => $deposit->no,
  301. "createtime" => time()
  302. ]);
  303. $this->uWalletModel->where("id", $uWallet->id)->setDec("money", $deposit->deposit_amount);
  304. Message::sendSystemMessage(
  305. \E_IDENTITY_TYPE::Agency,
  306. ["to_user_id" => $deposit->user_id],
  307. "提现审核",
  308. "您的提现申请已被管理员" . ($is_pass ? "通过!" : "拒绝!")
  309. );
  310. $this->model->update([
  311. "apply_status" => $check,
  312. "updatetime" => time(),
  313. ], ["id" => $deposit["id"]]);
  314. Db::commit();
  315. } catch (Exception $e) {
  316. Db::rollback();
  317. $this->error($e->getMessage());
  318. } finally {
  319. $redLock->unlock($uLock);
  320. }
  321. break;
  322. }
  323. $this->success("同意提现");
  324. }
  325. }