model = new \app\common\model\UserPledge; $this->product_order_model = new \app\common\model\ProductOrder; } /** * 查看 * * @return string|Json * @throws \think\Exception * @throws DbException */ public function index() { //设置过滤方法 $this->request->filter(['strip_tags', 'trim']); if (false === $this->request->isAjax()) { return $this->view->fetch(); } //如果发送的来源是 Selectpage,则转发到 Selectpage if ($this->request->request('keyField')) { return $this->selectpage(); } [$where, $sort, $order, $offset, $limit] = $this->buildparams(); $list = $this->model->with('users,pledges') ->where($where) ->order($sort, $order) ->paginate($limit); $result = ['total' => $list->total(), 'rows' => $list->items()]; return json($result); } /** * 详情 */ public function detail($ids) { $row = $this->model->get(['id' => $ids]); if (!$row) { $this->error(__('No Results were found')); } if ($this->request->isAjax()) { $this->success("Ajax请求成功", null, ['id' => $ids]); } $details = json_decode($row->details, true); // 提取所有id,返回一个包含id的一维数组 $order_no = array_column($details, 'order_no'); $orderData = $this->product_order_model ->where('order_no', 'in', $order_no) ->field('order_no, status') // 指定字段 ->select(); $status_txt=['已下单','支付','转让','提货','取消','关闭','存储中']; // 合并 status 到主数据 foreach ($details as &$item_outside) { foreach ($orderData as &$item_inside) { if($item_outside['order_no']==$item_inside['order_no']){ $status=$item_inside['status']; $item_outside['status']=$status; $item_outside['status_txt']=$status_txt[$status]; } } } dump($orderData); $userpledge_status=$row['status']; $this->view->assign("userpledge_status", $userpledge_status); $this->view->assign("userpledge_id", $ids); $this->view->assign("row", $details); return $this->view->fetch(); } /** * 取消存储 */ public function cancelStorage(){ $id = $this->request->param('id'); $userpledgeId = $this->request->param('userpledgeId'); $row = $this->model->get(['id' => $userpledgeId]); $userpledge_status=$row['status']; if($userpledge_status==0){ $resp=$this->product_order_model->where('id', $id)->update(['status' => 0]); if($resp){ $this->success(__('取消成功'), ''); } } $this->error(__('操作失败')); } /** * 编辑 * * @param $ids * @return string * @throws DbException * @throws \think\Exception */ public function cancel($ids = null) { $row = $this->model->get($ids); if (!$row) { $this->error(__('No Results were found')); } $adminIds = $this->getDataLimitAdminIds(); if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) { $this->error(__('You have no permission')); } if (false === $this->request->isPost()) { $this->view->assign('row', $row); return $this->view->fetch(); } $result = false; Db::startTrans(); try { //是否采用模型验证 if ($this->modelValidate) { $name = str_replace("\\model\\", "\\validate\\", get_class($this->model)); $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate; $row->validateFailException()->validate($validate); } $rows = $this->model::alias('a') ->join('product_pledge b', 'a.pledge_id = b.id', 'left') ->where('a.id', $ids)->where('a.status', UserPledgeModel::Ongoing) ->field('a.*,b.day_num') ->find(); if (empty($rows)) throw new Exception('暂无质押订单'); $day = 86400; $total = 0; //当前累计收益 $time = time(); $reta = bcdiv($rows->day_num, $day, 6); //天数 $inter = ($rows->last_time == 0) ? $time - $rows->create_time : $time - $rows->last_time; //最后收取时间 $total = bcmul($reta, $inter, 6) * $rows->num; //累计收益 $rows->total_self = bcadd($total, $rows->total_self, 6); //修改状态 $detail = json_decode($rows->details, true); ProductOrder::whereIn('id', array_column($detail, 'id'))->setField('status', ProductOrder::Paid); Loader::model('LedgerWalletModel')->changeWalletAccount($rows->user_id, Asset::TEAC, $total, LedgerTeacChangeModel::Pledge, 0); $rows->last_time = $time; $rows->status = UserPledgeModel::Close; $result = $rows->save(); Db::commit(); } catch (ValidateException | PDOException | Exception $e) { Db::rollback(); $this->error($e->getMessage()); } if (false === $result) { $this->error(__('No rows were updated')); } $this->success(); } }