<?php
$type = Dever::input('search_option_store_id');
$status = Dever::config('base')->status;
$button = array
(
    '新增仓库商品' => array('location', Dever::url('lib/set.home?id=' . Dever::input('search_option_store_id'), 'store')),
    '导出调整明细' => array('excel', 'shop/excel.goods_log'),
);
$list_button = array
(
    'list' => array('查看详情', '"goods_log&search_option_sid={id}&search_option_store_id={store_id}&oper_table=goods_sku&top_table=info"'),
    'fast_list' => array('库存设置', '"info_sku&project=goods&goods_id={goods_id}&other=store&other_id={store_id}&page_type=1"'),
);
if (!$type) {
    unset($button['新增仓库商品']);
    unset($button['导出调整明细']);
    unset($list_button['fast_list']);

    $list_button['list'] = array('查看详情', '"goods_log&search_option_sid={id}&search_option_store_id={store_id}&oper_table=goods_sku"');
}
$config = array
(
    # 表名
    'name' => 'goods_sku',
    # 显示给用户看的名称
    'lang' => '商品库存设置',
    'order' => 200,
    'menu' => false,
    'end' => array
    (
        'insert' => 'store/lib/manage.skuUpdate',
        'update' => 'store/lib/manage.skuUpdate',
    ),

    'fill' => array
    (
        # 从哪个表填充
        'goods/info' => array
        (
            # 条件，另外一个表的字段 => 本表的字段
            'where' => array('id' => 'goods_id'),
            # 要填充的数据，另外一个表的字段 => 本表的字段，如果不填写就是所有的
            'update' => array('status' => 'status'),
            # 不需要填充的字段 与update互斥
            //'no' => array('id', 'seller_id', 'service_id', 'chose', 'state', 'cdate')
        )
    ),
    # 数据结构 不同的字段放这里
    'struct' => array
    (
        'id'        => array
        (
            'type'      => 'int-11',
            'name'      => 'ID',
            'default'   => '',
            'desc'      => '',
            'match'     => 'is_numeric',
            //'list'        => true,
        ),

        'store_id'      => array
        (
            'type'      => 'int-11',
            'name'      => '仓库名称',
            'default'   => '',
            'desc'      => '仓库名称',
            'match'     => 'is_numeric',
            'list'      => 'Dever::load("store/info-find#name", {store_id})',
            'search'    => $type != 1 ? array
            (
                'api' => 'store/info-like',
                'col' => 'name',
                'result' => 'id',
                'search' => 'store_id',//本表的字段，默认为当前的字段
            ) : 'hidden',
            //'search'    => 'hidden',
            'list_order' => 1,
        ),

        'goods-info_sku-code'=> array
        (
            'name'      => '商品编码',
            'default'   => '',
            'desc'      => '商品编码',
            'match'     => 'option',
            # 读取另外表的关联方式
            'sync'      => array('sku_id', 'id', 'goods_id', 'info_id', 'key'),
            'search'    => array
            (
                'api' => 'goods/info_sku-like',
                'col' => 'code',
                'result' => 'id',
                'search' => 'sku_id',//本表的字段，默认为当前的字段
            ),
            'list'      => true,
            'list_order' => 2,
        ),

        'goods-info_sku-f_price'=> array
        (
            'name'      => '门店进货单价',
            'default'   => '',
            'desc'      => '门店进货单价',
            'match'     => 'option',
            # 读取另外表的关联方式
            'sync'      => array('sku_id', 'id', 'goods_id', 'info_id', 'key'),
            'list'      => $type != 1 ? true : false,
            'list_order' => 7,
        ),

        'goods_id'      => array
        (
            'type'      => 'int-11',
            'name'      => '商品名称',
            'default'   => '',
            'desc'      => '商品名称',
            'match'     => 'is_numeric',
            'search'    => array
            (
                'api' => 'goods/info-like',
                'col' => 'name',
                'result' => 'id',
            ),
            'list'      => 'Dever::load("goods/info-find#name", {goods_id})',
            'list_order' => 5,
        ),

        'sku_id'      => array
        (
            'type'      => 'int-11',
            'name'      => '规格型号',
            'default'   => '-1',
            'desc'      => '规格型号',
            'match'     => 'is_numeric',
            'list'      => 'Dever::load("shop/lib/manage.getSku", {goods_id}, {sku_id})',
            'list_order' => 6,
        ),

        'add_num'        => array
        (
            'type'      => 'int-11',
            'name'      => '新增库存-输入小于0的数字，则为减少库存',
            'default'   => '0',
            'desc'      => '新增库存',
            'match'     => 'is_numeric',
            'list_name' => '当前库存',
            'list'      => '{total_num} - {sell_num}',
            'list_order' => 8,
        ),

        'total_num'        => array
        (
            'type'      => 'int-11',
            'name'      => '总库存',
            'default'   => '0',
            'desc'      => '总库存',
            'match'     => 'is_numeric',
            'list'      => true,
            'list_order' => 10,
        ),

        'sell_num'      => array
        (
            'type'      => 'int-11',
            'name'      => '销量',
            'default'   => '0',
            'desc'      => '请填写销量',
            'match'     => 'option',
            //'search'    => 'order',
            'list_name' => '占用库存',
            'list'      => true,
            'list_order' => 9,
        ),

        'reorder'       => array
        (
            'type'      => 'int-11',
            'name'      => '排序-数值越大越靠前，相当于置顶',
            'default'   => '1',
            'desc'      => '请输入排序',
            'match'     => 'option',
            //'update'  => 'text',
            //'search'    => 'order',
            //'list'      => true,
            'order'     => 'desc',
            //'edit'      => true,
            'list_name' => '在途库存',
            'list'      => 'Dever::load("shop/lib/manage.getGoodsTotal", 2, {store_id}, {goods_id}, {sku_id})',
            'list_order' => 9,
        ),

        'operdate'     => array
        (
            'type'      => 'int-11',
            'name'      => '最近调整时间',
            'default'   => '',
            'match'     => 'is_numeric',
            'desc'      => '',
            'search'  => 'sday',
            'list'      => '"{operdate}" > 0 ? date("Y-m-d H:i", {operdate}) : date("Y-m-d H:i", {cdate})',
            'list_order' => 13,
        ),

        'status'        => array
        (
            'type'      => 'tinyint-1',
            'name'      => '商品状态',
            'default'   => '2',
            'desc'      => '状态',
            'match'     => 'is_numeric',
            //'update'  => 'select',
            'option'    => $status,
            'search'    => 'select',
            'list'      => true,
            'list_order' => 11,
            //'edit'      => true,
        ),

        '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'  => 'sday',
            'list'        => $type == 1 ? 'date("Y-m-d H:i", {cdate})' : false,
            'list_order' => 12,
        ),
    ),

    'manage' => array
    (
        'insert' => false,
        'edit' => false,
        'delete' => false,
        'num' => false,
        'excel' => true,
        'page_list_table' => 'sku',

        # 自定义快捷新增和编辑
        'button' => $button,

        'list_button' => $list_button,
    ),

    # request 请求接口定义
    'request' => array
    (
        'getDataPage' => array
        (
            # 匹配的正则或函数 选填项
            'option' => array
            (
                'store_id' => array('yes-t_1.store_id'),
                'name' => array('yes-t_2.name', 'like'),
                'total' => array('yes|t_1.total_num-t_1.sell_num', '<'),
                'column' => array('yes-t_2.column_id', 'like'),
                'category' => array('yes-t_2.category', 'like'),
                'top_category_id' => array('yes-t_2.top_category_id'),
                'second_category_id' => array('yes-t_2.second_category_id'),
                'category_id' => array('yes-t_2.category_id'),
                'status' => array('yes-t_2.status', 1),
                'state' => array('yes-t_2.state', 1),
                'state_1' => array('yes-t_1.state', 1),
            ),
            # 联表
            'join' => array
            (
                array
                (
                    'table' => 'goods/info',
                    'type' => 'left join',
                    'on' => array('goods_id','id'),
                    'col' => 'goods_id',
                ),
            ),
            'type' => 'all',
            'order' => array('t_2.reorder' => 'desc', 't_2.id' => 'desc', 't_2.price' => 'asc'),
            'page' => array(30, 'list'),
            'col' => '*,t_2.name,t_2.cover,t_2.id as id,t_1.total_num-t_1.sell_num as total,t_1.sell_num',
        ),
        
        # 列表
        'getData' => array
        (
            # 匹配的正则或函数 选填项
            'option' => array
            (
                'store_id' => 'yes',
                'sku_id' => 'yes',
                'goods_id' => 'yes',
                'state' => 1,
            ),
            'type' => 'all',
            'order' => array('id' => 'desc'),
            'col' => '*,total_num-sell_num as total|sku_id',
        ),

        # 获取单条数据
        'getOne' => array
        (
            # 匹配的正则或函数 选填项
            'option' => array
            (
                'store_id' => 'yes',
                'sku_id' => 'yes',
                'goods_id' => 'yes',
                'state' => 1,
            ),
            'type' => 'one',
            'col' => '*,total_num-sell_num as total',
        ),

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

        # 减少售出量
        'decSell' => array
        (
            'type' => 'update',
            'where' => array
            (
                'store_id' => 'yes',
                'goods_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
            (
                'store_id' => 'yes',
                'goods_id' => 'yes',
                'sku_id' => 'yes',
            ),
            'set' => array
            (
                'total_num' => array('yes', '+='),
            ),
        ),

        # 减少总库存
        'decTotal' => array
        (
            'type' => 'update',
            'where' => array
            (
                'store_id' => 'yes',
                'goods_id' => 'yes',
                'sku_id' => 'yes',
            ),
            'set' => array
            (
                'total_num' => array('yes', '-='),
            ),
        ),

        'getDataByDate' => array
        (
            # 匹配的正则或函数 选填项
            'option' => array
            (
                'store_id' => 'yes',
                'status' => 'yes',
                'start' => array('yes-cdate', '>='),
                'end' => array('yes-cdate', '<='),
                'sku_id' => array('yes', 'in'),
                'goods_id' => array('yes', 'in'),
                'state' => 1,
            ),
            'type' => 'all',
            'order' => array('id' => 'desc'),
            'col' => '*',
        ),
    ),
);

return $config;