OfflineRecharge.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <?php
  2. namespace app\admin\controller\offline;
  3. use app\common\controller\Backend;
  4. use app\common\model\LedgerWalletModel;
  5. use app\common\model\OfflineRechargeRecordModel;
  6. use app\common\model\ServersModel;
  7. use app\common\model\UserModel;
  8. use fast\Action;
  9. use fast\Asset;
  10. use fast\RechargeStatus;
  11. use fast\RechargeType;
  12. use fast\WithdrawStatus;
  13. use think\exception\DbException;
  14. use think\response\Json;
  15. use think\Db;
  16. /**
  17. * 充值记录管理
  18. *
  19. * @icon fa fa-circle-o
  20. */
  21. class OfflineRecharge extends Backend
  22. {
  23. /**
  24. * OfflineRechargeRecord模型对象
  25. * @var \app\admin\model\OfflineRechargeRecord
  26. */
  27. protected $model = null;
  28. public function _initialize()
  29. {
  30. parent::_initialize();
  31. $this->model = new \app\admin\model\OfflineRechargeRecord;
  32. }
  33. /**
  34. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  35. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  36. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  37. */
  38. /**
  39. * 查看
  40. *
  41. * @return string|Json
  42. * @throws \think\Exception
  43. * @throws DbException
  44. */
  45. public function index()
  46. {
  47. //设置过滤方法
  48. $this->request->filter(['strip_tags', 'trim']);
  49. if (false === $this->request->isAjax()) {
  50. return $this->view->fetch();
  51. }
  52. //如果发送的来源是 Selectpage,则转发到 Selectpage
  53. if ($this->request->request('keyField')) {
  54. return $this->selectpage();
  55. }
  56. [$where, $sort, $order, $offset, $limit] = $this->buildparams();
  57. $list = $this->model->alias('r')
  58. ->where($where)
  59. ->order($sort, $order)
  60. ->paginate($limit);
  61. foreach ($list as $k => $v) {
  62. $list[$k]['user_address'] = (new UserModel())->getById($v['user_id'])['address'];
  63. }
  64. $result = ['total' => $list->total(), 'rows' => $list->items()];
  65. return json($result);
  66. }
  67. /**
  68. * 发货
  69. * @param $ids
  70. * @return string|void
  71. * @throws \think\Exception
  72. * @throws \think\exception\DbException
  73. */
  74. public function delivery($ids = null)
  75. {
  76. $row = $this->model->get($ids);
  77. if (!$row) {
  78. $this->error(__('No Results were found'));
  79. }
  80. $adminIds = $this->getDataLimitAdminIds();
  81. if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) {
  82. $this->error(__('You have no permission'));
  83. }
  84. $user = (new UserModel())->getById($row['user_id']);
  85. if(empty($user)){
  86. $this->error("用户信息不存在");
  87. }
  88. if (false === $this->request->isPost()) {
  89. $this->view->assign('row', $row);
  90. return $this->view->fetch();
  91. }
  92. $params = $this->request->post('row/a');
  93. if (empty($params)) {
  94. $this->error(__('Parameter %s can not be empty', ''));
  95. }
  96. $params = $this->preExcludeFields($params);
  97. $result = false;
  98. Db::startTrans();
  99. try {
  100. //是否采用模型验证
  101. if ($this->modelValidate) {
  102. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  103. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  104. $row->validateFailException()->validate($validate);
  105. }
  106. $update = [
  107. 'express_status' => 1,
  108. 'express_name' => $params['express_name'],
  109. 'express_num' => $params['express_num'],
  110. 'update_time' => time(),
  111. ];
  112. // 仅处理"待处理"状态的订单,"已抢单"的暂不处理
  113. $result = (new OfflineRechargeRecordModel())->where('id', $row['id'])->update($update);
  114. if (empty($result)) {
  115. throw new Exception("订单发货失败");
  116. }
  117. Db::commit();
  118. } catch (\Exception $e) {
  119. Db::rollback();
  120. $this->error($e->getMessage());
  121. }
  122. if (false === $result) {
  123. $this->error(__('No rows were updated'));
  124. }
  125. $this->success();
  126. }
  127. /**
  128. * 手动报单
  129. *
  130. * @param $ids
  131. * @return string
  132. * @throws DbException
  133. * @throws \think\Exception
  134. */
  135. public function add()
  136. {
  137. if (false === $this->request->isPost()) {
  138. return $this->view->fetch();
  139. }
  140. $params = $this->request->post('row/a');
  141. if (empty($params)) {
  142. $this->error(__('Parameter %s can not be empty', ''));
  143. }
  144. $params = $this->preExcludeFields($params);
  145. $user_info = UserModel::where('address', $params['address'])->find();
  146. if(empty($user_info)){
  147. $this->error('用户钱包地址不存在');
  148. }
  149. $insert = [];
  150. if(empty($params['date_time'])){
  151. $this->error('报单日期不能为空');
  152. }
  153. $insert['user_id'] = $user_info['id'];
  154. $insert['create_time'] = time();
  155. $insert['update_time'] = strtotime($params['date_time']);
  156. $insert['order_type'] = 2;//服务器
  157. $insert['order_no'] = 'S' . $user_info['id'] . time();
  158. $insert['server_no'] = $params['server_no'];
  159. $insert['pro_name'] = $params['pro_name'];
  160. $insert['amount'] = $params['amount'];
  161. $insert['power'] = $params['power'];
  162. $insert['status'] = OfflineRechargeRecordModel::StatusSuccess;
  163. $insert['tx_hash'] = '手动报单_' . $user_info['id'] . '_操作员ID:' . $this->auth->id.
  164. // $pro_info = (new ServersModel())
  165. // ->where('price', $params['price'])
  166. // ->find();
  167. // if(empty($pro_info)){
  168. // $this->error(__('没有对应价格的服务器', ''));
  169. // }
  170. $result = false;
  171. Db::startTrans();
  172. try {
  173. //$order_id = (new OfflineRechargeRecordModel())->insertGetId(RechargeType::CashFromSys, '后台手动报单,admin_id:' . $this->auth->id . ',时间戳:' . time(), $user_info['id'], $params['price'], RechargeStatus::StatusAuthSuccess);
  174. $order_id = (new OfflineRechargeRecordModel())->insertGetId($insert);
  175. // 更新总算力和账变
  176. (new LedgerWalletModel)->changeWalletAccount($user_info['id'], Asset::POWER, $insert['power'], Action::PowerRentalPower, $order_id);
  177. //(new LedgerWalletModel)->changeWalletAccount($user_info['id'], Asset::SERVER_POWER, $pro_info['power'], Action::PowerRentalPower, $order_id);
  178. // 更新服务器算力,不账变
  179. //(new LedgerWalletModel)->changeWalletOnly($user_info['id'], Asset::SERVER_POWER, $insert['power']);
  180. // 更新自己激活时间
  181. if($user_info['effective_time'] < 1){
  182. (new UserModel())
  183. ->where('id', $user_info['id'])
  184. ->update(['effective_time' => $insert['update_time']]);
  185. }
  186. // 发放服务器市场推荐相关收益
  187. //(new LedgerWalletModel)->sendServersBonus($user_info['id'], $servers_info);
  188. Db::commit();
  189. } catch (ValidateException|PDOException|Exception $e) {
  190. Db::rollback();
  191. $this->error($e->getMessage());
  192. }
  193. $this->success('手动报单成功');
  194. }
  195. }