<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * 房豆消费
 *
 * @package         zsb
 * @subpackage      Models
 * @category        Models
 * @author          fisher
 */
class house_bean_consume_model extends MY_Model
{
    //经纪人ID
    public $uid;

    /**
     * Constructor
     */
    public function __construct()
    {
        parent::__construct(1,1,0);
        /*$this->user_session = $this->load->get_var('user_session');

        if(!$this->user_session || $this->user_session['uid'] <= 0)
        {
            //获取SESSION数据并为父属性赋值
            $this->user_session = $this->get_user_session();
            $this->load->vars(array('user_session'=>$this->user_session));
        }

        $this->uid = $this->user_session['uid'];*/
    }

    /**
     * 获取优先消费的房豆充值记录
     * @param int $brokerId   经纪人ID
     * @param int $score      房豆消费数量
     * @return array
     */
    public function getPriorityConsumeHousebean($brokerId = 0,$score = 0)
    {
        $result = array();
        if(empty($brokerId) || empty($score))
        {
            return $result;
        }

        //echo $brokerId.'-'.$score.'<br>';

        $time = time();

        $this->dbback->select('id,total');
        $this->dbback->where("broker_id = $brokerId AND endtime >= '$time'");
        $this->dbback->order_by('endtime','asc');
        $info = $this->dbback->get('payment_housebean_record')->result_array();

        if(!empty($info))
        {
            foreach($info as $key => $item)
            {
                if($item['total'] > 0)
                {
                    if($item['total'] >= $score)
                    {
                        $result[$item['id']] = $score;
                        break;
                    }
                    else
                    {
                        $score = $score - $item['total'];
                        $result[$item['id']] = $item['total'];
                    }
                }
            }
        }//print_r($result);die;

        return $result;
    }

    /**
     * 扣除房豆
     * @param array $data
     * @param int $type
     * @param int $broker_id
     * @param int $relation_id
     * @return bool
     */
    public function delHouseBean($data = array(),$type = 0,$broker_id = 0,$relation_id = 0)
    {
        if(empty($data) || empty($type))
        {
            return false;
        }

        foreach($data as $key => $value)
        {
            $sql = "update payment_housebean_record set total=total-".$value." where id=".$key;
            $this->db->query($sql);

            $sql = "insert into housebean_consume(id,uid,payment_housebean_record_id,num,type,relation_id,addtime) values('','".$broker_id."','" . $key . "','" . $value . "','" . $type . "','" . $relation_id . "','" . time() . "')";
            $this->db->query($sql);
        }

        return true;
    }








}
/* End of file Broker_model.php */
/* Location: ./zsb/model/Broker_model.php */