package com.blueland.ebpm.rest;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.blueland.ebpm.util.RestUtils;
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.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.blueland.bpm.engine.core.model.ExceptionErrorCode;
import com.blueland.bpm.idea.model.CommonIdeaEntity;
import com.blueland.bpm.idea.service.CommonIdeaManager;
import com.blueland.ebpm.bean.BeansBuilder;
import com.blueland.ebpm.bean.CommonIdeaBean;
import com.blueland.ebpm.exception.RestException;

@RestController
@RequestMapping("/common/idea")
public class CommonIdeaController {
	
	@Autowired
	CommonIdeaManager commonIdeaManager;
	
	@Autowired
	private BeansBuilder beansBuilder;

	@RequestMapping(value="/{id}", method=RequestMethod.GET, produces="application/json; charset=UTF-8")
	public CommonIdeaBean getCommonIdea(@PathVariable String id) {
		CommonIdeaEntity model = commonIdeaManager.getById(id);
		return beansBuilder.build(model);
	}
	
	@RequestMapping(value="/{id}", method=RequestMethod.POST, produces="application/json; charset=UTF-8")
	public Map<String,Object> addCommonIdea(@PathVariable String id,@RequestBody CommonIdeaEntity commonIdea) {
		Map<String,Object> result = new HashMap<String,Object>();
		if(RestUtils.isEmpty(id)) {
			throw new RestException(ExceptionErrorCode.B2001.name(), "id is null");
		}
		CommonIdeaEntity entity = commonIdeaManager.getById(id);
		if(entity != null) {
			throw new RestException(ExceptionErrorCode.B2001.name(), "CommonIdea exist,id=" + id);
		}
		commonIdea.setId(id);
		commonIdea.setCreateTime(new Date());
		commonIdeaManager.save(commonIdea);
		result.put("result", "OK");
		return result;
	}

	@RequestMapping(value="/{id}", method=RequestMethod.PUT, produces="application/json; charset=UTF-8")
	public Map<String,Object> updateCommonIdea(@PathVariable String id, @RequestBody CommonIdeaEntity commonIdea){
		Map<String,Object> result = new HashMap<String,Object>();
		if(RestUtils.isEmpty(id)) {
			throw new RestException(ExceptionErrorCode.B2001.name(), "id is null");
		}
		CommonIdeaEntity entity = commonIdeaManager.getById(id);
		if(entity == null) {
			throw new RestException(ExceptionErrorCode.B2001.name(), "CommonIdea not exist,id=" + id);
		}
		commonIdea.setId(id);
		commonIdeaManager.update(commonIdea);
		result.put("result", "OK");
		return result;
	}
	
	@RequestMapping(value="/{id}", method=RequestMethod.DELETE, produces="application/json; charset=UTF-8")
	public Map<String, Object> deleteCommonIdea(@PathVariable String id){
		Map<String,Object> result = new HashMap<String,Object>();
		if(RestUtils.isEmpty(id)) {
			throw new RestException(ExceptionErrorCode.B2001.name(), "id is null");
		}
		CommonIdeaEntity entity = commonIdeaManager.getById(id);
		if(entity == null) {
			throw new RestException(ExceptionErrorCode.B2001.name(), "CommonIdea not exist,id=" + id);
		}
		commonIdeaManager.removeById(id);
		result.put("result", "OK");
		return result;
	}
	
	@RequestMapping(value="/{usercode}/list", method=RequestMethod.GET, produces="application/json; charset=UTF-8")
	public List<CommonIdeaBean> getUserCommonIdeaList(@PathVariable String usercode){
		if(RestUtils.isEmpty(usercode))
			throw new RestException(ExceptionErrorCode.B2001.name(), "usercode is null");
		List<CommonIdeaEntity> result = commonIdeaManager.selectByUserCode(usercode);
		return beansBuilder.buildCommIdeaList(result);
	}
}
