StoreOrder.php 6.2 KB
<?php

namespace app\admin\controller\store;

use app\admin\model\House;
use app\common\controller\Backend;
use think\Db;
use think\exception\PDOException;
use think\exception\ValidateException;

/**
 * 开通新社区订单管理
 *
 * @icon fa fa-circle-o
 */
class StoreOrder extends Backend
{
    
    /**
     * StoreOrder模型对象
     * @var \app\admin\model\store\StoreOrder
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\store\StoreOrder;
        $this->view->assign("statusList", $this->model->getStatusList());
    }
    
    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */
    

    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax())
        {
            //如果发送的来源是Selectpage,则转发到Selectpage
            if ($this->request->request('keyField'))
            {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $this->model
                    ->with(['user','store'])
                    ->where($where)
                    ->order($sort, $order)
                    ->count();

            $list = $this->model
                    ->with(['user','store'])
                    ->where($where)
                    ->order($sort, $order)
                    ->limit($offset, $limit)
                    ->select();

            foreach ($list as $row) {
                
                $row->getRelation('user')->visible(['nickname']);
            }
            $list = collection($list)->toArray();
            foreach ($list as &$v) {
                $v['house_names'] = House::whereIn('id',$v['house_ids'])->column('name');
            }
            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }

    /**
     * 已支付
     */
    public function pay($ids = null)
    {
        if ($ids) {
            $row = $this->model->get($ids);
            if (!$row) {
                $this->error(__('No Results were found'));
            }
            $adminIds = $this->getDataLimitAdminIds();
            if (is_array($adminIds)) {
                if (!in_array($row[$this->dataLimitField], $adminIds)) {
                    $this->error(__('You have no permission'));
                }
            }
            $params = $this->request->param();
//            if ($row->pay_type != 3) {
//                $this->error('非线下支付无法修改支付状态');
//            }
            if ($row->status != 1) {
                $this->error('该订单已支付');
            }
            $row->status = 2;
            $row->is_admin = 1;
            $row->pay_time = time();
            Db::startTrans();
            try {
                $result = $row->save();
                // 修改或新增关联社区
                $time = time();
                $info = $row->toArray();
                $store = Db::name('store')->where('id',$info['store_id'])->find();
                $update = [
                    'house_ids' => $store['house_ids'].$info['house_ids'].','
                ];
                $res_store = Db::name('store')->where('id',$info['store_id'])->update($update);
                // 修改或新增社区有效期
                foreach (explode(',',$info['house_ids']) as $v) {
                    if($v) {
                        $store_house = Db::name('store_house')->where(['user_id' => $info['user_id'], 'store_id' => $info['store_id'], 'house_id' => $v])->find();
                        if ($store_house) {
                            $house_update = [
                                'id' => $store_house['id']
                            ];
                            if ($store_house['end_time'] > $time) {
                                $house_update['end_time'] = $store_house['end_time'] + config('site.house_valid') * 86400;
                            } else {
                                $house_update['end_time'] = $time + config('site.house_valid') * 86400;
                            }
                            $result_store_house = Db::name('store_house')->update($house_update);
                        } else {
                            $insert = [
                                'user_id' => $info['user_id'],
                                'store_id' => $info['store_id'],
                                'house_id' => $v,
                                'start_time' => $time,
                                'end_time' => $time + config('site.house_valid') * 86400,
                                'createtime' => $time,
                                'updatetime' => $time
                            ];
                            $result_store_house = Db::name('store_house')->insertGetId($insert);
                        }
                        if (!$result_store_house) {
                            Db::rollback();
                        }
                    }
                }
                if(!$result || !$res_store) {
                    Db::rollback();
                } else {
                    Db::commit();
                }
            } catch (ValidateException $e) {
                Db::rollback();
                $this->error($e->getMessage());
            } catch (PDOException $e) {
                Db::rollback();
                $this->error($e->getMessage());
            } catch (\think\Exception $e) {
                Db::rollback();
                $this->error($e->getMessage());
            }
            if (!$result) {
                $this->error('修改失败');
            }
            $this->success('修改成功');
        }
    }
}