<?php
namespace eapie\source\request\merchant;
use eapie\main;
use eapie\error;
class admin_config extends \eapie\source\request\merchant {
public function api_data($data = array()){
object(parent::REQUEST_ADMIN)->check(parent::AUTHORITY_CONFIG_READ);
$config = array(
'rmb_withdraw_merchant_money',
'rmb_buy_merchant_credit'
);
if( isset($data['config_id']) ){
object(parent::ERROR)->check($data, 'config_id', parent::TABLE_CONFIG, array('args'));
if( !in_array($data['config_id'], $config)){
return NULL;
}
$output = object(parent::TABLE_CONFIG)->find($data['config_id']);
if( empty($output)){
return NULL;
}
$output = object(parent::TABLE_CONFIG)->data($output);
}else{
$select_config = array(
"where" => array(),
"orderby" => array()
);
$select_config['where'][] = array("config_id IN ([-])", "\"".implode("\",\"", $config)."\"", true);
$select_config['orderby'][] = array('config_sort', false);
$select_config['orderby'][] = array('config_id', false);
$output = object(parent::TABLE_CONFIG)->select($select_config);
if( empty($output)){
return NULL;
}
foreach($output as $key => $value){
$output[$key] = object(parent::TABLE_CONFIG)->data($value);
}
}
return $output;
}
public function api_edit($data = array()){
object(parent::REQUEST_ADMIN)->check(parent::AUTHORITY_CONFIG_EDIT);
$old = array();
if( isset($data['rmb_withdraw_merchant_money']) ){
$old = object(parent::TABLE_CONFIG)->find('rmb_withdraw_merchant_money');
if( empty($old) ){
throw new error('未知配置编辑');
}else{
$old = object(parent::TABLE_CONFIG)->data($old);
}
object(parent::ERROR)->check( $data['rmb_withdraw_merchant_money'], 'min_merchant_money', parent::TABLE_CONFIG, array('args'), "rmb_withdraw_merchant_money[min_merchant_money]" );
object(parent::ERROR)->check( $data['rmb_withdraw_merchant_money'], 'state', parent::TABLE_CONFIG, array('args'), "rmb_withdraw_merchant_money[state]" );
$whitelist = array(
'min_merchant_money',
'state',
);
$whitelist_data = cmd(array($data['rmb_withdraw_merchant_money'], $whitelist), 'arr whitelist');
$value = cmd(array($whitelist_data), 'json encode');
if( !empty($whitelist_data) ){
foreach($whitelist_data as $k => $v){
if( isset($old['config_value'][$k])
&& $old['config_value'][$k] == $v ){
unset($whitelist_data[$k]);
}
}
}
if (empty($whitelist_data)){
throw new error('没有需要更新的数据');
}
$bool = object(parent::TABLE_CONFIG)->update_value('rmb_withdraw_merchant_money', $value);
}else
if( isset($data['rmb_buy_merchant_credit']) ){
$old = object(parent::TABLE_CONFIG)->find('rmb_buy_merchant_credit');
if( empty($old) ){
throw new error('未知配置编辑');
}else{
$old = object(parent::TABLE_CONFIG)->data($old);
}
object(parent::ERROR)->check( $data['rmb_buy_merchant_credit'], 'ratio_credit', parent::TABLE_CONFIG, array('args'), "rmb_buy_merchant_credit[ratio_credit]" );
object(parent::ERROR)->check( $data['rmb_buy_merchant_credit'], 'ratio_rmb', parent::TABLE_CONFIG, array('args'), "rmb_buy_merchant_credit[ratio_rmb]" );
object(parent::ERROR)->check( $data['rmb_buy_merchant_credit'], 'algorithm', parent::TABLE_CONFIG, array('args'), "rmb_buy_merchant_credit[algorithm]" );
object(parent::ERROR)->check( $data['rmb_buy_merchant_credit'], 'state', parent::TABLE_CONFIG, array('args'), "rmb_buy_merchant_credit[state]" );
$whitelist = array(
'ratio_credit',
'ratio_rmb',
'algorithm',
'state',
);
$whitelist_data = cmd(array($data['rmb_buy_merchant_credit'], $whitelist), 'arr whitelist');
$value = cmd(array($whitelist_data), 'json encode');
if( !empty($whitelist_data) ){
foreach($whitelist_data as $k => $v){
if( isset($old['config_value'][$k])
&& $old['config_value'][$k] == $v ){
unset($whitelist_data[$k]);
}
}
}
if (empty($whitelist_data)){
throw new error('没有需要更新的数据');
}
$bool = object(parent::TABLE_CONFIG)->update_value('rmb_buy_merchant_credit', $value);
}
if( !empty($bool) ){
object(parent::TABLE_ADMIN_LOG)->insert($data, $old);
return true;
} else {
throw new error('操作失败');
}
}
}
?>