Userpledge.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. /**
  15. * 用户存储列表 TeamRewards
  16. *
  17. * @icon fa fa-circle-o
  18. */
  19. class Userpledge extends Backend
  20. {
  21. /**
  22. * TeamRewards模型对象
  23. * @var \app\admin\model\TeamRewards
  24. */
  25. protected $model = null;
  26. public function _initialize()
  27. {
  28. parent::_initialize();
  29. $this->model = new \app\common\model\UserPledge;
  30. }
  31. /**
  32. * 查看
  33. *
  34. * @return string|Json
  35. * @throws \think\Exception
  36. * @throws DbException
  37. */
  38. public function index()
  39. {
  40. //设置过滤方法
  41. $this->request->filter(['strip_tags', 'trim']);
  42. if (false === $this->request->isAjax()) {
  43. return $this->view->fetch();
  44. }
  45. //如果发送的来源是 Selectpage,则转发到 Selectpage
  46. if ($this->request->request('keyField')) {
  47. return $this->selectpage();
  48. }
  49. [$where, $sort, $order, $offset, $limit] = $this->buildparams();
  50. $list = $this->model->with('users,pledges')
  51. ->where($where)
  52. ->order($sort, $order)
  53. ->paginate($limit);
  54. $result = ['total' => $list->total(), 'rows' => $list->items()];
  55. return json($result);
  56. }
  57. /**
  58. * 详情
  59. */
  60. public function detail($ids)
  61. {
  62. $row = $this->model->get(['id' => $ids]);
  63. if (!$row) {
  64. $this->error(__('No Results were found'));
  65. }
  66. if ($this->request->isAjax()) {
  67. $this->success("Ajax请求成功", null, ['id' => $ids]);
  68. }
  69. $details = json_decode($row->details, true);
  70. $this->view->assign("row", $details);
  71. return $this->view->fetch();
  72. }
  73. /**
  74. * 编辑
  75. *
  76. * @param $ids
  77. * @return string
  78. * @throws DbException
  79. * @throws \think\Exception
  80. */
  81. public function cancel($ids = null)
  82. {
  83. $row = $this->model->get($ids);
  84. if (!$row) {
  85. $this->error(__('No Results were found'));
  86. }
  87. $adminIds = $this->getDataLimitAdminIds();
  88. if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) {
  89. $this->error(__('You have no permission'));
  90. }
  91. if (false === $this->request->isPost()) {
  92. $this->view->assign('row', $row);
  93. return $this->view->fetch();
  94. }
  95. $result = false;
  96. Db::startTrans();
  97. try {
  98. //是否采用模型验证
  99. if ($this->modelValidate) {
  100. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  101. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  102. $row->validateFailException()->validate($validate);
  103. }
  104. $rows = $this->model::alias('a')
  105. ->join('product_pledge b', 'a.pledge_id = b.id', 'left')
  106. ->where('a.id', $ids)->where('a.status', UserPledgeModel::Ongoing)
  107. ->field('a.*,b.day_num')
  108. ->find();
  109. if(empty($rows)) throw new Exception('暂无质押订单');
  110. $day = 86400;
  111. $total = 0; //当前累计收益
  112. $time = time();
  113. $reta = bcdiv($rows->day_num, $day, 6); //天数
  114. $inter = ($rows->last_time == 0) ? $time - $rows->create_time: $time - $rows->last_time; //最后收取时间
  115. $total = bcmul($reta, $inter, 6) * $rows->num; //累计收益
  116. $rows->total_self= bcadd($total, $rows->total_self, 6);
  117. //修改状态
  118. $detail =json_decode($rows->details, true);
  119. ProductOrder::whereIn('id', array_column($detail, 'id'))->setField('status', ProductOrder::Paid);
  120. Loader::model('LedgerWalletModel')->changeWalletAccount($rows->user_id, Asset::TEAC, $total, LedgerTeacChangeModel::Pledge, 0);
  121. $rows->last_time = $time;
  122. $rows->status = UserPledgeModel::Close;
  123. $result = $rows->save();
  124. Db::commit();
  125. } catch (ValidateException|PDOException|Exception $e) {
  126. Db::rollback();
  127. $this->error($e->getMessage());
  128. }
  129. if (false === $result) {
  130. $this->error(__('No rows were updated'));
  131. }
  132. $this->success();
  133. }
  134. }