<?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_datetime = false;
    var $t_title = false;
    var $t_td_type = 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_datetime = false;
        $this->t_title = false;
        $this->t_td_type = 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 get_task_by_status($t_ht_op_code = false, $t_status = 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)
            $this->t_status = " AND t.t_status= '$t_status'  ";
        if ($t_status == 'review')
            $this->t_status = " AND (t.t_status= 'review' OR t.t_status= 'wait')";
        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' OR t.t_status= 'wait')";
        return $this->get_list();
    }

    //待结算任务
    function in_reviewed($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= 'reviewed' ";
        return $this->get_list();
    }

    //已完成任务
    function in_complete($t_ht_op_code = false, $start_date = false, $end_date = false, $t_title = 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_title)
            $this->t_title = " AND t.t_title like '%$t_title%' ";
        if ($start_date)
            $this->t_datetime = " AND t.t_datetime BETWEEN '$start_date' AND '$end_date' ";
        $this->t_status = " AND (t.t_status= 'complete' OR t.t_status= 'published')";
        return $this->get_list();
    }

    //正在编辑的任务
    function in_refuse($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= 'refuse'  ";
        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($get_count = 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, \n"
                . "       t.t_attach, \n"
                . "       t.t_sitecode, \n"
                . "       t.t_delete \n"
                . "FROM   infotasks t \n"
                . "WHERE  t.t_delete = 0 \n";
        if ($get_count)
            $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_a_id ? $sql.=$this->t_a_id : false;
        $this->t_td_type ? $sql.=$this->t_td_type : false;
        $this->t_title ? $sql.=$this->t_title : false;

        // $admin_info = $this->session->userdata('session_admin');
        //  if (!empty($admin_info->a_sitecode) && trim($this->t_a_id) == "AND t.t_a_id= 0")
        //     $sql.= " AND (t.t_sitecode= '$admin_info->a_sitecode' or t.t_sitecode is null) ";
        $sql.= " AND t.t_sitecode=  " . $this->HT->escape($this->config->item('site_code'));

        $this->t_datetime ? $sql.=$this->t_datetime : false;
        $this->order_by ? $sql.=$this->order_by : false;
        $query = $this->HT->query($sql);
        //print_r($this->HT->queries);
        if ($this->top_num == 1) {
            if ($query->num_rows() > 0) {
                $row = $query->row();
                return $row;
            } else {
                return FALSE;
            }
        } else {
            return $query->result();
        }
    }

    //根据给定id字符串获取任务列表
    function get_list_by_taskids($taskids) {
        $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_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) 
				ORDER BY t.t_datetime DESC";
        $query = $this->HT->query($sql);
        return $query->result();
    }

    //根据不同状态获取任务列表
    public function get_tasks_list($t_status = false, $t_ht_op_code = 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);
            }
        } else {
            $this->t_a_id = " AND t.t_a_id<= 0";
        }
        //已发布的和已完成的任务计算在已完成的任务里
        if ($t_status == 'complete') {
            $this->t_status = " AND (t.t_status= 'complete' OR t.t_status= 'published') ";
        } else if ($t_status == 'review') {
            $this->t_status = " AND (t.t_status= 'review' OR t.t_status= 'wait')";
        } else if ($t_status) {
            $this->t_status = " AND t.t_status = '$t_status' ";
        }
        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);
    }

    //写入
    function add($t_title, $t_content, $t_a_id, $t_ht_op_code, $t_td_type, $t_expires, $t_status = 'edit', $t_sitecode, $t_attach = NULL) {
        $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_status, \n"
                . "    t_sitecode, \n"
                . "    t_delete, \n"
                . "    t_datetime, \n"
                . "    t_attach \n"
                . "  ) \n"
                . "VALUES \n"
                . "  ( \n"
                . "    N?,N?,?,?,?,?,?,?,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_status, $t_sitecode, $t_attach));
        $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, $t_status = '', $t_attach = false) {
        $t_status_sql = '';
        if ($t_status != '') {
            $t_status_sql = "t_status='$t_status',";
        }
        if ($t_attach) {
            $t_status_sql.="t_attach = '$t_attach',";
        }
        $sql = "UPDATE infotasks SET    
						t_title = N?,
						t_content = N?,
						t_a_id = ?,
						t_td_type = ?,
						t_expires = ?,
						$t_status_sql
						t_datetime = GETDATE() 
				 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) {
        $sql = "UPDATE infotasks \n"
                . "SET    t_status = ? \n"
                . "WHERE  t_id = ?";
        $query = $this->HT->query($sql, array($t_status, $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 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 == 'published' AND $result->ic_status == 1) {
                return 1;
            } else {
                return 0;
            }
        }
    }

    //获取任务大厅任务个数
    public function task_hall_count() {
        $result = $this->in_hall();
        $num = count($result);
        return $num;
    }

    //获取每月优秀文章排行榜
    public function get_article_rank_list() {
        $day_end = date('Y-m-01');
        $day_start = date('Y-m-d', strtotime("$day_end -1 month"));
        $sql = "SELECT  
						t_id,
						t_title,
						t_a_id,
						t_ht_op_code,
						t_rating,
						t_rating_total,
						t_status
				FROM 	infotasks 
						INNER JOIN infoTaskArticles ON t_id=ta_t_id
			   WHERE 	ta_isbest between ? AND ? 
			ORDER BY 	t_rating_total DESC";
        $query = $this->HT->query($sql, array($day_start, $day_end));
        $result = $query->result();
        return $result;
    }

    //获取最新活跃作者
    public function get_author_rank_list($top = 1) {
        $sql = "SELECT TOP $top t_a_id,t_active_author FROM infotasks WHERE t_active_author is not null";
        $query = $this->HT->query($sql);
        $result = $query->result();
        if (!empty($result) && $top == 1) {
            return $result[0];
        }
        return $result;
    }

    //获取不同任务状态的任务数量
    public function get_task_count_by_status() {
        $admin_info = $this->session->userdata('session_admin');
        $t_ht_op_code = $admin_info->a_id;
        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'];
            $counts['edit'] = $edit_count;

            $review_count = $wait_count = 0;
            if (isset($counts['review']))
                $review_count = $counts['review'];
            if (isset($counts['wait']))
                $wait_count = $counts['wait'];
            $counts['review'] = $review_count + $wait_count;

            return $counts;
        }
        return false;
    }

}
