public function saveAgent()
 {
     $user_id = I('user_id');
     $mobile = I('phone');
     $refer_mobile = I('refer_mobile');
     $operator_share_ratio = I('operator_share_ratio') * 100;
     $operator_sale_limit = I('operator_sale_limit');
     $is_weight = I('is_weight');
     if (!empty($refer_mobile)) {
         $refer_user_id = M('user')->where(array('mobile' => array('eq', $refer_mobile)))->getField('user_id');
         if (!$refer_user_id) {
             $this->ajaxReturn(array('status' => 0, 'message' => '推荐人手机号码不存在!'));
         }
     }
     if ($operator_share_ratio > 10000) {
         $this->ajaxReturn(array('status' => 0, 'message' => '分成不能大于100%'));
     }
     $type = '';
     if (empty($mobile)) {
         $this->ajaxReturn(array('status' => 0, 'message' => '手机号码不能为空'));
     } else {
         if (empty($user_id)) {
             $type = 'add';
             $sub_where['username'] = array('eq', $mobile);
             $sub_where['mobile'] = array('eq', $mobile);
             $sub_where['_logic'] = 'or';
             $where_is['_complex'] = $sub_where;
             $username = M('user')->where($where_is)->field('mobile,is_operator,user_type,user_id,refer_user_id')->find();
             if (!empty($username['mobile'])) {
                 if ($username['is_operator']) {
                     $this->ajaxReturn(array('status' => 0, 'message' => '运营商已经存在'));
                 } else {
                     $user_id = $username['user_id'];
                     $type = 'save';
                 }
                 if (empty($refer_user_id)) {
                     $refer_user_id = $username['refer_user_id'];
                 }
             }
         } else {
             $type = 'save';
         }
     }
     $agentName = I('agentName');
     if (empty($agentName)) {
         $this->ajaxReturn(array('status' => 0, 'message' => '运营商姓名不能为空'));
     } else {
         //echo mb_strlen(trim($agentName));exit;
         if (mb_strlen(trim($agentName)) > 13) {
             //四个中文
             $this->ajaxReturn(array('status' => 0, 'message' => '运营商姓名长度太长了!'));
         }
     }
     $IDNumber = I('IDNumber');
     if (empty($IDNumber)) {
         $this->ajaxReturn(array('status' => 0, 'message' => '身份证不能为空'));
     } else {
         if (!isCreditNo($IDNumber)) {
             $this->ajaxReturn(array('status' => 0, 'message' => '身份证错误!'));
         }
     }
     $provinces = I('provinces');
     if (empty($provinces)) {
         $this->ajaxReturn(array('status' => 0, 'message' => '省份不能为空'));
     }
     $city = I('city');
     if (empty($city)) {
         $this->ajaxReturn(array('status' => 0, 'message' => '城市不能为空'));
     }
     $ID_z = I('ID_z');
     $ID_f = I('ID_f');
     if (empty($ID_z)) {
         $this->ajaxReturn(array('status' => 0, 'message' => '请上传身份证正面照'));
     }
     if (empty($ID_f)) {
         $this->ajaxReturn(array('status' => 0, 'message' => '请上传身份证反面照'));
     }
     $contract_valid = I('contract_valid');
     if (empty($contract_valid)) {
         $this->ajaxReturn(array('status' => 0, 'message' => '生效日/截止日不能为空!'));
     } else {
         $contract_valid = explode('-', I('contract_valid'));
     }
     $contract_sign_date = I('contract_sign_date');
     $info = M('user')->where(array('user_id' => array('eq', $user_id)))->find();
     if ($type == 'add') {
         $data_ID_z = array('biz_type' => '运营商', 'file_url' => $ID_z, 'upload_user_id' => $this->aid, 'upload_user_type' => 2);
         $identity_card_pic1 = M('attachment')->add($data_ID_z);
         $data_ID_f = array('biz_type' => '运营商', 'file_url' => $ID_f, 'upload_user_id' => $this->aid, 'upload_user_type' => 2);
         $identity_card_pic2 = M('attachment')->add($data_ID_f);
     } else {
         if (empty($info['identity_card_pic1']) || empty($info['identity_card_pic2'])) {
             $data_ID_z = array('biz_type' => '运营商', 'file_url' => $ID_z, 'upload_user_id' => $this->aid, 'upload_user_type' => 2);
             $identity_card_pic1 = M('attachment')->add($data_ID_z);
             $data_ID_f = array('biz_type' => '运营商', 'file_url' => $ID_f, 'upload_user_id' => $this->aid, 'upload_user_type' => 2);
             $identity_card_pic2 = M('attachment')->add($data_ID_f);
         } else {
             M('attachment')->where("`attachement_id`='{$info['identity_card_pic1']}'")->save(array('file_url' => $ID_z));
             M('attachment')->where("`attachement_id`='{$info['identity_card_pic2']}'")->save(array('file_url' => $ID_f));
             $identity_card_pic1 = I('identity_card_pic1');
             $identity_card_pic2 = I('identity_card_pic2');
         }
     }
     $district = I('district');
     $street = I('street');
     //if($type == 'add'){
     /*****************判断是否地区被代理start***********************/
     /*$is_Agent = $this->home_Agent($provinces,$city,$district,$street,$user_id);
     
             if($is_Agent){
                 $this->ajaxReturn(array('status'=>0,'message'=>'该地区已经被代理!'));
             }*/
     /*****************判断是否地区被代理end***********************/
     // }
     $agentCommission = I('agentCommission');
     //代理佣金
     $address = I('address');
     //通讯地址
     if (mb_strlen(trim($address)) > 90) {
         $this->ajaxReturn(array('status' => 0, 'message' => '通讯地址长度太长了!'));
     }
     $data = array('username' => $mobile, 'mobile' => $mobile, 'true_name' => $agentName, 'identity_card_no' => $IDNumber, 'slotting_fee' => $agentCommission, 'resident_town' => $address, 'identity_card_pic1' => $identity_card_pic1, 'identity_card_pic2' => $identity_card_pic2, 'contract_valid_from' => str_replace('/', '-', $contract_valid[0]), 'contract_valid_to' => str_replace('/', '-', $contract_valid[1]), 'contract_sign_date' => $contract_sign_date, 'refer_user_id' => $refer_user_id);
     $agent_type = 50;
     $user_role = '运营商';
     if ($type == 'add') {
         $randStr = str_shuffle('1234567890');
         $passd = substr($randStr, 0, 6);
         $data['password'] = md5($passd);
         //默认手机号码
         $data['user_id'] = rand_user();
         $data['register_type'] = 4;
         $data['is_operator'] = 1;
         if (M('user')->create($data)) {
             $result = M('user')->add($data);
         } else {
             $this->ajaxReturn(array('status' => 0, 'message' => M()->getDbError()));
         }
         /***********保存信息表******agent*********/
         $data_agent = array('user_id' => $result, 'agent_type' => $agent_type, 'province_id' => $provinces, 'city_id' => $city, 'district_id' => $district, 'town_id' => $street, 'sign_time' => $contract_sign_date, 'agent_valid_from' => str_replace('/', '-', $contract_valid[0]), 'agent_valid_to' => str_replace('/', '-', $contract_valid[1]), 'create_time' => date('Y-m-d H:i:s', time()), 'operator_share_ratio' => $operator_share_ratio, 'operator_sale_limit' => $operator_sale_limit, 'is_weight' => $is_weight);
         if (M('agent')->create($data_agent)) {
             $back = M('agent')->add($data_agent);
         } else {
             $this->ajaxReturn(array('status' => 0, 'message' => M()->getDbError()));
         }
         if ($result && $back) {
             self::addMyAccount($result, $agentName, $mobile, $user_role);
             notification('userInfoChange', array('userId' => $result));
             file_get_contents(C('APP_SERVER_URL') . "interface/serverCommunicate/sendAdminMobileSms?userName={$mobile}&mobile={$mobile}&code={$passd}&usage=addAgent");
             $this->ajaxReturn(array('status' => 1, 'message' => '保存成功'));
         } else {
             $this->ajaxReturn(array('status' => 0, 'message' => '保存失败!'));
         }
     } else {
         $data['is_operator'] = 1;
         $result = M('user')->where(array('user_id' => array('eq', $user_id)))->save($data);
         /***********保存信息表******agent*********/
         $where_agent = array('user_id' => array('eq', $user_id));
         $i_agent = M('agent')->where($where_agent)->getField('user_id');
         if ($i_agent) {
             $data_agent = array('agent_type' => $agent_type, 'province_id' => $provinces, 'city_id' => $city, 'district_id' => $district, 'town_id' => $street, 'sign_time' => $contract_sign_date, 'agent_valid_from' => str_replace('/', '-', $contract_valid[0]), 'agent_valid_to' => str_replace('/', '-', $contract_valid[1]), 'operator_share_ratio' => $operator_share_ratio, 'operator_sale_limit' => $operator_sale_limit, 'is_weight' => $is_weight);
             M('agent')->where($where_agent)->save($data_agent);
         } else {
             $data_agent = array('user_id' => $user_id, 'agent_type' => $agent_type, 'province_id' => $provinces, 'city_id' => $city, 'district_id' => $district, 'town_id' => $street, 'sign_time' => $contract_sign_date, 'agent_valid_from' => str_replace('/', '-', $contract_valid[0]), 'agent_valid_to' => str_replace('/', '-', $contract_valid[1]), 'create_time' => date('Y-m-d H:i:s', time()), 'operator_share_ratio' => $operator_share_ratio, 'operator_sale_limit' => $operator_sale_limit, 'is_weight' => $is_weight);
             $result = M('agent')->add($data_agent);
         }
         if ($result !== false) {
             self::addMyAccount($user_id, $agentName, $mobile, $user_role);
             notification('userInfoChange', array('userId' => $user_id));
             $this->ajaxReturn(array('status' => 1, 'message' => '保存成功'));
         } else {
             $this->ajaxReturn(array('status' => 0, 'message' => '保存成功'));
         }
     }
 }
 public function saveAgency()
 {
     $user_id = I('user_id');
     $mobile = I('phone');
     $refer_mobile = I('refer_mobile');
     if (!empty($refer_mobile)) {
         $refer_user_id = M('user')->where(array('mobile' => array('eq', $refer_mobile)))->getField('user_id');
         if (!$refer_user_id) {
             $this->ajaxReturn(array('status' => 0, 'message' => '推荐人手机号码不存在!'));
         }
     }
     $type = '';
     if (empty($mobile)) {
         $this->ajaxReturn(array('status' => 0, 'message' => '手机号码不能为空'));
     } else {
         if (empty($user_id)) {
             $type = 'add';
             $sub_where['username'] = array('eq', $mobile);
             $sub_where['mobile'] = array('eq', $mobile);
             $sub_where['_logic'] = 'or';
             $where_is['_complex'] = $sub_where;
             $username = M('user')->where($where_is)->field('mobile,user_type,user_id,refer_user_id')->find();
             if (!empty($username)) {
                 if ($username['user_type'] == 20) {
                     $this->ajaxReturn(array('status' => 0, 'message' => '经销商已经存在'));
                 }
                 if ($username['user_type'] == 0) {
                     $user_id = $username['user_id'];
                     $type = 'save';
                 } else {
                     $this->ajaxReturn(array('status' => 0, 'message' => '手机号码已经存在'));
                 }
                 if (empty($refer_user_id)) {
                     $refer_user_id = $username['refer_user_id'];
                 }
             }
         } else {
             $type = 'save';
         }
     }
     $agentName = I('agentName');
     if (empty($agentName)) {
         $this->ajaxReturn(array('status' => 0, 'message' => '经销商姓名不能为空'));
     } else {
         if (mb_strlen(trim($agentName)) > 13) {
             //四个中文
             $this->ajaxReturn(array('status' => 0, 'message' => '经销商姓名长度太长了!'));
         }
     }
     $IDNumber = I('IDNumber');
     if (empty($IDNumber)) {
         $this->ajaxReturn(array('status' => 0, 'message' => '身份证不能为空'));
     } else {
         if (!isCreditNo($IDNumber)) {
             $this->ajaxReturn(array('status' => 0, 'message' => '身份证错误!'));
         }
     }
     $ID_z = I('ID_z');
     $ID_f = I('ID_f');
     if (empty($ID_z)) {
         $this->ajaxReturn(array('status' => 0, 'message' => '请上传身份证正面照'));
     }
     if (empty($ID_f)) {
         $this->ajaxReturn(array('status' => 0, 'message' => '请上传身份证反面照'));
     }
     $contract_valid = I('contract_valid');
     if (empty($contract_valid)) {
         $this->ajaxReturn(array('status' => 0, 'message' => '生效日/截止日不能为空!'));
     } else {
         $contract_valid = explode('-', I('contract_valid'));
     }
     $contract_sign_date = I('contract_sign_date');
     //print_r($contract_sign_date);exit;
     $info = M('user')->where(array('user_id' => array('eq', $user_id)))->find();
     if ($type == 'add') {
         $data_ID_z = array('biz_type' => '经销商', 'file_url' => $ID_z, 'upload_user_id' => $this->aid, 'upload_user_type' => 2);
         $identity_card_pic1 = M('attachment')->add($data_ID_z);
         $data_ID_f = array('biz_type' => '经销商', 'file_url' => $ID_f, 'upload_user_id' => $this->aid, 'upload_user_type' => 2);
         $identity_card_pic2 = M('attachment')->add($data_ID_f);
     } else {
         if (empty($info['identity_card_pic1']) || empty($info['identity_card_pic2'])) {
             $data_ID_z = array('biz_type' => '经销商', 'file_url' => $ID_z, 'upload_user_id' => $this->aid, 'upload_user_type' => 2);
             $identity_card_pic1 = M('attachment')->add($data_ID_z);
             $data_ID_f = array('biz_type' => '经销商', 'file_url' => $ID_f, 'upload_user_id' => $this->aid, 'upload_user_type' => 2);
             $identity_card_pic2 = M('attachment')->add($data_ID_f);
         } else {
             M('attachment')->where("`attachement_id`='{$info['identity_card_pic1']}'")->save(array('file_url' => $ID_z));
             M('attachment')->where("`attachement_id`='{$info['identity_card_pic2']}'")->save(array('file_url' => $ID_f));
             $identity_card_pic1 = I('identity_card_pic1');
             $identity_card_pic2 = I('identity_card_pic2');
         }
     }
     $district = I('district');
     $street = I('street');
     $agentCommission = I('agentCommission');
     //代理佣金
     $address = I('address');
     //通讯地址
     $data = array('username' => $mobile, 'mobile' => $mobile, 'true_name' => $agentName, 'identity_card_no' => $IDNumber, 'slotting_fee' => $agentCommission, 'resident_town' => $address, 'identity_card_pic1' => $identity_card_pic1, 'identity_card_pic2' => $identity_card_pic2, 'contract_valid_from' => date('Y-m-d', strtotime($contract_valid[0])), 'contract_valid_to' => date('Y-m-d', strtotime($contract_valid[1])), 'contract_sign_date' => $contract_sign_date, 'refer_user_id' => $refer_user_id);
     //print_r($data);exit;
     if ($type == 'add') {
         //设置6位数密码
         $randStr = str_shuffle('1234567890');
         $passd = substr($randStr, 0, 6);
         $data['password'] = md5($passd);
         //默认手机号码
         $data['user_id'] = rand_user();
         $data['user_type'] = 20;
         $data['register_type'] = 4;
         $result = M('user')->add($data);
         if ($result) {
             $data_agent = array('user_id' => $result, 'agent_type' => 20, 'agent_status' => 1, 'sign_time' => $contract_sign_date, 'create_time' => date('Y-m-d H:i:s', time()), 'agent_valid_from' => date('Y-m-d', strtotime($contract_valid[0])), 'agent_valid_to' => date('Y-m-d', strtotime($contract_valid[1])));
             M('agent')->add($data_agent);
             self::addMyAccount($result, $agentName, $mobile, '经销商');
             notification('userInfoChange', array('userId' => $result));
             file_get_contents(C('APP_SERVER_URL') . "interface/serverCommunicate/sendAdminMobileSms?userName={$mobile}&mobile={$mobile}&code={$passd}&usage=addAgency");
             $this->ajaxReturn(array('status' => 1, 'message' => '保存成功'));
         } else {
             $this->ajaxReturn(array('status' => 0, 'message' => '无法添加数据'));
         }
     } else {
         $data['user_type'] = 20;
         $result = M('user')->where(array('user_id' => array('eq', $user_id)))->save($data);
         if ($result !== false) {
             self::addMyAccount($user_id, $agentName, $mobile);
             $where_agent = array('user_id' => array('eq', $user_id));
             $i_agent = M('agent')->where($where_agent)->getField('user_id');
             if ($i_agent) {
                 $data_agent = array('sign_time' => $contract_sign_date, 'create_time' => date('Y-m-d H:i:s', time()), 'agent_valid_from' => date('Y-m-d', strtotime($contract_valid[0])), 'agent_valid_to' => date('Y-m-d', strtotime($contract_valid[1])));
                 M('agent')->where($where_agent)->save($data_agent);
             } else {
                 $data_agent = array('user_id' => $user_id, 'agent_type' => 20, 'agent_status' => 1, 'sign_time' => $contract_sign_date, 'create_time' => date('Y-m-d H:i:s', time()), 'agent_valid_from' => date('Y-m-d', strtotime($contract_valid[0])), 'agent_valid_to' => date('Y-m-d', strtotime($contract_valid[1])));
                 //print_r($data_agent);exit;
                 M('agent')->add($data_agent);
             }
             self::addMyAccount($user_id, $agentName, $mobile, '经销商');
             notification('userInfoChange', array('userId' => $user_id));
             $this->ajaxReturn(array('status' => 1, 'message' => '保存成功'));
         } else {
             $this->ajaxReturn(array('status' => 0, 'message' => '保存成功'));
         }
     }
 }