441 lines
14 KiB
PHP
441 lines
14 KiB
PHP
<?php
|
||
|
||
/**
|
||
* 用户
|
||
*/
|
||
class Model_Customer_Customer
|
||
{
|
||
/**
|
||
* 禁止A仓交易,1:是;2:否
|
||
*/
|
||
const DISABLE_A_NO = 2;
|
||
|
||
const TYPE_2 = 2;
|
||
|
||
private $_customerTable;
|
||
private $_customerDeviceTable;
|
||
private $_customerDeviceLogTable;
|
||
|
||
public $db;
|
||
|
||
public function __construct()
|
||
{
|
||
$this->db = \system\engine\Registry::get('db');
|
||
|
||
$config = \system\engine\Registry::get('config');
|
||
|
||
$this->_customerTable = $config->database->prefix
|
||
. 'customer';
|
||
$this->_customerDeviceTable = $config->database->prefix
|
||
. 'customer_device';
|
||
$this->_customerDeviceLogTable = $config->database->prefix
|
||
. 'customer_device_log';
|
||
}
|
||
|
||
public function insertCustomer($data)
|
||
{
|
||
$this->db->insert($this->_customerTable, $data);
|
||
|
||
$customer_id = $this->db->lastInsertId();
|
||
|
||
return $customer_id;
|
||
}
|
||
|
||
public function updateCustomer($customer_id, $data)
|
||
{
|
||
$where = $this->db->quoteInto('customer_id = ?', $customer_id);
|
||
|
||
$this->db->update($this->_customerTable, $data, $where);
|
||
}
|
||
|
||
public function deleteCustomer($customer_id)
|
||
{
|
||
$where = $this->db->quoteInto('customer_id = ?', $customer_id);
|
||
|
||
$this->db->delete($this->_customerTable, $where);
|
||
}
|
||
|
||
public function getCustomer($customer_id)
|
||
{
|
||
return $this->db->fetchRow("SELECT * FROM " . $this->_customerTable . ' WHERE status = 1 AND customer_id = ' . $this->db->quote($customer_id));
|
||
}
|
||
|
||
public function insertCustomerDevice($data)
|
||
{
|
||
$this->db->insert($this->_customerDeviceTable, $data);
|
||
}
|
||
|
||
public function insertCustomerDeviceLog($data)
|
||
{
|
||
$this->db->insert($this->_customerDeviceLogTable, $data);
|
||
}
|
||
|
||
public function checkTelephoneExists($telephone)
|
||
{
|
||
$result = $this->db->fetchOne('SELECT customer_id FROM ' . $this->_customerTable
|
||
. ' WHERE telephone = ' . $this->db->quote($telephone));
|
||
|
||
if ($result) {
|
||
return TRUE;
|
||
} else {
|
||
return FALSE;
|
||
}
|
||
}
|
||
|
||
public function checkEmailExists($email)
|
||
{
|
||
$result = $this->db->fetchOne('SELECT customer_id FROM ' . $this->_customerTable
|
||
. ' WHERE email = ' . $this->db->quote($email));
|
||
|
||
if ($result) {
|
||
return TRUE;
|
||
} else {
|
||
return FALSE;
|
||
}
|
||
}
|
||
|
||
public function getCustomerByCode($code)
|
||
{
|
||
return $this->db->fetchRow('SELECT * FROM ' . $this->_customerTable . ' WHERE status = 1 and code = ' . $this->db->quote($code));
|
||
}
|
||
|
||
public function getCustomerByTelephone($telephone)
|
||
{
|
||
return $this->db->fetchRow('SELECT * FROM ' . $this->_customerTable .
|
||
' WHERE status = 1 AND telephone = ' . $this->db->quote($telephone) .
|
||
' ORDER BY customer_id');
|
||
}
|
||
|
||
public function lockTable()
|
||
{
|
||
$this->db->query('LOCK TABLE ' . $this->_customerTable . ' WRITE');
|
||
}
|
||
|
||
public function unLockTable()
|
||
{
|
||
$this->db->query('UNLOCK TABLES');
|
||
}
|
||
|
||
/*
|
||
* 后台使用的方法
|
||
*/
|
||
public function getTotalCustomersForBackEnd($data)
|
||
{
|
||
$sql = 'SELECT COUNT(*) AS total FROM ' . $this->_customerTable;
|
||
|
||
$implode = array();
|
||
|
||
if (!empty($data['filter_type'])) {
|
||
$implode[] = '`type` = ' . $this->db->quote($data['filter_type']);
|
||
}
|
||
|
||
if (!empty($data['filter_realname'])) {
|
||
$implode[] = '`realname` = ' . $this->db->quote($data['filter_realname']);
|
||
}
|
||
|
||
if (!empty($data['filter_code'])) {
|
||
$implode[] = 'LOCATE(' . $this->db->quote($data['filter_code']) . ', code) > 0';
|
||
}
|
||
|
||
if (!empty($data['filter_telephone'])) {
|
||
$implode[] = 'LOCATE(' . $this->db->quote($data['filter_telephone']) . ', telephone) > 0';
|
||
}
|
||
|
||
if (!empty($data['filter_email'])) {
|
||
$implode[] = 'LOCATE(' . $this->db->quote($data['filter_email']) . ', email) > 0';
|
||
}
|
||
|
||
if (!empty($data['filter_nickname'])) {
|
||
$implode[] = 'LOCATE(' . $this->db->quote($data['filter_nickname']) . ', nickname) > 0';
|
||
}
|
||
|
||
if (!empty($data['filter_name'])) {
|
||
$implode[] = 'LOCATE(' . $this->db->quote($data['filter_name']) . ', name) > 0';
|
||
}
|
||
|
||
if (!empty($data['filter_status'])) {
|
||
$implode[] = '`status` = ' . $this->db->quote($data['filter_status']);
|
||
}
|
||
|
||
if (!empty($data['filter_disable_a'])) {
|
||
$implode[] = '`disable_a` = ' . $this->db->quote($data['filter_disable_a']);
|
||
}
|
||
|
||
if (!empty($data['filter_parent_id'])) {
|
||
$implode[] = '`parent_id` = ' . $this->db->quote($data['filter_parent_id']);
|
||
}
|
||
|
||
if (!empty($data['filter_parent_id'])) {
|
||
$implode[] = '`parent_id` = ' . $this->db->quote($data['filter_parent_id']);
|
||
}
|
||
|
||
if (!empty($data['filter_parents']) && count($data['filter_parents']) != 0) {
|
||
$implode[] = '`parent_id` IN (' . implode(',', $data['filter_parents']) . ')';
|
||
}
|
||
|
||
if (!empty($data['filter_insert_date'])) {
|
||
$implode[] = 'DATE(`insert_date`) = ' . $this->db->quote($data['filter_insert_date']);
|
||
}
|
||
|
||
if (count($implode) != 0) {
|
||
$sql .= ' WHERE ' . implode(' AND ', $implode);
|
||
}
|
||
|
||
return $this->db->fetchOne($sql);
|
||
}
|
||
|
||
public function getCustomersForBackEnd($data)
|
||
{
|
||
$sql = 'SELECT * FROM ' . $this->_customerTable;
|
||
|
||
$implode = array();
|
||
|
||
if (!empty($data['filter_type'])) {
|
||
$implode[] = '`type` = ' . $this->db->quote($data['filter_type']);
|
||
}
|
||
|
||
if (!empty($data['filter_realname'])) {
|
||
$implode[] = '`realname` = ' . $this->db->quote($data['filter_realname']);
|
||
}
|
||
|
||
if (!empty($data['filter_code'])) {
|
||
$implode[] = 'LOCATE(' . $this->db->quote($data['filter_code']) . ', code) > 0';
|
||
}
|
||
|
||
if (!empty($data['filter_telephone'])) {
|
||
$implode[] = 'LOCATE(' . $this->db->quote($data['filter_telephone']) . ', telephone) > 0';
|
||
}
|
||
|
||
if (!empty($data['filter_email'])) {
|
||
$implode[] = 'LOCATE(' . $this->db->quote($data['filter_email']) . ', email) > 0';
|
||
}
|
||
|
||
if (!empty($data['filter_nickname'])) {
|
||
$implode[] = 'LOCATE(' . $this->db->quote($data['filter_nickname']) . ', nickname) > 0';
|
||
}
|
||
|
||
if (!empty($data['filter_name'])) {
|
||
$implode[] = 'LOCATE(' . $this->db->quote($data['filter_name']) . ', name) > 0';
|
||
}
|
||
|
||
if (!empty($data['filter_status'])) {
|
||
$implode[] = '`status` = ' . $this->db->quote($data['filter_status']);
|
||
}
|
||
|
||
if (!empty($data['filter_disable_a'])) {
|
||
$implode[] = '`disable_a` = ' . $this->db->quote($data['filter_disable_a']);
|
||
}
|
||
|
||
if (!empty($data['filter_parent_id'])) {
|
||
$implode[] = '`parent_id` = ' . $this->db->quote($data['filter_parent_id']);
|
||
}
|
||
|
||
if (!empty($data['filter_parent_id'])) {
|
||
$implode[] = '`parent_id` = ' . $this->db->quote($data['filter_parent_id']);
|
||
}
|
||
|
||
if (!empty($data['filter_parents']) && count($data['filter_parents']) != 0) {
|
||
$implode[] = '`parent_id` IN (' . implode(',', $data['filter_parents']) . ')';
|
||
}
|
||
|
||
if (!empty($data['filter_insert_date'])) {
|
||
$implode[] = 'DATE(`insert_date`) = ' . $this->db->quote($data['filter_insert_date']);
|
||
}
|
||
|
||
if (count($implode) != 0) {
|
||
$sql .= ' WHERE ' . implode(' AND ', $implode);
|
||
}
|
||
|
||
if (!empty($data['sort'])) {
|
||
$sql .= ' ORDER BY ' . $data['sort'] . ' ';
|
||
|
||
if (!empty($data['order'])) {
|
||
$sql .= $data['order'];
|
||
} else {
|
||
$sql .= 'DESC';
|
||
}
|
||
}
|
||
|
||
if (!empty($data['start']) || !empty($data['limit'])) {
|
||
$sql .= ' LIMIT';
|
||
|
||
if (!empty($data['start'])) {
|
||
$sql .= ' ' . $data['start'] . ',';
|
||
}
|
||
|
||
if (!empty($data['limit'])) {
|
||
$sql .= ' ' . $data['limit'];
|
||
} else {
|
||
$sql .= ' 10';
|
||
}
|
||
}
|
||
|
||
return $this->db->fetchAll($sql);
|
||
}
|
||
|
||
public function getCustomerByTelephoneForBackEnd($telephone)
|
||
{
|
||
return $this->db->fetchRow('SELECT * FROM ' . $this->_customerTable . ' WHERE telephone = ' . $this->db->quote($telephone));
|
||
}
|
||
|
||
public function getCustomerByEmailForBackEnd($email)
|
||
{
|
||
return $this->db->fetchRow('SELECT * FROM ' . $this->_customerTable . ' WHERE email = ' . $this->db->quote($email));
|
||
}
|
||
|
||
public function getCustomerForBackEnd($customer_id)
|
||
{
|
||
$sql = "SELECT * FROM " . $this->_customerTable . ' WHERE customer_id = '
|
||
. $this->db->quote($customer_id);
|
||
|
||
return $this->db->fetchRow($sql);
|
||
}
|
||
|
||
public function getCustomersForBackEndForAutocomplete($data)
|
||
{
|
||
$sql = 'SELECT * FROM ' . $this->_customerTable . ' WHERE 1';
|
||
|
||
if (!empty($data['filter_parents']) && count($data['filter_parents']) != 0) {
|
||
$sql .= ' AND `customer_id` IN (' . implode(',', $data['filter_parents']) . ')';
|
||
}
|
||
|
||
$implode = array();
|
||
|
||
if (!empty($data['filter_telephone'])) {
|
||
$implode[] = 'LOCATE(' . $this->db->quote($data['filter_telephone']) . ', telephone) > 0';
|
||
}
|
||
|
||
if (!empty($data['filter_email'])) {
|
||
$implode[] = 'LOCATE(' . $this->db->quote($data['filter_email']) . ', email) > 0';
|
||
}
|
||
|
||
if (!empty($data['filter_nickname'])) {
|
||
$implode[] = 'LOCATE(' . $this->db->quote($data['filter_nickname']) . ', nickname) > 0';
|
||
}
|
||
|
||
if (!empty($data['filter_name'])) {
|
||
$implode[] = 'LOCATE(' . $this->db->quote($data['filter_name']) . ', name) > 0';
|
||
}
|
||
|
||
if (!empty($data['filter_code'])) {
|
||
$implode[] = 'LOCATE(' . $this->db->quote($data['filter_code']) . ', code) > 0';
|
||
}
|
||
|
||
if (count($implode) != 0) {
|
||
$sql .= ' AND (' . implode(' OR ', $implode) . ')';
|
||
}
|
||
|
||
$sql .= ' ORDER BY';
|
||
|
||
if (!empty($data['sort'])) {
|
||
$sql .= ' ' . $data['sort'];
|
||
|
||
if (!empty($data['order'])) {
|
||
$sql .= ' ' . $data['order'];
|
||
}
|
||
} else {
|
||
$sql .= ' customer_id DESC';
|
||
}
|
||
|
||
if (!empty($data['start']) || !empty($data['limit'])) {
|
||
$sql .= ' LIMIT';
|
||
|
||
if (!empty($data['start'])) {
|
||
$sql .= ' ' . $data['start'] . ',';
|
||
}
|
||
|
||
if (!empty($data['limit'])) {
|
||
$sql .= ' ' . $data['limit'];
|
||
} else {
|
||
$sql .= ' 10';
|
||
}
|
||
}
|
||
|
||
return $this->db->fetchAll($sql);
|
||
}
|
||
|
||
public function getFullParents($parent_id)
|
||
{
|
||
$parents = array();
|
||
|
||
$customer = $this->getCustomer($parent_id);
|
||
|
||
if ($customer) {
|
||
$parents[] = array(
|
||
'customer_id' => $customer['customer_id'],
|
||
'telephone' => $customer['telephone'],
|
||
'name' => $customer['name'],
|
||
'nickname' => $customer['nickname'],
|
||
'email' => $customer['email'],
|
||
);
|
||
|
||
if (!empty($customer['parent_id'])) {
|
||
$temp = $this->getFullParents($customer['parent_id']);
|
||
|
||
if (count($temp) != 0) {
|
||
$parents = array_merge($parents, $temp);
|
||
}
|
||
}
|
||
}
|
||
|
||
return $parents;
|
||
}
|
||
|
||
public function getFullParentsFromSelf($customer_id)
|
||
{
|
||
$customer = $this->getCustomer($customer_id);
|
||
|
||
if ($customer && !empty($customer['parent_id'])) {
|
||
$parents = $this->getFullParents($customer['parent_id']);
|
||
} else {
|
||
$parents = array();
|
||
}
|
||
|
||
return $parents;
|
||
}
|
||
|
||
|
||
public function getFullChildren($parent_id)
|
||
{
|
||
$temp = array();
|
||
|
||
$children = $this->getCustomersForBackEnd(array(
|
||
'filter_parent_id' => $parent_id
|
||
));
|
||
|
||
if (count($children) != 0) {
|
||
foreach ($children as $child) {
|
||
$temp[] = array(
|
||
'customer_id' => $child['customer_id'],
|
||
'telephone' => $child['telephone'],
|
||
'name' => $child['name'],
|
||
'nickname' => $child['nickname'],
|
||
);
|
||
|
||
$temp = array_merge($temp, $this->getFullChildren($child['customer_id']));
|
||
}
|
||
}
|
||
|
||
return $temp;
|
||
}
|
||
|
||
public function getFullChildrenWithSelf($customer_id)
|
||
{
|
||
$customer = $this->getCustomer($customer_id);
|
||
|
||
$parents = array();
|
||
|
||
if ($customer) {
|
||
$parents[] = array(
|
||
'customer_id' => $customer['customer_id'],
|
||
'telephone' => $customer['telephone'],
|
||
'name' => $customer['name'],
|
||
'nickname' => $customer['nickname'],
|
||
);
|
||
|
||
$parents = array_merge($parents, $this->getFullChildren($customer['customer_id']));
|
||
}
|
||
|
||
return $parents;
|
||
}
|
||
} |