当前位置:首页 > 杂谈 > 代码总结

代码总结

1.地图配铺功能的Mongodb查询

<?php

/**
 * @filename MongoDB.php 
 * @encoding UTF-8 
 * @author niyang@lepu.cn
 * @datetime 2016-8-29  18:00:51
 * @version 1.0
 * @Description mongodb查询
  */
namespace common\models;

use Yii;
use yii\mongodb\ActiveQuery;
use yii\mongodb\Query;

class MongoDB extends ActiveQuery
{
    public function __construct($modelClass, $config = []) 
    {
        parent::__construct($modelClass,$config);
    }
    /**
     * 根据条件查铺
     * 一般为根据客户条件匹配铺
     * @param type $where
     * @param $target collection shop customer
     */
    public static function shopBuild($where=[])
    {
        $condition = ['collection' => 'shop'];
        //区县
        if (isset($where['area']) && $where['area'] && is_array($where['area'])) 
        {
            $condition['area'] = ['$in' => $where['area']];
        }
        //商圈
        if (isset($where['district']) && $where['district'] && is_array($where['district'])) 
        {
            $condition['district'] = ['$in' => $where['district']];
        }
        //租金范围
        if (isset($where['end_rent']) && $where['end_rent']) 
        {
            $condition['money'] = ['$gte' => (int)$where['start_rent'],'$lte' => (int)$where['end_rent']];
        }
        //面积
        if (isset($where['end_area']) && $where['end_area']) 
        {
            $condition['shop_area'] = ['$gte' => (int)$where['start_area'],'$lte' => (int)$where['end_area']];
        }
        //转让费
        if (isset($where['end_cost']) && $where['end_cost']) 
        {
            $condition['cost'] = ['$gte' => $where['start_cost'],'$lte' => $where['end_cost']];
        }
        //租售意向
        if (isset($where['sale_state']) && $where['sale_state']) 
        {
            $condition['sale_state'] = $where['sale_state'];
        }
        //商铺类型,传入数组
        if (isset($where['property_type']) && $where['property_type'] && is_array($where['property_type']) && !in_array('-1', $where['property_type'])) 
        {
            $property_type = array_filter($where['property_type']);
            $property_type = array_map('intval', $property_type);
            $condition['property_type'] = ['$in' => $property_type];
        }
        //面宽范围
        if (isset($where['end_wide']) && intval($where['end_wide'])) 
        {
            $condition['wide'] = ['$gte' => $where['start_wide'],'$lte' => $where['end_wide']];
        }
        //配套
        if (isset($where['equipment']) && $where['equipment'] && is_array($where['equipment'])) 
        {
            $equipment = array_map('intval', $where['equipment']);
            $condition['equipment'] = ['$in' => $equipment];
        }
        //可办照
        if (isset($where['license']) && $where['license']) 
        {
           $condition['license'] = ['$in' => [3,5,7,9]]; 
        }
        //可明火
        if (isset($where['naked_light']) && $where['naked_light']) 
        {
            $condition['naked_light'] = 1;
        }
        //可分租
        if (isset($where['block']) && $where['block']) 
        {
            $condition['block'] = 1;
        }
        //一级业态
        if (isset($where['cbusiness']) && $where['cbusiness']) 
        {
            $condition['cbusiness'] =(int)$where['cbusiness'];
        }
        //二级子业态
        if (isset($where['current_business']) && $where['current_business']) 
        {
            $condition['current_business'] = (int)$where['current_business'];
        }
        //适合经营
        if (isset($where['business_type']) && is_array($where['business_type']) && $where['business_type']) 
        {
            $condition['business_type'] = ['$in' => $where['business_type']];
        }
        //楼层
        if (isset($where['floor_num']) && is_array($where['floor_num']) && $where['floor_num']) 
        {
            $floor_num = array_map('intval',  $where['floor_num']);
            $condition['floor_num'] = ['$in' =>$floor_num];
        }
        if (isset($where['start_time_at']) && !empty($where['start_time_at']) && isset($where['end_time_at']) && !empty($where['end_time_at'])) 
        {
            $condition['created_at'] = ['$gte' => (int)$where['start_time_at'],'$lte' => (int)$where['end_time_at']];
        }
        //经纬度坐标周边范围内多少米
        if (isset($where['lonlat']) && !empty($where['lonlat']) && isset($where['radius']) && !empty($where['radius'])) 
        {
            $lonlat = explode(',', $where['lonlat']);

            $condition['loc'] = [
                '$near' =>
                    [
                        '$geometry' => 
                        ['type' => 'Point','coordinates' => [floatval($lonlat[0]), floatval($lonlat[1])]],
                        '$maxDistance' => (int)$where['radius']
                    ]
             ];
        }
        //铺源等级
        if (isset($where['vplevel'])) 
        {
            $condition['vplevel'] = is_array($where['vplevel']) ? ['$in' => array_map('intval', $where['vplevel'])] : (int)$where['vplevel'];
        }
        //排除一些id
        if (isset($where['shopids']) && is_array($where['shopids']) && !empty($where['shopids'])) 
        {
            $condition['id'] = ['$nin' => $where['shopids']];
        }
        $condition['status'] = ['$gt' => 0];
        //var_dump($condition);exit;
        return $condition;
    }
    /**
     * 根据条件查询客户
     * 一般为铺的条件匹配客户
     */
    public static function customerBuild($where=[])
    {
        $condition = ['collection' => 'customer'];
        //区县
        if (isset($where['area']) && $where['area']) 
        {
            $area = is_array($where['area']) ? $where['area'] : [$where['area']];
            $area = array_map('intval', $area);
            $condition['area'] = ['$in' => $area];
        }
        //商圈
        if (isset($where['district']) && $where['district']) 
        {
            $district = is_array($where['district']) ? $where['district'] : [$where['district']];
            $district = array_map('intval', $district);
            $condition['district'] = ['$in' => $district];
        }
        //面积
        if (isset($where['start_area']) && $where['start_area'] && isset($where['end_area']) && $where['end_area']) 
        {
            $condition['start_area'] = ['$gte' => (int)$where['start_area'],'$lte' => (int)$where['end_area']];
            $condition['end_area'] = ['$gte' => (int)$where['start_area'],'$lte' => (int)$where['end_area']];
        } else if (isset($where['shop_area']) && $where['shop_area']) 
        {
            $condition['start_area'] = ['$lte' => (int)$where['shop_area']];
            $condition['end_area'] = ['$gte' => (int)$where['shop_area']];
        }
        //转让费
        if (isset($where['start_cost']) && $where['start_cost'] && isset($where['end_cost']) && $where['end_cost']) 
        {
            $condition['start_cost'] = ['$gte' => (int)$where['start_cost'],'$lte' => (int)$where['end_cost']];
            $condition['end_cost'] = ['$gte' => (int)$where['start_cost'],'$lte' => (int)$where['end_cost']];
        } else if (isset($where['cost']) && $where['cost']) 
        {
            $condition['start_cost'] = ['$gte' => (int)$where['cost']];
            $condition['end_cost'] = ['$lte' => (int)$where['cost']];
        }
        //租金
        if (isset($where['start_price']) && $where['start_price'] && isset($where['end_price']) && $where['end_price']) 
        {
            $condition['start_rent'] = ['$gte' => (int)$where['start_price'],'$lte' => (int)$where['end_price']];
            $condition['end_rent'] = ['$gte' => (int)$where['start_price'],'$lte' => (int)$where['end_price']];
        } else if (isset($where['money']) && $where['money']) 
        {
            $condition['start_rent'] = ['$lte' => (int)$where['money']];
            $condition['end_rent'] = ['$gte' => (int)$where['money']];
        }
        //楼层
        if (isset($where['floor']) && $where['floor']) 
        {
            if (!is_array($where['floor'])) 
            {
                $floor = [$where['floor']];
            } else {
                $floor = $where['floor'];
            }
            $floor = array_map('intval', $floor);
             $condition['floor'] = ['$in' => $floor];
        }
        //面宽
        if (isset($where['wide']) && $where['wide']) 
        {
            $condition['start_wide'] = ['$lte' => $where['wide']];
            $condition['end_wide'] = ['$gte' => $where['wide']];
        }
        //是否分隔
        if (isset($where['block']) && $where['block']) 
        {
             $condition['block'] = (int)$where['block'];
        }
        //可明火
        if (isset($where['naked_light']) && $where['naked_light']) 
        {
            $condition['naked_light'] = (int)$where['naked_light'];
        }
        //证照
        if (isset($where['license']) && $where['license']) 
        {
           $condition['license'] = (int)$where['license'];
        }
        //配套
        if (isset($where['equipment']) && $where['equipment'] && is_array($where['equipment'])) 
        {
            $equipment = array_map('intval', $where['equipment']);
            $equipment = array_filter($equipment);
            $condition['equipment'] = ['$in' => $equipment];
        }
        //可经营类别,一级分类
        if (isset($where['business']) && $where['business'])
        {
            if (is_array($where['business']))
            {
                $condition['business'] = ['$in' => array_map('intval',$where['business'])];
            } else {
                $condition['business'] = (int)$where['business'];
            }
        }
        if (isset($where['current_business']) && $where['current_business'])
        {
            $condition['business_type'] = (int)$where['current_business'];
        }
        //物业类型
        if (isset($where['property_type']) && $where['property_type']) 
        {
            $condition['property_type'] = ['$in' => [(int)$where['property_type']]];
        }
        //签约客户
        if (isset($where['signup']) && $where['signup']) 
        {
            $condition['signup'] = (int)$where['signup'];
        }
        //环线
        if (isset($where['loop']) && $where['loop']) 
        {
            $condition['loop'] = (int)$where['loop'];
        }
        //电压可改
        if (isset($where['voltage']) && $where['voltage']) 
        {
            $condition['voltage'] = (int)$where['voltage'];
        }
        //租约
        if (isset($where['agreement']) && $where['agreement']) 
        {
            $condition['agreement'] = ['$lte' => $where['agreement']];
        }
        //找铺等级
        if (isset($where['level']) && $where['level']) 
        {
            $condition['level'] = (int)$where['level'];
        }
        //更新时间
        if (isset($where['updated_at']) && $where['updated_at']) 
        {
            $condition['updated_at'] = ['$lte' => (int)$where['updated_at']+3600*24];
            $condition['updated_at'] = ['$gte' => (int)$where['updated_at']];
        }
        //var_dump($condition);exit;
        return $condition;
    }
    /**
     * 
     * @param type $search
     * @param type $page
     * @param type $pagesize数据
     * @param type $$adp Description
     */
    public static function dataList($condition, $sort='created_at desc',$page=1, $pagesize=25,$adp=true)
    {
        $collection = $condition['collection'];
        unset($condition['collection']);

        $query = new Query();
        $query->from($collection);
        $query->where($condition);
        $query->offset(($page - 1) * $pagesize);
        $query->limit($pagesize);
        $query->orderBy($sort);
        //$search = Yii::$app->mongodb->getCollection($collection);
        //$query = $search->find($condition,[],['skip' => ,'limit' => $pagesize]);
        if ($adp == true) 
        {
            return new \yii\data\ActiveDataProvider([
                'query' => $query,
                'pagination' => [
                    'pageSize' => $pagesize,
                ],
            ]);
        } else {
            return $query->all();
        }
    }
    /**
     * 
     * @param type $v
     * @return type数字转字符串
     */
    public static function toStr($v)
    {
        return (string)$v;
    }
}

 

2.商铺表shop基类

<?php

namespace common\models;

use backend\models\BCityRoad;
use common\models\basedata\Road;
use Yii;
use yii\behaviors\TimestampBehavior;
use common\models\ShopImg;
use common\models\ShopBusinessType;
use common\models\City;
use common\models\CityMap;
use common\models\Search;
use common\models\ShopTag;
use common\helper\ShopFormat;
use common\models\peipu\ShopPeipuTongji;
USE common\models\ShopReport;
use backend\models\peipu\BShopPeipuTongji;

/**
 * This is the model class for table "shop".
 *
 */
class Shop extends \yii\db\ActiveRecord
{
    //状态-未定
    const STATUS_DELETE = 0;//删除
    const STATUS_NOMAL = 1;//新入库标准化铺源
    const STATUS_NOISE = -3; //噪音数据
    //const STATUS_MAYBE_NOISE = 3; // 疑似噪音 需要人工筛查
    const STATUS_CLAIMED = 15;//已认领未处理
    const STATUS_PHONED = 45;//电销核实铺源
    const STATUS_PHONED_MAINTAIN = 60;//电销待维护
    const STATUS_PHONED_FAILED = 75;//电销战败(已核实)
    const STATUS_BUSINESS = 80;//商务公海
    const STATUS_BUSINESS_CLAIMED = 85;//商务已经认领
    const STATUS_VERIFY = 90;//商务认证铺源-已实勘铺源
    const STATUS_VERIFY_FAILED = 100;//商务认证战败
    
    //是否优选
    const PREFERENCE_Y = 1; // 是
    const PREFERENCE_N = 0; // 否
    
    //是否认证
    const CERTIFIED_Y = 1; //已认证
    const CERTIFIED_N = 0; //未认证
    
    //状态信息
    public static $status_info = [
        self::STATUS_DELETE => '删除',
        self::STATUS_NOMAL => '新入库标准化铺源',
        self::STATUS_NOISE => '重复数据',
        self::STATUS_CLAIMED => '已认领未处理',
        self::STATUS_PHONED => '电销核实铺源',
        self::STATUS_PHONED_MAINTAIN => '电销待维护',
        self::STATUS_PHONED_FAILED => '已核实',
        self::STATUS_BUSINESS => '分配给商务',
        self::STATUS_BUSINESS_CLAIMED => '商务认领',
        self::STATUS_VERIFY => '商务实勘',
        self::STATUS_VERIFY_FAILED => '商务认证战败'
    ];
    //商铺级别状态信息
    const VPLEVEL_ROLLOUT = -1; //已转出
    const VPLEVEL_NOMAL = 5; //普通
    const VPLEVEL_PREFERENCE = 10; //已核实
    const VPLEVEL_VERFIY = 15; //实勘
    const VPLEVEL_CERTIFIED = 20; //认证,作废不用
    const VPLEVEL_SIGN = 25; //签约
    public static $vplevel = [
        self::VPLEVEL_ROLLOUT => '已转出',
        self::VPLEVEL_NOMAL => '普通',
        self::VPLEVEL_PREFERENCE => '优选',
        self::VPLEVEL_VERFIY => '实勘',
        self::VPLEVEL_CERTIFIED => '认证',
        self::VPLEVEL_SIGN => '签约'
    ];
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'shop';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['tel'],'required','on' => 'default'],
            [['shop_type', 'cbusiness', 'area', 'shop_name', 'address', 'shop_area', 'money', 'cost', 'contact', 'tel', 'telphone'],'required','on' => 'puguanjia'],
            [['tel', 'telphone'],'match','pattern'=>'/^1[345789]\d{9}$/','message'=>'手机号格式错误','on' => 'puguanjia'],
            [['tel'],'unique','message'=>'手机号已经被占用了。','on' => 'puguanjia'],
            ['tel', function($attribute, $params){
                $tel = trim($this->$attribute);
                if (!empty($tel))
                {
                    $tel = trim($this->$attribute);
                    $tel = str_replace(array('-'), array(''), $tel);
                    if (!is_numeric($tel)) 
                    {
                        $this->addError($attribute, '电话号码格式不正确');
                    }
                }
            }],
            [['city', 'area', 'district', 'info_type', 'sex', 'shop_type', 'money', 'pay_bet', 'pay_pay', 'deposit', 'property_type', 'block','can_food',
                'cbusiness','current_business', 'state','community_id','office_id','sale_state', 'pic_count','is_verify','is_certified','certified_time','naked_light','shading',
                'phone_id','commerce_id','street','adviser_id','creater_id','source_id','preference','updated_at', 'created_at', 'status'], 'integer'],
            [['floor_height','wide','cost','cost_out','shop_area','usage_area','green_belt','agreement','long_agreement','money_stream'], 'number'],
            [['tel','contact','tel_other'], 'string', 'max' => 20],
            [['lease_surplus'], 'string', 'max' => 3],
            [['title', 'address', 'address_out', 'shop_name','lonlat','equipment','property_right','main_img','superiority','money_type','floor_num','money_addition','is_agreement'], 'string', 'max' => 100],
            [['shop_review', 'peipu_review', 'inner_review','before_question','before_question_other'], 'string', 'max' => 500],
            [['introduce'], 'string'],
            [['vplevel'], 'default', 'value' => self::VPLEVEL_NOMAL],
            [['city', 'area', 'district', 'info_type', 'sex', 'shop_area', 'usage_area', 'shop_type', 'money', 'cost', 'cost_out', 'pay_bet', 'pay_pay', 'deposit', 'property_type', 'block',
                'cbusiness','current_business', 'state','sale_state', 'pic_count','is_verify','is_certified','certified_time','agreement','long_agreement','lease_surplus','naked_light','floor_height',
                'green_belt','shading','can_food','loop','money_stream','fitment',
                'phone_id','commerce_id','adviser_id','guanjia_id','creater_id','source_id','preference','updated_at', 'created_at', 'status'], 'default', 'value' => 0]
        ];
    }
    public function scenarios() {
        return [
            'default' => ['title','shop_name','city','area','district','street','info_type','contact','sex','tel','tel_other','shop_area','usage_area','wide','shop_type','cost','cost_out','money', 'money_addition','money_type','money_stream','introduce','property_type',
                'floor_num','floor_height','block','address','address_out','lonlat','cbusiness','current_business','state','sale_state','pic_count','main_img','is_verify','is_certified','vplevel','loop','certified_time','agreement','long_agreement','is_agreement','lease_surplus','naked_light','pay_bet',
                'green_belt','shading','license','license_imgs','license_type','can_food','source_id','superiority','shop_review','peipu_review','inner_review','before_question','before_question_other',
                'pay_pay','deposit','equipment','property_right','fitment','orientation','updated_at','updated_at','community_id','office_id','phone_id','commerce_id','adviser_id','guanjia_id','creater_id','preference','status'],
            'puguanjia' => ['shop_type', 'cbusiness', 'area', 'shop_name', 'address', 'shop_area', 'money', 'cost', 'contact', 'tel']
        ];
    }
    public function behaviors() {
        return [
            TimestampBehavior::className()
        ];
    }
    //扩展属性
    protected $extend =  ['day_money', 'month_money','unit_money', 'district_name','phone_claimed_time','commerce_claimed_time','telphone','ctitle','te','peipu_review'];
    /**
     * @return type添加扩展属性到属性
     */
    public function attributes() {
        $table = parent::attributes();
        return array_merge($table, $this->extend);
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID','contact' => '联系人','sex' => '性别','tel' => '电话', 'tel_other' => '其他联系方式', 'info_type' => '信息类型', 'shop_type' => '租售类型',
            'city' => '市','area' => '区县','district' => '商圈', 'street' => '街道','address' => '真实地址','address_out' => '外显地址','lonlat' => '经纬度','shop_name' => '店名', 'money_addition' => '租金递增',
            'property_type' => '物业类型','state' => '经营状态','sale_state' =>'商铺租售状态','shop_area' => '建筑面积','usage_area' => '使用面积','cost' => '转让费','cost_out' => '外显转让费','money' => '月租金','money_type' => '租金包含',
            'agreement' => '租约','is_agreement' => '续约情况','lease_surplus' => '租期剩余','long_agreement' => '最长可租约','naked_light' => '是否可明火','equipment' => '配套','property_right' => '产权', 'pay_bet' => '押', 'pay_pay' => '付','deposit' => '押金','money_stream' => '流水抽成/扣点',
            'title' => '标题', 'introduce' => '介绍', 'community_id' => '小区ID', 'office_id' => '写字楼ID','floor_num' => '楼层','floor_height' => '层高', 'current_business' => '当前经营','peipu_review' => '配铺专家点评',
            'block' => '是否分隔','wide' => '面宽','is_verify' => '是否需要实勘','is_certified'=>'是否认证','certified_time'=>'实勘认证审核时间','license' => '执照','license_imgs' => '营业执照图片','license_type' => '当前已有证件',
            'pic_count' => '图片', 'main_img' => '主图','remark' => '备注','green_belt' => '绿化带宽度','shading' => '遮挡程度','superiority' => '本店优势', 'shop_review' => '店主点评',
            'updated_at' => '更新时间', 'created_at' => '创建时间','status' => '状态','preference' => '分级','can_food'=>'可餐饮', 'inner_review' => '内部说明', 'fitment' => '装修情况', 'orientation' => '朝向',
            'business_type_c' => '可经营业态', 'telphone' => '您的电话','source_id' =>'来源','cbusiness'=>'经营类别','loop' =>'环线','before_question' => '过往经营最大的问题','before_question_other' => '过往经营最大的问题其他',
            'admin' => '电销认领','commerce' => '商务认领','guanjia' => '商铺管家','create' => '创建人','month_money' => '月租','day_money' => '日租','total_money' => '总价格', 'vplevel' => '等级',
            'district_name' => '商圈名称',//格式化前台应用
        ];
    }
    //信息类型  4转让
    static $info_type = array( 1 => '房东直租',2 => '中介',3 => '转租');
    
    //性别
    static $sex = array(1 => '男',2 => '女',0 => '未知');
    //售卖类型
    static $shop_type = array(1 => '商铺出售',2 => '商铺出租',4 => '生意转让');

    //物业类别--商铺类型
    static $property_type = array(
        1 => '商业街商铺',2 => '社区住宅底商',3 => '写字楼配套',7 => '百货/购物中心',8 => '临街门面',9 => '档口摊位',10 => '其它'
    );
    //可经营类别,业态---放到对应model里
    static $business_type = array(
        1 => '酒楼餐饮',2 => '服饰鞋包',3 => '休闲娱乐', 4 => '美容美发',5 => '生活服务',6 => '百货超市',7 => '家居建材',
        8 => '电器通讯', 9 => '汽修美容',10 => '医药保健',11 => '教育培训',12 => '旅馆宾馆',13 => '其它业态'
    );
    //现经营类型 1(01)第二三位为可经营类别id号,取整即可
    static $current_business = [
        1 => [
            1011 => '面包房',1012 => '火锅店', 1013 => '自助餐厅', 1014=> '小吃快餐店', 1015=>'日本料理店',1016 => '韩国料理店', 1017 => '西餐厅', 1018 => '烤肉店', 
            1019 => '烧烤/烤串',10110 => '湘菜馆', 10111 => '江浙菜馆',10112 => '香锅烤鱼店',  10113 => '粤港菜馆', 10114 => '西北菜馆', 10115 => '云南菜馆', 10116 => '贵州菜馆', 
            10118 => '海鲜馆', 10121 => '粥铺', 10123 => '新疆菜馆', 10126=> '湖北菜馆', 10127 => '清真菜馆',10128 => '东北菜馆', 10129 => '家常菜馆', 
            10130 => '饮料店/水吧', 10131 => '咖啡厅',10132 => '私房菜馆',10133 => '徽菜馆',10134 => '粉面馆',10135 => '江西菜馆',10136 => '山西菜馆',10137 => '藏餐馆',10140 => '甜品店', 10144 => '鲁菜馆',10150 =>'川菜馆', 10155 => '素菜馆',10160 =>'炖菜馆', 10166 => '蒙餐馆',
            10170 => '北京菜馆', 10180 => '东南亚菜馆',10190 => '台湾菜/客家菜',10194 => '快餐配送',10196 => '酒吧',10199 => '其他'
            ],//1032 => '酒吧',变更为10196
        2 => [1021 => '服装店', 1022 => '内衣店', 1023 => '童装店', 1024 => '鞋店', 1025 => '箱包店', 1026 => '饰品店', 10210 => '黄金珠宝店', 10220 => '婚纱礼服馆'],
        3 => [
            1031 => '网吧', 1033 => '足浴/按摩店', 1039 => '桌球城', 10315 => '茶馆/茶楼',10319 => '水疗', 10322 => '球馆', 10325 => '麻将馆/棋牌室', 10328 => '歌舞厅(ktv)',
            10331 => '夜总会',10333 => '健身房',10336 => '瑜伽馆',10340 => '休闲中心',  10346 => '浴池浴场', 10350 => '桌游', 10353 => '溜冰场', 10356 => '电玩城',
            10360 => '电影院', 10365 => '游乐场', 10370 =>'密室俱乐部',10372 => 'VR体验馆',10374 => 'DIY手工坊',10376 => '桌游室',  10380 => '儿童乐园'
        ],//10344 => '棋牌室',合并为10325-- 10515 => '儿童乐园' 变更为:10380
        4 => [1041 => '美容院', 1042 => '美发店', 1043 => '美甲店', 1044 => 'SPA馆',  1047 => '母婴养生会所',1048 => '纹身店'],//1045 => '产后修复所', 1046 => '养生馆',合并为1047
        5 => [
            1051 => '干洗店', 1052 => '花店', 1053 => '水族店', 1054 => '彩票店', 1055 => '报刊亭', 1056 => '送水送气店', 1057 => '宠物店', 1058 => '照相馆', 1059 => '婚纱/儿童摄影', 10510 => '打字复印店',
            10512 => '广告制作店', 10517 => '婚庆店',10518 => '房产中介',10519 => '银行',10520 => '快递点'
            ,10521 => '旅行社',10522 => '投资理财保险咨询',10523 => '家政服务中心',10524 => '典当行',10525 => '丧葬用品店',10526 => '劳务中介',10527 => '法律咨询',10528 => '票务代售点',10529 => '裁缝改衣店'
            ,10530 => '皮具护理店'
        ],//,10230 => '皮具护理店',变更为10530
        6 => [
            1061 => '超市', 1062 => '便利店', 1063 => '小卖部', 1064 => '精品店', 1065 => '杂货店', 1066 => '烟酒茶叶店', 1067 => '母婴用品店', 1068 => '玩具店', 1069 => '文具店', 10610 => '书店', 10611 => '眼镜店',
            10612 => '化妆品店', 10614 => '乐器店', 10615 => '工艺品店', 10617 => '休闲食品店', 10618 => '专柜', 10619 => '床上用品店', 10620 => '水产肉类熟食店', 10622 => '特产类商店', 
            10623 => '档口/摊位', 10624 => '副食品店', 10625 => '水果店',10635 => '格子铺', 10640 => '古玩字画',10643 => '体育用品店'
        ],//10215 => '格子铺',变更为10635-- 10363 => '古玩字画',变更为10640-- 10513 => '体育用品店'变更为11643
        7 => [1071 => '五金建材店', 1072 => '建材店', 1073 => '家具店', 1078 => '灯饰店', 10711 => '家居饰品店', 10716 => '装饰装修材料店', 10720 => '布艺家纺店', 10725 => '装潢店'],
        8 => [1081 => '数码店', 1082 => '电脑店', 1083 => '电器店', 1084 => '维修店', 1085 => '通讯营业厅'],
        9 => [1091 => '汽修厂', 1092 => '汽配店', 1093 => '轮胎店', 1094 => '汽车美容店', 10910 => '维修点', 10915 => '汽车4S店', 10920 => '电瓶车店'],
        10 => [1101 => '医院', 1102 => '诊所', 1103 => '药店', 1104 => '保健品店', 1105 => '成人用品店',1106 => '体检中心',1107 => '养老院', 1108 => '月子会所'],//10514 => '养老院',变更为1107--  10516 => '月子会所',变更为1108
        11 => [1111 => '学校',1112 => '幼儿园',1113 => '培训机构',1116 => '家教中心', 1119 => '早教中心', 11115 => '婴儿游泳馆'],//10511 => '婴儿游泳馆' 变更为11115
        12 => [1121 => '旅馆',1122 => '宾馆酒店',1126 => '招待所',1129 => '公寓房',11231 => '度假山庄',11213 => '出租房'],//10368 => '度假山庄',变更为11231
        13 => [113100 => '其他']
    ];
    //运营状态
    static $state = [1 => '营业中',2 => '空铺'];
    //租售意向
    const IS_SALE_STATE = 1; //租售意向(是)
    //const SALE_STATE_NO = 5; //租售意向(否)
    const SALE_STATE_NO = 8; //租售意向(否)现改成已成交
    static $sale_state = [self::IS_SALE_STATE => '租赁中', self::SALE_STATE_NO => '已成交', 11 => '部分出租', 14 => '停转/租-违建拆除', 17 => '停转/租-房东收回', 20 => '停转/租-被查封', 23 => '停转/租-继续经营'];

    //配套设施
    static $equipment = [14 => '上水', 15 => '下水', 16 => '380伏', 17 => '煤气罐', 18 => '烟管道', 19 => '排污管道', 20 => '停车位', 21 => '天然气', 23=> '外摆区'];
    //是否可以分割
    const IS_BLOCK = 1;
    static $block = [self::IS_BLOCK => '是',2 => '否'];
    //是否实勘
    const IS_VERIFY = 1;  //需要实勘 
    const HAS_VERIFY = 3;//已实勘同步修改vplevel,status
    static $is_verify = [self::IS_VERIFY => '需要实勘', self::HAS_VERIFY => '已实勘'];
    //是否优选,此字段作为后台推荐优选用
    static $is_certified = [self::CERTIFIED_N => '未认证', self::CERTIFIED_Y => '已认证'];
    //是否可明火
    static $naked_light = [1 => '可以', 0 => '不可以'];
    static $floor_num = [1 => '一层', 2 => '二层', 3 => '三层', 4 => '高层', 5 => '半地下'/*, 6 => '1到地下1层', 7 => '1到2层', 8 => '1到3层', 9 => '1到4层'*/];
    //遮挡程度
    static $percent = [0 => 0, 10 => 10, 20 => 20, 30 => 30, 40 => 40, 50 => 50, 60 => 60, 70 => 70, 80 => 80, 90 => 90, 100 => 100];

    //办照信息
    static $license_info = [1 => '不可办照', 3 => '可办照', 5 => '有照可过户', 7 => '有照可增项', 9 => '大照/集体照'];
    static $license_type = [1 => '营业执照', 3 => '卫生许可证', 5 => '环评', 7 => '餐饮服务许可证', 9 => '食品加工许可证', 11 => '食品流通许可证', 13 => '食品经营许可证', 35=> '其他'];
    //环线
    static $loop_info = [
        1 => '二环内',
        4 => '东二环到东三环',
        7 => '南二环到南三环',
        10 => '西二环到西三环',
        13 => '北二环到北三环',
        16 => '东三环到东四环',
        19 => '南三环到南四环',
        22 => '西三环到西四环',
        25 => '北三环到北四环',
        28 => '东四环到东五环',
        31 => '南四环到南五环',
        34 => '西四环到西五环',
        37 => '北四环到北五环',
        40 => '五环外',
    ];
    //本店优势
    static $superiority = [
        4 => '人流大',
        7 => '消费力高',
        10 => '竞争少',
        13 => '租金低/转让费低',
        17 => '展示效果好',
    ];
    //过往经营最大的问题
    static $before_question = [
        4 => '装修不够好',
        7 => '新手经营不够专业',
        10 => '采购/进货渠道成本较高',
        13 => '合伙人出了问题',
        17 => '其他',
    ];
    //租金包含
    static $money_type = [
        4 => '物业费',
        7 => '取暖费',
    ];
    //产权信息
    static $property_right = [1 => '商业', 3 => '住宅', 5=> '商住两用', 50 => '其他'];
    //续约情况
    static $is_agreement = [1 => '继续使用原租约', 3 => '另签一份'];
    //是否可餐饮
    static $can_food = [1 => '是', 0 => '否'];
    //装修情况
    static $fitment = [1 => '精装',2 => '简装',3 => '毛坯/清水房'];

    //铺源分级
    static  $preference = [
        5 => 'S级',
        8 => 'A级',
        11 => 'B级',
        14 => 'C级',
        17 => 'D级',
    ];

    /**
     * 得到图片
     */
    public function getImage()
    {
        return $this->hasMany(ShopImg::className(), ['shop_id' => 'id'])->where(['state' => ShopImg::STATE_NOMAL])->orderby('ptype');
    }

    /**
     * 商铺类型
     * 业态
     */
    public function getBusiness()
    {
        return $this->hasMany(ShopBusinessType::className(), ['shop_id' => 'id'])->where(['shop_business_type.state' => ShopBusinessType::STATUS_NOMAL]);
    }
    /**
     * 得到标签
     */
    public function getTag()
    {
        return $this->hasMany(ShopTag::className(), ['shop_id' => 'id'])->where(['state' => ShopTag::STATUS_NOMAL])->andWhere(['!=','labels_id',  ShopTag::LABELS_TYPE_BACKRUN]);
    }
    /**
     * 系统自动打的标签
     */
    public function getAutotag()
    {
        return $this->hasOne(ShopTag::className(), ['shop_id' => 'id'])->where(['state' => ShopTag::STATUS_NOMAL,'labels_id'=> ShopTag::LABELS_TYPE_BACKRUN]);
    }

    /**
     * 得到城市信息
     */
    public function getCityinfo()
    {
       return $this->hasOne(City::className(), ['id' => 'city']);
    }
    /**
     * 得到区县信息
     */
    public function getAreainfo()
    {
        return $this->hasOne(CityMap::className(), ['id' => 'area']);
    }
    /**
     * 得到商圈
     */
    public function getDistrictinfo()
    {
        return $this->hasOne(CityMap::className(), ['id' => 'district']);
    }
    /**
     * 得到道路
     */
    public function getRoad($area){
       return  BCityRoad::find()->where(['area' => $area])->andWhere(['!=','state', BCityRoad::STATUS_DELETE])->all();
    }
    /**
     * 得到道路名称
     */
    public static function getRoadname($id){
        $road = Road::find()->where(['road_id' => (int)$id])->one();
        return $road->road_name;
    }
    /**
     * 得到道路
     */
    public function getRoadinfo(){
        return $this->hasOne(Road::className(), ['road_id' => 'street']);
    }
    /**
     * 得到铺源报告
     */
    public function getReport()
    {
        return $this->hasOne(ShopReport::className(), ['shop_id' => 'id'])->where(['state' => ShopReport::STATUS_NOMAL]);
    }
    /**
     * 自定义属性 物业类型名称
     */
    public function getProperty_type_name()
    {
        return isset(self::$property_type[$this->property_type]) ? self::$property_type[$this->property_type] : '';
    }
    /**
     * 得到全景数据
     */
    public function getPanorama()
    {
        return $this->hasOne(ShopPanorama::className(), ['shop_id' => 'id'])->andWhere(['!=','panorama_id',''])->andWhere(['!=','panorama_lonlat','']);
    }
    /**
     * 得到合同信息
     */
    public function getContract()
    {
        return $this->hasOne(adviser\ShopContract::className(), ['shop_id' => 'id']);
    }
    /**
     * 得到转让费降价记录
     */
    public function getCostdown()
    {
        return $this->hasOne(ShopFieldHistory::className(), ['shop_id' => 'id'])->where(['field' => 'cost'])->orderBy('created_at desc');
    }
    /**
     * 得到拼装标题
     */
    public function getCtitle()
    {
        return self::formatTitle($this);
    }
    /**
     * 得到来源页信息
     */
    public function getSource()
    {
        return $this->hasOne(ShopSourceUrl::className(), ['shop_id' => 'id']);
    }

    /**
     * 获取视频信息
     */
    public function getVideo()
    {
        return $this->hasOne(ShopImg::className(), ['shop_id' => 'id'])->where(['ptype' => ShopImg::PIC_VIDEO, 'state' => ShopImg::STATE_NOMAL]);
    }

    /**
     * 计算热度电询1分,推荐3,上门10分
     */
    public function getHot()
    {
        return $this->hasOne(ShopPeipuTongji::className(), ['shop_id' => 'id']);
    }

    /**
     * 得到配铺信息
     */
    public function getPeipu()
    {
        return $this->hasOne(BShopPeipuTongji::className(), ['shop_id' => 'id']);
    }
    
    /**
     * 格式化标题,即返回ctitle
     * @param $shopobj 商铺对象
     */
    public static function formatTitle($shopobj)
    {
        $title = '';
        if ($shopobj->area) 
        {
            $title .= \common\helper\Common::getCityName($shopobj->area);
        }
        if ($shopobj->district && $shopobj->district!=170 && $shopobj->district!=243 && $shopobj->district!=247 && $shopobj->district!=249) 
        {
            $title .= \common\helper\Common::getCityName($shopobj->district);
        }
        if ($shopobj->street && $shopobj->roadinfo && $shopobj->roadinfo->road_name) 
        {
            $title .= $shopobj->roadinfo->road_name;
        }
        if ($shopobj->usage_area) 
        {
            $title .= $shopobj->usage_area.'㎡';
        } elseif ($shopobj->shop_area) 
        {
            $title .= $shopobj->shop_area.'㎡';
        }
        if ($shopobj->current_business) 
        {
            if ($shopobj->cbusiness && ($shopobj->current_business == 10199 || $shopobj->current_business == 113100)) 
            {
                $title .= ShopFormat::cbusiness($shopobj->cbusiness);
            } else {
                $title .= ShopFormat::current_business($shopobj->current_business);
            }
        }
        if ($shopobj->shop_type) 
        {
            if ($shopobj->current_business) 
            {
                $title .= str_replace(['商铺','生意','-'], '', self::$shop_type[$shopobj->shop_type]);
            } else {
                $title .= self::$shop_type[$shopobj->shop_type];
            }
        } else {
            $title .= '出租';
        }
        $tag = [];
        if ($shopobj->license && in_array($shopobj->license,[3,5,7,9])) 
        {
            $tag[] = ShopFormat::license($shopobj->license);
        }
        if ($shopobj->naked_light==1) 
        {
            $tag[] = '可明火';
        }
        if ($shopobj->can_food ==1) 
        {
            $tag[] = '可餐饮';
        }
        if ($tag) 
        {
            $title .= ','.join(',', $tag);
        }
        if (mb_strlen($title) <= 14 && $shopobj->cost == 0) 
        {
            $title .=',无转让费';
        }
        if (mb_strlen($title) <= 14 && $shopobj->report->business_time > 3) 
        {
            $title .= '多年老店';
        }
        

        return  $title;
    }
    public function beforeValidate() 
    {
        parent::beforeValidate();
        if (is_array($this->equipment)) {
            $this->equipment = join(',', $this->equipment);
        }
        if (is_array($this->floor_num)) {
            $this->floor_num = join(',', $this->floor_num);
        }        
        return true;
    }
    /**
     * 在保存前要删除扩展属性
     * @param type $insert
     */
    public function beforeSave($insert) 
    {
        parent::beforeSave($insert);
        if ($this->extend) 
        {
            foreach ($this->extend as $extend) 
            {
                unset($this->$extend);
            }
        }
        if (is_array($this->equipment)) {
            $this->equipment = join(',', $this->equipment);
        }
        if (is_array($this->floor_num)) {
            $this->floor_num = join(',', $this->floor_num);
        }
        //拆分坐标 后续需要使用这个做区间搜索
        if ($this->lonlat){
            $lonlat =  explode(',',$this->lonlat);
            if (count($lonlat) == 2){
                if ($this->lon != $lonlat[0])
                    $this->lon = $lonlat[0];
                if ($this->lat != $lonlat[1])
                    $this->lat = $lonlat[1];
            };
        }

        return true;
    }
    /**
     * 保存成功之后的操作
     */
    public function afterSave($insert, $changedAttributes)
    {
        parent::afterSave($insert, $changedAttributes);
        //添加配铺统计
        if ($insert === true)
        {
            ShopPeipuTongji::add($this->id);
        }
        //新建,坐标改变,生成铺源报告
        if (!empty($this->lonlat) && ($insert == true ||  isset($changedAttributes['lonlat']))) 
        {
            \common\helper\TaskHelper::put(\common\helper\TaskHelper::BUILD_SHOP_REPORT_TASK, ['id' => $this->id]);
        }
    }
    /**
     * 推送到搜索引擎
     * @param type $changedAttributes
     */
    public function pushSearch($changedAttributes)
    {
        //@todo 4.地推采集数据,不在乐铺前台显示。
        if (!(Yii::$app instanceof \yii\console\Application)) {
            $session = Yii::$app->getSession();
        } else {
            $session = false;
        }
        try {
            //同步更新索引内容
            //if (isset($changedAttributes['status'])){
            if (!empty($changedAttributes)) {
                if ($this->status <= self::STATUS_CLAIMED) {
                    Search::deleteAll(['id' => $this->id]);
                } else {//电销核实,但是不包括已核状态的
                    if ($this->status >= self::STATUS_PHONED && $this->status != self::STATUS_PHONED_FAILED) {
                        Search::updateById($this->id);
                    }
                }
            }
            if ($session) {
                $session->setFlash('xunsearchInfo', '搜索引擎推送成功.');
            }
        } catch (\Exception $e) {
            $error = $e->getMessage();
            $error = explode('C#', $error);
            $error = explode(',', $error[1]);
            if ($session) {
                $session->setFlash('xunsearchInfo', sprintf('搜索引擎推送失败#%s#%s', $error[0], $e->getCode()));
            } else {
                //error
            }
        }
    }

    /**
     * 任务和主动的命令行推送都会这里取Query对象 可以在这里定义需要哪些With 方便两边保持同一
     * @return \yii\db\ActiveQuery
     */
    public static function getPushQuery()
    {
        return self::find()->with([
            'tag'      => function ($q) {
                $q->select(['shop_id', 'tag_name']);
            },
            'business' => function ($q) {
                $q->select(['shop_id', 'business_type']);
            },
            'areainfo' => function ($q) {
                $q->select(['id', 'name']);
            },
            'roadinfo' => function($q){
                $q->select(['road_id', 'road_name']);
            },
            'video'    => function ($q) {
                $q->select(['img_id']);
            },
            //'hot',
            'panorama',
            'report'
        ]);
    }
}

 

3. MySQL shop表数据同步到Mongodb处理代码

<?php

namespace common\components\asynctsk;
use Yii;
use \bazilio\async\models\AsyncTask;
use console\models\CMShop;
class SyncMongoShopTask extends AsyncTask {
    use AsyncTaskTrait;
    public $id;
    public $data;
    public static $queueName = 'SyncMongoShop';

    public function execute()
    {
        self::output('Executeing :'.var_export($this->id,1));
        if (empty($this->id)){
            $msg = 'Missing of ID in queen: '.self::$queueName;
            self::logError($msg);
            return false;
        }
        if (empty($this->data)){
            $this->data = CMShop::getPushQuery()->where(['id'=>$this->id])->asArray()->all();
        }
        if (empty($this->data) || !is_array($this->data)){
            $msg = 'Missing Data of ID in queen: '.self::$queueName;
            self::logError($msg);
            return false;
        }
        /** @var  $collection \yii\mongodb\collection */
        $collection = Yii::$app->mongodb->getCollection('shop');
        $res=[];
        foreach($this->data as $data){
            //self::output($data['status']);
            $data['id'] = (int)$data['id'];
            if ($data['status'] <= CMShop::STATUS_DELETE) {
                self::output('Delete ..'.$data['id']);
                $res[$data['id']] = $collection->remove(['id' => $data['id']]);
            }else{
                self::output('Update ..'.$data['id']);
                $data = CMShop::format($data);
                $res[$data['id']] = $collection->update(['id' => $data['id']],['$set' => $data], ['upsert' => true]);
            }
        }
        $this->data = null;
        var_export($res);
        return $res;
    }
}

 

插入Redis队列代码,最后一段就是同步Mongodb数据的

    public function afterSave($insert, $changedAttributes) {
        parent::afterSave($insert, $changedAttributes);
        //电销认领
        if ($this->status == BShop::STATUS_CLAIMED)
        {
            BShopExtend::updateClaim($this->id, self::ADMIN_PHONE);
        }
        //商务认领
        if ($this->status == BShop::STATUS_BUSINESS_CLAIMED)
        {
            BShopExtend::updateClaim($this->id, self::ADMIN_COMMERCE);
        }
        //后台添加的商铺
        if ($insert === true)
        {
            ShopSourceUrl::add($this->id, ShopSourceUrl::SOURCE_LEPU);
            //转铺电销新建即认领
            if (Yii::$app->user->identity->group_id == ManageUser::PHONE_GROUP_ID)
            {
                if (BShop::updateAll(['adviser_id' => Yii::$app->user->identity->admin_id], 'id = :id and adviser_id = 0', [':id' => $this->id]))
                {
                    ShopAdviser::add($this->id, $this->tel);
                }
            }
        }

        //分级不是S,A级时取消优选,如果是推荐优选,但分级修改
        if(in_array($this->preference,[11,14,17]) && $this->is_certified == 1){
           if($data = BShopOptimization::findOne(['shop_id' => $this->id])){
               $data->status = BShopOptimization::STATUS_DELETE;
               $data->save();
           }
        }
        //记录分级操作人和时间
        if (isset($changedAttributes['preference'])) {
            if( $extend = BShopExtend::findOne($this->id)){
                $extend->phone_recommend_time = CURRENT_TIMESTAMP;
                $extend->recommend_id =  Yii::$app->user->identity->admin_id;
                $extend->save();
            } else {
                $extend = new BShopExtend();
                $extend->shop_id = $this->id;
                $extend->phone_recommend_time = CURRENT_TIMESTAMP;
                $extend->recommend_id =  Yii::$app->user->identity->admin_id;
                $extend->save();
            }
        }
        //需要实勘记录到shop_extend表
        if ($this->is_verify == 1)
        {
            BShopExtend::updateAll(['is_verify' => 1], 'shop_id = :shop_id and is_verify = 0', [':shop_id' => $this->id]);
        }
        //不实勘
        else if (empty($this->is_verify))
        {
            BShopExtend::updateAll(['is_verify' => 0], 'shop_id = :shop_id and is_verify = 1', [':shop_id' => $this->id]);
        }
        //记录操作日志
        \backend\models\ShopOplog::add($this ,$insert ? '新建' : $changedAttributes);
        //记录字段变动历史记录
        BShopFieldHistory::add($this, $changedAttributes);
        //搜索引擎推送
        $this->pushSearch($changedAttributes);
        //同步mongodb数据
        \common\helper\TaskHelper::put(\common\helper\TaskHelper::SYNC_MONGO_SHOP_TASK,['id' => $this->id]);

        return true;
    }

 

  • «
  • »
  • 作者:
    除非注明,本文原创:OpenFree,专注于IT互联网,欢迎转载!转载请以链接形式注明本文地址,谢谢。
    原文链接:http://www.it163.org/post/代码总结

    发表评论

    电子邮件地址不会被公开。 必填项已用*标注


    您可以使用这些HTML标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>