query($sql); $check_name = $result->fetchAll(PDO::FETCH_ASSOC); if(count($check_name) > 0) { $json['code'] = '1'; $json['info'] = '用户名已存在'; echo json_encode($json); exit(); } //1.获取销售方式详情数组 $sale_array = $this->getSale($sale); //2.获取联系人详情数组 $link_array = $this->getLinkInfo($link_info); //3.判断必填数据是否为空 $judgeData = $this->judgeData($supplier_name, $manage_type, $area_id, $company_name, $sett_type, $sett_frequency, $sales_man); if ($judgeData['code'] == '1') { echo json_encode($judgeData); exit(); } //4.向数据库中中添加数据 $res = $this->addPdoChannel($supplier_name, $area_id, $manage_type, $company_name, $id_card, $sale_array, $sett_type, $sett_frequency, $account_bank, $account_num, $account_name, $link_array, $sales_man); echo json_encode($res); exit(); } //获取销售方式详情数组 function getSale($sale) { if ($sale == '') { $sale_array = array(); return $sale_array; } $sale = explode(",", $sale); $key = $_COOKIE['memcache']; $sale_memcache = get_memcache('ZHANGS_SALE' . $key); $i = 0; foreach ($sale as $item => $value) { $sale_array[$i] = $sale_memcache[$value]; $i++; } return $sale_array; } //获取联系人详情数组 function getLinkInfo($link_info) { if ($link_info == '') { $link_array = array(); return $link_array; } $link_info = explode(",", $link_info); $key = $_COOKIE['memcache']; $link_memcache = get_memcache('ZHANGS_LINK' . $key); $i = 0; foreach ($link_info as $item => $value) { $status = 0; if (empty($link_memcache[$value]['link_name']) || empty($link_memcache[$value]['contact_name'])) { $status = 1; } if ($status == 0) { $link_array[$i] = $link_memcache[$value]; $i++; } } return $link_array; } //检查必填数据是否为空 function judgeData($supplier_name, $manage_type, $area_id, $company_name, $sett_type, $sett_frequency, $sales_man) { if (empty($supplier_name)) { $json['code'] = '1'; $json['info'] = '供应商名称不能为空'; } elseif (empty($area_id)) { $json['code'] = '1'; $json['info'] = '供应商区域不能为空'; } elseif (empty($manage_type)) { $json['code'] = '1'; $json['info'] = '经营性质'; } elseif (empty($company_name)) { $json['code'] = '1'; $json['info'] = '供应商公司不能为空'; } elseif (empty($sett_type)) { $json['code'] = '1'; $json['info'] = '结算方式不能为空'; } elseif (empty($sett_frequency)) { $json['code'] = '1'; $json['info'] = '结算周期名称不能为空'; } elseif (empty($sales_man)) { $json['code'] = '1'; $json['info'] = '业务员不能为空'; } else { $json['code'] = '0'; $json['info'] = '成功'; } return ($json); } //在数据库中添加供应商信息 function addPdoChannel($supplier_name, $area_id, $manage_type, $company_name, $id_card, $sale_array, $sett_type, $sett_frequency, $account_bank, $account_num, $account_name, $link_array, $sales_man) { global $pdo; $user_id = getUserId();//$_COOKIE['user_id']; //运营主体 $main_corp_id_sql = "select main_corp_id from base_user where id = " . $user_id . ' and cancel_flag = 0 limit 1'; $result = $pdo->query($main_corp_id_sql); $main_corp_id = $result->fetchAll(PDO::FETCH_ASSOC); $main_corp_id = $main_corp_id[0]['main_corp_id']; $time = date('Y-m-d H:i:s', time()); try { $pdo->beginTransaction(); //1.添加base_supplier表 $supplier_sql = "INSERT INTO base_supplier ( create_user_id, create_time, supplier_type, supplier_name, area_id, manage_type, company_name, id_card, is_disabled, sett_type, sett_frequency, account_bank, account_num, account_name, sales_man, main_corp_id ) VALUES (" . $user_id . ",'" . $time . "',301,'" . $supplier_name . "'," . $area_id . "," . $manage_type . ",'" . $company_name . "','" . $id_card . "',0," . $sett_type . "," . $sett_frequency . ",'" . $account_bank . "','" . $account_num . "','" . $account_name . "','" . $sales_man . "'," . $main_corp_id .")"; writeLog("INSERT INTO base_supplier:" . $supplier_sql); $result = $pdo->exec($supplier_sql); if (!$result) { throw new Exception("db die"); } $supplier_id = $pdo->lastInsertId(); //2.添加base_supplier_sale表 if (count($sale_array) > 0) { $sale_sql = "INSERT INTO base_supplier_sale ( create_user_id, create_time, supplier_id, prod_supplier_id, product_type, parent_type, sale_type, commision_flag, commision_type, back_commision_type, back_commision_method, back_percent, back_value ) VALUES "; foreach ($sale_array as $key => $v) { $sale_sql_array[] = "(" . $user_id . ",'" . $time . "'," . $supplier_id . "," . $v['prod_supplier_id'] . "," . $v['product_type'] . "," . $v['parent_type'] . "," . $v['sale_type'] . "," . $v['commision_flag'] . "," . $v['commision_type'] . "," . $v['back_commision_type'] . "," . $v['back_commision_method'] . ",'" . $v['back_percent'] . "','" . $v['back_value'] . "')"; } $sale_sql .= implode(",", $sale_sql_array); writeLog("INSERT INTO base_supplier_purchase:" . $sale_sql); $result = $pdo->exec($sale_sql); if (!$result) { throw new Exception("db die"); } } //3.添加base_supplier_link表 if (count($link_array) > 0) { $link_sql = "INSERT INTO base_supplier_link ( create_user_id, create_time, supplier_id, link_name, contact_name, contact_mobile, contact_telphone, fax, email, remark ) VALUES "; foreach ($link_array as $key => $v) { $link_sql_array[] = "(" . $user_id . ",'" . $time . "'," . $supplier_id . ",'" . $v['link_name'] . "','" . $v['contact_name'] . "','" . $v['contact_mobile'] . "','" . $v['contact_telphone'] . "','" . $v['fax'] . "','" . $v['email'] . "','" . $v['remark'] . "')"; } $link_sql .= implode(",", $link_sql_array); writeLog("INSERT INTO base_supplier_link:" . $link_sql); $result = $pdo->exec($link_sql); if (!$result) { throw new Exception("db die"); } } $pdo->commit(); //4.删除缓存 $key = $_COOKIE['memcache']; delete_memcache('ZHANGS_LINK' . $key); delete_memcache('ZHANGS_SALE' . $key); $json['code'] = '0'; $json['info'] = '添加数据成功'; } catch (PDOException $ex) { $pdo->rollBack(); $json['code'] = '1'; $json['info'] = '添加失败'; } return $json; } } $supplier_name = trim($_POST['supplier_name']);//渠道商名称 $area_id = trim($_POST['area_id']);//所属区域市id $manage_type = trim($_POST['manage_type']);//经营性质 1:公司 2:个人 $company_name = trim($_POST['company_name']);//公司名称 $id_card = trim($_POST['id_card']);//身份证号(企业传空) $sale = trim($_POST['sale']);//销售方式 $sett_type = trim($_POST['sett_type']);//结算方式 (授信:275,预付:288,单结:292) $sett_frequency = trim($_POST['sett_frequency']);//结算周期(日结:293,周结:294,月结:295) $account_bank = trim($_POST['account_bank']);//开户银行 $account_num = trim($_POST['account_num']);//银行账号 $account_name = trim($_POST['account_name']);//账号名称 $link_info = trim($_POST['link_info']);//联系人详情 $sales_man = trim($_POST['sales_man']);//业务员 $add_supply = new add_channel(); $add_supply->execAddChannel($supplier_name, $area_id, $manage_type, $company_name, $id_card, $sale, $sett_type, $sett_frequency, $account_bank, $account_num, $account_name, $link_info, $sales_man); ?>