package com.qmth.qrzk.web.controller;

import com.qmth.qrzk.repository.service.query.CorrigendumQueryParams;
import com.qmth.qrzk.repository.task.PaperCorrigendum;
import com.qmth.qrzk.repository.user.model.Course;
import com.qmth.qrzk.repository.user.model.User;
import com.qmth.qrzk.repository.user.service.impl.PaperCorrigendumService;
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 javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/corrigendum")
public class PaperCorrigendumController extends BaseAjaxController{

	
	private static final Logger log = Logger.getLogger(PaperCorrigendumController.class);
	
	@Autowired 
	private PaperCorrigendumService paperCorrigendumService;

	@RequestMapping(value = "/list")
	@ResponseBody
	public Map<String, Object> query(@RequestBody CorrigendumQueryParams params) {
		Map<String, Object> result = new HashMap<String, Object>();
		List<PaperCorrigendum> rows = paperCorrigendumService.queryCorrigendum(params);
		result.put("rows", rows);
		result.put("total", params.getTotalNum());
		return result;
	}

	@RequestMapping(value = "/user/list")
	@ResponseBody
	public List<PaperCorrigendum> userList(HttpSession session) {
		User user = this.getCurUser(session);
		List<PaperCorrigendum> rows = paperCorrigendumService.list();
		return rows;
	}

	@RequestMapping(value = "/save")
	@ResponseBody
	public Map<String, Object> save(HttpServletRequest request , @RequestBody PaperCorrigendum corrigendum) {
		try {
			if (corrigendum.getId() != null) {
				paperCorrigendumService.updateCorrigendum(corrigendum);
			} else {
				paperCorrigendumService.addCorrigendum(corrigendum);
			}
			Map<String, Object> resultMap = this.successResult(null);
			resultMap.put("corrigendum", paperCorrigendumService.findById(corrigendum.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{
			paperCorrigendumService.delete(id);
			return this.successResult("操作成功");
		}catch(Exception e){
			return this.failedResult("删除失败！");
		}
	}

	@RequestMapping(value = "/get/{id}")
	@ResponseBody
	public PaperCorrigendum getById(@PathVariable Integer id){
		return paperCorrigendumService.findById(id);
	}

	@RequestMapping(value = "/queryAllBatch")
	@ResponseBody
	public List<String> queryAllBatch(HttpSession session) {
		List<String> rows = paperCorrigendumService.queryAllBatch();
		return rows;
	}

}
