<?php

class Infotasks_model extends CI_Model {

    var $insert_id = -1;
    var $top_num = false;
    var $t_id = false;
    var $order_by = false;
    var $t_status = false;
    var $t_ht_op_code = false;
    var $t_a_id = false;
    var $t_td_type = false;
    var $t_datetime = false;
    var $t_title = false;

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

    function init() {
        $this->top_num = false;
        $this->t_id = false;
        $this->order_by = " ORDER BY t.t_id DESC ";
        $this->t_status = false;
        $this->t_a_id = false;
        $this->t_td_type = false;
        $this->t_title = false;
    }

    //正在编辑的任务
    function in_edit($t_ht_op_code = false) {
        $this->init();
        if (!empty($t_ht_op_code)) {
            if (is_numeric($t_ht_op_code)) {
                $this->t_a_id = " AND t.t_a_id= " . $this->HT->escape($t_ht_op_code);
            } else {
                $this->t_ht_op_code = " AND t.t_ht_op_code=" . $this->HT->escape($t_ht_op_code);
            }
        }
        $this->t_status = " AND t.t_status= 'edit'  ";
        return $this->get_list();
    }

    //待审核任务
    function in_review($t_ht_op_code = false) {
        $this->init();
        if (!empty($t_ht_op_code)) {
            if (is_numeric($t_ht_op_code)) {
                $this->t_a_id = " AND t.t_a_id= " . $this->HT->escape($t_ht_op_code);
            } else {
                $this->t_ht_op_code = " AND t.t_ht_op_code=" . $this->HT->escape($t_ht_op_code);
            }
        }
        $this->t_status = " AND t.t_status= 'review' ";
        return $this->get_list();
    }

    //待结算任务
    function in_reviewed($t_ht_op_code = false, $t_a_id = false, $t_td_type = false, $start_date = false, $end_date = false) {
        $this->init();
        if (!empty($t_ht_op_code)) {
            if (is_numeric($t_ht_op_code)) {
                $this->t_a_id = " AND t.t_a_id= " . $this->HT->escape($t_ht_op_code);
            } else {
                $this->t_ht_op_code = " AND t.t_ht_op_code=" . $this->HT->escape($t_ht_op_code);
            }
        }
        if ($t_a_id)
            $this->t_a_id = " AND t.t_a_id = $t_a_id ";
        if ($t_td_type)
            $this->t_td_type = " AND t.t_td_type = '$t_td_type' ";
        $this->t_status = " AND t.t_status = 'reviewed' ";
        if ($start_date)
            $this->t_datetime = " AND t.t_datetime BETWEEN '$start_date' AND '$end_date' ";
        return $this->get_list();
    }

    //已完成任务
    function in_complete($t_ht_op_code = false) {
        $this->init();
        if (!empty($t_ht_op_code)) {
            if (is_numeric($t_ht_op_code)) {
                $this->t_a_id = " AND t.t_a_id= " . $this->HT->escape($t_ht_op_code);
            } else {
                $this->t_ht_op_code = " AND t.t_ht_op_code=" . $this->HT->escape($t_ht_op_code);
            }
        }
        $this->t_status = " AND (t.t_status= 'complete' OR t.t_status= 'published')";
        return $this->get_list();
    }

    //待审核投稿
    function in_wait($t_ht_op_code = false) {
        $this->init();
        if (!empty($t_ht_op_code)) {
            if (is_numeric($t_ht_op_code)) {
                $this->t_a_id = " AND t.t_a_id= " . $this->HT->escape($t_ht_op_code);
            } else {
                $this->t_ht_op_code = " AND t.t_ht_op_code=" . $this->HT->escape($t_ht_op_code);
            }
        }
        $this->t_status = " AND t.t_status= 'wait' ";
        return $this->get_list();
    }

    //大厅任务
    function in_hall() {
        $this->init();
        $this->t_a_id = " AND t.t_a_id= 0 ";
        return $this->get_list();
    }

    //获取单个任务详细信息
    function detail($t_id) {
        $this->init();
        $this->top_num = 1;
        $this->t_id = " AND t.t_id=" . $this->HT->escape($t_id);
        return $this->get_list();
    }

    function get_list($page_flag = false) {
        $this->top_num ? $sql = "SELECT TOP " . $this->top_num : $sql = "SELECT ";
        $sql .= "	t.t_id, \n"
                . "       t.t_title, \n"
                . "       t.t_content, \n"
                . "       t.t_status, \n"
                . "       t.t_a_id, \n"
                . "       t.t_ht_op_code, \n"
                . "       t.t_td_type, \n"
                . "       t.t_expires, \n"
                . "       t.t_datetime, \n"
                . "       t.t_rating_total, \n"
                . "       t.t_active_author, \n"
                . "       t.t_attach, \n"
                . "       t.t_rating, \n"
                . "       t.t_delete \n"
                . "FROM   infotasks t \n"
                . "WHERE  t.t_delete = 0 \n";
        if ($page_flag)
            $sql = "SELECT t.t_id FROM infotasks t WHERE  t.t_delete = 0";
        $this->t_id ? $sql.=$this->t_id : false;
        $this->t_status ? $sql.=$this->t_status : false;
        $this->t_ht_op_code ? $sql.=$this->t_ht_op_code : false;
        $this->t_td_type ? $sql.=$this->t_td_type : false;
        $this->t_a_id ? $sql.=$this->t_a_id : false;
        $this->t_title ? $sql.=$this->t_title : false;
        $this->t_datetime ? $sql.=$this->t_datetime : false;
        $site_code = $this->config->item('site_code');
        if ($site_code == 'cht') { //早期的任务没有设置站点，默认是cht
            $sql.=" AND ( t.t_sitecode= '$site_code' OR t.t_sitecode is null )";
        } else {
            $sql.=" AND  t.t_sitecode= '$site_code' ";
        }
        $this->order_by ? $sql.=$this->order_by : false;
        $query = $this->HT->query($sql);
        if ($this->top_num == 1) {
            if ($query->num_rows() > 0) {
                $row = $query->row();
                return $row;
            } else {
                return FALSE;
            }
        } else {
            return $query->result();
        }
    }

    //写入
    function add($t_title, $t_content, $t_a_id, $t_ht_op_code, $t_td_type, $t_expires, $t_sitecode = 'cht') {
        $sql = "INSERT INTO infotasks \n"
                . "  ( \n"
                . "    t_title, \n"
                . "    t_content, \n"
                . "    t_a_id, \n"
                . "    t_ht_op_code, \n"
                . "    t_td_type, \n"
                . "    t_expires, \n"
                . "    t_sitecode, \n"
                . "    t_status, \n"
                . "    t_delete, \n"
                . "    t_datetime \n"
                . "  ) \n"
                . "VALUES \n"
                . "  ( \n"
                . "    N?,N?,?,?,?,?,?,'edit',0,GETDATE() \n"
                . "  )";
        $query = $this->HT->query($sql, array($t_title, $t_content, $t_a_id, $t_ht_op_code, $t_td_type, $t_expires, $t_sitecode));
        $this->insert_id = $this->HT->last_id('infotasks');
        return $query;
        //print_r($this->HT->queries);
    }

    //更新
    function update($t_id, $t_title, $t_content, $t_a_id, $t_td_type, $t_expires) {
        $sql = "UPDATE infotasks \n"
                . "SET    t_title = N?, \n"
                . "       t_content = N?, \n"
                . "       t_a_id = ?, \n"
                . "       t_td_type = ?, \n"
                . "       t_expires = ?, \n"
                . "       t_datetime = GETDATE() \n"
                . "WHERE  t_id = ?";
        $query = $this->HT->query($sql, array($t_title, $t_content, $t_a_id, $t_td_type, $t_expires, $t_id));
        return $query;
        //print_r($this->HT->queries);
    }

    //删除
    function delete($t_id) {
        $sql = "UPDATE infotasks \n"
                . "SET    t_delete = 1 \n"
                . "WHERE  t_id = ?";
        $query = $this->HT->query($sql, array($t_id));
        return $query;
    }

    //审核
    function reviwed($t_id, $t_status, $t_rating = '', $t_rating_total = '') {
        $sql = "UPDATE infotasks \n"
                . "SET    t_status = ?, t_rating=?, t_rating_total=? \n"
                . "WHERE  t_id = ?";
        $query = $this->HT->query($sql, array($t_status, $t_rating, $t_rating_total, $t_id));
        return $query;
    }

    //承接任务
    function undertake($t_id, $a_id) {
        $sql = "UPDATE infotasks \n"
                . "SET    t_a_id = ? \n"
                . "WHERE  t_id = ?";
        $query = $this->HT->query($sql, array($a_id, $t_id));
        return $query;
    }

    //取消任务
    function canceltake($t_id) {
        $sql = "UPDATE infotasks \n"
                . "SET    t_a_id = 0 \n"
                . "WHERE  t_id = ?";
        $query = $this->HT->query($sql, array($t_id));
        return $query;
    }

    //根据不同状态获取任务列表
    public function get_tasks_list($t_status = false, $t_ht_op_code = false, $t_a_id = false, $t_td_type = false, $start_date = false, $end_date = false, $tasktitle = false, $page_flag = false) {
        $this->init();
        if (!empty($t_ht_op_code)) {
            if (is_numeric($t_ht_op_code)) {
                $this->t_a_id = " AND t.t_a_id= " . $this->HT->escape($t_ht_op_code);
            } else {
                $this->t_ht_op_code = " AND t.t_ht_op_code=" . $this->HT->escape($t_ht_op_code);
            }
        }
        //已发布的和已完成的任务计算在已完成的任务里
        if ($t_status == 'complete') {
            $this->t_status = " AND (t.t_status= 'complete' OR t.t_status= 'published') ";
        } elseif ($t_status == 'edit') {
            $this->t_status = " AND (t.t_status= 'edit' OR t.t_status= 'unreviewed') ";
        } else if ($t_status) {
            $this->t_status = " AND t.t_status = '$t_status' ";
        }
        if ($t_a_id == '-1') {
            $this->t_a_id = " AND t.t_a_id = '' ";
        } elseif ($t_a_id) {
            $this->t_a_id = " AND t.t_a_id = $t_a_id ";
        }
        if ($t_td_type)
            $this->t_td_type = " AND t.t_td_type = '$t_td_type' ";
        if ($tasktitle)
            $this->t_title = " AND t.t_title like '%$tasktitle%' ";
        if ($start_date)
            $this->t_datetime = " AND t.t_datetime BETWEEN '$start_date' AND '$end_date' ";
        return $this->get_list($page_flag);
    }

    //根据给定id字符串获取任务列表
    function get_list_by_taskids($taskids, $order = false) {
        $ordermap = " ORDER BY t.t_datetime DESC ";
        if ($order) {
            $ordermap = " ORDER BY t.t_rating_total DESC,t.t_datetime DESC ";
        }
        $sql = "SELECT  t.t_id,
						t.t_title,
						t.t_content,
						t.t_status,
						t.t_a_id,
						t.t_ht_op_code,
						t.t_td_type,
						t.t_expires,
						t.t_datetime,
						t.t_rating,
						t.t_rating_total,
						t.t_delete,
						(select top 1 count(0) from infoTaskArticles where ta_t_id=t.t_id AND ta_status='version') as t_edit_count
				 FROM   infotasks t
				WHERE   t.t_id IN ($taskids) 
				$ordermap";
        $query = $this->HT->query($sql);
        return $query->result();
    }

    //评选最活跃作者
    function set_active_author($t_id, $t_active_author, $ta_id) {
        //设置活跃作者标识
        $sql = "UPDATE infotasks \n"
                . "SET    t_active_author = ? \n"
                . "WHERE  t_id = ?";
        $query = $this->HT->query($sql, array($t_active_author, $t_id));
        //给活跃作者添加奖励
        if ($query) {
            $sql = "UPDATE infotaskarticles \n"
                    . "SET    ta_charge = ta_charge+150 \n"
                    . "WHERE  ta_id = ?";
            $query2 = $this->HT->query($sql, array($ta_id));
            return $query2;
        } else {
            return false;
        }
    }

    //获取作者总字数排行榜
    public function get_author_count() {
        $sql = "SELECT t_id,t_a_id,(SELECT top 1 ta_count FROM infotaskarticles WHERE ta_t_id=t_id order by ta_datetime desc) as ta_count,(SELECT top 1 ta_id FROM infotaskarticles WHERE ta_t_id=t_id order by ta_datetime desc) as ta_id FROM infotasks WHERE t_status = 'reviewed'";
        $query = $this->HT->query($sql);
        $result = $query->result();
        $rank = array();
        $rank_author = array();
        $active_author = array();
        foreach ($result as $item) {
            if (!isset($rank_author[$item->t_a_id]))
                $rank_author[$item->t_a_id] = $item;
            if (!isset($rank[$item->t_a_id]))
                $rank[$item->t_a_id] = 0;
            $rank[$item->t_a_id]+=$item->ta_count;
        }
        //获取第一名的数据
        arsort($rank);
        foreach ($rank as $key => $v) {
            $active_author['author_num'] = count($rank);
            $active_author['count'] = $v;
            $active_author['task_id'] = $rank_author[$key]->t_id;
            $active_author['ta_id'] = $rank_author[$key]->ta_id;
            $active_author['t_a_id'] = $key;
            break;
        }
        return $active_author;
    }

    //查询是否已设置当月活跃作者
    public function is_active_author() {
        $datetime = date('Y-m-01');
        $sql = "SELECT TOP 1 t_active_author FROM infotasks WHERE t_active_author>'$datetime'";
        $query = $this->HT->query($sql);
        $result = $query->result();
        if (!empty($result)) {
            return 1;
        }
        return 0;
    }

    public function get_tasks_rank($top = 5) {
        $sql = "  SELECT TOP $top * FROM infotasks
    			WHERE t_status='reviewed'
    			ORDER BY t_rating_total DESC";
        $query = $this->HT->query($sql);
        $result = $query->result();
        return $result;
    }

    //获取不同任务状态的任务数量
    public function get_task_count_by_status() {
        $admin_info = $this->session->userdata('session_admin');
        $t_ht_op_code = $admin_info['OPI_Code'];
        if (is_numeric($t_ht_op_code)) {
            $where = " t_a_id= " . $this->HT->escape($t_ht_op_code);
        } else {
            $where = " t_ht_op_code=" . $this->HT->escape($t_ht_op_code);
        }
        
        $site_code = $this->config->item('site_code');
        if ($site_code == 'cht') { //早期的任务没有设置站点，默认是cht
            $where.=" AND ( t_sitecode= '$site_code' OR t_sitecode is null )";
        } else {
            $where.=" AND  t_sitecode= '$site_code' ";
        }
        
        $sql = "SELECT t_status, count(0) as task_count FROM infotasks WHERE $where AND t_delete=0 group by t_status";
        $query = $this->HT->query($sql);
        $result = $query->result();
        $counts = array();
        if (!empty($result)) {
            foreach ($result as $v) {
                $counts[$v->t_status] = $v->task_count;
            }
            //已完成数量=完成数量+已发布的数量
            $complete_count = $published_count = 0;
            if (isset($counts['complete']))
                $complete_count = $counts['complete'];
            if (isset($counts['published']))
                $published_count = $counts['published'];
            $counts['complete'] = $complete_count + $published_count;

            //正在进行中的任务=edit+unreviewed的数量
            $edit_count = $unreviewed_count = 0;
            if (isset($counts['edit']))
                $edit_count = $counts['edit'];
            if (isset($counts['unreviewed']))
                $unreviewed_count = $counts['unreviewed'];
            $counts['edit'] = $edit_count + $unreviewed_count;
            return $counts;
        }
        return false;
    }

    //判断任务是否已上线
    public function is_in_used($ta_id) {
        $sql = "SELECT 	TOP 1 t_status,ic_status
				FROM 	infoTaskArticles 
						INNER JOIN infoContents ON ic_id=ta_ic_id 
						LEFT JOIN infotasks ON t_id=ta_t_id
			   WHERE    ta_id=?";
        $query = $this->HT->query($sql, array($ta_id));
        $result = $query->result();
        if (empty($result)) {
            return 0;
        } else {
            $result = $result[0];
            //信息平台和作者平台同时是发布状态时
            if ($result->t_status == 'complete' AND $result->ic_status == 1) {
                return 1;
            } else {
                return 0;
            }
        }
    }

}
