Userpledge.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. namespace app\admin\controller\user;
  3. use app\common\controller\Backend;
  4. use app\common\model\ProductOrder;
  5. use think\exception\DbException;
  6. use app\common\model\UserPledge as UserPledgeModel;
  7. use Exception;
  8. use think\Db;
  9. use fast\Asset;
  10. use think\Loader;
  11. use app\common\model\LedgerTeacChangeModel;
  12. use think\exception\PDOException;
  13. use think\exception\ValidateException;
  14. use think\Request;
  15. /**
  16. * 用户存储列表 TeamRewards
  17. *
  18. * @icon fa fa-circle-o
  19. */
  20. class Userpledge extends Backend
  21. {
  22. /**
  23. * TeamRewards模型对象
  24. * @var \app\admin\model\TeamRewards
  25. */
  26. protected $model = null;
  27. protected $product_order_model = null;
  28. public function _initialize()
  29. {
  30. parent::_initialize();
  31. $this->model = new \app\common\model\UserPledge;
  32. $this->product_order_model = new \app\common\model\ProductOrder;
  33. }
  34. /**
  35. * 查看
  36. *
  37. * @return string|Json
  38. * @throws \think\Exception
  39. * @throws DbException
  40. */
  41. public function index()
  42. {
  43. //设置过滤方法
  44. $this->request->filter(['strip_tags', 'trim']);
  45. if (false === $this->request->isAjax()) {
  46. return $this->view->fetch();
  47. }
  48. //如果发送的来源是 Selectpage,则转发到 Selectpage
  49. if ($this->request->request('keyField')) {
  50. return $this->selectpage();
  51. }
  52. [$where, $sort, $order, $offset, $limit] = $this->buildparams();
  53. $list = $this->model->with('users,pledges')
  54. ->where($where)
  55. ->order($sort, $order)
  56. ->paginate($limit);
  57. $result = ['total' => $list->total(), 'rows' => $list->items()];
  58. return json($result);
  59. }
  60. /**
  61. * 详情
  62. */
  63. public function detail($ids)
  64. {
  65. $row = $this->model->get(['id' => $ids]);
  66. if (!$row) {
  67. $this->error(__('No Results were found'));
  68. }
  69. if ($this->request->isAjax()) {
  70. $this->success("Ajax请求成功", null, ['id' => $ids]);
  71. }
  72. $details = json_decode($row->details, true);
  73. // 提取所有id,返回一个包含id的一维数组
  74. $order_no = array_column($details, 'order_no');
  75. $orderData = $this->product_order_model
  76. ->where('order_no', 'in', $order_no)
  77. ->field('order_no, status') // 指定字段
  78. ->select();
  79. $status_txt=['已下单','支付','转让','提货','取消','关闭','存储中'];
  80. // 3. 合并 status 到主数据
  81. foreach ($details as &$item_outside) {
  82. foreach ($orderData as &$item_inside) {
  83. if($item_outside['order_no']==$item_inside['order_no']){
  84. $status=$item_inside['status'];
  85. $item_outside['status']=$status;
  86. $item_outside['status_txt']=$status_txt[$status];
  87. }
  88. }
  89. }
  90. $userpledge_status=$row['status'];
  91. $this->view->assign("userpledge_status", $userpledge_status);
  92. $this->view->assign("row", $details);
  93. return $this->view->fetch();
  94. }
  95. /**
  96. * 取消存储
  97. */
  98. public function cancelStorage(){
  99. $id = $this->request->param('id');
  100. $resp=$this->product_order_model->where('id', $id)
  101. ->update(['status' => 5]);
  102. return $resp;
  103. // $this->success('取消存储', $resp);
  104. }
  105. /**
  106. * 编辑
  107. *
  108. * @param $ids
  109. * @return string
  110. * @throws DbException
  111. * @throws \think\Exception
  112. */
  113. public function cancel($ids = null)
  114. {
  115. $row = $this->model->get($ids);
  116. if (!$row) {
  117. $this->error(__('No Results were found'));
  118. }
  119. $adminIds = $this->getDataLimitAdminIds();
  120. if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) {
  121. $this->error(__('You have no permission'));
  122. }
  123. if (false === $this->request->isPost()) {
  124. $this->view->assign('row', $row);
  125. return $this->view->fetch();
  126. }
  127. $result = false;
  128. Db::startTrans();
  129. try {
  130. //是否采用模型验证
  131. if ($this->modelValidate) {
  132. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  133. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  134. $row->validateFailException()->validate($validate);
  135. }
  136. $rows = $this->model::alias('a')
  137. ->join('product_pledge b', 'a.pledge_id = b.id', 'left')
  138. ->where('a.id', $ids)->where('a.status', UserPledgeModel::Ongoing)
  139. ->field('a.*,b.day_num')
  140. ->find();
  141. if (empty($rows)) throw new Exception('暂无质押订单');
  142. $day = 86400;
  143. $total = 0; //当前累计收益
  144. $time = time();
  145. $reta = bcdiv($rows->day_num, $day, 6); //天数
  146. $inter = ($rows->last_time == 0) ? $time - $rows->create_time : $time - $rows->last_time; //最后收取时间
  147. $total = bcmul($reta, $inter, 6) * $rows->num; //累计收益
  148. $rows->total_self = bcadd($total, $rows->total_self, 6);
  149. //修改状态
  150. $detail = json_decode($rows->details, true);
  151. ProductOrder::whereIn('id', array_column($detail, 'id'))->setField('status', ProductOrder::Paid);
  152. Loader::model('LedgerWalletModel')->changeWalletAccount($rows->user_id, Asset::TEAC, $total, LedgerTeacChangeModel::Pledge, 0);
  153. $rows->last_time = $time;
  154. $rows->status = UserPledgeModel::Close;
  155. $result = $rows->save();
  156. Db::commit();
  157. } catch (ValidateException | PDOException | Exception $e) {
  158. Db::rollback();
  159. $this->error($e->getMessage());
  160. }
  161. if (false === $result) {
  162. $this->error(__('No rows were updated'));
  163. }
  164. $this->success();
  165. }
  166. }