Userpledge.php 6.4 KB

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