package com.xintong.visualinspection.controller;

import com.xintong.system.err.BusinessException;
import com.xintong.visualinspection.bean.CheckItem;
import com.xintong.visualinspection.bean.CheckRule;
import com.xintong.visualinspection.service.CheckItemService;
import com.xintong.visualinspection.service.ItemService;
import com.xintong.visualinspection.util.CacheUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

    @Autowired
    private CheckItemService checkItemService;
    @Autowired
    private ItemService itemService;

   
    /**
     * 添加考核项
     * @return
     * String
     * @exception
     * @since  1.0.0
     */
    @RequestMapping(value = "/add")
    public String add(@Valid @RequestBody CheckItem checkItem){
    	checkItemService.insert(checkItem);
        CacheUtil.refreshItemMap(itemService);
        CacheUtil.refreshItemNameMap(itemService);
    	return super.returnSuccessResult("添加成功");
    }
    
    /**
     * 修改考核项
     * @return
     * String
     * @exception
     * @since  1.0.0
     */
    @RequestMapping(value = "/update")
    public String update(@Valid @RequestBody CheckItem checkItem){
    	checkItemService.update(checkItem);
        CacheUtil.refreshItemMap(itemService);
        CacheUtil.refreshItemNameMap(itemService);
    	return super.returnSuccessResult("修改成功");
    }
    
    /**
     * 删除考核项
     * @return
     * String
     * @exception
     * @since  1.0.0
     */
    @RequestMapping(value = "/delete")
    public String delete(@RequestBody CheckItem checkItem){
    	checkItemService.delete(checkItem.getId());
        CacheUtil.refreshItemMap(itemService);
        CacheUtil.refreshItemNameMap(itemService);
    	return super.returnSuccessResult("删除成功");
    }
    
    /**
     * 根据id获取考核项数据
     * @param checkItem
     * @return
     */
    @RequestMapping(value = "/getById")
    public String getById(@RequestBody CheckItem checkItem){
    	if(checkItem.getId()==null){
    		throw new BusinessException(20101);
    	}
    	CheckItem checkItemOne =  checkItemService.getById(checkItem.getId()) ;
    	return super.returnSuccessResult(checkItemOne);
    }
    
    @RequestMapping(value = "/getAll")
    public String getAll(){
    	List<CheckItem> checkItems =  checkItemService.getAll() ;
    	return super.returnSuccessResult(checkItems);
    }
    
    @RequestMapping(value = "/getForTaskById")
    public String getForTaskById(@RequestBody CheckItem checkItem){
    	List<CheckItem> checkItems =  checkItemService.getAll();
    	return super.returnSuccessResult(checkItems);
    }
    
    /**
     * 通过父亲id获取考核项
     * @return
     * String
     * @exception
     * @since  1.0.0
     */
    @RequestMapping(value = "/getByParentId")
    public String getByParentId(@RequestBody CheckItem checkItem){
    	if(checkItem.getParent_id()==null){
    		throw new BusinessException(20101);
    	}
    	List<CheckItem> checkItemList = checkItemService.getByParentId(checkItem.getParent_id());
    	return super.returnSuccessResult(checkItemList);
    }
    
    /**
     * 通过方法id获取考核项
     * @return
     * String
     * @exception
     * @since  1.0.0
     */
    @RequestMapping(value = "/getByRuleId")
    public String getByRuleId(@RequestBody CheckItem checkItem){
    	if(checkItem.getRule_id()==null){
    		throw new BusinessException(20101);
    	}
    	List<CheckItem> checkItemList = checkItemService.getByRuleId(checkItem.getRule_id());
    	return super.returnSuccessResult(checkItemList);
    }
    
    @RequestMapping(value = "/getCheckItemByRuleType")
    public String getCheckItemByRuleType(@RequestBody CheckRule checkRule){
    	Map<String,String> param = new HashMap<String,String>();
    	param.put("rule_type", checkRule.getRule_type());
    	List<CheckItem> checkItems =  checkItemService.getCheckItemByRuleType(param) ;
    	return super.returnSuccessResult(checkItems);
    }
}
