<?php
/**
 * @Copyright (C) 2016 汉潮 All rights reserved.
 * @License http://www.hanchao9999.com
 * @Author xiaogg <xiaogg@sina.cn>
 */
 
namespace Common\Model;
use Think\Model;
class GiftCardModel extends Model{
    /**
     * 所有礼品卡信息
     * @param $postdata['keyword'] int  会员ID/卡号/会员/门店/员工
     * @order_status $status int  礼品卡状态  0：全部礼品卡，1：已激活，2：已作废,3：未激活，4：未出售
     * @is_num $is_num true  只取数量
     * @page_num $page_num int  每页显示的礼品卡数量
     * @field  string  要查询的礼品卡字段
     * @num_type int 0礼品卡总额，1面值总额2售价总额3导出
     * @return  mixed
     */
    public function all_gift_card($postdata=array(),$status=0,$is_num=0,$page_num=20,$field=true,$num_type=0){
        $map=array();      
        if($postdata['keyword']){ 
            $keyword=$postdata['keyword'];    
            $whereor['id']=array('eq',$keyword);     
            $whereor['card_sn']=array('like','%'.$keyword.'%');  

            $member_where['username']=array('like','%'.$keyword.'%');
            $member_where['nickname']=array('like','%'.$keyword.'%');
            $member_where['mobile']=array('like','%'.$keyword.'%');
            $member_where['_logic'] = 'or';
            $member_map['_complex'] = $member_where;
            $uid_arr=M('Member')->where($member_map)->field('id')->select();
            if($uid_arr){
                foreach ($uid_arr as $key => $value) {
                    $arr_uid[]=$value['id'];
                }
                $whereor['bind_uid']=array('in',$arr_uid);
                $whereor['buy_uid']=array('in',$arr_uid);
            }            

            $account_id_arr=M('ShopsAccount')->where($shop_where)->field('id')->select();
            if($account_id_arr){
                foreach ($account_id_arr as $key => $value) {
                    $arr_account_id[]=$value['id'];
                }
                $whereor['cashier']=array('in',$arr_account_id);
                $whereor['daogou']=array('in',$arr_account_id);
            }
            $whereor['_logic'] = 'or';
            $map['_complex'] = $whereor;
        }   
        if($postdata['shop_id'])$map['shop_id']=$postdata['shop_id'];
        if($postdata['reg_from'])$map['addtime'][] = array('egt',strtotime($postdata['reg_from']));
        if($postdata['reg_to'])$map['addtime'][] = array('elt',strtotime($postdata['reg_to'])+24*3600);

        if(is_numeric($status))$map['status']=$status;
        if($is_num && empty($num_type)){$all_gift_card['num']=M('GiftCard')->where($map)->count();return $all_gift_card;}//礼品卡数量
        if($is_num && $num_type==1){$all_gift_card['all_money'] = M('GiftCard')->where($map)->sum('card_money');return $all_gift_card;}//面值总额
        if($is_num && $num_type==2){$all_gift_card['all_price'] = M('GiftCard')->where($map)->sum('shop_price');return $all_gift_card;}//售价总额
        if($is_num && $num_type==3){$all_gift_card['list']=M('GiftCard')->where($map)->field(true)->order('id desc')->select();return $all_gift_card;}//导出
        $page=empty($postdata['page'])?1:$postdata['page'];
        $all_gift_card=page_list('GiftCard',$map,'id desc',$field,$page_num,'',$page);
        return $all_gift_card;
    }
}
?>