package com.xintong.visualinspection.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xintong.system.err.BusinessException;
import com.xintong.visualinspection.bean.CheckApply;
import com.xintong.visualinspection.bean.Task;
import com.xintong.visualinspection.bean.TaskStatus;
import com.xintong.visualinspection.bean.User;
import com.xintong.visualinspection.service.CheckApplyService;
import com.xintong.visualinspection.service.TaskService;
import com.xintong.visualinspection.util.CacheUtil;
import com.xintong.visualinspection.util.Constants;

/**
 * 文件名：CheckItemController
 * 版本信息：日期：2017/3/30 Copyright 江苏省交通规划设计院 Corporation 2017 版权所有.
 */
@RestController
@RequestMapping("/checkApply")
public class CheckApplyController extends BaseController {

    @Autowired
    private CheckApplyService checkApplyService;

    @Autowired
    private TaskService taskService;
   
    /**
     * 添加考核项
     * @return
     * String
     * @exception
     * @since  1.0.0
     */
    @RequestMapping(value = "/add")
    public String add(HttpServletRequest request,@Valid @RequestBody CheckApply checkAppeal){
    	
    	CheckApply ca = checkApplyService.getByTaskId(checkAppeal.getTask_id());
		if(ca!=null) {
			throw new BusinessException(21201);
		}
		
    	User u = getCurrentUser(request);
    	checkAppeal.setApply_man(new Long(u.getId()));
    	checkAppeal.setCheck_status(Constants.STATUS_APPLYING);
    	if(u.getOrganid()!=null)
    		checkAppeal.setApply_dept(new Long(u.getOrganid()));
    	checkApplyService.insert(checkAppeal);
    	Task task = taskService.getById(checkAppeal.getTask_id());
    	task.setCheck_status(checkAppeal.getCheck_status());
    	taskService.update(task);
//    	//添加状态
//    	TaskStatus status = new TaskStatus();
//		status.setTask_id(checkAppeal.getTask_id());
//		status.setUpdate_user(new Long(u.getId()));
//		status.setUpdate_username(u.getTruename());
//		status.setCheck_status(checkAppeal.getCheck_status());
//		String name = (CacheUtil.codeMap.get("task_status_"+status.getCheck_status())).getCode_name();
//		status.setCheck_status_name(name);
//
//    	taskService.insertStatus(status);
    	return super.returnSuccessResult("添加成功");
    }
    
    /**
     * 修改考核项
     * @return
     * String
     * @exception
     * @since  1.0.0
     */
    @RequestMapping(value = "/update")
    public String update(@Valid @RequestBody CheckApply checkAppeal){
    	checkApplyService.update(checkAppeal);
    	return super.returnSuccessResult("修改成功");
    }
    
    /**
     * 修改考核项
     * @return
     * String
     * @exception
     * @since  1.0.0
     */
    @RequestMapping(value = "/agree")
    public String agree(HttpServletRequest request,@Valid @RequestBody CheckApply checkAppeal){
    	if(checkAppeal.getId()==null) {
    		throw new BusinessException(20001); 
    	}
    	User user = getCurrentUser(request);
    	checkAppeal.setVerify_person(new Long(user.getId()));
    	int check_status = Constants.STATUS_APPEAL_SUCCEED;
    	checkAppeal.setCheck_status(check_status);
    	checkApplyService.update(checkAppeal);
    	Task task = new Task();
    	task.setId(checkAppeal.getTask_id());
    	task.setCheck_status(check_status);
    	taskService.update(task);
    	return super.returnSuccessResult("同意成功");
    }
    
    /**
     * 修改考核项
     * @return
     * String
     * @exception
     * @since  1.0.0
     */
    @RequestMapping(value = "/refuse")
    public String refuse(HttpServletRequest request,@Valid @RequestBody CheckApply checkAppeal){
    	if(checkAppeal.getId()==null) {
    		throw new BusinessException(20001); 
    	}
    	User user = getCurrentUser(request);
    	checkAppeal.setVerify_person(new Long(user.getId()));
    	int check_status = Constants.STATUS_APPEAL_FAILED;
    	checkAppeal.setCheck_status(check_status);
    	checkApplyService.update(checkAppeal);
    	Task task = new Task();
    	task.setId(checkAppeal.getTask_id());
    	task.setCheck_status(check_status);
    	taskService.update(task);
    	return super.returnSuccessResult("拒绝成功");
    }
    
    /**
     * 删除考核项
     * @return
     * String
     * @exception
     * @since  1.0.0
     */
    @RequestMapping(value = "/delete")
    public String delete(@RequestBody CheckApply checkAppeal){
    	checkApplyService.delete(checkAppeal.getId());
    	return super.returnSuccessResult("删除成功");
    }
    
    /**
     * 根据id获取考核项数据
     * @param checkItem
     * @return
     */
    @RequestMapping(value = "/getById")
    public String getById(@RequestBody CheckApply checkAppeal){
    	if(checkAppeal.getId()==null){
    		throw new BusinessException(20001);
    	}
    	CheckApply checkItemOne =  checkApplyService.getById(checkAppeal.getId()) ;
    	return super.returnSuccessResult(checkItemOne);
    }
    
    /**
     * 根据任务id获取无效任务申请数据
     * @param checkItem
     * @return
     */
    @RequestMapping(value = "/getByTaskId")
    public String getByTaskId(@RequestBody CheckApply checkAppeal){
    	if(checkAppeal.getId()==null){
    		throw new BusinessException(20101);
    	}
    	CheckApply checkItemOne =  checkApplyService.getByTaskId(checkAppeal.getId()) ;
    	return super.returnSuccessResult(checkItemOne);
    }
    
    @RequestMapping(value = "/getList/{page}/{size}")
    public String getList(@RequestBody CheckApply param ,@PathVariable Integer page,@PathVariable Integer size){
    	PageHelper.startPage(page, size);
    	List<CheckApply> checkAppeal =  checkApplyService.getList(param) ;
    	return super.returnSuccessResult(new PageInfo(checkAppeal));
    }
    
    
}
