<?php

namespace Home\Model;
use Think\Model;

/**
 * 优惠券模型
 */
class MemberCouponModel extends Model{

    public function getInfo($id){
    	return $this->find($id);
    }

    public function getList($userid){
        $map['userid'] = $userid;
        $map['status'] = 0;
        $map['expire_time'] = array('EGT',time());

    	$lists = $this->where($map)->select();
    	return $lists;
    }


//     public function getAvlList($userid,$goods_ids){
//         $lists = array();
//         $gid_arr = explode(',', $goods_ids);
//         foreach ($gid_arr as $gid){
//             if($gid!=0){
//                 $map['userid'] = $userid;
//                 $map['goods_id'] = $gid;
//                 $map['status'] = 0;
//                 $map['expire_time'] = array('EGT',time());

//                 $tmp_lists = $this->where($map)->select();
//                 if($tmp_lists)
//                     $lists = array_merge($lists,$tmp_lists);


//             }
//         }

//         return $lists;
//     }

    public function getAvlList($mid,$userid,$order_total=0,$goods_ids=''){
        $lists = array();
        $mids = '0,'.$mid;
        $map['userid'] = $userid;
        $map['mid'] = array('in',$mids);
        $map['status'] = 0;
        $map['expire_time'] = array('EGT',time());
        $map['order_total'] = array('ELT',$order_total);
        if($goods_ids){
        	$map['goods_id'] = array('in',$goods_ids);
        }

        $lists = $this->where($map)->select();

        return $lists;
    }


    public function setUsed($id){
        $data['id'] = $id;
        $data['status'] = 1;

        $this->save($data);
    }


    public function createCoupon($mid,$uid,$title,$money,$order_total,$end_date,$goods_id=0){
        $coupon['userid'] = $uid;
        $coupon['mid'] = $mid;
        $coupon['title'] = $title;
        $coupon['goods_id'] = $goods_id;
        $coupon['money'] = $money;
        $coupon['order_total'] = $order_total;
        $coupon['create_time'] = time();
        $coupon['expire_time'] = strtotime($end_date)+3600*24-1;

        $this->add($coupon);
    }







}
