<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use think\Db;
/**
 * 套餐转让申请
 *
 * @icon fa fa-circle-o
 */
class Packagemove extends Backend
{

    /**
     * Packagemove模型对象
     * @var \app\admin\model\Packagemove
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Packagemove;
        $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();

            $list = $this->model
                    ->with(['usera','order','userb'])
                    ->where($where)
                    ->order($sort, $order)
                    ->paginate($limit);

            foreach ($list as $row) {
                
                $row->getRelation('usera')->visible(['firstname','lastname']);
				$row->getRelation('order')->visible(['order_no']);
				$row->getRelation('userb')->visible(['firstname','lastname']);
            }

            $result = array("total" => $list->total(), "rows" => $list->items());

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

    /**
     * 审核
     */
    public function audit(){
        $id = input('id');
        $info = Db::name('package_move')
            ->where('id',$id)
            ->find();

        if ($this->request->isPost()) {
            $params = $this->request->post('row/a');

            $status = input('status',0);
            $data = [
                'status' => $status,
                'auditremark' => input('auditremark',''),
                'audittime' => time(),
                'updatetime' => time(),
            ];
            $data = array_merge($params,$data);
            Db::startTrans();

            $rs = Db::name('package_move')->where('id',$id)->update($data);
            if($rs === false){
                Db::rollback();
                $this->error('保存失败');
            }

            if($status == 1){
                //老订单
                $package_info = Db::name('package_order')->where('id',$info['package_order_id'])->lock(true)->find();

                //剩余课时判断
                $new_remain = $package_info['remain'] - $info['sessions'];
                if($new_remain < 0){
                    Db::rollback();
                    $this->error('原套餐订单的剩余课程已不足转出');
                }

                //老订单修改
                $package_update = [
                    'remain' => $new_remain,
                    'updatetime' => time(),
                ];
                $order_rs = Db::name('package_order')->where('id',$info['package_order_id'])->update($package_update);
                if($order_rs === false){
                    Db::rollback();
                    $this->error('转让失败');
                }

                //生成新订单
                $new_order = $package_info;
                unset($new_order['id']);
                $new_order['order_no'] = 'Z'.$package_info['order_no'];//转让订单特殊订单号
                $new_order['user_id'] = $info['to_user_id'];

                $new_order['sessions'] = $info['sessions'];
                $new_order['remain'] = $info['sessions'];
                $new_order['createtime'] = time();
                $new_order['updatetime'] = time();
                $new_order['is_gift'] = 0;
                $new_order['notice_status'] = 0;
                $new_order['remark'] = '来自转让';
                $order_id = Db::name('package_order')->insertGetId($new_order);
                if(!$order_id){
                    Db::rollback();
                    $this->error('转让失败');
                }

                Db::commit();
                $this->success('转让成功');
            }elseif($status == 2){
                Db::commit();
                $this->success('审核完成');
            }

            //多余
            Db::commit();
            $this->success('修改完成');
        }

        $this->assign('row',$info);
        return $this->view->fetch();
    }

}