public function do_batch($act = '')
 {
     if (!$this->check_power('comparison_manage')) {
         return;
     }
     $act = trim($act);
     $ids = $this->input->post('ids');
     $res = FALSE;
     if ($ids && is_array($ids)) {
         $ids = my_intval($ids);
         switch ($act) {
             case 'on':
             case 'restore':
                 $res = ComparisonTypeModel::update($ids, array('cmp_type_flag' => 1));
                 break;
             case 'off':
                 $res = ComparisonTypeModel::update($ids, array('cmp_type_flag' => 0));
                 break;
             case 'delete':
                 $res = ComparisonTypeModel::update($ids, array('cmp_type_flag' => -1));
                 break;
             case 'remove':
                 $res = ComparisonTypeModel::delete($ids);
                 admin_log('remove', 'comparison_type', implode(',', $ids));
                 break;
             default:
                 break;
         }
         if ($res) {
             message('批量操作成功!', 'admin/comparison_type/index');
         } else {
             message('分类操作失败!', 'admin/comparison_type/index');
         }
     } else {
         message('请选择要删除的分类');
     }
 }
 public function save()
 {
     if (!$this->check_power('comparison_manage')) {
         return;
     }
     $act = $this->input->post('act');
     $act = $act == 'add' ? 'add' : 'edit';
     $id = null;
     if ($act == 'edit') {
         // check old info
         $id = (int) $this->input->post('id');
         $id && ($old_info = ComparisonInfoModel::get_comparison_info_by_id($id));
         if (empty($old_info)) {
             message('信息不存在');
         }
     }
     // check type info
     $type_id = (int) $this->input->post('type_id');
     $type_id && ($type_info = ComparisonTypeModel::get_comparison_type_by_id($type_id));
     if (empty($type_info) or $act == 'edit' && $old_info['cmp_type_id'] != $type_info['cmp_type_id']) {
         message('信息分类不存在');
     }
     $subject_id = intval($this->input->post('subject_id'));
     // init input data
     $info = array('cmp_type_id' => $type_id, 'cmp_info_year' => (int) $this->input->post('cmp_info_year'), 'cmp_info_score' => (int) $this->input->post('cmp_info_score'));
     $knowledge_ids = $this->input->post('knowledge_ids');
     //知识点
     $percents = $this->input->post('percents');
     //知识点占比
     $group_type_ids = $this->input->post('group_type_ids');
     //信息提取方式
     $group_type_percents = $this->input->post('group_type_percents');
     //信息提取方式占比
     $extraction_ratio = $this->input->post('extraction_ratio');
     //知识点和信息提取方式比例
     // validate input data
     $message = array();
     if ($info['cmp_info_year'] < 1990 or $info['cmp_info_year'] > date('Y')) {
         $message[] = '请正确填写考试年份';
     } else {
         $tmp_id = ComparisonInfoModel::get_comparison_info_by_year($type_id, $info['cmp_info_year'], 'cmp_info_id');
         if ($tmp_id && $tmp_id != $id) {
             message('年份 ' . $info['cmp_info_year'] . ' 信息已存在,不能重复添加');
         }
     }
     if ($info['cmp_info_score'] < 0 or $info['cmp_info_score'] > 1000) {
         $message[] = '请正确填写考试分数';
     }
     if (empty($knowledge_ids) or !is_array($knowledge_ids) or empty($percents) or !is_array($percents)) {
         $knowledge_ids = array();
     }
     /*
      * 知识点对比项
      */
     $items = array();
     $total_percent = 100;
     $current_percent = 0;
     foreach ($knowledge_ids as $knowledge_id) {
         // filter invalid data
         if (!($knowledge_id = intval($knowledge_id))) {
             continue;
         }
         $percent = isset($percents[$knowledge_id]) ? intval($percents[$knowledge_id]) : 0;
         if ($percent < 0 or $percent > 100) {
             continue;
         }
         if (isset($items[$knowledge_id])) {
             continue;
         }
         $current_percent += $percent;
         if ($current_percent > $total_percent) {
             $message[] = '一级知识点占比总和不能超过' . $total_percent;
             break;
         }
         // valid data
         $items[$knowledge_id] = array('cmp_info_id' => $id, 'item_knowledge_id' => $knowledge_id, 'item_percent' => $percent);
     }
     //外部知识点对比项
     $external_knowledge_names = $this->input->post('external_knowledge_names');
     $external_percents = $this->input->post('external_percents');
     if (empty($external_knowledge_names) or !is_array($external_knowledge_names) or empty($external_percents) or !is_array($external_percents)) {
         $external_knowledge_names = array();
     }
     $external_items = array();
     foreach ($external_knowledge_names as $k => $item) {
         // filter invalid data
         $item = trim($item);
         if ($item == '' || !isset($external_percents[$k]) || !($percent = intval($external_percents[$k]))) {
             continue;
         }
         //过滤与内部一级知识点重复的新增知识点
         $count_result = $this->db->query("select count(*) as count from {pre}knowledge where knowledge_name='{$item}' and subject_id={$subject_id}")->row_array();
         if ($count_result['count']) {
             $message[] = '新增知识点名称(<font color="red">' . $item . '</font>)不能与已有一级知识点重复';
             break;
         }
         if ($percent < 0 or $percent > $total_percent) {
             continue;
         }
         if (isset($external_items[$item])) {
             continue;
         }
         $current_percent += $percent;
         if ($current_percent > $total_percent) {
             $message[] = '一级知识点占比总和不能超过' . $total_percent;
             break;
         }
         // valid data
         $external_items[$item] = array('external_knowledge_name' => $item, 'item_percent' => $percent);
     }
     //控制知识点占比总和是否等于 100
     if ($current_percent != $total_percent) {
         $message[] = '知识点占比总和必须等于' . $total_percent;
     }
     $items2 = array();
     if ($subject_id == 3) {
         /*
          * 信息提取方式对比项
          */
         $total_percent = 100;
         $current_percent = 0;
         foreach ($group_type_ids as $gr_id) {
             if (!($gr_id = intval($gr_id))) {
                 continue;
             }
             $percent = isset($group_type_percents[$gr_id]) ? intval($group_type_percents[$gr_id]) : 0;
             if ($percent < 0 or $percent > 100) {
                 continue;
             }
             if (isset($items2[$gr_id])) {
                 continue;
             }
             $current_percent += $percent;
             if ($current_percent > $total_percent) {
                 $message[] = '信息提取方式占比总和不能超过' . $total_percent;
                 break;
             }
             // valid data
             $items2[$gr_id] = array('cmp_info_id' => $id, 'item_group_type_id' => $gr_id, 'item_percent' => $percent);
         }
         //控制信息提取方式占比总和是否等于 100
         if ($current_percent != $total_percent) {
             $message[] = '信息提取方式占比总和必须等于' . $total_percent;
         }
         /*
          * 知识点和信息提取方式比例
          */
         $total_percent = 100;
         $current_percent = 0;
         foreach ($extraction_ratio as $ratio) {
             if ($ratio < 0 || $ratio > 100) {
                 $message[] = '知识点和信息提取方式比例范围[0-100]';
             }
             $current_percent += intval($ratio);
             if ($current_percent > $total_percent) {
                 $message[] = '知识点和信息提取方式比例总和不能超过' . $total_percent;
                 break;
             }
         }
         //控制知识点和信息提取方式比例总和是否等于 100
         if ($current_percent != $total_percent) {
             $message[] = '知识点和信息提取方式比例总和必须等于' . $total_percent;
         }
     }
     //知识点和信息提取方式比例
     $info['cmp_extraction_ratio'] = json_encode($extraction_ratio);
     //试题题型对比项(难易度)
     $difficulty_percent = $this->input->post('difficulty_percent');
     $question_amount = $this->input->post('question_amount');
     $q_types = C('q_type');
     //试题题型
     $diff_types = array('0', '1', '2');
     //难易度程度(0:低, 1:中, 2:高)
     $item_difficulties = array();
     $total_difficulty_percent = 100;
     foreach ($q_types as $q_type => $q_type_name) {
         $c_difficulty_percent = 0;
         $has_error = false;
         $tmp_difficulty_percents = array();
         foreach ($diff_types as $diff_type) {
             if ($c_difficulty_percent > $total_difficulty_percent) {
                 $message[] = '题型:' . $q_type_name . '的难易度占比总和不能超过' . $total_difficulty_percent;
                 $has_error = true;
                 break;
             }
             if (!is_array($difficulty_percent) || !isset($difficulty_percent[$q_type]) || !isset($difficulty_percent[$q_type][$diff_type])) {
                 continue;
             }
             $diff_percent = intval($difficulty_percent[$q_type][$diff_type]);
             $c_difficulty_percent += $diff_percent;
             if ($diff_percent < 0 || $diff_percent > $total_difficulty_percent) {
                 continue;
             }
             $tmp_difficulty_percents[] = $diff_percent;
         }
         //题目数量
         $tmp_question_amount = 0;
         if (!(!is_array($question_amount) || !isset($question_amount[$q_type]))) {
             $tmp_question_amount = intval($question_amount[$q_type]);
         }
         if ($tmp_question_amount > 0 && $c_difficulty_percent != $total_difficulty_percent) {
             $message[] = '题型:' . $q_type_name . '的难易度占比总和 必须等于' . $total_difficulty_percent;
             $has_error = true;
         }
         if ($has_error) {
             break;
         }
         $item_difficulties[] = array('q_type' => $q_type, 'difficulty_percent' => implode(',', $tmp_difficulty_percents), 'question_amount' => $tmp_question_amount);
     }
     //试题题型对比项(外部题型 难易度)
     $e_difficulty_name = $this->input->post('e_difficulty_name');
     $e_question_amount = $this->input->post('e_question_amount');
     $external_difficulties = array();
     !is_array($e_difficulty_name) && ($e_difficulty_name = array());
     !is_array($e_question_amount) && ($e_question_amount = array());
     if (count($e_difficulty_name)) {
         $t_q_types = C('q_type');
         foreach ($e_difficulty_name as $k => $v) {
             $v = trim($v);
             if ($v == '' || !isset($e_question_amount[$k])) {
                 continue;
             }
             if (in_array($v, $t_q_types)) {
                 $message[] = '新增题型名称(<font color="red">' . $v . '</font>)不能与已有题型名称重复';
                 continue;
             }
             $e_q_amount = intval($e_question_amount[$k]);
             if ($e_q_amount <= 0) {
                 continue;
             }
             $external_difficulties[$v] = array('name' => $v, 'question_amount' => $e_q_amount);
         }
     }
     //对比项(方法策略)
     $method_tactic_ids = $this->input->post('method_tactic_ids');
     $percents = $this->input->post('method_tactic_percents');
     !is_array($method_tactic_ids) && ($method_tactic_ids = array());
     !is_array($percents) && ($percents = array());
     $item_method_tactics = array();
     $total_percent = 100;
     $current_percent = 0;
     foreach ($method_tactic_ids as $method_tactic_id) {
         // filter invalid data
         if (!($method_tactic_id = intval($method_tactic_id))) {
             continue;
         }
         $percent = isset($percents[$method_tactic_id]) ? intval($percents[$method_tactic_id]) : 0;
         if ($percent < 0 or $percent > 100) {
             continue;
         }
         if (isset($item_method_tactics[$method_tactic_id])) {
             continue;
         }
         $current_percent += $percent;
         if ($current_percent > $total_percent) {
             $message[] = '方法策略占比总和不能超过' . $total_percent;
             break;
         }
         // valid data
         $item_method_tactics[$method_tactic_id] = array('cmp_info_id' => $id, 'method_tactic_id' => $method_tactic_id, 'percent' => $percent);
     }
     //对比项(外部方法策略)
     $external_method_tactic_names = $this->input->post('e_method_tactic_names');
     $external_method_tactic_percents = $this->input->post('e_method_tactic_percents');
     !is_array($external_method_tactic_names) && ($external_method_tactic_names = array());
     !is_array($external_method_tactic_percents) && ($external_method_tactic_percents = array());
     $external_method_tactics = array();
     foreach ($external_method_tactic_names as $k => $item) {
         // filter invalid data
         $item = trim($item);
         if ($item == '' || !isset($external_method_tactic_percents[$k]) || !($percent = intval($external_method_tactic_percents[$k]))) {
             continue;
         }
         //过滤与内部方法策略重复的新增方法策略
         $count_result = $this->db->query("select count(*) as count from {pre}method_tactic where name='{$item}'")->row_array();
         if ($count_result['count']) {
             $message[] = '新增方法策略名称(<font color="red">' . $item . '</font>)不能与已有方法策略重复';
             break;
         }
         if ($percent < 0 or $percent > 100) {
             continue;
         }
         if (isset($external_method_tactics[$item])) {
             continue;
         }
         $current_percent += $percent;
         if ($current_percent > $total_percent) {
             $message[] = '方法策略和外部方法策略占比总和不能超过' . $total_percent;
             break;
         }
         // valid data
         $external_method_tactics[$item] = array('name' => $item, 'percent' => $percent);
     }
     //控制方法策略占比总和是否等于 100
     $db = Fn::db();
     $res = $db->fetchRow('SELECT group_concat(DISTINCT subject_id) as subject_ids FROM rd_subject_category_subject');
     $subject_ids = explode(',', $res['subject_ids']);
     if ($current_percent != $total_percent && in_array($subject_id, $subject_ids)) {
         $message[] = '方法策略和外部方法策略占比总和必须等于' . $total_percent;
     }
     if (empty($items) && empty($external_items) && empty($item_difficulties) && empty($external_difficulties) && empty($item_method_tactics) && empty($external_method_tactics)) {
         $message[] = '请填写知识点对比项';
     }
     if ($message) {
         message(implode('<br/>', $message));
     }
     //pr($items,1);
     // process
     $res = FALSE;
     if ($act == 'add') {
         $actname = '添加';
         $data = array('items' => $items, 'items2' => $items2, 'external_items' => $external_items, 'item_difficulties' => $item_difficulties, 'external_difficulties' => $external_difficulties, 'item_method_tactics' => $item_method_tactics, 'external_method_tactics' => $external_method_tactics);
         $insert_id = 0;
         $res = ComparisonInfoModel::insert($info, $insert_id, $data);
         if ($res) {
             $id = $insert_id;
         }
     } else {
         $actname = '编辑';
         $data = array('items' => $items, 'items2' => $items2, 'external_items' => $external_items, 'item_difficulties' => $item_difficulties, 'external_difficulties' => $external_difficulties, 'item_method_tactics' => $item_method_tactics, 'external_method_tactics' => $external_method_tactics);
         $res = ComparisonInfoModel::update($id, $info, $data);
     }
     if ($res) {
         admin_log($act, 'comparison_info', $id);
         message('对比信息' . $actname . '成功', 'admin/comparison_info/index/' . $type_id);
     } else {
         //message('对比信息'.$actname.'失败');
     }
 }