| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198 |
- <?php
- namespace app\admin\controller\user;
- use app\common\controller\Backend;
- use app\common\model\ProductOrder;
- use think\exception\DbException;
- use app\common\model\UserPledge as UserPledgeModel;
- use Exception;
- use think\Db;
- use fast\Asset;
- use think\Loader;
- use app\common\model\LedgerTeacChangeModel;
- use think\exception\PDOException;
- use think\exception\ValidateException;
- use think\Request;
- /**
- * 用户存储列表 TeamRewards
- *
- * @icon fa fa-circle-o
- */
- class Userpledge extends Backend
- {
- /**
- * TeamRewards模型对象
- * @var \app\admin\model\TeamRewards
- */
- protected $model = null;
- protected $product_order_model = null;
- public function _initialize()
- {
- parent::_initialize();
- $this->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');
- dump($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($details);
- $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();
- }
- }
|