package com.xintong.visualinspection.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

import com.github.pagehelper.Page;
import com.xintong.visualinspection.bean.User;
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.Constant;
import com.xintong.visualinspection.bean.Task;
import com.xintong.visualinspection.service.CheckApplyService;
import com.xintong.visualinspection.service.TaskService;
import com.xintong.visualinspection.util.AuthorUtil;
import com.xintong.visualinspection.util.CacheUtil;
import com.xintong.visualinspection.util.CodeUtil;
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());
    	task.setUpdate_user(new Long(u.getId()));
    	if(checkAppeal.isIsappeal()){
    	    task.setInvalidFrom(1);
        }
    	taskService.update(task);
    	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_APPLY_SUCCEED;
    	checkAppeal.setCheck_status(check_status);
    	checkApplyService.update(checkAppeal);
    	Task task = new Task();
    	task.setId(checkAppeal.getTask_id());
    	task.setCheck_status(check_status);
    	task.setUpdate_user(new Long(user.getId()));
    	taskService.update(task);
    	//删除此任务的用户稽查次数
    	taskService.deleteInvalidTaskCount(checkAppeal.getTask_id());
    	return super.returnSuccessResult("同意成功");
    }
    
    /**
     * 批量同意无效任务
     * @return
     * String
     * @exception
     * @since  1.0.0
     */
    @RequestMapping(value = "/agreePatch")
    public String agreePatch(HttpServletRequest request,@Valid @RequestBody CheckApply checkAppeal){
    	if(checkAppeal.getApply_ids()==null) {
    		throw new BusinessException(20001); 
    	}
    	User user = getCurrentUser(request);
    	List<CheckApply> checkApplyList = checkApplyService.getList(checkAppeal);
    	for(CheckApply ca :checkApplyList) {
    		ca.setVerify_person(new Long(user.getId()));
    		int check_status = Constants.STATUS_APPLY_SUCCEED;
    		ca.setCheck_status(check_status);
    		checkApplyService.update(ca);
    		//更新任务
    		Task task = new Task();
    		task.setId(ca.getTask_id());
    		task.setCheck_status(check_status);
    		task.setUpdate_user(new Long(user.getId()));
    		taskService.update(task);
    		
    		//删除此任务的用户稽查次数
        	taskService.deleteInvalidTaskCount(ca.getTask_id());
    	}
    	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_APPLY_FAILED;
//    	checkAppeal.setCheck_status(check_status);
//    	checkApplyService.update(checkAppeal);
        checkApplyService.delete(checkAppeal.getId());
    	Task task = new Task();
    	task.setId(checkAppeal.getTask_id());
    	Task temp = taskService.getById(checkAppeal.getTask_id());
    	if(temp.getInvalidFrom().equals(1)){
    	    //申诉任务  则退回申诉待稽查中
            task.setCheck_status(Constants.STATUS_APPEAL_SUCCEED);
        }else{
            task.setCheck_status(Constants.STATUS_CHECK_SUBMIT);
        }
    	//无效任务拒绝后，任务该外待稽查状态

    	task.setUpdate_user(new Long(user.getId()));
    	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()) ;
    	checkItemOne.setApply_man_name(CacheUtil.getUserTurenameFromMap(checkItemOne.getApply_man()));
    	return super.returnSuccessResult(checkItemOne);
    }
    
    @RequestMapping(value = "/getList/{page}/{size}")
    public String getList(HttpServletRequest request,@RequestBody CheckApply param ,@PathVariable Integer page,@PathVariable Integer size){
		final Page<Object> objects = PageHelper.startPage(page, size);
		User user = getCurrentUser(request);
    	if(AuthorUtil.hasRole(user, Constants.ROLE_JICHA)){
    		param.setApply_man(new Long(user.getId()));
    	}
    	List<CheckApply> checkAppeal =  checkApplyService.getList(param) ;
    	return super.returnSuccessResult(new PageInfo(checkAppeal));
    }
    
    /**
     * 获取无效任务原因排名前10
     * @param checkItem
     * @return
     */
    @RequestMapping(value = "/getTopReason")
    public String getTopReason(){
    	Map<String,Object> param = new HashMap<String,Object>();
    	// 获取原因显示条数
    	int showNum = 10;
		Constant t =  CodeUtil.getCodeByFlagAndValue("top_show_num", "1");
		if(t!=null) showNum = Integer.parseInt(t.getCode_name());
    	param.put("num", showNum);
    	List<CheckApply> checkApplyList =  checkApplyService.getTopReasonList(param);
    	return super.returnSuccessResult(checkApplyList);
    }
}
