<?php
/**礼品卡
 * @Copyright (C) 2016 汉潮 All rights reserved.
 * @License http://www.hanchao9999.com
 * @Author xiaol <xiaol@sina.cn>
 */
namespace Pad\Model;
use Think\Model;
class GiftCardModel extends Model{
	/**
     * 根据商品编号进行唯一查询
     * @param $sn
     * @return array
     */
	public function search_gift_sn($sn){
        $return=array('status'=>0,'msg'=>'');
        $sn=trim($sn);
        if(empty($sn)){$return['msg']='请输入礼品卡号';return $return;}
		
		$map = array();$map['card_sn'] = $sn;
        $gift_card_info = M('GiftCard')->where($map)->field(true)->find();
		if($gift_card_info){
			if($gift_card_info['end_time'] != 0 && $gift_card_info['end_time'] < NOW_TIME){
				$return['msg'] = '该礼品卡已过期';
			}else if($gift_card_info['status'] == 1){
				$return['msg'] = '该礼品卡已激活';
			}else if($gift_card_info['status'] == 2){
				$return['msg'] = '该礼品卡已作废';
			}else if($gift_card_info['sell_time'] != 0){
				$return['msg'] = '该礼品卡已售出';
			}else{
				if($gift_card_info['end_time']){
					$gift_card_info['end_time'] = time_format($gift_card_info['end_time']);
				}else{
					$gift_card_info['end_time'] = '长期有效';
				}
				$return['status']=1;
				$return['data']['list'] = $gift_card_info;
			}
		}else{
			$return['msg'] = '没有查到相应的礼品卡';
		}
		return $return;
    }
    /**
    *查询卡销售记录
    */
    public function get_sale_list($postdata){
    	$return=array('status'=>0,'msg'=>'');
    	$map=$order=array();
    	$map['shop_id']=$postdata['shop_id'];
    	$map['type']=$postdata['type']?$postdata['type']:15;
    	$order['addtime']='desc';
    	$order['id']='desc';
    	$all_order=page_list('OrderInfo',$map,$order,true,10);
    	if(empty($all_order['list'])){$return['status']=2;$return['msg']='没有更多数据了';return $return;}
    	if($all_order['list']){$return['status']=1;$return['list']=$all_order['list'];}
    	return $return;
    }
    /**
    *查询销售详情
    */
    public function get_sale_cont($postdata){
    	$return=array('status'=>0,'msg'=>'');
    	$map=array();
    	$map['id']=$postdata['order_id'];
    	$order_info['infos']=M('OrderInfo')->where($map)->field(true)->find();
    	if(empty($order_info['infos'])){$return['msg']='订单信息错误'; return $return;}
    	if($order_info['infos']['uid']){
    	    $username=show_user_name($order_info['infos']['uid']);
            $shop_id=$postdata['shop_id'];
            if($shop_id && $username && strlen($username)==11){
                $is_hide_phone=idtoname($shop_id,'Shops','is_hide_phone');
                if($is_hide_phone==1){$username=str_cutmobile($username);}
            }
            $order_info['infos']['username']=$username;
        }else $order_info['infos']['username']='游客';

        $payname='';
        if(str_exists($order_info['infos']['assist_code'],'ALIPAY'))$payname='支付宝';
        if(str_exists($order_info['infos']['assist_code'],'WEIXIN'))$payname='微信';
        if(empty($payname) && $order_info['infos']['pay_code'])$payname=idtoname($order_info['infos']['pay_code'],'Payment','name','code');
        if(empty($payname))$payname='在线支付';
        if (floatval($order_info['infos']['cash_money']) > 0) $cash_name = '现金：' . format_price($order_info['infos']['cash_money']);
        if (floatval($order_info['infos']['card_money']) > 0) $card_name = '刷卡：' . format_price($order_info['infos']['card_money']);
        if (floatval($order_info['infos']['bank_money']) > 0) $bank_name = '台卡：' . format_price($order_info['infos']['bank_money']);
        if (floatval($order_info['infos']['scan_money']) > 0) $scan_name = $payname.'扫码：' . format_price($order_info['infos']['scan_money']);
        if (floatval($order_info['infos']['used_money']) > 0) $used_name = '余额：' . format_price($order_info['infos']['used_money']);
        if (floatval($order_info['infos']['pay_amount']) > 0) $cash_name = $payname.'：' . format_price($order_info['infos']['pay_amount']);
        if (floatval($order_info['infos']['used_money']) > 0) $used_name = '余额：' . format_price($order_info['infos']['used_money']);
        $order_info['infos']['pay_name']=$cash_name.$card_name.$bank_name.$scan_name.$used_name;
    	$map=array();
    	$map['shop_id']=$postdata['shop_id'];
    	$map['order_id']=$postdata['order_id'];
    	$all_card=M('GiftCard')->where($map)->field(true)->select();    	
    	$return['status']=1;
    	$return['infos']=$order_info['infos'];
    	$return['list']=$all_card;
    	return $return;
    }
	
}