package com.xintong.visualinspection.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mysql.jdbc.StringUtils;
import com.xintong.system.err.BusinessException;
import com.xintong.visualinspection.bean.CheckItem;
import com.xintong.visualinspection.bean.CheckRule;
import com.xintong.visualinspection.service.CheckRuleService;
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 javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

    @Autowired
    private CheckRuleService checkRuleService;
	@Autowired
	private ItemService itemService;

    /**
     * 获取所有考核办法及下面的
     * @return
     * String
     * @exception
     * @since  1.0.0
     */
    @RequestMapping(value = "/getAllRule/{page}/{size}")
    public String getAllRules(@Valid @RequestBody CheckRule checkRule,@PathVariable Integer page,@PathVariable Integer size){
    	PageHelper.startPage(page, size); 
    	List<CheckRule> crList = checkRuleService.getAllAndCountItem(checkRule);
    	return super.returnSuccessResult(new PageInfo(crList));
    }
   
    /**
     * 添加考核办法
     * @return
     * String
     * @exception
     * @since  1.0.0
     */
    @RequestMapping(value = "/add")
    public String add(@Valid @RequestBody CheckRule checkRule){
    	checkRuleService.insert(checkRule);
		CacheUtil.refreshItemMap(itemService);
		CacheUtil.refreshItemNameMap(itemService);
    	return super.returnSuccessResult("添加成功");
    }
    
    /**
     * 修改考核办法
     * @return
     * String
     * @exception
     * @since  1.0.0
     */
    @RequestMapping(value = "/update")
    public String update(@Valid @RequestBody CheckRule checkRule){
    	if(checkRule.getId()==null){
    		throw new BusinessException(20002);
    	}
    	checkRuleService.update(checkRule);
		CacheUtil.refreshItemMap(itemService);
		CacheUtil.refreshItemNameMap(itemService);
    	return super.returnSuccessResult("修改成功");
    }
    
    /**
     * 删除考核办法
     * @return
     * String
     * @exception
     * @since  1.0.0
     */
    @RequestMapping(value = "/delete")
    public String delete(@RequestBody CheckRule checkRule){
    	if(checkRule.getId()==null){
    		throw new BusinessException(20002);
    	}
    	checkRuleService.delete(checkRule.getId());
		CacheUtil.refreshItemMap(itemService);
		CacheUtil.refreshItemNameMap(itemService);
    	return super.returnSuccessResult("删除成功");
    }
    
    /**
     * 通过id获取考核办法
     * @return
     * String
     * @exception
     * @since  1.0.0
     */
    @RequestMapping(value = "/getById")
    public String getById(@RequestBody CheckRule checkRule){
    	if(checkRule.getId()==null){
    		throw new BusinessException(20002);
    	}
    	CheckRule cr = checkRuleService.getById(checkRule.getId());
    	return super.returnSuccessResult(cr);
    }
    
    /**
     * 通过id获取考核项List
     * @return
     * String
     * @exception
     * @since
     */
    @RequestMapping(value = "/getListById")
    public String getByListId(@RequestBody CheckRule checkRule){
    	if(checkRule.getId()==null){
    		throw new BusinessException(20002);
    	}
    	CheckRule cr = checkRuleService.getListById(checkRule.getId());
    	return super.returnSuccessResult(cr);
    }
    
    /**
     * 通过id获取考核项List
     * @return
     * String
     * @exception
     * @since
     */
    @RequestMapping(value = "/getItemListByRuleType")
    public String getItemListByRuleType(@RequestBody CheckRule checkRule){
    	if(checkRule.getRule_type()==null){
    		throw new BusinessException(20002);
    	}
    	Map<String,String> param = new HashMap<String,String>();
    	param.put("rule_type", checkRule.getRule_type());
    	CheckRule cr = checkRuleService.getItemListByRuleType(param);
    	List<CheckItem> itemList=null;
    	if(cr!=null)
    		itemList = cr.getCheck_items();
    	return super.returnSuccessResult(itemList);
    }
    
    /**
     * 通过名称获取考核办法
     * @return
     * String
     * @exception
     * @since  1.0.0
     */
    @RequestMapping(value = "/getByName")
    public String getByName(@RequestBody CheckRule checkRule){
    	if(StringUtils.isNullOrEmpty(checkRule.getName())){
    		throw new BusinessException(21101);
    	}
    	List<CheckRule> crList = checkRuleService.getByName(checkRule.getName());
    	return super.returnSuccessResult(crList);
    }
}
