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.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.xintong.system.err.BusinessException;
import com.xintong.visualinspection.bean.Constant;
import com.xintong.visualinspection.bean.Score;
import com.xintong.visualinspection.service.ScoreService;
import com.xintong.visualinspection.util.CodeUtil;

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

    @Autowired
    private ScoreService scoreService;

   
    /**
     * 添加扣分项
     * @return
     * String
     * @exception
     * @since  1.0.0
     */
    @RequestMapping(value = "/add")
    public String add(@Valid @RequestBody Score score){
    	scoreService.insert(score);
    	return super.returnSuccessResult("添加成功");
    }
    
    /**
     * 修改扣分项
     * @return
     * String
     * @exception
     * @since  1.0.0
     */
    @RequestMapping(value = "/update")
    public String update(HttpServletRequest request,@Valid @RequestBody Score score){
    	User user = getCurrentUser(request);
    	score.setUpdate_user(new Long(user.getId()));
    	scoreService.update(score);
    	return super.returnSuccessResult("修改成功");
    }
    
    /**
     * 删除扣分项
     * @return
     * String
     * @exception
     * @since  1.0.0
     */
    @RequestMapping(value = "/delete")
    public String delete(@RequestBody Score score){
    	scoreService.delete(score.getId());
    	return super.returnSuccessResult("删除成功");
    }
    
    /**
     * 通过任务id获取考核扣分项
     * @return
     * String
     * @exception
     * @since  1.0.0
     */
    @RequestMapping(value = "/getByTaskId")
    public String getByTaskId(@RequestBody Score score){
    	if(score.getTask_id()==null){
    		throw new BusinessException(20002);
    	}
    	List<Score> scorelist = scoreService.getScoreListByTaskId(score.getTask_id());
    	return super.returnSuccessResult(scorelist);
    }
    
    /**
     * 通过多条件获取考核扣分项
     * @return
     * String
     * @exception
     * @since  1.0.0
     */
    @RequestMapping(value = "/getScoreList/{page}/{size}")
    public String getScoreList(@PathVariable Integer page,@PathVariable Integer size, @RequestBody Score score){
    	List<Score> scorelist = scoreService.getScoreList(score);
    	return super.returnSuccessPageResult(scorelist);
    }
    
    /**
     * 获取备注排名前10
     * @param checkItem
     * @return
     */
    @RequestMapping(value = "/getTopRemark")
    public String getTopRemark(){
    	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<Score> scoreList =  scoreService.getTopRemarkList(param);
    	return super.returnSuccessResult(scoreList);
    }
    
    /**
     * 修改旧的考核人到新的考核人扣分项
     * @return
     * String
     * @exception
     * @since  1.0.0
     */
    @RequestMapping(value = "/updateByTaskIdAndPerson")
    public String updateByTaskIdAndPerson(@Valid @RequestBody Score score){
    	if(score.getChecked_person()==null || score.getNew_checked_person()==null){
    		throw new BusinessException(21301);
    	}
    	if(score.getTask_id()==null){
    		throw new BusinessException(21302);
    	}
    	scoreService.updateByTaskIdAndPerson(score);
    	return super.returnSuccessResult("修改成功");
    }
    
    /**
     * 撤销扣分
     * @return
     * String
     * @exception
     * @since  1.0.0
     */
    @RequestMapping(value = "/backoutScore")
    public String backoutScore(HttpServletRequest request,@Valid @RequestBody Score score){
    	if(score.getId()==null){
    		throw new BusinessException(20002);
    	}
    	User user = getCurrentUser(request);
    	score.setUpdate_user(new Long(user.getId()));
    	scoreService.backoutScore(score);
    	return super.returnSuccessResult("撤销成功");
    }
}
