<?php

//$type = Dever::db('setting/role_goods')->config['set']['type'];

$type = array
(
    1 => '商品',
    2 => '礼品卡',
    3 => '名额',
);

$button = array();
$info = '';

$mid = Dever::input('search_option_mid');
$search_api_mid = Dever::input('search_api_mid');
if ($search_api_mid) {
    $member = Dever::db('agent/member')->find(array('mobile' => $search_api_mid));
    if ($member) {
        if (Dever::load('manage/auth')->checkFunc('agent.member_goods', 'edit', '手动调整权益')) {
            $button['手动调整权益'] = array('location', 'agent/lib/member_set.home?mid=' . $member['id'] . '&search_api_mid=' . $search_api_mid . '&[refer]');
        }
        $button['权益操作记录'] = array('location', 'l=project/database/list&project=agent&table=member_goods_log&search_option_mid='.$member['id'].'&search_api_mid=' . $search_api_mid . '&[refer]');
        // $button = array
        // (
        //     '手动调整权益' => array('location', 'agent/lib/member_set.home?mid=' . $member['id'] . '&search_api_mid=' . $search_api_mid),
        //     '权益操作记录' => array('location', 'l=project/database/list&project=agent&table=member_goods_log&search_option_mid='.$member['id'].'&search_api_mid=' . $search_api_mid),
        // );
        // $info = Dever::load('agent/lib/member')->getInfoString($member);
        $mid = false;
    }
    
} else {
    if ($mid) {
        $button['返回上一页'] = array('location', 'l=project/database/list&project=agent&table=member_area&page_type=1&mid=' . $mid);
        if (Dever::load('manage/auth')->checkFunc('agent.member_goods', 'edit', '手动调整权益')) {
            $button['手动调整权益'] = array('location', 'agent/lib/member_set.home?mid=' . $mid . '&[refer]');
        }
        $button['权益操作记录'] = array('location', 'l=project/database/list&project=agent&table=member_goods_log&search_option_mid=' . $mid . '&[refer]');

        // $button = array
        // (
        //     '返回上一页' => array('location', 'l=project/database/list&project=agent&table=member_area&page_type=1&mid=' . $mid),
        //     '手动调整权益' => array('location', 'agent/lib/member_set.home?mid=' . $mid),
        //     '权益操作记录' => array('location', 'l=project/database/list&project=agent&table=member_goods_log&search_option_mid=' . $mid),
        // );
        // $info = Dever::load('agent/lib/member')->getInfoString($mid);
    }
}
$excel = false;
if(Dever::load('manage/auth')->checkFunc('agent.member_goods', 'editMout', '数据导出')){
    $excel[] = array('数据导出','权益管理', 'agent/lib/dhorder.out_member_goods');
}
return array
(
    # 表名
    'name' => 'member_goods',
    # 显示给用户看的名称
    'lang' => '权益管理',
    'order' => 81,
    'info' => $info,
    'set' => array
    (
        'type' => $type,
    ),
    # 数据结构
    'struct' => array
    (
    
        'id'        => array
        (
            'type'      => 'int-11',
            'name'      => 'ID',
            'default'   => '',
            'desc'      => '',
            'match'     => 'is_numeric',
            'search'    => 'order',
            'update'    => 'hidden',
            //'list'        => true,
        ),

        'mid'      => array
        (
            'type'      => 'int-11',
            'name'      => '代理商姓名/手机号',
            'default'   => '-1',
            'desc'      => '代理商',
            'match'     => 'is_string',
            'update'    => 'text',
            'search'    => array
            (
                'api' => 'agent/member-getSearch',
                'col' => 'col',
                'result' => 'id',
                'search' => 'mid',
            ),
            'list_name' => '代理商信息',
            'list'      => 'Dever::load("agent/lib/member.getOne", {mid}, "agent/member", true)',
            'list_order' => 3,
        ),

        'agent-member-name'=> array
        (
            'name'      => '姓名',
            'default'   => '',
            'desc'      => '姓名',
            'match'     => 'option',
            'searchs'    => array
            (
                'api' => 'agent/member-find',
                'col' => 'name',
                'result' => 'id',
                'search' => 'mid',
            ),
            # 读取另外表的关联方式
            'sync'      => array('mid', 'id'),
            //'list'      => $mid ? false : true,
            //'list_order' => 1,
        ),
        'agent-member-idcard'=> array
        (
            'name'      => '身份证号',
            'default'   => '',
            'desc'      => '手机号',
            'match'     => 'option',
            'searchs'    => array
            (
                'api' => 'agent/member-find',
                'col' => 'idcard',
                'result' => 'id',
                'search' => 'mid',
            ),
            # 读取另外表的关联方式
            'sync'      => array('mid', 'id'),
        ),
        'agent-member-role'=> array
        (
            'name'      => '代理角色',
            'default'   => '',
            'desc'      => '代理角色',
            'match'     => 'option',
            # 读取另外表的关联方式
            'sync'      => array('mid', 'id'),
            //'list'      => 'Dever::load("setting/role-find#name", "{agent-member-role}")',
            //'list_order' => 3,
        ),

        'type'        => array
        (
            'type'      => 'tinyint-1',
            'name'      => '类型',
            'default'   => '1',
            'desc'      => '类型',
            'match'     => 'is_numeric',
            'update'  	=> 'radio',
            'option'    => $type,
            'search'    => 'select',
            'list'      => true,
            'list_order' => 4,
        ),

        'type_id'      => array
        (
            'type'      => 'int-11',
            'name'      => '类型id',
            'default'   => '-1',
            'desc'      => '类型id',
            'match'     => 'is_numeric',
            'list_name' => '权益名称',
            //'list'      => 'Dever::load("agent/lib/manage.getGoods#name", "{type}", "{type_id}", "{sku_id}")',
            'list_order' => 5,
        ),

        'sku_id'      => array
        (
            'type'      => 'int-11',
            'name'      => 'sku_id',
            'default'   => '-1',
            'desc'      => 'sku_id',
            'match'     => 'is_numeric',
        ),

        'name'      => array
        (
            'type'      => 'varchar-100',
            'name'      => '权益名称',
            'default'   => '',
            'desc'      => '权益名称',
            'match'     => 'is_string',
            'update'    => 'text',
            'search'    => 'fulltext',
            'list'      => 'Dever::load("agent/lib/manage.getGoods#name", "{type}", "{type_id}", "{sku_id}", "{name}")',
            'list_order' => 5,
        ),

        'total_num'        => array
        (
            'type'      => 'int-11',
            'name'      => '总数量',
            'default'   => '0',
            'desc'      => '总数量',
            'match'     => 'is_numeric',
            'list_name' => '剩余数量/总数量',
            'list'      => '{total_num} - {sell_num} . " / " . {total_num}',
            'list_order' => 6,
        ),

        'sell_num'      => array
        (
            'type'      => 'int-11',
            'name'      => '已兑换数量',
            'default'   => '0',
            'desc'      => '已兑换数量',
            'match'     => 'option',
            //'list'      => 'Dever::load("agent/lib/manage.getGoods#price", "{type}", "{type_id}", "{sku_id}")',
            'list_name' => '单件价值',
            'list'      => '{price}',
            'list_order' => 7,
        ),

        'cash'      => array
        (
            'type'      => 'decimal-11,2',
            'name'      => '总价值',
            'default'   => '0',
            'desc'      => '总价值',
            'match'     => 'is_numeric',
            'update'    => 'text',
            'list'      => true,
            'list_name' => '未兑换价值',
            'list'      => '({total_num} - {sell_num})*{price}',
            'list_order' => 9,
        ),

        'price'      => array
        (
            'type'      => 'decimal-11,2',
            'name'      => '单件价值',
            'default'   => '0',
            'desc'      => '单件价值',
            'match'     => 'is_numeric',
            'update'    => 'text',
            'list'      => true,
            'list_name' => '已兑换价值',
            'list'      => '{sell_num}*{price}',
            'list_order' => 10,
        ),

        'state'     => array
        (
            'type'      => 'tinyint-1',
            'name'      => '状态',
            'default'   => '1',
            'desc'      => '请选择状态',
            'match'     => 'is_numeric',
        ),
        
        'cdate'     => array
        (
            'type'      => 'int-11',
            'name'      => '分配时间',
            'match'     => array('is_numeric', time()),
            'desc'      => '',
            # 只有insert时才生效
            'insert'    => true,
            'search'    => 'date',
            //'list'      => 'date("Y-m-d H:i:s", {cdate})',
        ),
    ),

    'alter' => array
    (
        5 => array
        (
            array('update', 'price', 'price', 'decimal-11,2 0 单件价值'),
            array('update', 'cash', 'cash', 'decimal-11,2 0 总价值'),
        ),
        'version' => 5,
    ),

    'manage' => array
    (
        'edit' => false,
        'insert' => false,
        'delete' => false,
        'button' => $button,
        'excel' => $excel,
    ),

    'request' => array
    (
        'getData' => array
        (
            # 匹配的正则或函数 选填项
            'option' => array
            (
                'mid' => 'yes',
                'type' => 'yes',
                'type_id' => 'yes',
                'sku_id' => 'yes',
                'state' => 1,
            ),
            'order' => array('type' => 'desc', 'id' => 'desc'),
            'type' => 'all',
            'page' => array(500, 'list'),
            'col' => '*, total_num-sell_num as num',
        ),

        'getOne' => array
        (
            # 匹配的正则或函数 选填项
            'option' => array
            (
                'id' => 'yes',
                'mid' => 'yes',
                'type' => 'yes',
                'type_id' => 'yes',
                'sku_id' => 'yes',
                'state' => 1,
            ),
            'type' => 'one',
            'col' => '*, total_num-sell_num as num',
        ),

        'getAll' => array
        (
            # 匹配的正则或函数 选填项
            'option' => array
            (
                'id' => 'yes',
                'mid' => 'yes',
                'type' => 'yes',
                'type_id' => 'yes',
                'sku_id' => 'yes',
                'state' => 1,
            ),
            'type' => 'all',
            'col' => '*, total_num-sell_num as num',
        ),

        'findData' => array
        (
            # 匹配的正则或函数 选填项
            'where' => array
            (
                'mid' => 'yes',
                'type' => 'yes',
                'type_id' => 'yes',
                'sku_id' => 'yes',
                'state' => 1,
            ),
            'type' => 'one',
            'col' => '*, total_num-sell_num as num',
        ),

        'findAll' => array
        (
            # 匹配的正则或函数 选填项
            'option' => array
            (
                'mid' => 'yes',
                'type' => 'yes',
                'type_id' => 'yes',
                'sku_id' => 'yes',
                'start' => array('yes-cdate', '>='),
                'end' => array('yes-cdate', '<='),
                'state' => 1,
            ),
            'type' => 'all',
            'group' => 'type,type_id,sku_id',
            'col' => '*,sum(total_num) as total_num, sum(sell_num) as sell_num, sum(total_num*price) as cash, sum(sell_num*price) as y_cash',
        ),

        # 更新售出量
        'incSell' => array
        (
            'type' => 'update',
            'where' => array
            (
                'mid' => 'yes',
                'type' => 'yes',
                'type_id' => 'yes',
                'sku_id' => 'yes',
            ),
            'set' => array
            (
                'sell_num' => array('yes', '+='),
            ),
        ),

        # 减少售出量
        'decSell' => array
        (
            'type' => 'update',
            'where' => array
            (
                'mid' => 'yes',
                'type' => 'yes',
                'type_id' => 'yes',
                'sku_id' => 'yes',
            ),
            'set' => array
            (
                'sell_num' => array('yes', '-='),
            ),
        ),

        # 更新总库存
        'updateTotal' => array
        (
            'type' => 'update',
            'where' => array
            (
                'id' => 'yes',
            ),
            'set' => array
            (
                'total_num' => array('yes', '+='),
            ),
        ),

        # 增加总库存
        'incTotal' => array
        (
            'type' => 'update',
            'where' => array
            (
                'mid' => 'yes',
                'type' => 'yes',
                'type_id' => 'yes',
                'sku_id' => 'yes',
            ),
            'set' => array
            (
                'total_num' => array('yes', '+='),
            ),
        ),

        # 减少总库存
        'decTotal' => array
        (
            'type' => 'update',
            'where' => array
            (
                'mid' => 'yes',
                'type' => 'yes',
                'type_id' => 'yes',
                'sku_id' => 'yes',
            ),
            'set' => array
            (
                'total_num' => array('yes', '-='),
            ),
        ),
        'getExcelAll' => array
        (
            # 匹配的正则或函数 选填项
            'option' => array
            (
                'type' => 'yes',
                'name' => array('yes', 'like'),
                'status' => 'yes',
                'state' => 1,
            ),
            'type' => 'all',
            'col' => '*, total_num-sell_num as num',
        ),
        'getQone' => array
        (
            # 匹配的正则或函数 选填项
            'option' => array
            (
                'mid' => 'yes',
                'type' => 'yes',
                'type_id' => 'yes',
                'state' => 1,
            ),
            'type' => 'one',
            'order' => array('cdate'=>'desc'),
            'col' => '*',
        ),

        'getTotal' => array
        (
            # 匹配的正则或函数 选填项
            'option' => array
            (
                'start' => array('yes-cdate','>='),
                'end' => array('yes-cdate','<='),
                'mid' => 'yes',
                'type' => array('yes','in'),
                'type_id' => 'yes',
                'state' => 1,
            ),
            'type' => 'one',
            // 'order' => array('cdate'=>'desc'),
            'col' => 'sum(cash) as cash,sum(total_num) as total_num,sum(sell_num) as sell_num,price',
        ),
        'getProduct' => array
        (
            # 匹配的正则或函数 选填项
            'option' => array
            (
                'start' => array('yes-cdate','>='),
                'end' => array('yes-cdate','<='),
                'mid' => 'yes',
                'type' => array('yes','in'),
                'type_id' => array('yes','in'),
                'state' => 1,
            ),
            'type' => 'all',
            'group' => 'type,type_id',
            'order' => array('type'=>'asc', 'type_id' => 'desc'),
            'col' => '*,sum(total_num)-sum(sell_num) as num',
        ),

        'getSearchAll' => array
        (
            # 匹配的正则或函数 选填项
            'option' => array
            (
                'start' => array('yes-cdate','>='),
                'end' => array('yes-cdate','<='),
                'type' => array('yes','in'),
                'state' => 1,
            ),
            'type' => 'all',
            
            'col' => '*',
        ),

        'getCardAll' => array
        (
            # 匹配的正则或函数 选填项
            'option' => array
            (
                'start' => array('yes-cdate','>='),
                'end' => array('yes-cdate','<='),
                'mid' => 'yes',
                'type'=>'yes',
                'type_id' => array('yes','in'),
                'state' => 1,
            ),
            'type' => 'all',
            
            'col' => 'sku_id',
        ),
        'getTOne' => array
        (
            # 匹配的正则或函数 选填项
            'option' => array
            (
                'mid' => 'yes',
                'type'=>array('yes','in'),
                'type_id' => array('yes','in'),
                'state' => 1,
            ),
            'type' => 'one',
            
            'col' => '*',
        ),
    ),
);
