Userpledge.php 6.5 KB

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