package com.xintong.visualinspection.controller.gettway;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xintong.system.err.BusinessException;
import com.xintong.visualinspection.bean.*;
import com.xintong.visualinspection.controller.BaseController;
import com.xintong.visualinspection.service.*;
import com.xintong.visualinspection.util.AuthorUtil;
import com.xintong.visualinspection.util.Constants;
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 javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

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


    @Reference(version = "1.0.0",
            application = "${dubbo.application.id}",
            registry = "${dubbo.registry.address}")
    private CheckAppealService_F checkAppealService;

    @Reference(version = "1.0.0",
            application = "${dubbo.application.id}",
            registry = "${dubbo.registry.address}")
    private TaskService_F taskService;
   
    /**
     * 添加考核项
     * @return
     * String
     * @exception
     * @since  1.0.0
     */
    @RequestMapping(value = "/add")
    public String add(HttpServletRequest request,@Valid @RequestBody CheckAppeal_F checkAppeal){
//    	CheckAppeal ca = checkAppealService.getByTaskId(checkAppeal.getTask_id());
//		if(ca!=null) {
//			throw new BusinessException(21201);
//		}
    	CheckAppeal_F ca = checkAppealService.getByTaskId(checkAppeal.getTask_id());
    	if(ca==null){
    		User u = getCurrentUser(request);
    		checkAppeal.setAppeal_man(new Long(u.getId()));
    		checkAppeal.setCheck_status(Constants.STATUS_APPEALING);
    		if(u.getOrganid()!=null) {
                checkAppeal.setAppeal_dept(new Long(u.getOrganid()));
            }
    		checkAppealService.insert(checkAppeal);
            FwqCheckTask task = new FwqCheckTask();
            task.setId(checkAppeal.getTask_id().intValue());
    		task = taskService.getTask(task);
    		task.setCheck_status(checkAppeal.getCheck_status());
    		task.setUpdate_user(new Long(u.getId()));
    		taskService.updateStatus(task);
    		return super.returnSuccessResult("添加成功");
    	}else{
    		throw new BusinessException(21401);
    	}
    }
    
    /**
     * 修改考核项
     * @return
     * String
     * @exception
     * @since  1.0.0
     */
    @RequestMapping(value = "/update")
    public String update(@Valid @RequestBody CheckAppeal_F checkAppeal){
    	checkAppealService.update(checkAppeal);
        CheckAppeal_F ca = checkAppealService.getById(checkAppeal.getId());
    	return super.returnSuccessResult("修改成功",ca);
    }
    
    /**
     * 删除考核项
     * @return
     * String
     * @exception
     * @since  1.0.0
     */
    @RequestMapping(value = "/delete")
    public String delete(@RequestBody CheckAppeal_F checkAppeal){
    	checkAppealService.delete(checkAppeal.getId());
    	return super.returnSuccessResult("删除成功");
    }
    
    /**
     * 根据id获取考核项数据
     * @param checkAppeal
     * @return
     */
    @RequestMapping(value = "/getById")
    public String getById(@RequestBody CheckAppeal_F checkAppeal){
    	if(checkAppeal.getId()==null){
    		throw new BusinessException(20101);
    	}
        CheckAppeal_F checkItemOne =  checkAppealService.getById(checkAppeal.getId()) ;
    	return super.returnSuccessResult(checkItemOne);
    }
    
    /**
     * 根据taskid获取申诉数据
     * @param checkAppeal
     * @return
     */
    @RequestMapping(value = "/getByTaskId")
    public String getByTaskId(@RequestBody CheckAppeal_F checkAppeal){
    	if(checkAppeal.getTask_id()==null){
    		throw new BusinessException(20002);
    	}
        CheckAppeal_F checkItemOne =  checkAppealService.getByTaskId(checkAppeal.getTask_id()) ;
    	return super.returnSuccessResult(checkItemOne);
    }
    
    @RequestMapping(value = "/getList/{page}/{size}")
    public String getList(@RequestBody CheckAppeal_F param ,@PathVariable Integer page,@PathVariable Integer size){
    	PageHelper.startPage(page, size);
    	param.setCode_flag("task_status");
    	List<CheckAppeal_F> checkAppeal =  checkAppealService.getList(param) ;
    	return super.returnSuccessResult(new PageInfo(checkAppeal));
    }
    
    /**
     * 通过多条件获取考核任务
     * @return
     * String
     * @exception
     * @since  1.0.0
     */
    @RequestMapping(value = "/getTaskList/{page}/{size}")
    public String getTaskList(HttpServletRequest request, @PathVariable Integer page,@PathVariable Integer size, @RequestBody FwqCheckTask task){
    	PageHelper.startPage(page, size); 
    	User user = getCurrentUser(request);
    	if(AuthorUtil.hasRole(user, Constants.ROLE_FWQ_ADMIN)
    			||AuthorUtil.hasRole(user, Constants.ROLE_FWQ)
    			||AuthorUtil.containsRole(user, "ROLE_FWQ")){
    		task.setChecked_dept((long)user.getOrganid());
    	}else if(AuthorUtil.hasRole(user, Constants.ROLE_JICHA)){
    		task.setRecheckman(new Long(user.getId()));
    	}
        Object  taskList =  taskService.getAllTask(page,size,task);
    	if(taskList==null) return super.returnSuccessResult(new PageInfo(new ArrayList()));
    	return super.returnSuccessResult(taskList);
    }
}
