<?php

defined('InShopNC') or exit('Access Invalid!');

class stat_anoticeControl extends SystemControl
{
    public function __construct() {
        parent::__construct();
    }

    public function indexOp()
    {
        $goods_name = trim($_REQUEST['goods_name']);
        $sort = trim($_REQUEST['sort']);

        $cond = [];
        if(!empty($goods_name)) {
            $cond['goods_name'] = array('like',"%{$goods_name}%");
        }

        if($sort == 'descending') {
            $order = 'nc_count desc';
        }
        elseif($sort == 'ascending') {
            $order = 'nc_count asc';
        }
        else {
            $order = '';
        }

        $mod_notice = Model('arrival_notice');
        $result = $mod_notice->where($cond)->field('count(*) as nc_count,goods_id,goods_name')->group('goods_id')->order($order)->select();

        Tpl::output('list', $result);
        Tpl::showpage('stat.anotice.list');
    }

    public function favoritesOp()
    {
        $goods_name = trim($_REQUEST['goods_name']);
        $sort = trim($_REQUEST['sort']);

        $cond = [];
        if(!empty($goods_name)) {
            $cond['goods_name'] = array('like',"%{$goods_name}%");
        }

        if($sort == 'descending') {
            $order = 'nc_count desc';
        }
        elseif($sort == 'ascending') {
            $order = 'nc_count asc';
        }
        else {
            $order = '';
        }

        $cond = array('fv_type=goods');
        $on = 'goods.goods_id=favorites.fav_id';

        $mod = Model();
        $result = $mod->table('goods,favorites')->join('inner')->on($on)->where($cond)->field('goods_name,goods_id,count(*) as nc_count')->group('fav_id')->order($order)->select();

        Tpl::output('list', $result);
        Tpl::showpage('stat.anotice.favorite.list');
    }

    public function fcodeOp()
    {

        $mod_goods = Model('goods');
        $mod_fcode = Model('goods_fcode');
        $items = $mod_fcode->field('goods_commonid,batch_code,count(*) as nc_count')->group('goods_commonid,batch_code')->select();

        $result = [];
        foreach ($items as $item)
        {
            $val = [];

            $val['goods_commonid'] = $item['goods_commonid'];
            $val['batch_code'] = $item['batch_code'];
            $val['count'] = $item['nc_count'];

            $unused = $mod_fcode->where(array('goods_commonid' => $item['goods_commonid'],'batch_code' => $item['batch_code'],'fc_state' => 0))->count();
            $binded  = $mod_fcode->where(array('goods_commonid' => $item['goods_commonid'],'batch_code' => $item['batch_code'],'grab_state' => 2))->count();

            $val['used'] = $item['nc_count'] - $unused;
            $val['binded'] = $binded;

            $url = "http://p.lrlz.com/mobile/index.php?act=fcode&op=index&common_id={$item['goods_commonid']}&batch_code={$item['batch_code']}";
            $val['fc_link'] = $url;
            $goods = $mod_goods->where(array('goods_commonid' => $item['goods_commonid']))->limit(1)->select();
            if(empty($goods)) {
                $val['goods_name'] = "不存在了!";
            } else {
                $val['goods_name'] = $goods[0]['goods_name'];
            }

            $result[] = $val;
        }

        Tpl::output('list', $result);
        Tpl::showpage('stat.anotice.fcode.list');
    }
}