AdminFreightController.php 8.2 KB
<?php
// +----------------------------------------------------------------------
// | bronet [ 以客户为中心 以奋斗者为本 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.bronet.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 老猫 <bronet@126.com>
// +----------------------------------------------------------------------
namespace app\portal\controller;

use app\portal\model\AddRentModel;
use app\portal\model\ChangeRentModel;
use app\portal\model\ConditionModel;
use app\portal\model\FreightModel;
use app\portal\model\VehicleModel;
use cmf\controller\AdminBaseController;
use app\portal\model\PortalPostModel;
use app\portal\service\PostService;
use app\portal\model\PortalCategoryModel;
use think\Db;
use app\admin\model\ThemeModel;
use think\Loader;
/**
 * Class AdminFreightController
 * @package app\portal\controller
 * @adminMenuRoot(
 *     'name'   =>'运费管理',
 *     'action' =>'default',
 *     'parent' =>'',
 *     'display'=> true,
 *     'order'  => 30,
 *     'icon'   =>'th',
 *     'remark' =>'运费管理'
 * )
 */
class AdminFreightController extends AdminBaseController
{
    /**
     * 运费列表
     * @adminMenu(
     *     'name'   => '运费列表',
     *     'parent' => 'portal/AdminFreight/default',
     *     'display'=> true,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '运费列表',
     *     'param'  => ''
     * )
     */
    public function index()
    {
        $data = $this->request->param();
        $final = $this->adminIndex($data);
        $where_freight = $final['where_arr'];
        $page_arr['page_arr'] = $final['page_arr'];
        if(!empty($data['keyword'])){
            $page_arr['keyword'] = $data['keyword'];
            $where_freight['address_name'] = array('like','%'.$data['keyword'].'%');
            $this->assign('keyword', $data['keyword']);
        }
        $model = new FreightModel();

        $where_freight['status'] = array('neq',9);
        $freight = $model->where($where_freight)->paginate(10,false,['query'=>$page_arr]);
        $page = $freight->render();
        $this->assign('page',$page);
        $this->assign('list',$freight);
        return $this->fetch();
    }



    /**
     * 添加运费
     * @adminMenu(
     *     'name'   => '添加运费',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '添加运费',
     *     'param'  => ''
     * )
     */
    public function add()
    {
        $data   = $this->request->param();
        if($this->request->isPost()){
//            判空
            if(empty($data['city_name'])){
                $this->apiResponse('0','城市不能为空');
            }
            if(empty($data['min_weight'])){
                $this->apiResponse('0','最小重量不能为空');
            }
            if(empty($data['max_weight'])){
                $this->apiResponse('0','最大重量不能为空');
            }
            if(empty($data['money'])){
                $this->apiResponse('0','运费金额不能为空');
            }
            $model = new FreightModel();
//            查询是否已配置
            $where_isset['max_weight'] = array('elt',$data['max_weight']);
            $where_isset['min_weight'] = array('egt',$data['min_weight']);
            $isSet_arr = $model->where($where_isset)->select()->toArray();
            $city_arr = explode(',',$data['city_id']);
            $isset_num = 0;
            if($city_arr){
                if($isSet_arr){
                    foreach ($isSet_arr as $issetk=>$issetv){
                        foreach ($city_arr as $k=>$v){
                            if(strpos($issetv['address_id'],$v)){
                                $isset_num += 1;
                            }else{
                                $isset_num += 0;
                            }
                        }
                    }
                }
            }
            if($isset_num > 0){
                $this->apiResponse('0','运费配置重复');
            }
            $add = $data;
            $add['address_id'] = $data['city_id'];
            $add['address_name'] = $data['city_name'];
            unset($add['city_id']);
            unset($add['city_name']);
            $res = $model->save($add);
            if($res){
                $this->apiResponse('1','成功');
            }else{
                $this->apiResponse('0','失败');
            }

        }else{
            return $this->fetch();
        }
    }

    /**
     * 查看运费
     * @adminMenu(
     *     'name'   => '查看运费',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '查看运费',
     *     'param'  => ''
     * )
     */
    public function view(){
        $data = $this->request->param();
        $where_freight['id'] = $data['id'];
        $where_freight['status'] = array('neq',9);
        $freight = Db::name('Freight')->where($where_freight)->find();
        $this->assign('list',$freight);
        return $this->fetch('edit');
    }

    /**
     * 修改运费
     * @adminMenu(
     *     'name'   => '修改运费',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> false,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '修改运费',
     *     'param'  => ''
     * )
     */
    public function edit(){
        $data = $this->request->param();
        if($this->request->isPost()){
//            判空
            if(empty($data['city_name'])){
                $this->apiResponse('0','城市不能为空');
            }
            if(empty($data['min_weight'])){
                $this->apiResponse('0','最小重量不能为空');
            }
            if(empty($data['max_weight'])){
                $this->apiResponse('0','最大重量不能为空');
            }
            if(empty($data['money'])){
                $this->apiResponse('0','运费金额不能为空');
            }
            $model = new FreightModel();
//            查询是否已配置
            $where_isset['max_weight'] = array('elt',$data['max_weight']);
            $where_isset['min_weight'] = array('egt',$data['min_weight']);
            $isSet_arr = $model->where($where_isset)->select()->toArray();
            $city_arr = explode(',',$data['city_id']);
            $isset_num = 0;
            if($city_arr){
                if($isSet_arr){
                    foreach ($isSet_arr as $issetk=>$issetv){
                        foreach ($city_arr as $k=>$v){
                            if($issetv['id'] != $data['id']){
                                if(strpos($issetv['address_id'],$v)){
                                    $isset_num += 1;
                                }else{
                                    $isset_num += 0;
                                }
                            }

                        }
                    }
                }
            }
            if($isset_num > 0){
                $this->apiResponse('0','运费配置重复');
            }
            $add = $data;
            $add['address_id'] = $data['city_id'];
            $add['address_name'] = $data['city_name'];
            unset($add['city_id']);
            unset($add['city_name']);
            $res = $model->isUpdate(true)->save($add);
            if($res){
                $this->apiResponse('1','成功');
            }else{
                $this->apiResponse('0','失败');
            }

        }
    }
    /**
     * 删除运费
     * @adminMenu(
     *     'name'   => '删除运费',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '删除运费',
     *     'param'  => ''
     * )
     */
    public function del()
    {
        $model = new FreightModel();
        $this->adminDel($model);
    }





}