Userpledge.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. dump($order_no);
  76. $orderData = $this->product_order_model
  77. ->where('order_no', 'in', $order_no)
  78. ->field('order_no, status') // 指定字段
  79. ->select();
  80. $status_txt=['已下单','支付','转让','提货','取消','关闭','存储中'];
  81. // 合并 status 到主数据
  82. foreach ($details as &$item_outside) {
  83. foreach ($orderData as &$item_inside) {
  84. if($item_outside['order_no']==$item_inside['order_no']){
  85. $status=$item_inside['status'];
  86. $item_outside['status']=$status;
  87. $item_outside['status_txt']=$status_txt[$status];
  88. }
  89. }
  90. }
  91. dump($details);
  92. $userpledge_status=$row['status'];
  93. $this->view->assign("userpledge_status", $userpledge_status);
  94. $this->view->assign("userpledge_id", $ids);
  95. $this->view->assign("row", $details);
  96. return $this->view->fetch();
  97. }
  98. /**
  99. * 取消存储
  100. */
  101. public function cancelStorage(){
  102. $id = $this->request->param('id');
  103. $userpledgeId = $this->request->param('userpledgeId');
  104. $row = $this->model->get(['id' => $userpledgeId]);
  105. $userpledge_status=$row['status'];
  106. if($userpledge_status==0){
  107. $resp=$this->product_order_model->where('id', $id)->update(['status' => 0]);
  108. if($resp){
  109. $this->success(__('取消成功'), '');
  110. }
  111. }
  112. $this->error(__('操作失败'));
  113. }
  114. /**
  115. * 编辑
  116. *
  117. * @param $ids
  118. * @return string
  119. * @throws DbException
  120. * @throws \think\Exception
  121. */
  122. public function cancel($ids = null)
  123. {
  124. $row = $this->model->get($ids);
  125. if (!$row) {
  126. $this->error(__('No Results were found'));
  127. }
  128. $adminIds = $this->getDataLimitAdminIds();
  129. if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) {
  130. $this->error(__('You have no permission'));
  131. }
  132. if (false === $this->request->isPost()) {
  133. $this->view->assign('row', $row);
  134. return $this->view->fetch();
  135. }
  136. $result = false;
  137. Db::startTrans();
  138. try {
  139. //是否采用模型验证
  140. if ($this->modelValidate) {
  141. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  142. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  143. $row->validateFailException()->validate($validate);
  144. }
  145. $rows = $this->model::alias('a')
  146. ->join('product_pledge b', 'a.pledge_id = b.id', 'left')
  147. ->where('a.id', $ids)->where('a.status', UserPledgeModel::Ongoing)
  148. ->field('a.*,b.day_num')
  149. ->find();
  150. if (empty($rows)) throw new Exception('暂无质押订单');
  151. $day = 86400;
  152. $total = 0; //当前累计收益
  153. $time = time();
  154. $reta = bcdiv($rows->day_num, $day, 6); //天数
  155. $inter = ($rows->last_time == 0) ? $time - $rows->create_time : $time - $rows->last_time; //最后收取时间
  156. $total = bcmul($reta, $inter, 6) * $rows->num; //累计收益
  157. $rows->total_self = bcadd($total, $rows->total_self, 6);
  158. //修改状态
  159. $detail = json_decode($rows->details, true);
  160. ProductOrder::whereIn('id', array_column($detail, 'id'))->setField('status', ProductOrder::Paid);
  161. Loader::model('LedgerWalletModel')->changeWalletAccount($rows->user_id, Asset::TEAC, $total, LedgerTeacChangeModel::Pledge, 0);
  162. $rows->last_time = $time;
  163. $rows->status = UserPledgeModel::Close;
  164. $result = $rows->save();
  165. Db::commit();
  166. } catch (ValidateException | PDOException | Exception $e) {
  167. Db::rollback();
  168. $this->error($e->getMessage());
  169. }
  170. if (false === $result) {
  171. $this->error(__('No rows were updated'));
  172. }
  173. $this->success();
  174. }
  175. }