package com.xintong.visualinspection.controller;

import java.util.List;

import javax.validation.Valid;

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.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.CheckItem;
import com.xintong.visualinspection.service.CheckRuleItemService;

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

	@Autowired
	private CheckRuleItemService checkRuleItemService;
	@Autowired
	private ItemService itemService;
	
	/**
     * 增加考核项
     * @return
     * String
     * @exception
     * @since  1.0.0
     */
    @RequestMapping(value = "/add")
    public String add(@Valid @RequestBody CheckItem checkItem){
    	if(checkItem.getId()==null && checkItem.getRule_id()==null){
    		throw new BusinessException(20002);
    	}
    	checkRuleItemService.insert(checkItem);
		CacheUtil.refreshItemMap(itemService);
		CacheUtil.refreshItemNameMap(itemService);
    	return super.returnSuccessResult("增加成功");
    }
    
    /**
     * 增加考核项列表
     * @param checkItem
     * @return
     */
    @RequestMapping(value = "/add/list")
    public String add(@Valid @RequestBody List<CheckItem> checkItems){
    	if(checkItems ==null && checkItems.size()==0){
    		throw new BusinessException(20002);
    	}
    	String returnMsg = checkRuleItemService.insertList(checkItems);
		CacheUtil.refreshItemMap(itemService);
		CacheUtil.refreshItemNameMap(itemService);
    	return super.returnSuccessResult(returnMsg==null?"增加成功":returnMsg);
    }
    
    
    /**
     * 删除考核项
     * @return
     * String
     * @exception
     * @since  1.0.0
     */
    @RequestMapping(value = "/delete")
    public String delete(@RequestBody CheckItem checkItem){
    	if(checkItem.getId() ==null && checkItem.getRule_id()==null){
    		throw new BusinessException(20002);
    	}
    	checkRuleItemService.delete(checkItem);
		CacheUtil.refreshItemMap(itemService);
		CacheUtil.refreshItemNameMap(itemService);
    	return super.returnSuccessResult("删除成功");
    }
}
