<?php

defined('BASEPATH') OR exit('No direct script access allowed');

class Wechat_service_model extends CI_Model {

    public function __construct()
    {
        parent::__construct();
        $this->info = $this->load->database('INFO', TRUE);
    }

    public function insert_wechat_notify($arr)
    {
        $this->info->insert('wx_service_notify', $arr);
        return $this->info->query("SELECT MAX(wn_id) wn_id from wx_service_notify")
                          ->row()->wn_id;
    }

    public function if_user_exists($arr)
    {
        $sql = "SELECT 1 from wx_user where wu_useropenid=? ";
        $if = $this->info->query($sql, $arr['wu_useropenid'])->num_rows();
        return $if > 0;
    }

    public function update_wechat_user($arr)
    {
        $where = " wu_useropenid='" . $arr['wu_useropenid'] . "'";
        return $this->info->update('wx_user', $arr, $where);
    }

    public function insert_wechat_user($arr)
    {
        if (true === $this->if_user_exists($arr)) {
            $this->update_wechat_user($arr);
        } else {
            $this->info->insert('wx_user', $arr);
        }
        return $this->info->query("SELECT MAX(wu_id) wu_id from wx_user where wu_useropenid=?", array($arr['wu_useropenid']))
                          ->row()->wu_id;
    }

    public function insert_captcha($arr)
    {
        $this->info->insert('Wx_CaptCha', $arr);
        return $this->info->query("SELECT MAX(WCC_id) WCC_id from Wx_CaptCha")
                          ->row()->wcc_id;
    }

    public function insert_wechat_customer($arr)
    {
        $this->info->insert('wx_customer', $arr);
        return $this->info->query("SELECT MAX(wc_id) wc_id from wx_customer")
                          ->row()->wc_id;
    }

    public function insert_wechat_push($arr)
    {
        $this->info->insert('wx_service_push', $arr);
        return $this->info->query("SELECT MAX(wp_id) wp_id from wx_service_push")
                          ->row()->wp_id;
    }

    public function set_captcha_pass($WCC_id)
    {
        $sql = "UPDATE wx_captcha
                SET wcc_status=1, wcc_successtime=GETDATE()
                WHERE wcc_id=? ";
        return $this->info->query($sql, array($WCC_id));
    }

    public function get_user($user_id=null, $appid=null, $openid=null)
    {
        $param_arr = array();
        $where_id = $user_id===null ? "" : " AND wu_id=? ";
        $where_openid = ($openid===null) ? "" : " AND wu_hostappid=? and wu_useropenid=? ";
        $sql = "SELECT * from wx_user
                where 1=1 $where_id $where_openid ";
        $where_id==="" ? $param_arr=array($appid, $openid) : null;
        $where_openid==="" ? $param_arr=array($user_id) : null;
        if ($where_id==='' && $where_openid==='') {
            return array();
        }
        $user = $this->info->query($sql, $param_arr)->row_array();
        return $user;
    }

    public function get_verify($WU_id, $captcha)
    {
        $sql = "SELECT top 1 *
                    ,DATEADD(S, ISNULL(wcc_expireseconds, 300),wcc_createtime) expire_time
                from wx_captcha
                where wcc_wu_id=? and wcc_value=?
                order by wcc_status asc,wcc_createtime desc";
        $user_captcha = $this->info->query($sql, array($WU_id, $captcha))->row_array();
        return $user_captcha;
    }

    public function if_order_bound($coli_sn, $order_type)
    {
        $sql = "SELECT 1 from wx_user
                where wu_coli_sn = ?
                and wu_htordertype=? ";
        $ret = $this->info->query($sql, array($coli_sn, $order_type))->num_rows();
        return $ret>0;
    }

    public function get_customer($coli_sn)
    {
        $sql = "SELECT top 1 * from wx_customer
                where wc_coli_sn=?";
        $ret = $this->info->query($sql, array($coli_sn))->row_array();
        return $ret;
    }

}

/* End of file Wechat_service_model.php */
