<?php
namespace eapie\source\request\shop;
use eapie\main;
use eapie\error;
class admin_goods_region extends \eapie\source\request\shop {
public function api_add($data = array()){
object(parent::REQUEST_ADMIN)->check(parent::AUTHORITY_GOODS_REGION_ADD);
object(parent::ERROR)->check($data, 'shop_goods_id', parent::TABLE_SHOP_GOODS, array('args'));
object(parent::ERROR)->check($data, 'shop_goods_region_info', parent::TABLE_SHOP_GOODS_REGION, array('args'));
object(parent::ERROR)->check($data, 'shop_goods_region_state', parent::TABLE_SHOP_GOODS_REGION, array('args'));
object(parent::ERROR)->check($data, 'shop_goods_region_sort', parent::TABLE_SHOP_GOODS_REGION, array('args'));
object(parent::ERROR)->check($data, 'shop_goods_region_scope', parent::TABLE_SHOP_GOODS_REGION, array('args'));
$shop_goods_data = object(parent::TABLE_SHOP_GOODS)->find($data['shop_goods_id']);
if( empty($shop_goods_data) ){
throw new error("ID有误,数据不存在");
}
if( !object(parent::REQUEST_ADMIN)->check(parent::AUTHORITY_GOODS_ADMINISTRATOR, true) ){
if( $shop_goods_data['user_id'] != $_SESSION['user_id'] ){
throw new error("权限不足,不能操作非自己添加的数据");
}
}
if( $data['shop_goods_region_scope'] == 3 ){
object(parent::ERROR)->check($data, 'shop_goods_region_province', parent::TABLE_SHOP_GOODS_REGION, array('args'));
object(parent::ERROR)->check($data, 'shop_goods_region_city', parent::TABLE_SHOP_GOODS_REGION, array('args'));
object(parent::ERROR)->check($data, 'shop_goods_region_district', parent::TABLE_SHOP_GOODS_REGION, array('args'));
}else
if( $data['shop_goods_region_scope'] == 2 ){
object(parent::ERROR)->check($data, 'shop_goods_region_province', parent::TABLE_SHOP_GOODS_REGION, array('args'));
object(parent::ERROR)->check($data, 'shop_goods_region_city', parent::TABLE_SHOP_GOODS_REGION, array('args'));
$data['shop_goods_region_district'] = '';
}else
if( $data['shop_goods_region_scope'] == 1 ){
object(parent::ERROR)->check($data, 'shop_goods_region_province', parent::TABLE_SHOP_GOODS_REGION, array('args'));
$data['shop_goods_region_city'] = '';
$data['shop_goods_region_district'] = '';
}
$is_exist = object(parent::TABLE_SHOP_GOODS_REGION)->find_goods_scope_pcd($data['shop_goods_id'], $data['shop_goods_region_province'], $data['shop_goods_region_city'], $data['shop_goods_region_district']);
if( !empty($is_exist) ){
throw new error("该商品所在的销售地区已经存在,请勿重复添加");
}
$whitelist = array(
'shop_goods_id',
'shop_goods_region_scope',
'shop_goods_region_province',
'shop_goods_region_city',
'shop_goods_region_district',
'shop_goods_region_info',
'shop_goods_region_state',
'shop_goods_region_sort',
);
$insert_data = cmd(array($data, $whitelist), 'arr whitelist');
$insert_data['shop_goods_region_id'] = object(parent::TABLE_SHOP_GOODS_REGION)->get_unique_id();
$insert_data['user_id'] = $_SESSION['user_id'];
$insert_data['shop_goods_region_insert_time'] = time();
$insert_data['shop_goods_region_update_time'] = time();
if( object(parent::TABLE_SHOP_GOODS_REGION)->insert($insert_data) ){
object(parent::TABLE_ADMIN_LOG)->insert($data, $insert_data);
return $insert_data['shop_goods_region_id'];
}else{
throw new error("操作失败");
}
}
public function api_edit( $data = array() ){
object(parent::REQUEST_ADMIN)->check(parent::AUTHORITY_GOODS_REGION_EDIT);
object(parent::ERROR)->check($data, 'shop_goods_region_id', parent::TABLE_SHOP_GOODS_REGION, array('args'));
if( isset($data['shop_goods_region_info']) )
object(parent::ERROR)->check($data, 'shop_goods_region_info', parent::TABLE_SHOP_GOODS_REGION, array('args'));
if( isset($data['shop_goods_region_state']) )
object(parent::ERROR)->check($data, 'shop_goods_region_state', parent::TABLE_SHOP_GOODS_REGION, array('args'));
if( isset($data['shop_goods_region_sort']) )
object(parent::ERROR)->check($data, 'shop_goods_region_sort', parent::TABLE_SHOP_GOODS_REGION, array('args'));
$original = object(parent::TABLE_SHOP_GOODS_REGION)->find($data['shop_goods_region_id']);
if (empty($original)) throw new error('数据不存在');
$whitelist = array(
'shop_goods_region_info',
'shop_goods_region_state',
'shop_goods_region_sort',
);
$update_data = cmd(array($data, $whitelist), 'arr whitelist');
foreach( $update_data as $k => &$v ){
if (isset($original[$k]) && $original[$k] == $v)
unset($update_data[$k]);
}
if (empty($update_data))
throw new error('没有需要更新的数据');
$update_data['shop_goods_region_update_time'] = time();
if( object(parent::TABLE_SHOP_GOODS_REGION)->update(array(array('shop_goods_region_id=[+]', $data['shop_goods_region_id'])), $update_data) ){
object(parent::TABLE_ADMIN_LOG)->insert($data, $update_data);
return $data['shop_goods_region_id'];
}else{
throw new error('操作失败');
}
}
public function api_list($data = array()){
object(parent::REQUEST_ADMIN)->check(parent::AUTHORITY_GOODS_REGION_READ);
$config = array(
'orderby' => array(),
'where' => array(),
'limit' => object(parent::REQUEST)->limit($data, parent::REQUEST_ADMIN),
);
$config["orderby"] = object(parent::REQUEST)->orderby($data, array(
'shop_goods_name_desc' => array('shop_goods_name', true),
'shop_goods_name_asc' => array('shop_goods_name', false),
'shop_goods_state_desc' => array('shop_goods_state', true),
'shop_goods_state_asc' => array('shop_goods_state', false),
'scope_desc' => array('shop_goods_region_scope', true),
'scope_asc' => array('shop_goods_region_scope', false),
'province_desc' => array('shop_goods_region_province', true),
'province_asc' => array('shop_goods_region_province', false),
'city_desc' => array('shop_goods_region_city', true),
'city_asc' => array('shop_goods_region_city', false),
'district_desc' => array('shop_goods_region_district', true),
'district_asc' => array('shop_goods_region_district', false),
'state_desc' => array('shop_goods_region_state', true),
'state_asc' => array('shop_goods_region_state', false),
'insert_time_desc' => array('shop_goods_region_insert_time', true),
'insert_time_asc' => array('shop_goods_region_insert_time', false),
'update_time_desc' => array('shop_goods_region_update_time', true),
'update_time_asc' => array('shop_goods_region_update_time', false),
'sort_desc' => array('shop_goods_region_sort', true),
'sort_asc' => array('shop_goods_region_sort', false),
));
$config["orderby"][] = array('sgr.shop_goods_id', false);
if( !object(parent::REQUEST_ADMIN)->check(parent::AUTHORITY_GOODS_ADMINISTRATOR, true) ){
$config["where"][] = array('[and] sg.user_id=[+]', $_SESSION['user_id']);
}
if(!empty($data['search'])){
if( isset($data['search']['shop_goods_id']) && is_string($data['search']['shop_goods_id']) ){
$config["where"][] = array('[and] sg.shop_goods_id=[+]', $data['search']['shop_goods_id']);
}
if( isset($data['search']['shop_goods_name']) && is_string($data['search']['shop_goods_name']) ){
$config['where'][] = array('[and] sg.shop_goods_name LIKE "%[-]%"', $data['search']['shop_goods_name']);
}
if( isset($data['search']['shop_goods_region_province']) && is_string($data['search']['shop_goods_region_province']) ){
$config['where'][] = array('[and] sgr.shop_goods_region_province LIKE "%[-]%"', $data['search']['shop_goods_region_province']);
}
if( isset($data['search']['shop_goods_region_city']) && is_string($data['search']['shop_goods_region_city']) ){
$config['where'][] = array('[and] sgr.shop_goods_region_city LIKE "%[-]%"', $data['search']['shop_goods_region_city']);
}
if( isset($data['search']['shop_goods_region_district']) && is_string($data['search']['shop_goods_region_district']) ){
$config['where'][] = array('[and] sgr.shop_goods_region_district LIKE "%[-]%"', $data['search']['shop_goods_region_district']);
}
if( isset($data['search']['state']) &&
(is_string($data['search']['state']) || is_numeric($data['search']['state'])) &&
in_array($data['search']['state'], array("0", "1")) ){
$config["where"][] = array('[and] sgr.shop_goods_region_state=[+]', $data['search']['state']);
}
if( isset($data['search']['scope']) &&
(is_string($data['search']['scope']) || is_numeric($data['search']['scope'])) &&
in_array($data['search']['scope'], array("1", "2", "3")) ){
$config["where"][] = array('[and] sgr.shop_goods_region_scope=[+]', $data['search']['scope']);
}
if( isset($data['search']['shop_goods_state']) &&
(is_string($data['search']['shop_goods_state']) || is_numeric($data['search']['shop_goods_state'])) &&
in_array($data['search']['shop_goods_state'], array("0", "1", "2", "3")) ){
$config["where"][] = array('[and] sg.shop_goods_state=[+]', $data['search']['shop_goods_state']);
}
}
return object(parent::TABLE_SHOP_GOODS_REGION)->select_page($config);
}
public function api_remove( $data = array() ){
object(parent::REQUEST_ADMIN)->check(parent::AUTHORITY_GOODS_REGION_REMOVE);
object(parent::ERROR)->check($data, 'shop_goods_region_id', parent::TABLE_SHOP_GOODS_REGION, array('args'));
$original = object(parent::TABLE_SHOP_GOODS_REGION)->find($data['shop_goods_region_id']);
if (empty($original)) throw new error('数据不存在');
if( object(parent::TABLE_SHOP_GOODS_REGION)->remove($data['shop_goods_region_id']) ){
object(parent::TABLE_ADMIN_LOG)->insert($data, $original);
return $data['shop_goods_region_id'];
}else{
throw new error("操作失败");
}
}
}
?>