<?php
namespace eapie\source\request\shop;
use eapie\main;
use eapie\error;
class admin_goods_type extends \eapie\source\request\shop {
public function api_edit($data = array()){
object(parent::REQUEST_ADMIN)->check(parent::AUTHORITY_GOODS_TYPE_EDIT);
object(parent::ERROR)->check($data, 'shop_goods_id', parent::TABLE_SHOP_GOODS, 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( !empty($data['type_id']) && is_array($data['type_id'])){
$type_id = array();
foreach($data['type_id'] as $value){
if(is_string($value) || is_numeric($value)){
$type_id[] = cmd(array($value), 'str addslashes');
}
}
}
$type_data = array();
if( !empty($type_id) ){
$in_string = "\"".implode("\",\"", $type_id)."\"";
$type_where = array();
$type_where[] = array("type_id IN([-])", $in_string, true) $type_where[] = array("[and] type_module=[+]", parent::MODULE_SHOP_GOODS_TYPE);
$type_data = object(parent::TABLE_TYPE)->select(array("where"=>$type_where));
}
$goods_type_data = object(parent::TABLE_SHOP_GOODS_TYPE)->select(array(
"where" => array( array("shop_goods_id=[+]", (string)$data['shop_goods_id']) )
));
$clear_data = array();
$clear_data["type_id"] = array();
$clear_data["delete"] = array();
$clear_data["insert"] = array();
if( !empty($goods_type_data) ){
foreach($goods_type_data as $value){
$clear_data["delete"][$value["type_id"]] = $value["shop_goods_type_id"];
}
}
if( !empty($type_data) ){
foreach($type_data as $type_value){
$clear_data["type_id"][] = $type_value["type_id"];
if( isset($clear_data["delete"][$type_value["type_id"]]) ){
unset($clear_data["delete"][$type_value["type_id"]]);
}else{
$insert_data = array(
"shop_goods_type_id" => object(parent::TABLE_SHOP_GOODS_TYPE)->get_unique_id(),
"shop_goods_id" => $data['shop_goods_id'],
"type_id" => $type_value["type_id"],
"user_id" => $_SESSION["user_id"],
"shop_goods_type_time" => time()
);
$clear_data["insert"][] = $insert_data;
}
}
}
if( !empty($clear_data["insert"]) ){
object(parent::TABLE_SHOP_GOODS_TYPE)->insert_batch($clear_data["insert"]);
}
if( !empty($clear_data["delete"]) ){
$in_string = "\"".implode("\",\"", $clear_data["delete"])."\"";
object(parent::TABLE_SHOP_GOODS_TYPE)->delete(array( array("shop_goods_type_id IN([-])", $in_string, true) ));
}
if( empty($clear_data["insert"]) && empty($clear_data["delete"]) ){
throw new error("没有需要更新的数据");
}
object(parent::TABLE_SHOP_GOODS)->update(
array( array('shop_goods_id=[+]', $data['shop_goods_id']) ),
array('shop_goods_update_time' => time() )
);
object(parent::TABLE_ADMIN_LOG)->insert($data, $clear_data);
return true;
}
}
?>