<?php
namespace admin\Controller;
use Think\Controller;
class TaskController extends CommonController {
	 
	public function lists(){
	
		$type = I('request.type','0','intval');
		$Task = D("Task");
		if ($type>0){
			$map['type'] = $type;
		}		
		$list = $Task->get_list($map);
		$this->assign('type',$map['type']);
		$this->assign('list',$list);
		$this->display();
	}
//保存所有分类
	public function allsave(){
	 	$id = I('post.id');
        $gold = I('post.gold','0','intval');
		$exp = I('post.exp','0','intval');
		$sort = I('post.sort','0','intval');
		$is_display = I('post.is_display','0','intval');
       
		 
		$num = 0;
		$data = M("Task");
		if (is_array($id) && count($id)>0)
        {
            foreach($id as $k=>$v)
            {
                unset($map);
                $map['id'] = intval($v);
                $map['gold']=intval($gold[$k]);
                $map['exp']=intval($exp[$k]);
				$map['sort']=intval($sort[$k]);
				$map['is_display']=intval($is_display[$k]);
				$data->save($map);
                $num++;
         
            }
        }
		if($num>0){
            $this->success('保存成功！');
        }else{
            $this->error('未保存任何分类！');
        }
		 
	
 	}
	//等级设置
	public function lv(){
		$Task = D("Lv");		
		$list = $Task->get_lv_list();
		$this->assign('list',$list);
		$this->display();
	}
	//等级设置保存
	public function lvsave(){
	 	$id = I('post.id');
        $name = I('post.name','未知','trim');
		$exp_min = I('post.exp_min','0','intval');
		$Lv = M("Lv");
		//保存
		if (is_array($id) && count($id)>0)
        {
			if (count($id) != count(array_unique($exp_min))){   
				   $this->error('经验要求不能有重复！');
			}
			unset($map);
            foreach($id as $k=>$v)
            {
                $map['id'] = intval($v);
                $map['name']=empty($name[$k])?"无命名":trim($name[$k]);
                $map['exp_min']=intval($exp_min[$k]);
				$Lv->save($map);
                $num++;
         
            }
			//重新排序
			$list = $Lv->order('exp_min ASC')->select();
			$lv_id=1;
			unset($map);
			foreach($list as $key=>$v)
            {
				$nextkey=$key+1;
				$exp_max=intval($list[$nextkey]['exp_min']);
                $map['id'] = intval($v['id']);
                $map['lv_id']=intval($lv_id);
				$map['exp_max']=$exp_max?($exp_max-1):99999999;
				$Lv->save($map);
                $lv_id++;
            }
		}
		//新增
		$add_name    =   I('post.add_name');
		$add_exp_min =   I('post.add_exp_min','0','intval');
		if (is_array($add_exp_min) && count($add_exp_min)>0)
        {
			$add_exp_min_str = implode(",",$add_exp_min);
			if (preg_match("/^(\d{1,10},)*(\d{1,10})$/",$add_exp_min_str))
			{
				$mapsel['exp_min']  = array('IN',$add_exp_min_str);
				$info = $Lv->where($mapsel)->select();
				if (!empty($info))
				{
				 $this->error('新增内容经验要求有重复，添加失败！');
				 exit();
				}
			}
			unset($map);
            foreach($add_exp_min as $k=>$v)
            {
                $map['name']=empty($add_name[$k])?"无命名":trim($add_name[$k]);
                $map['exp_min']=intval($add_exp_min[$k]);
				$Lv->add($map);
                $num++;
         
            }
			//重新排序
			$list = $Lv->order('exp_min ASC')->select();
			$lv_id=1;
			unset($map);
			foreach($list as $key=>$v)
            {
				$nextkey=$key+1;
				$exp_max=intval($list[$nextkey]['exp_min']);
                $map['id'] = intval($v['id']);
                $map['lv_id']=intval($lv_id);
				$map['exp_max']=$exp_max?($exp_max-1):99999999;
				$Lv->save($map);
                $lv_id++;
            }
		}
		if($num>0){
            $this->success('保存成功！');
        }else{
            $this->error('未保存任何信息！');
        }
		 
	
 	}
	//删除等级设置
	public function lv_delete(){
	
		$ids = I('request.id');
		$ids = is_array($ids)?implode(",",$ids):$ids;
		if ($ids && preg_match("/^(\d{1,10},)*(\d{1,10})$/",$ids))
		{
			$lv = M("Lv");
			$s=$lv->delete($ids);
			//重新排序
			$list = $lv->order('exp_min ASC')->select();
			$lv_id=1;
			unset($map);
			foreach($list as $key=>$v)
            {
				$nextkey=$key+1;
				$exp_max=intval($list[$nextkey]['exp_min']);
                $map['id'] = intval($v['id']);
                $map['lv_id']=intval($lv_id);
				$map['exp_max']=$exp_max?($exp_max-1):99999999;
				$lv->save($map);
                $lv_id++;
            }
			$this->success("成功删除{$s}条数据");
		}
		else
		{
		$this->error('请选择要删除的信息！');
		}
	
 	}
 
}
?>