<?php
namespace eapie\source\request\shop;
use eapie\main;
use eapie\error;
class admin_goods_group extends \eapie\source\request\shop
{
public function api_add($input = array())
{
object(parent::REQUEST_ADMIN)->check(parent::AUTHORITY_GOODS_GROUP_ADD);
object(parent::ERROR)->check($input, 'shop_goods_id', parent::TABLE_SHOP_GOODS, array('args'));
object(parent::ERROR)->check($input, 'shop_goods_sku_id', parent::TABLE_SHOP_GOODS_SKU, array('args'));
object(parent::ERROR)->check($input, 'shop_goods_group_name', parent::TABLE_SHOP_GOODS_GROUP, array('args'));
object(parent::ERROR)->check($input, 'shop_goods_group_info', parent::TABLE_SHOP_GOODS_GROUP, array('args'));
object(parent::ERROR)->check($input, 'shop_goods_group_sort', parent::TABLE_SHOP_GOODS_GROUP, array('args'));
object(parent::ERROR)->check($input, 'shop_goods_group_price', parent::TABLE_SHOP_GOODS_GROUP, array('args'));
object(parent::ERROR)->check($input, 'shop_goods_group_people', parent::TABLE_SHOP_GOODS_GROUP, array('args'));
object(parent::ERROR)->check($input, 'shop_goods_group_start_time', parent::TABLE_SHOP_GOODS_GROUP, array('args'));
object(parent::ERROR)->check($input, 'shop_goods_group_end_time', parent::TABLE_SHOP_GOODS_GROUP, array('args'));
$shop_goods = object(parent::TABLE_SHOP_GOODS)->find($input['shop_goods_id']);
if (!$shop_goods) {
throw new error('商品不存在');
}
if (!object(parent::REQUEST_ADMIN)->check(parent::AUTHORITY_GOODS_ADMINISTRATOR, true)) {
if ($shop_goods['user_id'] !== $_SESSION['user_id']) {
throw new error('权限不足,不能操作非自己添加的数据');
}
}
$shop_goods_sku = object(parent::TABLE_SHOP_GOODS_SKU)->find($input['shop_goods_sku_id']);
if (!$shop_goods_sku) {
throw new error('商品规格不存在');
}
if ($shop_goods_sku['shop_goods_id'] !== $input['shop_goods_id']) {
throw new error('商品规格和商品不匹配');
}
$shop_goods_group = object(parent::TABLE_SHOP_GOODS_GROUP)->find_where(array(
array('shop_goods_id = [+]', $input['shop_goods_id']),
array('shop_goods_sku_id = [+]', $input['shop_goods_sku_id']),
));
if ($shop_goods_group) {
throw new error('该拼团商品已存在');
}
$insert_data = cmd(array($input, array(
'shop_goods_id',
'shop_goods_sku_id',
'shop_goods_group_name',
'shop_goods_group_info',
'shop_goods_group_sort',
'shop_goods_group_price',
'shop_goods_group_people',
'shop_goods_group_start_time',
'shop_goods_group_end_time',
)), 'arr whitelist');
$insert_data['shop_goods_group_id'] = object(parent::TABLE_SHOP_GOODS_GROUP)->get_unique_id();
$insert_data['shop_goods_group_start_time'] = cmd(array($insert_data['shop_goods_group_start_time']), 'time mktime');
$insert_data['shop_goods_group_end_time'] = cmd(array($insert_data['shop_goods_group_end_time']), 'time mktime');
$insert_data['shop_goods_group_insert_time'] = time();
$insert_data['shop_goods_group_update_time'] = time();
$insert_data['user_id'] = $_SESSION['user_id'];
if (object(parent::TABLE_SHOP_GOODS_GROUP)->insert($insert_data)) {
object(parent::TABLE_ADMIN_LOG)->insert($input, $insert_data);
return $insert_data['shop_goods_id'];
} else {
throw new error('操作失败');
}
}
public function api_edit($input = array())
{
object(parent::REQUEST_ADMIN)->check(parent::AUTHORITY_GOODS_GROUP_EDIT);
object(parent::ERROR)->check($input, 'shop_goods_group_id', parent::TABLE_SHOP_GOODS_GROUP, array('args'));
if (isset($input['shop_goods_group_name'])) {
object(parent::ERROR)->check($input, 'shop_goods_group_name', parent::TABLE_SHOP_GOODS_GROUP, array('args'));
}
if (isset($input['shop_goods_group_info'])) {
object(parent::ERROR)->check($input, 'shop_goods_group_info', parent::TABLE_SHOP_GOODS_GROUP, array('args'));
}
if (isset($input['shop_goods_group_sort'])) {
object(parent::ERROR)->check($input, 'shop_goods_group_sort', parent::TABLE_SHOP_GOODS_GROUP, array('args'));
}
if (isset($input['shop_goods_group_price'])) {
object(parent::ERROR)->check($input, 'shop_goods_group_price', parent::TABLE_SHOP_GOODS_GROUP, array('args'));
}
if (isset($input['shop_goods_group_people'])) {
object(parent::ERROR)->check($input, 'shop_goods_group_people', parent::TABLE_SHOP_GOODS_GROUP, array('args'));
}
if (isset($input['shop_goods_group_start_time'])) {
object(parent::ERROR)->check($input, 'shop_goods_group_start_time', parent::TABLE_SHOP_GOODS_GROUP, array('args'));
$input['shop_goods_group_start_time'] = cmd(array($input['shop_goods_group_start_time']), 'time mktime');
}
if (isset($input['shop_goods_group_end_time'])) {
object(parent::ERROR)->check($input, 'shop_goods_group_end_time', parent::TABLE_SHOP_GOODS_GROUP, array('args'));
$input['shop_goods_group_end_time'] = cmd(array($input['shop_goods_group_end_time']), 'time mktime');
}
$original = object(parent::TABLE_SHOP_GOODS_GROUP)->find($input['shop_goods_group_id']);
if (empty($original)) {
throw new error('数据不存在');
}
if (!object(parent::REQUEST_ADMIN)->check(parent::AUTHORITY_GOODS_ADMINISTRATOR, true)) {
if ($original['user_id'] !== $_SESSION['user_id']) {
throw new error('权限不足,不能操作非自己添加的数据');
}
}
$update_data = cmd(array($input, array(
'shop_goods_group_name',
'shop_goods_group_info',
'shop_goods_group_sort',
'shop_goods_group_price',
'shop_goods_group_people',
'shop_goods_group_start_time',
'shop_goods_group_end_time',
)), 'arr whitelist');
if (!empty($update_data)) {
foreach ($update_data as $key => $value) {
if (isset($original[$key])) {
if ($original[$key] == $value) {
unset($update_data[$key]);
}
}
}
}
if (empty($update_data)) {
throw new error('没有需要更新的数据');
}
$update_data['shop_goods_group_update_time'] = time();
$update_where = array(array('shop_goods_group_id = [+]', $input['shop_goods_group_id']));
$res = object(parent::TABLE_SHOP_GOODS_GROUP)->update($update_where, $update_data);
if ($res) {
object(parent::TABLE_ADMIN_LOG)->insert($input, $update_data);
return $input['shop_goods_group_id'];
} else {
throw new error('操作失败');
}
}
public function api_remove($input = array())
{
object(parent::REQUEST_ADMIN)->check(parent::AUTHORITY_GOODS_GROUP_REMOVE);
object(parent::ERROR)->check($input, 'shop_goods_group_id', parent::TABLE_SHOP_GOODS_GROUP, array('args'));
$original = object(parent::TABLE_SHOP_GOODS_GROUP)->find($input['shop_goods_group_id']);
if (empty($original)) {
throw new error('数据不存在');
}
if (!object(parent::REQUEST_ADMIN)->check(parent::AUTHORITY_GOODS_ADMINISTRATOR, true)) {
if ($original['user_id'] !== $_SESSION['user_id']) {
throw new error('权限不足,不能操作非自己添加的数据');
}
}
if (object(parent::TABLE_SHOP_GOODS_GROUP)->remove($original['shop_goods_group_id'])) {
object(parent::TABLE_ADMIN_LOG)->insert($input, $original);
return $input['shop_goods_group_id'];
} else {
throw new error('删除失败');
}
}
public function api_list($input = array())
{
object(parent::REQUEST_ADMIN)->check(parent::AUTHORITY_GOODS_GROUP_READ);
object(parent::TABLE_SHOP_GOODS_GROUP)->update_state();
$where = array();
$where[] = array('sg.shop_goods_trash=0');
if (!object(parent::REQUEST_ADMIN)->check(parent::AUTHORITY_GOODS_ADMINISTRATOR, true)) {
$where[] = array('[and] sgw.user_id = [+]', $_SESSION['user_id']);
}
if (!empty($input['search'])) {
$search = $input['search'];
if (isset($search['shop_goods_id']) && is_string($search['shop_goods_id'])) {
$where[] = array('[and] sg.shop_goods_id = [+]', $search['shop_goods_id']);
}
if (isset($search['shop_goods_group_id']) && is_string($search['shop_goods_group_id'])) {
$where[] = array('[and] sgw.shop_goods_group_id = [+]', $search['shop_goods_group_id']);
}
if (isset($search['shop_goods_name']) && is_string($search['shop_goods_name'])) {
$where[] = array('[and] sg.shop_goods_name LIKE "%[-]%"', $search['shop_goods_name']);
}
if (isset($search['state']) && is_numeric($search['state'])) {
$where[] = array('[and] sgw.shop_goods_group_state = [+]', $search['state']);
}
if (isset($search['shop_goods_state']) && is_numeric($search['shop_goods_state'])) {
$where[] = array('[and] sg.shop_goods_state = [+]', $search['shop_goods_state']);
}
}
$orderby = object(parent::REQUEST)->orderby($input, 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),
'name_desc' => array('shop_goods_group_name', true),
'name_asc' => array('shop_goods_group_name', false),
'sort_desc' => array('shop_goods_group_sort', true),
'sort_asc' => array('shop_goods_group_sort', false),
'state_desc' => array('shop_goods_group_state', true),
'state_asc' => array('shop_goods_group_state', false),
'start_time_desc' => array('shop_goods_group_start_time', true),
'start_time_asc' => array('shop_goods_group_start_time', false),
'end_time_desc' => array('shop_goods_group_end_time', true),
'end_time_asc' => array('shop_goods_group_end_time', false),
'insert_time_desc' => array('shop_goods_group_insert_time', true),
'insert_time_asc' => array('shop_goods_group_insert_time', false),
'update_time_desc' => array('shop_goods_group_update_time', true),
'update_time_asc' => array('shop_goods_group_update_time', false),
));
$orderby[] = array('shop_goods_group_id', false);
$config = array(
'where' => $where,
'orderby' => $orderby,
'limit' => object(parent::REQUEST)->limit($input, parent::REQUEST_ADMIN),
);
return object(parent::TABLE_SHOP_GOODS_GROUP)->select_page($config);
}
public function api_get($input = array())
{
object(parent::REQUEST_ADMIN)->check(parent::AUTHORITY_GOODS_GROUP_READ);
object(parent::ERROR)->check($input, 'shop_goods_group_id', parent::TABLE_SHOP_GOODS_GROUP, array('args'));
object(parent::TABLE_SHOP_GOODS_GROUP)->update_state();
return object(parent::TABLE_SHOP_GOODS_GROUP)->find($input['shop_goods_group_id']);
}
public function api_edit_check()
{
object(parent::REQUEST_ADMIN)->check(parent::AUTHORITY_GOODS_GROUP_EDIT);
return true;
}
}