package com.qmth.qrzk.web.controller;

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

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
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.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.qmth.qrzk.repository.service.query.QueryParams;
import com.qmth.qrzk.repository.user.model.Book;
import com.qmth.qrzk.repository.user.service.impl.BookServiceImpl;
import com.qmth.qrzk.repository.utils.Consts;

@Controller
@RequestMapping("/book")
public class BookController extends BaseAjaxController{
	
	private static final Logger log = Logger.getLogger(BookController.class);
	
	@Autowired
	private BookServiceImpl bookService;
	
	private static final String BASE_JSP_PATH = "baseData/book/";
	
    @RequestMapping(value = "/list")
    public ModelAndView list(HttpServletRequest request, HttpSession session) {
    	request.setAttribute("bookStatusDic", Consts.BookStatus.dicMap);
        return new ModelAndView(BASE_JSP_PATH + "list");
    }
    
	@RequestMapping(value="/queryBooks")
	@ResponseBody
	public Map<String, Object> queryBooks(@RequestBody QueryParams params){
		Map<String,Object> result = new HashMap<String, Object>();
		List<Book> rows = bookService.queryBooks(params);
		result.put("rows", rows);
		result.put("total", params.getTotalNum());
		return result;
	}
	
	@RequestMapping(value="/getAllBooks")
	@ResponseBody
	public List<Book> getAllBooks(){
		List<Book> rows = bookService.getAllBooks();
		return rows;
	}
	
	@RequestMapping(value ="/queryByCourseId")
	@ResponseBody
	public String queryByCourseId(HttpServletRequest request, String courseId){
		JSONArray array=new JSONArray();
		
		if(courseId != null && !"".equals(courseId))
		{
			List<Integer> courseIds = new ArrayList<Integer>();
			courseIds.add(Integer.parseInt(courseId));
			List<Book> books = bookService.findBooksInCourse(Integer.parseInt(courseId));
			
			for(Book book : books){
				JSONObject object=new JSONObject();
				object.put("id", book.getId());
				object.put("code", book.getCode());
				object.put("name", book.getName());
				object.put("author", book.getEditor());
				object.put("press", book.getPress());
				object.put("version", book.getVersion());
				object.put("chapterSize", book.getChapterSize());
				array.add(object);
			}
		}
		
		return array.toString();
	}
	
	@RequestMapping(value ="/save")
	@ResponseBody
	public Map<String,Object> save(HttpServletRequest request,  @RequestBody Book book){
		try{
			if(book.getId() != null){
				bookService.updateBook(book);
			}else{
				bookService.addBook(book);
			}
			Map<String,Object> resultMap = this.successResult(null);
			resultMap.put("book", bookService.queryById(book.getId()));
			return resultMap;
		}catch(Exception exp){
			log.error("更新教材信息失败，原因:" + exp.getMessage(), exp);
			return this.failedResult(exp);
		}
	}
	@RequestMapping(value = "/delete/{id}")
	@ResponseBody
	public Map<String, Object> delete(@PathVariable int id){
		try{
			bookService.delBook(id);
			return this.successResult("操作成功");
		}catch(Exception e){
			return this.failedResult("删除失败！");
		}
	}
	
	@RequestMapping(value = "/queryById/{id}")
	@ResponseBody
	public Book queryById(@PathVariable Integer id){
		return bookService.queryById(id);
	}
}
