<?php

/**
 * Survey Model
 * @author Candison <www.kandisheng.com>
 */

class Rent_Coupon_model extends MY_Model
{
    const  MODEL_TABLE_NAME = 'rent_coupon_share_log';
    
    function __construct()
    {
       parent::__construct();
    }

    /**
     * 检查租房用户是否有8折优惠券可以使用
     * @param int $uid 为passport_uid
     * @return int
     */
    public function checkCoupon($uid = 0){
        $table = self::MODEL_TABLE_NAME;
        $now_time = time();
        $sql = "select * from {$table} where is_coupon_use = 0 and get_coupon = 1 and share_account_id = {$uid} and end_time > {$now_time} and lock_time < {$now_time} limit 0,1";
        $data = $this->db_esfhouse->query($sql)->result_array();
        if( $data ) return 1;

        return 0;
    }

    /**
     * 使用优惠券
     * @param int $uid 为passport_uid
     * @return int
     */
    public function useCoupon($uid = 0){
        $arr = array('share_account_id'=>$uid,'get_coupon'=>1,'is_coupon_use'=>0);
        $up = array('is_coupon_use'=>1,'update_time'=>time(),'use_coupon_time'=>time(),'buy_package_type'=>'列表优先');

        $this->set_table('rent_coupon_share_log');
        $result = $this->save($arr,$up,'esfhouse');

        return $result;
    }

    /**
     * 设置复选框的选中状态
     * $status  1 选中复选框 0 未选中复选框
     * $uid 为passport_uid
     */
    public function setCheckbox($uid,$status){
        $where = array('share_account_id'=>$uid,'get_coupon'=>1 ,'is_coupon_use'=> 0);
        $update = array('checkbox_select'=> $status);
        $this->set_table('rent_coupon_share_log');
        $result = $this->save($where,$update,'esfhouse');
        return $result;
    }

    /**
     * @param $uid 为passport_uid
     * @return int
     */
    public function checkCheckBox($uid){
        $table = self::MODEL_TABLE_NAME;
        $now_time = time();
        $sql = "select * from {$table} where share_account_id={$uid} and get_coupon = 1 and is_coupon_use=0 and checkbox_select = 1 and end_time > {$now_time} ";
        $data = $this->db_esfhouse->query($sql)->result_array();
        if( !empty( $data )){
            return $data[0]['checkbox_select'];
        }
        return 0;

    }
    // 8折优惠券锁定30分钟
    public function lockCoupon($uid){
        $lock_time = time() + 1800;
        $now_time = time();
        $sql = "update rent_coupon_share_log set lock_time={$lock_time} where get_coupon = 1 and is_coupon_use = 0  and share_account_id = {$uid} and end_time >{$now_time} and lock_time < {$now_time} ";

        $res = $this->db_esfhouse->query($sql);
        return $res;
    }

}