AgencyController.php 7.7 KB
<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 老猫 <thinkcmf@126.com>
// +----------------------------------------------------------------------
namespace app\portal\controller;
use app\portal\model\UserNeedModel;
use cmf\controller\AdminBaseController;
use app\portal\model\NeedAnswerModel;
use app\portal\model\MemberModel;
use app\portal\model\UserModel;
use app\portal\service\PostService;
use think\Db;
use think\Request;
use think\Loader;
/**
 * @title 保证金内容管理
 * @description 接口说明
 * @group 接口分组
 */

class AgencyController extends AdminBaseController
{
//    列表
    public function index(Request $request)
    {
        $where_coo['a.status'] = array('neq',9);
//        $where_coo['type'] = 2;
//        $where_coo = array();
        $arr = array();
        $status = 0;
        if($request->param()){
            $search = $request->param();
            $startTime = empty($search['start_time']) ? 0 : strtotime($search['start_time']);
            $endTime   = empty($search['end_time']) ? 0 : strtotime($search['end_time']);
            if ($startTime && $endTime) {
                $arr['start_time'] = $search['start_time'];
                $arr['end_time'] = $search['end_time'];
                $where_coo['a.create_time'] = array('between',"$startTime,$endTime");
                $this->assign('start_time', $search['start_time']);
                $this->assign('end_time', $search['end_time']);
            }else{
                if($startTime){
                    $arr['start_time'] = $search['start_time'];
                    $where_coo['a.create_time'] = array('egt',$startTime);
                    $this->assign('start_time', $search['start_time']);
                }
                if($endTime){
                    $arr['end_time'] = $search['end_time'];
                    $where_coo['a.create_time'] = array('elt',$endTime);
                    $this->assign('end_time', $search['end_time']);
                }
            }
            if(!empty($search['name'])){
                $arr['name'] = $search['name'];
                $where_coo['m.name'] = array('like',"%".$search['name']."%");
                $this->assign('name',$search['name']);
            }
            if(!empty($search['tel'])){
                $arr['tel'] = $search['tel'];
                $where_coo['m.tel'] = array('like',"%".$search['tel']."%");
                $this->assign('tel',$search['tel']);
            }
            if(!empty($search['need_id'])){
                $arr['need_id'] = $search['need_id'];
                $where_coo['a.need_id'] = $search['need_id'];
                $this->assign('need_id',$search['need_id']);
            }
            if(!empty($search['status'])){
                $arr['status'] = $search['status'];
                $where_coo['a.status'] = $search['status'];
                $status = $search['status'];
            }
        }
        $this->assign('status',$status);
        $coo_list = Db::name('Agency')->alias('a')
            ->join("Member m",'m.id = a.agency_id')
            ->where($where_coo)
            ->field("m.name as user_name,m.tel as user_tel,a.*")
            ->order('create_time desc')
            ->paginate(10,false,['query'=>$arr]);
        $page = $coo_list->render();
        $this->assign('page',$page);
        $this->assign('list',$coo_list);
        return $this->fetch('index');
    }


//    详情
    public function detail(){
        $data = $this->request->param();
        $where_need['a.id'] = $data['id'];
        $where_need['a.status'] = array('neq',9);
        $need = Db::name('Agency')->alias('a')
            ->join("Member m",'m.id = a.agency_id')
            ->where($where_need)
            ->field('m.name as user_name,a.*')
            ->find();
        if($need['pay_time']){
            $need['pay_time'] = date('Y-m-d H:i:s',$need['pay_time']);
        }else{
            $need['pay_time'] = '';
        }

        if(!$need){
            $this->error('需求错误');
        }
        $this->assign('list',$need);
        return $this->fetch();
    }

//    修改信息
    public function change(Request $request)
    {
        $data = $request->Post();
        $where_agency['id'] = $data['id'];
        $where_agency['status'] = array('neq',9);
        $agency = Db::name('Agency')->where($where_agency)->find();
//        判断是否存在报价
        if($agency){
            $validate = Loader::validate('IntermediaryNeed');
            if(!$validate->check($_POST)){
                return json(array('code'=>0,'msg'=>$validate->getError()));
            }
//            保证金
            if($agency['status'] == 1) {
                $where_inteRule['low'] = array('lt',$data['housing_price']);
                $where_inteRule['height'] = array('egt',$data['housing_price']);
                $inte_money = Db::name('IntermediaryRule')->where($where_inteRule)->field('money')->find();
                if(!$inte_money){
                    $this->error('保证金获取失败');
                }else{
                    $data['deposit'] = $inte_money['money'];
                }
            }
//            加数据库
            $save = Db::name('Agency')->where($where_agency)->update($data);
            }
            if($save){
                $this->success('修改成功',url('Agency/index'));
            }else{
                $this->error('修改失败');
            }

    }


    //  删除
    public function del(Request $request){
        $ids = $this->request->post();
        $id = $request->param('id');
        if($ids){
            $where_del['id'] = array('in',$ids['ids']);
        }else if($id){
            $where_del['id'] = $request->param('id');
        }else{
            $this->error('删除失败');
        }
        $add_del['status'] = 9;
        $add_del['update_time'] = time();
        $del = Db::name('Agency')->where($where_del)->update($add_del);
        if($del){
            $this->success('删除成功',url('index'));
        }else{
            $this->error('删除失败');
        }
    }

//    返回上一級狀態
    public function level(){
        $data = $this->request->param();
//        判断是否状态正确
        $where_need['id'] = $data['need_id'];
        $where_need['status'] = 7;
        $need = Db::name('UserNeed')->where($where_need)->find();
        if($need){
//            修改需求状态
            $save_need['status'] = 5;
            $save_need['update_time'] = time();
            $save_need['choose_time'] = time();
            $save_need['next_choose_time'] = '';
            $save_need['complaint_time'] = '';
            $changeNeed = Db::name('UserNeed')->where($where_need)->update($save_need);
//            修改报价状态
            $where_agency['need_id'] = $data['need_id'];
            $save_agency['status'] = 4;
            $save_agency['update_time'] = time();
            $save_agency['choose_time'] = '';
            $save_agency['complaint_time'] = '';
            $changeAgency = Db::name('Agency')->where($where_agency)->update($save_agency);
            if(!$changeAgency || !$changeNeed){
                $this->error('修改失败');
            }else{
                $this->success('修改成功');
            }
        }else{
            $this->error('当前状态不支持修改');
        }




    }


}