<?php namespace Admin\Model; use Think\Model; /** * 文档基础模型 */ class BusinessAffairsModel extends Model{ /* 自动验证规则 */ protected $_validate = array( array('account', '6,16', '商务专员账号长度为6-16个字符', self::EXISTS_VALIDATE, 'length'), array('account','','账号被占用',0,'unique',1), /* 验证密码 */ array('password','require', "请输入登录密码", self::EXISTS_VALIDATE, 'regex'), //密码长度不合法 array('password','6,30', "密码长度不合法", self::EXISTS_VALIDATE, 'length'), //密码长度不合法 array('sw_name', 'require', '请输入姓名', self::EXISTS_VALIDATE, 'regex'), array('sw_name', '1,16', '姓名长度为1-16个字符', self::EXISTS_VALIDATE, 'length'), array('phone','require','请输入联系电话','0','regex',1), array('phone','/^1[3|4|5|7|8][0-9]\d{4,8}$/','联系电话错误!','0','regex',1), ); /* 自动完成规则 */ protected $_auto = array( array('password', 'think_ucenter_md5', self::MODEL_BOTH, 'function', UC_AUTH_KEY), array('create_time', 'getCreateTime', self::MODEL_INSERT,'callback'), array('inferiors', 0, self::MODEL_INSERT), array('promote_id', 0, self::MODEL_INSERT), ); /** * 构造函数 * @param string $name 模型名称 * @param string $tablePrefix 表前缀 * @param mixed $connection 数据库连接信息 */ public function __construct($name = '', $tablePrefix = '', $connection = '') { /* 设置默认的表前缀 */ $this->tablePrefix ='tab_'; /* 执行构造方法 */ parent::__construct($name, $tablePrefix, $connection); } public function sw_add($add_data=array()){ $data = array( 'account' => $add_data['account'], 'password' => $add_data['password'], 'sw_name' => $add_data['sw_name'], 'phone' => $add_data['phone'], 'qq' => $add_data['qq'], 'inferiors' => $add_data['inferiors'], 'status' => $add_data['status'], 'create_time' => NOW_TIME, ); /* 添加用户 */ $result = $this->create($data); if($result){ $uid = $this->add(); return $uid ? $uid : 0; //0-未知错误,大于0-注册成功 } else { return $this->getError(); //错误详情见自动验证注释 } } public function add_child($id,$promote_id){ $data = $this->field("promote_id")->find($id); if($data['promote_id'] == 0 || $data['promote_id']=="" || $data["promote_id"] == null){ $field = $promote_id; }else{ $field = $data['promote_id'].",".$promote_id; $this->where("id=".$id)->setInc("inferiors",1); } $this->where("id=".$id)->setField('promote_id',$field); } /** *修改商务专员旗下推广员数量 */ public function update_child($id=0,$new_id=null,$promote_id=null){ $p_ids = array(); $data_child = D('Promote')->where("chain like '%/{$promote_id}/%'")->getField('id',true); if(empty($data_child)){ $p_ids[0] = $promote_id; }else{ array_unshift($data_child,$promote_id); $p_ids = $data_child; } if($id != 0){ $ba_old_ids = $this->where('id='.$id)->getField('promote_id'); $ba_old_ids = explode(",",$ba_old_ids); $ba_old_ids = array_diff($ba_old_ids,$p_ids); if(empty($ba_old_ids)){ $ba_old_ids[0] = 0; } $this->where("id=".$id)->setField('promote_id', implode(",", $ba_old_ids)); $this->where("id=".$id)->setDec("inferiors",count($p_ids)); } $ba_new_ids = $this->where("id=".$new_id)->getField('promote_id'); if(empty($ba_new_ids)){ $ba_new_ids = $p_ids; }else{ $ba_new_ids = explode(",", $ba_new_ids); array_push($ba_new_ids,$p_ids); } D('Promote')->where("chain like '%/{$promote_id}/%'")->setField('ba_id',$new_id); $this->where("id=".$new_id)->setField('promote_id', implode(",", $ba_new_ids)); $this->where("id=".$new_id)->setInc("inferiors",count($p_ids)); } /** *删除商务专员旗下推广员 */ public function del_child($ba_id = 0,$promote_ids = 0){ //查找所有父级和子级id array_unique $pMap['parent_id'] = array("IN",$promote_ids); $data_child = D('Promote')->where($pMap)->getField('id',true); if(empty($data_child)){ $p_ids = $promote_ids; }else{ $p_ids = array_merge($data_child,$promote_ids); $p_ids = array_unique($p_ids); } $num = count($p_ids); $ba_promote_id_new = $this->where('id = '.$ba_id)->getField('promote_id'); $ba_promote_id_new = explode(",", $ba_promote_id_new); $ba_promote_id_new = array_diff($ba_promote_id_new,$p_ids); if(empty($ba_promote_id_new)){ $ba_promote_id_new[0] = 0; } $setPromoteMap['id'] = array('IN',$p_ids); $promote = D('Promote')->where($setPromoteMap)->setField(array('ba_id'=>0)); $baMap['id'] = $ba_id; if($ba_promote_id_new[0] == 0){ $ba = $this->where($baMap)->setField('inferiors',0); }else{ $ba = $this->where($baMap)->setDec('inferiors',$num); } $ba_p = $this->where($baMap)->setField(array('promote_id'=>implode(",", $ba_promote_id_new))); } /** * 创建时间不写则取当前时间 * @return int 时间戳 * @author huajie <banhuajie@163.com> */ protected function getCreateTime(){ $create_time = I('post.create_time'); return $create_time?strtotime($create_time):NOW_TIME; } }