MoneyController.php 5.9 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\MoneyModel;
use app\portal\model\MoneyRuleModel;
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 MoneyController extends AdminBaseController
{
//    列表
    public function index(Request $request)
    {
        $where_coo['r.status'] = 1;
//        $where_coo = array();
        if($request->post()){
            $search = $request->post();
            if($search['start_time']){
                $start = strtotime($search['start_time']);
                if($search['end_time']){
                    $end = strtotime($search['end_time']);
                }else{
                    $end = time();
                }
                $where_coo['r.update_time'] = array('between',"$start,$end");
                $this->assign('start_time',date('Y-m-d H:i',$start));
                $this->assign('end_time',date('Y-m-d H:i',$end));
            }
            if($search['keyword']){
                $where_area['area_name'] = array('like',"%".$search['keyword']."%");
                $where_area['parent_id'] = 0;
                $city = Db::name('Areas')->where($where_area)->select()->toArray();
                $city_arr = array();
                foreach ($city as $cityk=>$cityv){
                    $city_arr[] = $cityv['area_id'];
                }
                $where_coo['r.city'] = array('in',$city_arr);
                $this->assign('keyword',$search['keyword']);
            }
        }



        $coo_list = Db::name('MoneyRule')->alias('r')
            ->where($where_coo)
//            ->join("hp_areas a",'a.area_id = r.city')
//            ->field("a.area_name , r.id as id, r.low_area,r.height_area,r.money,r.create_time,r.update_time")
            ->field("r.city, r.id as id, r.low_area,r.height_area,r.money,r.create_time,r.update_time")
            ->order('create_time desc')
            ->select()->toArray();
        foreach ($coo_list as $coolk=>$coolv){
            $where_findarea['area_id'] = array('in',$coolv['city']);
//            $where_findarea['parent_id'] = 0;
            $name_arr = Db::name('Areas')->where($where_findarea)->field('area_name')->select();
            $middlearr = array();
            foreach ($name_arr as $namek=>$namev){
                $middlearr[] = $namev['area_name'];
            }
            $name_str = implode(',',$middlearr);

            $coo_list[$coolk]['area_name'] = $name_str;
        }
        $this->assign('list',$coo_list);
        return $this->fetch();
    }


    public function add(Request $request)
    {
        if($request->post()){
//            添加数据
            $data = $request->post();
            if($request->Post('id')){
//                修改
                $add['low_area'] = $data['low_area'];
                $add['city'] = implode(',',$data['city']);
                $add['height_area'] = $data['height_area'];
                $add['money'] = $data['money'];
                $validate = Loader::validate('Money');
                if(!$validate->scene('edit')->check($add)){
                    $mes = $validate->getError();
                    $this->error("$mes");
                }
                $add['id'] = $request->param('id');
                $model = new MoneyRuleModel();
                $final = $model->isUpdate(true)->allowField(true)->save($add);
            }else{
//                添加
                $add['low_area'] = $data['low_area'];
                $add['city'] = implode(',',$data['city']);
                $add['height_area'] = $data['height_area'];
                $add['money'] = $data['money'];
                $validate = Loader::validate('Money');
                if(!$validate->scene('add')->check($add)){
                    $mes = $validate->getError();
                    $this->error("$mes");
                }

                $model = new MoneyRuleModel();
                $final = $model->save($add);
            }
            if($final){
                $this->success('成功',url('index'));
            }else{
                $this->error('失败');
            }
        }else{
            $where_find['id'] = $request->param('id');
            $list = Db::name('MoneyRule')->where($where_find)->find();
            $this->assign('list',$list);
//            地区
            $where_area['parent_id'] = 0;
            $area = Db::name('Areas')->where($where_area)->select()->toArray();
            $this->assign('area',$area);
            return $this->fetch('add');
        }
    }


    //  删除
    public function del(Request $request){
        $ids = $this->request->post();
        $id = $request->param('id');
        if($ids){
            $add_del['id'] = array('in',$ids['ids']);
        }else if($id){
            $add_del['id'] = $request->param('id');
        }else{
            $this->error('删除失败');
        }
        $add_del['status'] = 9;
        $model = new MoneyRuleModel();
        $del = $model->isUpdate(true)->allowField(true)->save($add_del);
        if($del){
            $this->success('删除成功',url('index'));
        }else{
            $this->error('删除失败');
        }
    }

}